$(document).ready(function(){
//Begin: Validate login form element when the form is submitted
//************ BEGIN Client Registration validation Part ************
	$("#client_reg").submit(function(){
	var companyName      = $("#companyName"),
		contactName      = $("#contactName"),
		city			 =	$('#city'),
		state			 =	$('#state'),
		zipCode			 =	$('#zipCode'),
		emailAddress1	 =	$('#emailAddress1'),
		verifyemail1	 =	$('#verifyemail1'),
		phoneNumber		 =	$('#phoneNumber'),
		emailAddress2	 =	$('#emailAddress2'),
		verfiyemail2	 =	$('#verfiyemail2'),
		address1		 =	$('#address1'),
		password		 =	$('#password'),
		confirmpassword	 =	$('#confirmpassword'),
		allContainerArray = new Array('companyname_msg','contactname_msg','address_msg','city_msg','state_msg','zipcode_msg','email1_msg','email1verification_msg','phonenumber_msg','email2_msg','email2verification_msg','password_msg','confrmpassword_msg');
		allFields 		  = $([]).add(companyName).add(contactName).add(city).add(state).add(zipCode).add(emailAddress2).add(verfiyemail2).add(password).add(confirmpassword).add(emailAddress1).add(emailAddress1).add(verifyemail1).add(phoneNumber);
		allFields.removeClass('ui-state-error'); //Remove error class if any attr("checked")),
		hideDomElement(allContainerArray); //Hide all error message container
		$("#errorFlag").val(0);
		checkBlank(companyName, "Company name", "companyname_msg");
		checkBlank(contactName, "Contact name", "contactname_msg");
		checkBlank(address1,"Address", "address_msg");
		checkBlank(city, "City", "city_msg");
		checkBlank(state, "State", "state_msg");
		checkBlank(zipCode, "Zip code", "zipcode_msg");
		checkBlank(phoneNumber, "Phone Number ", "phonenumber_msg");
		checkBlank(emailAddress1, "Email Address", "email1_msg");
		checkBlank(verifyemail1, "Verify Email Address", "email1verification_msg");
		checkBlank(password,"Password","password_msg");
		checkLength(password,"Password",8,25,"password_msg");
		checkBlank(confirmpassword,"Confirm password","confrmpassword_msg");
		if($("#confirmpassword").val()!='')	{
			compareElements(password,confirmpassword,"Password and confirm password does not match","confrmpassword_msg");
		}	
		e 	=	/^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-Z0-9]{2,4}$/;
		if($("#emailAddress1").val()!='') {	
				checkRegexp(emailAddress1,e,"Email address format is invalid.","email1_msg");
		}	
		if($("#verifyemail1").val()!='') {
			 checkRegexp(verifyemail1,e,"Verify email address format is invalid.","email1verification_msg");
		}
		if($("#emailAddress1").val()!='' && $("#verifyemail1").val()!='')
			compareElements(emailAddress1,verifyemail1,"Verify email does not match.","email1verification_msg");
		$('#email2_msg_contianer').hide();
		if($("#emailAddress2").val()!='') {
			checkRegexp(emailAddress2,e,"2nd email address format is invalid.","email2_msg");
			checkBlank(verfiyemail2, "Verfiy 2nd email address", "email2verification_msg");
		}
		if($('#verfiyemail2').val()!='') checkRegexp(verfiyemail2,e,"Verify 2nd email address format is invalid.","email2verification_msg");
		if($("#emailAddress2").val()!='' && $("#verfiyemail2").val()!='') {
		    compareElements(emailAddress2,verfiyemail2,"Verfiy 2nd email does not match.","email2verification_msg");
		}	
		if ( $("#errorFlag").val() == 1) {
			$('#validateTips').text('');
			return false;
		} else { 
			return true;
		}
    });
//************ End Client Registration validation Part ************
//************ Begiin New Client Registration ********************
$("#registerfrm").submit(function(){
								  $("#prescript_msg_container").hide();
	var fname     	    =   $("#fname"),
		lname      	    =   $("#lname"),
		city			=	$('#city'),
		state			=	$('#state'),
		phone			=	$('#phone'),
		zipcode			=	$('#zipcode'),
		address1		=	$('#add1'),
		emailaddr		=	$('#emailaddr'),
		password		=	$('#password'),
		conpassword		=	$('#conpassword'),
		countrySelect	=	$('#countrySelect'),
		dob	=	$('#dob'),
		allContainerArray = new Array('fname_msg','lname_msg','city_msg','state_msg','phone_msg','zipcode_msg','emailAddress_msg','address1_msg','countrySelect_msg','password_msg','confirmpassword_msg', 'dob_msg');
		allFields 		  = $([]).add(fname).add(lname).add(city).add(state).add(phone).add(zipcode).add(address1).add(countrySelect).add(emailaddr).add(password).add(conpassword).add(dob);
		allFields.removeClass('ui-state-error'); //Remove error class if any attr("checked")),
		hideDomElement(allContainerArray); //Hide all error message container
		$("#errorFlag").val(0);
		checkBlank(fname, "First Name", "fname_msg");
		checkBlank(lname, "Last name", "lname_msg");
		checkBlank(city,"City", "city_msg");		
		checkBlank(state, "State", "state_msg");
		checkBlank(phone, "Phone", "phone_msg");
		checkBlank(zipcode, "Zip Code", "zipcode_msg");
		checkBlank(address1, "Address", "address1_msg");		
		checkBlank(emailaddr,"E-mail","emailAddress_msg");
		checkBlank(password,"Password","password_msg");
		checkLength(password,"Password",8,25,"password_msg");
		checkBlank(conpassword,"Confirm password","confirmpassword_msg");
		checkBlank(fname, "Date of Birth", "dob_msg");
		if( ($('#countrySelect').val()) == 'NZ-New Zealand')
			{
				if( document.getElementById('emailBtn').checked == false && document.getElementById('faxBtn').checked == false) { 
					$("#prescript_msg_container").show();
					$("#errorFlag").val('1');
					return false;
				}
			}
		if ( $("#errorFlag").val() == 1) {
			$('#validateTips').text('');
			return false;
		} else { 
			return true;
		}
    });

$("#shipping").submit(function(){
if($("#dispatchFlag").val() == 0)
{
	var fname     	    =   $("#fname"),
		lname      	    =   $("#lname"),
		city			=	$('#city'),
		state			=	$('#state'),
		phone			=	$('#phone'),
		zipcode			=	$('#zipcode'),
		address1		=	$('#add1'),
		countrySelect	=	$('#countrySelect'),
		allContainerArray = new Array('fname_msg','lname_msg','city_msg','state_msg','phone_msg','address1_msg','countrySelect_msg');
		allFields 		  = $([]).add(fname).add(lname).add(city).add(city).add(state).add(phone).add(address1).add(countrySelect);
		allFields.removeClass('ui-state-error'); //Remove error class if any attr("checked")),
		hideDomElement(allContainerArray); //Hide all error message container
		$("#errorFlag").val(0);
		checkBlank(fname, "First Name", "fname_msg");
		checkBlank(lname, "Last name", "lname_msg");
		checkBlank(city,"City", "city_msg");		
		checkBlank(state, "State", "state_msg");
		checkBlank(phone, "Phone", "phone_msg");
		checkBlank(address1, "Address", "address1_msg");
		checkBlank(address1, "Country", "countrySelect_msg");
		
		if ( ($("#errorFlag").val() == 1) && ($("#dispatchFlag").val() == 0) ) {
			$('#validateTips').text('');
			return false;
		} else { 
			return true;
		}
}
    });
//************End New Client Registration*************************
//************ BEGIN Login Validation part ***********
	$("#loginfrm").submit(function(){
								  
		$('#emailAddress_msg_container').hide();
		$('#Password_msg_container').hide();
		var loginemail      = $("#emailaddr"),
		loginpassword       = $("#Password"),
		allContainerArray = new Array('emailAddress_msg','Password_msg');
		$("#errorFlag").val(0);
		checkBlank1(loginemail, "Email Address", "emailAddress_msg");
		e 	=	/^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-Z0-9]{2,4}$/;
		if($("#emailaddr").val()!='') checkRegexp1(loginemail,e,"Email address format is invalid.","emailAddress_msg");
		if($("#Password").val()=='')
		{
			$("#Password_msg_container").show();
			$("#Password_msg").text("Password is required");
			 $("#errorFlag").val('1');
		}
		//checkBlank1(loginpassword, "Password", "Password_msg");
		
		 if ( $("#errorFlag").val() == 1) {
			return false;
		} else { 
			loginExistsName();
		}
		
		return false;
	});
