$(document).ready(function(){
	$('#editProfileForm').submit(function() {
		$('#phone').each(function() {
			this.value = this.value.toString().split(" ").join("").split("-").join("").split("+").join("00");
		});
	});
	
	$.validator.addMethod(
		"regex",
		function(value, element, regexp) {
			var check = false;
			var re = new RegExp(regexp);
			return this.optional(element) || re.test(value);
		},
		"pole musi zawierać tylko litery"
	);

	$.validator.addMethod(
		"requiredCondition",
		function(value, element, param) {
			if ($(param).val() != '' && value == '') {
				return false;
			} else {
				return true;
			}
		},
		"Pole nie może być puste"
	);

	$.validator.addMethod(
		"notEqualTo",
		function(value, element, param) {
			if ($(param).val() == value) {
				return false;
			} else {
				return true;
			}
		},
		'Nie może być takie samo'
	);

	$('#editProfileForm').validate({
		rules: {
			email : {
				required: true,
				email: true
			},
			lastName : {
				required: true
			},
			firstName : {
				required: true
			},
//			address : {
//				required: true
//			},
			city : {
				required: true
			},
			phone : {
				required: true,
				regex: /^[0-9\+]{9,14}$/,
				//remote: mainUrl + "check/phone.html"
			},
			zipCode: {
				regex: /[0-9]{2}\-[0-9]{3}/
			}
		},
		messages: {
			email : {
				required: translate('required_email'),
				email: translate('incorrect_email')
			},
			lastName : {
				required: translate('required_last_name')
			},
			firstName : {
				required: translate('required_first_name')
			},
			address: {
				required: translate('required_address')
			},
			city: {
				required: translate('required_city')
			},
			phone : {
				required: translate('required_phone'),
				regex: translate('incorrect_phone_number'),
				remote: translate('phone_in_use')
			},
			zipCode : {
				regex: translate('incorrect_zip_code')
			}
		},
		errorElement: "p",
		errorClass: "warning"
	});

	$('#changePasswordForm').validate({
		rules: {
			oldPasswd: {
				minlength: 5,
				required: true
			},
			newPasswd: {
				required: true,
				minlength: 5,
				notEqualTo: "#oldPasswd"

			},
			newPasswd2: {
				required: true,
				minlength: 5,
				equalTo: "#newPasswd"
			}
		},
		messages: {
			oldPasswd: {
				minlength: translate('too_short_password'),
				required: translate('required_password')
			},
			newPasswd: {
				required: translate('required_password'),
				minlength: translate('too_short_password'),
				notEqualTo: translate('new_and_old_password_are_equal')

			},
			newPasswd2: {
				required: translate('required_password'),
				minlength: translate('too_short_password'),
				equalTo: translate('not_equal_passwords')

			}
		},
		errorElement: "p",
		errorClass: "warning"
	});
	
});