$(document).ready(function(){
	$('#registerForm').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 true;
			} else {
				return false;
			}
		},
		"Pole nie może być puste"
	);

//	$.validator.addMethod(
//	"company",
//	function(value, element, param) {
//		if(document.getElementById('companyName').value!=''){
//			if(document.getElementsByName('nip').value=='') {
//				return false;
//			}
//		}
//		return true
//	},
//	"Proszę wypełnić NIP"
//	);

	$('#registerForm').validate({
		rules: {
			login : {
				required: true,
				regex: /^[a-zA-Z0-9_-]{4,25}$/,
				remote: mainUrl + "check_login"
			},
			agreement : "required",
			email : {
				required: true,
				email: true,
				//remote: mainUrl + "check/email.html"
			},
			captcha : {
				required: true,
				minlength: 5,
				maxlength: 5,
				//remote: mainUrl + "check/captcha.html"
			},
			password : {
				required: true,
				minlength: 5
			},
			password2 : {
				required: true,
				minlength: 5,
				equalTo: "#password"
			},
			lastName : {
				required: true
			},
			firstName : {
				required: true
			},
//			address : {
//				required: true
//			},
			phone : {
				required: true,
				regex: /^[0-9\+]{9,14}$/,
				//remote: mainUrl + "check/phone.html"
			}
		},
		messages: {
			login: {
				required: translate("required_username"),
				regex: translate('invalid_username'),
				remote: translate("login_in_use")
			},
			agreement: translate('required_agreement'),
			email: {
				required: translate('required_email'),
				email: translate('invalid_email'),
				//remote: translate('email_in_use')
			},
			password: {
				required: translate('required_password'),
				minlength: translate('too_short_password')
			},
			password2: {
				required: translate('required_password2'),
				minlength: translate('too_short_password'),
				equalTo: translate('not_equal_passwords')
			},
			captcha: {
				required: translate('required_captcha'),
				minlength: translate('too_short_captcha'),
				maxlength: translate('too_long_captcha'),
				//remote: translate('incorrect_captcha')
			},
			lastName : {
				required: translate('required_last_name')
			},
			firstName: {
				required: translate('required_first_name')
			},
			address: {
				required: translate('required_address')
			},
			city: {
				required: translate('required_city')
			},
			phone : {
				regex: translate('incorrect_phone_number'),
				required: translate('required_phone'),
				remote: translate('phone_in_use')
			},
			zipCode : {
				required: translate('required_zip_code'),
				regex: translate('incorrect_zip_code')
			}
		},
		errorElement: "p",
		errorClass: "warning"
	});
});