//************ End Login Validation part ***********
//************ BEGIN Forgot Password validation ***********
	$("#forgot_frm").submit(function(){
		var emailaddress      = $("#emailaddress"),
		allContainerArray = new Array('forgotemail_msg');
		allFields 		  = $([]).add(emailaddress);
		allFields.removeClass('ui-state-error'); //Remove error class if any attr("checked")),
		hideDomElement(allContainerArray); //Hide all error message container
		$("#errorFlag").val(0);
		e 	=	/^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-Z0-9]{2,4}$/;
		checkBlank(emailaddress, "Email Address", "forgotemail_msg");
		if($("#emailaddress").val()!='') checkRegexp(emailaddress,e,"Email address format is invalid.","forgotemail_msg");
		 if ( $("#errorFlag").val() == 1) {
			return false;
		} else { 
			checkForgotPasswordMail();
			return false;
		}
		return false;
		});
//************ END Forgot Password validation ***********
//************ BEGIN Client Order Form Validation ***********
	$("#client_order_frm").submit(function(){
		var borrowerName    = 	$("#borrowerName"),
		currentAddress		= 	$("#currentAddress"),
		currentAddress2		= 	$("#currentAddress2"),
		currentCity			= 	$("#currentCity"),
		currentState		=   $("#currentState"),			
		currentZipCode		=	$("#currentZipCode"),
		contactPhone		=	$("#contactPhone"),
		contactPhoneCode	=	$("#contactPhoneCode"),
		homeNumber			=	$("#homeNumber"),
		homeNumberCode		=	$("#homeNumberCode"),
		otherNumner			=	$("#otherNumner"),
		otherNumnerCode		=	$("#otherNumnerCode"),
		propertyAddress		=	$("#propertyAddress"),
		propertyAddress2	=	$("#propertyAddress2"),
		propertyCity		=	$("#propertyCity"),
		propertyState		=	$("#propertyState"),
		propertyZipCode		=	$("#propertyZipCode"),
		apnNumber			=	$("#apnNumber"),
		appraisalPurpose	=	$("#appraisalPurpose"),
		appraisalType		=	$("#appraisalType"),
		formType			=	$("#formType"),
		minEstimatedValue	=	$("#minEstimatedValue"),
		fee					=	$("#fee"),
		contactName			=	$("#contactName"),
		modPayment			=	$("#modPayment"),
		Addother			=	$("#Addother"),
		allContainerArray = new Array('borrwoer_msg','currentAddress_msg','currentCity_msg','currentState_msg','currentZipCode_msg','contactPhone_msg','propertyAddress_msg','propertyCity_msg','propertyState_msg','propertyZipCode_msg','appraisalPurpose_msg','appraisalType_msg','formType_msg','minEstimatedValue_msg','fee_msg','modPayment_msg','other_msg','homeNumber_msg','otherNumner_msg');
		allFields 		  = $([]).add(borrowerName).add(currentAddress).add(currentAddress2).add(currentCity).add(currentState).add(currentZipCode).add(contactPhone).add(homeNumber).add(homeNumberCode).add(otherNumner).add(otherNumnerCode).add(propertyAddress).add(propertyAddress2).add(propertyCity).add(propertyState).add(propertyZipCode).add(apnNumber).add(appraisalPurpose).add(appraisalType).add(formType).add(minEstimatedValue).add(fee).add(modPayment).add(modPayment).add(Addother);
		allFields.removeClass('ui-state-error'); //Remove error class if any attr("checked")),
		hideDomElement(allContainerArray); //Hide all error message container
		$("#contactPhone_msg_container").hide();
		$("#homeNumber_msg_container").hide();
		$("#otherNumner_msg_container").hide();
		$("#errorFlag").val(0);
		checkBlank(borrowerName, "Borrower name", "borrwoer_msg");
		checkBlank(currentAddress, "Current address", "currentAddress_msg");
		checkBlank(currentCity, "City", "currentCity_msg");
		checkBlank(currentState, "State", "currentState_msg");
		checkBlank(currentZipCode, "Zip ode", "currentZipCode_msg");
		checkBlank(contactPhone, "Contact phone", "contactPhone_msg");
		checkBlank(propertyAddress, "Property address", "propertyAddress_msg");
		checkBlank(propertyCity, "City", "propertyCity_msg");
		checkBlank(propertyState, "State", "propertyState_msg");
		checkBlank(propertyZipCode, "Zip code", "propertyZipCode_msg");
		checkBlank(appraisalPurpose, "Purpose of appraisal", "appraisalPurpose_msg");
		checkBlank(appraisalType, "Appraisal type", "appraisalType_msg");
		checkBlank(formType, "Form type", "formType_msg");
		checkBlank(minEstimatedValue, "Minimum estimated value", "minEstimatedValue_msg");
		checkBlank(fee, "Fee", "fee_msg");
		checkBlank(modPayment, "Method of payment", "modPayment_msg");
		var otherValue		=	$("input[name='other']:checked").val();
		if(otherValue==1) {
			checkBlank(Addother, "Other", "other_msg");
		}
		var contactPhonelength = $('#contactPhone').val().length;
			if($('#contactPhone').val() !='') {
				if(contactPhonelength <14) {
						$("#contactPhone").addClass('ui-state-error');
						$("#contactPhone_msg").html("Contact phone is invalid.");
						$("#contactPhone_msg_container").show();
						$("#errorFlag").val('1');
				}
			}
		var homeNumberlength = $('#homeNumber').val().length;
			if($('#homeNumber').val() !='') {
				if(homeNumberlength <14) {
						$("#homeNumber").addClass('ui-state-error');
						$("#homeNumber_msg").html("Home number is invalid.");
						$("#homeNumber_msg_container").show();
						$("#errorFlag").val('1');
				}
		}
		var otherNumnerlength = $('#otherNumner').val().length;
			if($('#otherNumner').val() !='') {
				if(homeNumberlength <14) {
						$("#homeNumber").addClass('ui-state-error');
						$("#otherNumner_msg").html("Other number is invalid.");
						$("#otherNumner_msg_container").show();
						$("#errorFlag").val('1');
				}
		}
		 if ( $("#errorFlag").val() == 1) {
			return false;
		} else { 
			return true;
		}
		return false;
	});
