( function($) {
	$( function() {
		
		// bind click to the submit button
		/*
		$("#regForm #regSubBtn").click( function() {
			$("#regForm #error-log").css("display", "none");
		});
		
		// bind event to the reset button
		$("#regForm #reset").bind("click", function() {
			if (confirm("Are you sure you want to cancel?")) {
				return true;
			} else {
				return false;
			}
		});
		 */
//		$("#regForm").submit(function(){
//			alert("submit");
//		}
//		
//		);
		// 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 #error-log").css("display", "none");
//					$("#regForm").submit();
//				}
//			});
//		});
		$('#npiNumber').click(showNpiPopup).focus(showNpiPopup);
                $('#emailAddress, #registrationProfile\\.emailAddress, #password, #regpassword, #userName').click(showRegPopup).focus(showRegPopup);
                $('#npiNumber, #emailAddress, #registrationProfile\\.emailAddress, #password, #regpassword, #userName').blur(hideRegPopup);

	});
	
	$( 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."
					}
				}
			};
			
			samplingValidationConfig = {
				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) {
					$("#regSamplingForm").find("label[for=" + element.name + "]").css("color", "#F00");
					//$("#regSamplingForm").find("label[for=" + element.name + "]").css("display", "block");
	
					/* set bg yellow */
					$("#regSamplingForm table").addClass("borderSpacing");
					$("#regSamplingForm").find("label[for=" + element.name + "]").parent().css( {
						background: "#FEF7D0"
					});
				},
				unhighlight: function(element, errorClass) {
					$("#regSamplingForm").find("label[for=" + element.name + "]").css("color", "#746C61");
					//$("#regSamplingForm").find("label[for=" + element.name + "]").css("display", "block");
					/* reset bg yellow */
					$("#regSamplingForm").find("label[for=" + element.name + "]").parent().css( {
						background: "none"
					});
				},
				check: function(element) {
					$("#regSamplingForm").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 enter a 5-digit Zip code.",
						rangelength: "Please enter 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 confirm 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 confirm 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);
			samplingValidator = $("#regSamplingForm").validate(samplingValidationConfig);
			
			$("#regForm #reset").bind("click", function() {
				if (confirm("Are you sure you want to cancel?")) {
					//$("#regForm").resetForm();
					window.location.href = ctx + "/login/";
				}
			});
			
			$("#regSamplingForm #regSubBtn").click( function() {
				$("#regSamplingForm #password").attr("value", $("#regSamplingForm #regpassword").val());
				if (samplingValidator.form()) {
					$("#regSamplingForm #error-log").css("display", "none");
					// $("#regForm #errot-image").css("display","none");
					$("#regSamplingForm").submit();
				} else {
					var regNum = /[0-9]+/;
					var regAlpha = /[a-zA-Z]+/;
					if($("#regSamplingForm #password").val() != $("#regSamplingForm #confirmPassword").val()
							|| !(regNum.test($("#regSamplingForm #password").val()) && regAlpha.test($("#regSamplingForm #password").val()))
							|| !(regNum.test($("#regSamplingForm #confirmPassword").val()) && regAlpha.test($("#regSamplingForm #confirmPassword").val()))
							|| $("#regSamplingForm #password").val().length<6 || $("#regSamplingForm #password").val().length >12
							|| $("#regSamplingForm #confirmPassword").val().length<6 || $("#regSamplingForm #confirmPassword").val().length >12) {
						$("#regSamplingForm #regpassword").attr("value", "");
						$("#regSamplingForm #password").attr("value", "");
						$("#regSamplingForm #confirmPassword").attr("value", "");
					}
					$("#regSamplingForm #error-log-sampling").css("display", "");
					// $("#regSamplingForm #errot-image").css("display","block");
				}
			});
			$("#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", "");
					// $("#regForm #errot-image").css("display","block");
					if (!checkNpiNumber($("#npiNumber").val())) {
						$("#npiContainer").addClass("error");
						$("#npiContainer").parent().addClass("error");
					}
				}
			});

			// 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;
		}
	}
	
	function showNpiPopup(){
		var p = $('#npi-pop');
		var label = $(this).parent().find('label[for="' + this.id + '"]');
		var pos = label.offset();
		pos.top = pos.top - p.height() + label.height() - 3;
		pos.left = pos.left + label.width() - 38;
		p.css({'zIndex': 999, 'position': 'absolute', 'left': pos.left + 'px', 'top': pos.top + 'px'});
		p.bgiframe().show();
		label.addClass('showRegPopup');
	}
	function showRegPopup(){
		var p = $('#' + $(this).attr('popupname'));
		//var label = $(this).parent().find('label[for="' + this.id + '"]');
		var label = $(this).prevAll('label[for="' + this.id + '"]');
		var pos = label.offset();
		pos.top = pos.top - p.height() + label.height();
		pos.left = pos.left + 42;
		p.css({'zIndex': 999, 'position': 'absolute', 'left': pos.left + 'px', 'top': pos.top + 'px'});
		p.bgiframe().show();
		label.addClass('showRegPopup');
	}
	function hideRegPopup(){
		var p = $('#' + $(this).attr('popupname'));
		var label = $(this).parent().find('label[for="' + this.id + '"]');
		p.hide();
		label.removeClass('showRegPopup');
	}
})(jQuery);

