( function($) {
	$( function() {
		// search form, hide it, search labels to modify, filter classes nocmx and error
	});

	$( function() {
		$().ready( function() {
			jQuery.validator.addMethod("emptyCheck", function(value, element) {
				return value != '' && value != '0';
			}, " the format is not valid");
			
			jQuery.validator.addMethod("alphaNumericCheck", function(value, element) {
				var reg = /^[a-zA-Z0-9]+$/;
				return this.optional(element) || reg.test(value);
			}, " the format is not valid");
			
			jQuery.validator.addMethod("nameCheck", function(value, element) {
				var reg = /^[a-zA-Z '-]+$/;
				return this.optional(element) || reg.test(value);
			}, " the format is not valid");
			
			jQuery.validator.addMethod("passwordCheck", function(value, element) {
				//var reg = /^[A-Za-z]+[0-9]+[A-Za-z0-9]*|[0-9]+[A-Za-z]+[A-Za-z0-9]*$/;
				var regNum = /[0-9]+/;
				var regAlpha = /[a-zA-Z]+/;
				if (regNum.test(value) && regAlpha.test(value))
					return true;
				return false;
			}, " the format is not valid");
			
			jQuery.validator.addMethod("npiNumberCheck", function(value, element) {
				if (!checkNpiNumber(value)) {
					return false;
				} else {
					return true;
				}
			}, " the format is not valid");
			
			jQuery.validator.addMethod("cityCheck", function(value, element) {
				return checkCity(value);
			}, " the format is not valid");

			validationConfig = {
				errorClass: "red",
				onfocusin: function(element) {
					// this.lastActive = element;

					// hide error label and remove error class on focus if enabled
					// if ( this.settings.focusCleanup && !this.blockFocusCleanup )
					// {
					if (element.name != "registrationProfile.title") {
						//this.settings.unhighlight && this.settings.unhighlight.call( this, element, this.settings.errorClass );
						// this.errorsFor(element).hide();
					}
					//}
				},
				onfocusout: function(element) {
					//this.settings.unhighlight && this.settings.unhighlight.call( this, element, this.settings.errorClass );
					// this.errorsFor(element).hide();
				},
				onkeyup: function(element) {
				},
				onclick: function(element) {
					//if ( this.settings.focusCleanup && !this.blockFocusCleanup ) {
					//this.settings.unhighlight && this.settings.unhighlight.call( this, element, this.settings.errorClass );
					// this.errorsFor(element).hide();
					// }
				},
				highlight: function(element, errorClass) {
					$("#regForm").find("label[for=" + element.name + "]").css("color", "#F00");
					$("#regForm").find("label[for=" + element.name + "]").css("display", "block");
	
					/* set bg yellow */
					$("#regForm table").addClass("borderSpacing");
					$("#regForm").find("label[for=" + element.name + "]").parent().css( {
						background: "#FEF7D0"
					});
				},
				unhighlight: function(element, errorClass) {
					$("#regForm").find("label[for=" + element.name + "]").css("color", "#746C61");
					$("#regForm").find("label[for=" + element.name + "]").css("display", "block");
					/* reset bg yellow */
					$("#regForm").find("label[for=" + element.name + "]").parent().css( {
						background: "none"
					});
				},
				check: function(element) {
					$("#regForm").find("label[for=" + element.name + "]").css("display", "block");
				},
				rules: {
					"registrationProfile.title": {
						required: true
					},
					"registrationProfile.roleId": {
						emptyCheck: true
					},
					//"registrationProfile.specialtyId" : {emptyCheck:true},
					"registrationProfile.firstName": {
						required: true,
						maxlength: 24,
						nameCheck: true
					},
					"registrationProfile.lastName": {
						required: true,
						maxlength: 24,
						nameCheck: true
					},
					"registrationProfile.address1": {
						required: true,
						maxlength: 100
					},
					"registrationProfile.address2": {
						maxlength: 100
					},
					"registrationProfile.city": {
						required: true,
						maxlength: 20,
						cityCheck: true
					},
					"registrationProfile.state": {
						required: true
					},
					"registrationProfile.zip": {
						required: true,
						digits: true,
						rangelength: [5, 5]
					},
					//"zipExt" : {digits:true, rangelength:[4,4]},
					"registrationProfile.emailAddress": {
						required: true,
						email: true,
						maxlength: 75,
						equalTo: '[id=confirmEmail]'
					},
					"confirmEmail": {
						required: true,
						email: true,
						equalTo: '[id=registrationProfile.emailAddress]'
					},
					"regpassword": {
						required: true,
						minlength: 6,
						maxlength: 12,
						equalTo: '[id=confirmPassword]',
						passwordCheck: true
					},
					"confirmPassword": {
						required: true,
						equalTo: '[id=regpassword]',
						passwordCheck: true
					},
					"npiNumber": {
						required: true,
						digits: true,
						rangelength: [10, 10],
						npiNumberCheck: true
					}
				},
				messages: {
					"registrationProfile.title": {
						required: "Please fill in Title."
					},
					"registrationProfile.roleId": {
						emptyCheck: "Please fill in Role."
					},
					//"registrationProfile.specialtyId":{
					//	emptyCheck : "Please fill in Specialty."
					// },
					"registrationProfile.firstName": {
						required: "Please fill in First Name.",
						maxlength: "Please enter valid First Name using letters only.",
						nameCheck: "Please enter valid First Name using letters only."
					},
					"registrationProfile.lastName": {
						required: "Please fill in Last Name.",
						maxlength: "Please enter valid Last Name using letters only.",
						nameCheck: "Please enter valid Last Name using letters only."
					},
					"registrationProfile.address1": {
						required: "Please fill in Practice Address Line 1.",
						maxlength: "Pratice Address line1 must enter no more than 100 characters."
					},
					"registrationProfile.address2": "Pratice Address line2 must enter no more than 100 characters.",
					"registrationProfile.city": {
						required: "Please fill in City.",
						maxlength: "City must enter no more than 20 characters.",
						cityCheck: "The City you have entered is invalid."
					},
					"registrationProfile.state": "Please fill in State.",
					"registrationProfile.zip": {
						required: "Please fill in Zip code.",
						digits: "Please use a 5-digit Zip code.",
						rangelength: "Please use a 5-digit Zip code."
					},
					//"zipExt"	:{
					//	digits:	"Zip Code extension must be numeric.",
					//	rangelength : "Zip Code extension must be four length."
					// },
					"registrationProfile.emailAddress": {
						required: "Please fill in E-mail Address.",
						email: "A valid e-mail address is required. Please re-enter e-mail address in the proper format (name@domain.com).",
						equalTo: " The e-mail addresses you entered do not match. Please carefully re-enter your e-mail address."
					},
					"confirmEmail": {
						required: "Please fill in Confirm E-mail Address.",
						email: "A valid e-mail address is required. Please re-enter e-mail address in the proper format (name@domain.com).",
						equalTo: " The e-mail addresses you entered do not match. Please carefully re-enter your e-mail address."
					},
					"regpassword": {
						required: "Please fill in Password.",
						minlength: "Your password must contain BOTH letters and numbers and be between 6-12 characters.",
						maxlength: "Your password must contain BOTH letters and numbers and be between 6-12 characters.",
						equalTo: "Password and confirmed password do not match. Please ensure the caps lock is off and retype your password carefully.",
						passwordCheck: "Your password must contain BOTH letters and numbers and be between 6-12 characters."
					},
					"confirmPassword": {
						required: "Please fill in Confirm Password.",
						equalTo: "Password and confirmed password do not match. Please ensure the caps lock is off and retype your password carefully.",
						passwordCheck: "Your password must contain BOTH letters and numbers and be between 6-12 characters."
					},
					"npiNumber": {
						required: "Please enter a full 10-digit individual NPI Number.",
						digits: "Please enter a full 10-digit individual NPI Number.",
						rangelength: "Please enter a full 10-digit individual NPI Number.",
						npiNumberCheck: "The NPI number you have entered cannot be verified. Please check the number and try again."
					}
				}
			};

			validator = $("#regForm").validate(validationConfig);
			
			$("#regForm #reset").bind("click", function() {
				if (confirm("Are you sure you want to cancel?")) {
					//$("#regForm").resetForm();
					window.location.href = ctx + "/login/";
				}
			});
			
			$("#regForm #regSubBtn").click( function() {
				$("#regForm #password").attr("value", $("#regForm #regpassword").val());
				if (validator.form()) {
					$("#regForm #error-log").css("display", "none");
					// $("#regForm #errot-image").css("display","none");
					$("#regForm").submit();
				} else {
					var regNum = /[0-9]+/;
					var regAlpha = /[a-zA-Z]+/;
					if($("#regForm #password").val() != $("#regForm #confirmPassword").val()
							|| !(regNum.test($("#regForm #password").val()) && regAlpha.test($("#regForm #password").val()))
							|| !(regNum.test($("#regForm #confirmPassword").val()) && regAlpha.test($("#regForm #confirmPassword").val()))
							|| $("#regForm #password").val().length<6 || $("#regForm #password").val().length >12
							|| $("#regForm #confirmPassword").val().length<6 || $("#regForm #confirmPassword").val().length >12) {
						$("#regForm #regpassword").attr("value", "");
						$("#regForm #password").attr("value", "");
						$("#regForm #confirmPassword").attr("value", "");
					}
					$("#regForm #error-log").css("display", "block");
					// $("#regForm #errot-image").css("display","block");
				}
			});

			// for enter keyboard submit
			$("#regForm :input").each( function() {
				$(this).keypress( function(e) {
					var key = window.event ? e.keyCode : e.which;
					if (key.toString() == "13") {
						$("#regForm #regSubBtn").click();
					}
				});
			});
		});
	});
	
	function checkCity(city) {
		if(/^[^@]*$/.test(city)) {
			return true;
		}else {
			return false;
		}
	}
})(jQuery);