//************ END Client Order Form Validation ***********
//***************BEGIN: PDF upload page validation ********
	$("#attchpdf").submit(function(){
	$('#uploaderror_container').hide();
	$("#errorFlag").val('0');
	$("#pdfattachment").removeClass('ui-state-error');
		if($('#pdfattachment').val()=='') {
			$('#uploaderror_container').show();	
			$("#errorFlag").val('1');
			$("#pdfattachment").addClass('ui-state-error');
		}
		
		 if ( $("#errorFlag").val() == 1) {
			return false;
		} else { 
			return true;
		}
	});
//***************END: PDF upload page validation ********


//************ BEGIN Client promotion validation Part ************
	$("#promotionfrm").submit(function(){
	var fname      = $("#fname"),
		lname      = $("#lname"),
		emailaddr  = $('#emailaddr'),
		adrs1	   = $('#adrs1'),
		adrs2	   = 	$('#adrs2'),
		city	   =	$('#city'),
		state	   =	$('#state'),
		phone	   =	$('#phone'),
		fax	       =	$('#fax'),
		countrySelect	 =	$('#countrySelect'),
		zipcode		 =	$('#zipcode'),
	
  		allContainerArray = new Array('fname_msg','lname_msg','emailaddr_msg','adrs1_msg','adrs2_msg','city_msg','state_msg','phone_msg','fax_msg','countrySelect_msg','zipcode_msg');
		allFields 		  = $([]).add(fname).add(lname).add(emailaddr).add(adrs1).add(adrs2).add(city).add(state).add(phone).add(fax).add(countrySelect).add(zipcode);
		allFields.removeClass('ui-state-error'); //Remove error class if any attr("checked")),
		hideDomElement(allContainerArray); //Hide all error message container
		$("#errorFlag").val('0');
		checkBlank(fname, "First name", "fname_msg");
		checkBlank(lname, "Last  name", "lname_msg");
		checkBlank(emailaddr, "Email Address", "emailaddr_msg");
		
		checkBlank(adrs1,"Address", "adrs1_msg");
		checkBlank(adrs2,"Address", "adrs2_msg");
		checkBlank(city, "City", "city_msg");
		checkBlank(state, "State", "state_msg");
		checkBlank(phone, "Phone Number ", "phone_msg");
		checkBlank(countrySelect, "Country ", "countrySelect_msg");

			
		e 	=	/^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-Z0-9]{2,4}$/;
		if($("#emailaddr").val()!='') {	
				checkRegexp(emailaddr,e,"Email address format is invalid.","emailaddr_msg");
		}	
/*
		if(!($("input[@name='votedCountry']:checked").val())){
						$("#errorFlag").val('1');
						alert("please select a country to vote")
						
						
		}*/
		if ( $("#errorFlag").val() == 1) {
			$('#validateTips').text('');
			return false;
		} else { 
//alert("else ...");
//return checkPromotionMailid();
			return true;
		}
    });
