( 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("bgHilight", function(element) {
				alert("fasdf");
			}, "");

			var validator = $("#regForm").validate( {
				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 */
				$(".reg-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", "#3D362D");
				$("#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
				},
				"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,
					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."
				},
				"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 be between 6-12 characters in length. Passwords are case sensitive and can only contain letters and numbers (no special characters).",
					maxlength: "Your password must be between 6-12 characters in length. Passwords are case sensitive and can only contain letters and numbers (no special 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 be between 6-12 characters in length. Passwords are case sensitive and can only contain letters and numbers (no special 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 confirm password must be between 6-12 characters. Passwords are case sensitive and must contain letters and numbers."
				},
				"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."
				}
			}
			});

			// $("#regForm
				// #registrationProfile.specialty.role.id").bind("change",function()
				// {
				// $("#regForm #registrationProfile.specialty.role.id").valid();
				// $("#regForm #registrationProfile.specialtyId").valid();
				// });
				//			
				// $("#regForm
				// #registrationProfile.specialtyId").bind("change",function() {
				// $("#regForm #registrationProfile.specialtyId").valid();
				// $("#regForm #registrationProfile.specialty.role.id").valid();
				// });

				// check if confirm password is still valid after password
				// changed
				// $("#regForm #password").blur(function() {
				// $("#regForm #confirmPassword").valid();
				// });
				// // check if confirm password is still valid after password
				// changed
				// $("#regForm
				// #registrationProfile.emailAddress").blur(function() {
				// $("#regForm #confirmEmail").valid();
				// });

				// $("#regForm #esamplingAccess").click(function() {
				// if($("#regForm #esamplingAccess").attr("checked")==true){
				// $("#regForm #deaNumber").attr("disabled", "");
				// $("#regForm #deaNumber").attr("value","");
				// $("#regForm #deaNumberDiv").css("display", "block");
				//					
				// }else{
				// $("#regForm #deaNumber").attr("value","");
				// $("#regForm #deaNumber").attr("disabled", "disabled");
				// $("#regForm #deaNumberDiv").css("display", "none");
				// }
				// });

				// ${"#regForm"}.keypress(function(e) {
				// if(e.keyCode == 13){
				// $("#regForm").submit();
				// }
				// });
				$("#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").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");
					}

				});

				// 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();
						}
					});
				});
			});
	});

})(jQuery);