//************ End Client promotions validation Part ************



}); //End jquery function

//*******Clear Text box Value ********
function clearValue(getId)
	{
		if($('#'+getId).val() == 'Enter your emall address')
			$('#'+getId).val('');
	}
function clearValue1(getId)
{
	if($('#'+getId).val() == 'AdminPass')
		$('#'+getId).val('');
}	
//******* End Clear Text box Value ********
//BEGIN: set same address

//END: set same address
function duplicateAddress()
	{
		if(document.getElementById('sameaddress').checked==true)
			{
				var currentAddress	=	$('#currentAddress').val();
				var currentAddress2	=	$('#currentAddress2').val();
				var currentCity		=	$('#currentCity').val();
				var currentState	=	$('#currentState').val();
				var currentZipCode	=	$('#currentZipCode').val();
				$('#propertyAddress').val(currentAddress);
				$('#propertyAddress2').val(currentAddress2);
				$('#propertyCity').val(currentCity);
				$('#propertyState').val(currentState);
				$('#propertyZipCode').val(currentZipCode);
			}
		else {
				$('#propertyAddress').val('');
				$('#propertyAddress2').val('');
				$('#propertyCity').val('');
				$('#propertyState').val('');
				$('#propertyZipCode').val('');
		}
	}
function changeReadonly()
{
	if(document.getElementById('other').checked==true)
		$('#Addother').removeAttr("readonly","false");
	else {
		$('#Addother').val('');
		$('#Addother').attr("readonly","true");
	}
}
function showPrescription(getvalue)
{
	if(getvalue == 'NZ-New Zealand')
	{
		$("#emailBtn").removeAttr("disabled",false);
		$("#faxBtn").removeAttr("disabled",false);
	} else {
		$("#emailBtn").attr("disabled",true);
		$("#faxBtn").attr("disabled",true);
	
	}
}
