// user validate
$(document).ready(function(){

	// user login from (#userlogin_fm) -- validate login
	if($("#userlogin_fm").length>0)
	{
		$("#userlogin_fm :input[id!='REmail']").keydown(function(event){
			if(event.keyCode == 13)
			{$("#userlogin_fm #btn_login").trigger("click");}
		});

		$("#REmail").keydown(function(event){
			if(event.keyCode == 13)
			{$("#userlogin_fm #btn_recover").trigger("click");}
		});

		// recover password
		$("#forgot_pwd").toggle(
			function(){$("#recover").show();$("#recover_info,#pswinfo").hide();$("input#REmail").focus();},
			function(){$("#recover").hide();$("#recover_info,#pswinfo").hide();}
		);
		
		$("#btn_recover").click(function(){
			if ($("input#REmail").val()=="")
			{alert("Please enter your email address to recover your password.");$("input#REmail").focus();return false;}
			if (!isEmail($("input#REmail").val()))
			{alert("Please enter a valid email address.");$("input#REmail").focus();return false;}
			
			window.location.href="/recover.asp?Email="+$("input#REmail").val();
			window.event.returnValue=false;
		});

	}
	
	$("#userlogin_fm #btn_login").click(function(){
		if ($("input#UserName").val()=="")
		{alert("Please enter your email address.");$("input#UserName").focus();return false;}
		if (!isEmail($("input#UserName").val()))
		{alert("Please enter a valid email address.");$("input#UserName").focus();return false;}
		
		if ($("input#Password").val()=="")
		{alert("Please enter your password.");$("input#Password").focus();return false;}
		if ($("input#Password").val().length<5)
		{alert("Password requires a minimum 5 characters");$("input#Password").focus();return false;}
		if (!isValidInput($("input#Password").val()))
		{alert("Please enter only letter, digit, whitespace and \"-.\" characters in the \"Password\" field.");$("input#Password").focus();return false;}
		
		$("#userlogin_fm").submit();
	});
	

	// input keydown check
	if($("#newuser_fm").length>0)
	{
		$("#newuser_fm :input[id!='LoginEmail'][id!='LoginPassword'][id!='REmail']").keydown(function(event){
			if(event.keyCode == 13)
			{$("#newuser_fm #btn_reg").trigger("click");}
		});
		
		$("#LoginEmail,#LoginPassword").keydown(function(event){
			if(event.keyCode == 13)
			{$("#newuser_fm #btn_login").trigger("click");}
		});
		
		$("#REmail").keydown(function(event){
			if(event.keyCode == 13)
			{$("#newuser_fm #btn_recover").trigger("click");}
		});

		// recover password
		$("#forgot_pwd").toggle(
			function(){$("#recover").show();$("#recover_info,#pswinfo").hide();$("input#REmail").focus();},
			function(){$("#recover").hide();$("#recover_info,#pswinfo").hide();}
		);
		
		$("#btn_recover").click(function(){
			if ($("input#REmail").val()=="")
			{alert("Please enter your email address to recover your password.");$("input#REmail").focus();return false;}
			if (!isEmail($("input#REmail").val()))
			{alert("Please enter a valid email address.");$("input#REmail").focus();return false;}
			
			window.location.href="/recover.asp?Email="+$("input#REmail").val();
			window.event.returnValue=false;
		});

	}
	
	if($("#edituser_fm").length>0)
	{
		$("#edituser_fm :input").keydown(function(event){
			if(event.keyCode == 13)
			{$("#newuser_fm #btn_edit").trigger("click");}
		});
	}
	
	// newuser form (#newuser_fm) login area	--	validate login
	$("#newuser_fm #btn_login").click(function(){
		if ($("input#LoginEmail").val()=="")
		{alert("Please enter your email address.");$("input#LoginEmail").focus();return false;}
		if (!isEmail($("input#LoginEmail").val()))
		{alert("Please enter a valid email address.");$("input#LoginEmail").focus();return false;}
		
		if ($("input#LoginPassword").val()=="")
		{alert("Please enter your password.");$("input#LoginPassword").focus();return false;}
		if ($("input#LoginPassword").val().length<5)
		{alert("Password requires a minimum 5 characters");$("input#LoginPassword").focus();return false;}
		if (!isValidInput($("input#LoginPassword").val()))
		{alert("Please enter only letter, digit, whitespace and \"-.\" characters in the \"Password\" field.");$("input#LoginPassword").focus();return false;}
		
		if ($("input#TargetPage").val()=="")
		{window.location.href="/login.asp?UserName="+$("input#LoginEmail").val()+"&Password="+$("input#LoginPassword").val();window.event.returnValue=false;}
		else
		{window.location.href="/login.asp?UserName="+$("input#LoginEmail").val()+"&Password="+$("input#LoginPassword").val()+"&TargetPage="+$("input#TargetPage").val();window.event.returnValue=false;}
	});
	
	
	// newuser from (#newuser_fm) newuser area	--	validate reg
	// edituser from (#edituser_fm) edituser area	--	validate edit
	$("#newuser_fm #btn_reg, #edituser_fm #btn_edit").click(function(){
		//email and password
		if ($("input#Email").val()=="")
		{alert("Please enter a value for the \"Email\" field.");$("input#Email").focus();return false;}
		if (!isEmail($("input#Email").val()))
		{alert("Please enter a valid email address");$("input#Email").focus();return false;}

		if ($("input#Password").val()=="")
		{alert("Please enter a value for the \"Password\" field.");$("input#Password").focus();return false;}
		if ($("input#Password").val().length<5)
		{alert("Please enter at least 5 characters in the \"Password\" field.");$("input#Password").focus();return false;}
		if ($("input#Password").val().length>50)
		{alert("Please enter at most 50 characters in the \"Password\" field.");$("input#Password").focus();return false;}
		if (!isValidInput($("input#Password").val()))
		{alert("Please enter only letter, digit, whitespace and \"-.\" characters in the \"Password\" field.");$("input#Password").focus();return false;}
		if ($("input#Password2").val()!=$("input#Password").val())
		{alert("Please re-enter your password so that it matches.");$("input#Password2").focus();return false;}
		
		
		//billing information
		if ($("input#FirstName").val()=="")
		{alert("Please enter a value for the \"First Name\" field.");$("input#FirstName").focus();return false;}
		if ($("input#FirstName").val().length<2)
		{alert("Please enter at least 2 characters in the \"First Name\" field.");$("input#FirstName").focus();return false;}
		if ($("input#FirstName").val().length>50)
		{alert("Please enter at most 50 characters in the \"First Name\" field.");$("input#FirstName").focus();return false;}
		if (!isValidInput($("input#FirstName").val()))
		{alert("Please enter only letter, whitespace and \"-.\" characters in the \"First Name\" field.");$("input#FirstName").focus();return false;}
		
		if ($("input#LastName").val()=="")
		{alert("Please enter a value for the \"Last Name\" field.");$("input#LastName").focus();return false;}
		if ($("input#LastName").val().length<2)
		{alert("Please enter at least 2 characters in the \"Last Name\" field.");$("input#LastName").focus();return false;}
		if ($("input#LastName").val().length>50)
		{alert("Please enter at most 50 characters in the \"Last Name\" field.");$("input#LastName").focus();return false;}
		if (!isValidInput($("input#LastName").val()))
		{alert("Please enter only letter, whitespace and \"-.\" characters in the \"Last Name\" field.");$("input#LastName").focus();return false;}
		
		if ($("input#Address1").val()=="")
		{alert("Please enter a value for the \"Address\" field.");$("input#Address1").focus();return false;}
		if ($("input#Address1").val().length<5)
		{alert("Please enter at least 5 characters in the \"Address\" field.");$("input#Address1").focus();return false;}
		if ($("input#Address1").val().length>50)
		{alert("Please enter at most 50 characters in the \"Address\" field.");$("input#Address1").focus();return false;}
		//if (!isValidInput($("input#Address1").val()))
		if (!(/^([a-zA-Z0-9]|[ .,-]){1,}$/.test($("input#Address1").val())))
		{alert("Please enter only letter, digit, whitespace and \"-.,\" characters in the \"Address\" field.");$("input#Address1").focus();return false;}
		
		if ($("input#City").val()=="")
		{alert("Please enter a value for the \"City\" field.");$("input#City").focus();return false;}
		if ($("input#City").val().length<2)
		{alert("Please enter at least 2 characters in the \"City\" field.");$("input#City").focus();return false;}
		if ($("input#City").val().length>25)
		{alert("Please enter at most 25 characters in the \"City\" field.");$("input#City").focus();return false;}
		if (!(/^[a-z A-Z]{2,}$/.test($("input#City").val())))
		{alert("Please enter only letter in the \"City\" field.");$("input#City").focus();return false;}
		
		//--	when state field is input box
		//if ($("input#State").val()=="")
		//{alert("Please enter a value for the \"State\" field.");$("input#State").focus();return false;}
		//if ($("input#State").val().length<2)
		//{alert("Please enter at least 2 characters in the \"State\" field.");$("input#State").focus();return false;}
		//if ($("input#State").val().length>25)
		//{alert("Please enter at most 25 characters in the \"State\" field.");$("input#State").focus();return false;}
		//if (!(/^[a-zA-Z]{2,}$/.test($("input#State").val())))
		//{alert("Please enter only letter in the \"State\" field.");$("input#State").focus();return false;}
		
		if ($("select#State").val()=="")
		{alert("Please select an option for the \"State\" field.");$("select#State").focus();return false;}
		
		if ($("input#PostalCode").val()=="")
		{alert("Please enter a value for the \"Zip Code\" field.");$("input#PostalCode").focus();return false;}
		if ($("input#PostalCode").val().length<5)
		{alert("Please enter at least 5 characters in the \"Zip Code\" field.");$("input#PostalCode").focus();return false;}
		if ($("input#PostalCode").val().length>15)
		{alert("Please enter at most 15 characters in the \"Zip Code\" field.");$("input#PostalCode").focus();return false;}
		
		if ($("select#Country").val()=="USA")
		{
			if (!(/^([0-9]|[ -]){2,}$/.test($("input#PostalCode").val())))
			{alert("Please enter only digit, whitespace and \"-\" characters in the \"Zip Code\" field.");$("input#PostalCode").focus();return false;}
		}
		else
		{
			if (!(/^([a-z0-9A-Z]|[ -]){2,}$/.test($("input#PostalCode").val())))
			{alert("Please enter only letter, digit, whitespace and \"-\" characters in the \"Zip Code\" field.");$("input#PostalCode").focus();return false;}
		}
		
		if ($("input#Phone").val()=="")
		{alert("Please enter a value for the \"Phone\" field.");$("input#Phone").focus();return false;}
		if ($("input#Phone").val().length<10)
		{alert("Please enter at least 10 characters in the \"Phone\" field.");$("input#Phone").focus();return false;}
		if ($("input#Phone").val().length>15)
		{alert("Please enter at most 15 characters in the \"Phone\" field.");$("input#Phone").focus();return false;}
		if (!(/[0-9( )-]{10,}/.test($("input#Phone").val())))
		{alert("Please enter only digit and \"-\" characters in the \"Phone\" field.");$("input#Phone").focus();return false;}
		
		
		//shipping information
		if ($("input#ShipFirstName").val()=="")
		{alert("Please enter a value for the \"Shipping First Name\" field.");$("input#ShipFirstName").focus();return false;}
		if ($("input#ShipFirstName").val().length<2)
		{alert("Please enter at least 2 characters in the \"Shipping First Name\" field.");$("input#ShipFirstName").focus();return false;}
		if ($("input#ShipFirstName").val().length>50)
		{alert("Please enter at most 50 characters in the \"Shipping First Name\" field.");$("input#ShipFirstName").focus();return false;}
		if (!isValidInput($("input#ShipFirstName").val()))
		{alert("Please enter only letter, whitespace and \"-.\" characters in the \"Shipping First Name\" field.");$("input#ShipFirstName").focus();return false;}
		
		if ($("input#ShipLastName").val()=="")
		{alert("Please enter a value for the \"Shipping Last Name\" field.");$("input#ShipLastName").focus();return false;}
		if ($("input#ShipLastName").val().length<2)
		{alert("Please enter at least 2 characters in the \"Shipping Last Name\" field.");$("input#ShipLastName").focus();return false;}
		if ($("input#ShipLastName").val().length>50)
		{alert("Please enter at most 50 characters in the \"Shipping Last Name\" field.");$("input#ShipLastName").focus();return false;}
		if (!isValidInput($("input#ShipLastName").val()))
		{alert("Please enter only letter, whitespace and \"-.\" characters in the \"Shipping Last Name\" field.");$("input#ShipLastName").focus();return false;}

		if ($("input#ShipAddress1").val()=="")
		{alert("Please enter a value for the \"Shipping Address\" field.");$("input#ShipAddress1").focus();return false;}
		if ($("input#ShipAddress1").val().length<5)
		{alert("Please enter at least 5 characters in the \"Shipping Address\" field.");$("input#ShipAddress1").focus();return false;}
		if ($("input#ShipAddress1").val().length>50)
		{alert("Please enter at most 50 characters in the \"Shipping Address\" field.");$("input#ShipAddress1").focus();return false;}
		//if (!isValidInput($("input#ShipAddress1").val()))
		if (!(/^([a-zA-Z0-9]|[ .,-]){1,}$/.test($("input#Address1").val())))
		{alert("Please enter only letter, digit, whitespace and \"-.,\" characters in the \"Shipping Address\" field.");$("input#ShipAddress1").focus();return false;}

		if ($("input#ShipCity").val()=="")
		{alert("Please enter a value for the \"Shipping City\" field.");$("input#ShipCity").focus();return false;}
		if ($("input#ShipCity").val().length<2)
		{alert("Please enter at least 2 characters in the \"Shipping City\" field.");$("input#ShipCity").focus();return false;}
		if ($("input#ShipCity").val().length>25)
		{alert("Please enter at most 25 characters in the \"Shipping City\" field.");$("input#ShipCity").focus();return false;}
		if (!(/^[a-z A-Z]{2,}$/.test($("input#ShipCity").val())))
		{alert("Please enter only letter in the \"Shipping City\" field.");$("input#ShipCity").focus();return false;}

		//--	when shipstate field is input box
		//if ($("input#ShipState").val()=="")
		//{alert("Please enter a value for the \"Shipping State\" field.");$("input#ShipState").focus();return false;}
		//if ($("input#ShipState").val().length<2)
		//{alert("Please enter at least 2 characters in the \"Shipping State\" field.");$("input#ShipState").focus();return false;}
		//if ($("input#ShipState").val().length>25)
		//{alert("Please enter at most 25 characters in the \"Shipping State\" field.");$("input#ShipState").focus();return false;}
		//if (!(/^[a-zA-Z]{2,}$/.test($("input#ShipState").val())))
		//{alert("Please enter only letter in the \"Shipping State\" field.");$("input#ShipState").focus();return false;}
		
		if ($("select#ShipState").val()=="")
		{alert("Please select an option for the \"Shipping State\" field.");$("select#ShipState").focus();return false;}
		
		if ($("input#ShipPostalCode").val()=="")
		{alert("Please enter a value for the \"Shipping Zip Code\" field.");$("input#ShipPostalCode").focus();return false;}
		if ($("input#ShipPostalCode").val().length<5)
		{alert("Please enter at least 5 characters in the \"Shipping Zip Code\" field.");$("input#ShipPostalCode").focus();return false;}
		if ($("input#ShipPostalCode").val().length>15)
		{alert("Please enter at most 15 characters in the \"Shipping Zip Code\" field.");$("input#ShipPostalCode").focus();return false;}
		
		if ($("select#ShipCountry").val()=="USA")
		{
			if (!(/^([0-9]|[ -]){2,}$/.test($("input#ShipPostalCode").val())))
			{alert("Please enter only digit, whitespace and \"-\" characters in the \"Shipping Zip Code\" field.");$("input#ShipPostalCode").focus();return false;}
		}
		else
		{
			if (!(/^([a-z0-9A-Z]|[ -]){2,}$/.test($("input#ShipPostalCode").val())))
			{alert("Please enter only letter, digit, whitespace and \"-\" characters in the \"Shipping Zip Code\" field.");$("input#ShipPostalCode").focus();return false;}
		}
		

		if ($("input#ShipPhone").val()=="")
		{alert("Please enter a value for the \"Phone\" field.");$("input#ShipPhone").focus();return false;}
		if ($("input#ShipPhone").val().length<10)
		{alert("Please enter at least 10 characters in the \"Phone\" field.");$("input#ShipPhone").focus();return false;}
		if ($("input#ShipPhone").val().length>15)
		{alert("Please enter at most 15 characters in the \"Phone\" field.");$("input#ShipPhone").focus();return false;}
		if (!(/[0-9( )-]{10,}/.test($("input#ShipPhone").val())))
		{alert("Please enter only digit and \"-\" characters in the \"Phone\" field.");$("input#ShipPhone").focus();return false;}
		
		if($("#newuser_fm").length>0)
		{$("#newuser_fm").submit();}
		else
		{$("#edituser_fm").submit();}

	});
	
	// edituser form (#edituser_fm) -- state selected option fix
	$("#edituser_fm Select#State").val($("#edituser_fm Select#State").attr("title"));
	$("#edituser_fm Select#ShipState").val($("#edituser_fm Select#ShipState").attr("title"));
	
	// state or country change events
	$("Select#State,Select#ShipState").change(function(){
		var obj=$(this).attr("id").replace("State","")+"Country";
		if($(this).children("option:selected").is(".canda"))
		{$("#"+obj).val("Canada");}
		else
		{$("#"+obj).val("USA");}
	});
	
	$("Select#Country,Select#ShipCountry").change(function(){
		var obj=$(this).attr("id").replace("Country","")+"State";
		if($(this).val()=="USA")
		{$("#"+obj).val("AL");}
		else
		{$("#"+obj).val("AB");}
	});

	// shipping same as billing
	$("input#FillFields").click(function(){
		if ($("input#FillFields:checked").val()=="on")
		{
			$("input#ShipFirstName").val($("input#FirstName").val());
			$("input#ShipLastName").val($("input#LastName").val());
			$("input#ShipAddress1").val($("input#Address1").val());
			$("input#ShipAddress2").val($("input#Address2").val());
			$("input#ShipCity").val($("input#City").val());
			$("input#ShipPostalCode").val($("input#PostalCode").val());
			$("select#ShipState").val($("select#State").val());
			$("select#ShipCountry").val($("select#Country").val());
			$("input#ShipPhone").val($("input#Phone").val());
		}
	});


	$("#wholesale_reg_fm").submit(function(){
		if ($("input#FirstName").val()=="")
		{alert("Please enter a value for the \"First Name\" field.");$("input#FirstName").focus();return false;}
		if ($("input#FirstName").val().length<2)
		{alert("Please enter at least 2 characters in the \"First Name\" field.");$("input#FirstName").focus();return false;}
		if ($("input#FirstName").val().length>50)
		{alert("Please enter at most 50 characters in the \"First Name\" field.");$("input#FirstName").focus();return false;}
		if (!isValidInput($("input#FirstName").val()))
		{alert("Please enter only letter, whitespace and \"-.\" characters in the \"First Name\" field.");$("input#FirstName").focus();return false;}
		
		if ($("input#LastName").val()=="")
		{alert("Please enter a value for the \"Last Name\" field.");$("input#LastName").focus();return false;}
		if ($("input#LastName").val().length<2)
		{alert("Please enter at least 2 characters in the \"Last Name\" field.");$("input#LastName").focus();return false;}
		if ($("input#LastName").val().length>50)
		{alert("Please enter at most 50 characters in the \"Last Name\" field.");$("input#LastName").focus();return false;}
		if (!isValidInput($("input#LastName").val()))
		{alert("Please enter only letter, whitespace and \"-.\" characters in the \"Last Name\" field.");$("input#LastName").focus();return false;}
		
		if ($("input#Address1").val()=="")
		{alert("Please enter a value for the \"Address\" field.");$("input#Address1").focus();return false;}
		if ($("input#Address1").val().length<5)
		{alert("Please enter at least 5 characters in the \"Address\" field.");$("input#Address1").focus();return false;}
		if ($("input#Address1").val().length>50)
		{alert("Please enter at most 50 characters in the \"Address\" field.");$("input#Address1").focus();return false;}
		if (!(/^([a-zA-Z0-9]|[ .,-]){1,}$/.test($("input#Address1").val())))
		{alert("Please enter only letter, digit, whitespace and \"-.,\" characters in the \"Address\" field.");$("input#Address1").focus();return false;}
		
		if ($("input#City").val()=="")
		{alert("Please enter a value for the \"City\" field.");$("input#City").focus();return false;}
		if ($("input#City").val().length<2)
		{alert("Please enter at least 2 characters in the \"City\" field.");$("input#City").focus();return false;}
		if ($("input#City").val().length>25)
		{alert("Please enter at most 25 characters in the \"City\" field.");$("input#City").focus();return false;}
		if (!(/^[a-z A-Z]{2,}$/.test($("input#City").val())))
		{alert("Please enter only letter in the \"City\" field.");$("input#City").focus();return false;}
		
		if ($("input#State").val()=="")
		{alert("Please enter a value for the \"State\" field.");$("input#State").focus();return false;}
		if ($("input#State").val().length<2)
		{alert("Please enter at least 2 characters in the \"State\" field.");$("input#State").focus();return false;}
		if ($("input#State").val().length>25)
		{alert("Please enter at most 25 characters in the \"State\" field.");$("input#State").focus();return false;}
		if (!(/^[a-z A-Z]{2,}$/.test($("input#State").val())))
		{alert("Please enter only letter and whitespace characters in the \"State\" field.");$("input#State").focus();return false;}

		if ($("input#PostalCode").val()=="")
		{alert("Please enter a value for the \"Zip Code\" field.");$("input#PostalCode").focus();return false;}
		if ($("input#PostalCode").val().length<5)
		{alert("Please enter at least 5 characters in the \"Zip Code\" field.");$("input#PostalCode").focus();return false;}
		if ($("input#PostalCode").val().length>15)
		{alert("Please enter at most 15 characters in the \"Zip Code\" field.");$("input#PostalCode").focus();return false;}
		if (!(/^([a-z0-9A-Z]|[ -]){2,}$/.test($("input#PostalCode").val())))
		{alert("Please enter only letter, digit, whitespace and \"-\" characters in the \"Zip Code\" field.");$("input#PostalCode").focus();return false;}
		
		if ($("input#Country").val()=="")
		{alert("Please enter a value for the \"Country\" field.");$("input#Country").focus();return false;}
		if ($("input#Country").val().length<2)
		{alert("Please enter at least 2 characters in the \"Country\" field.");$("input#Country").focus();return false;}
		if ($("input#Country").val().length>25)
		{alert("Please enter at most 25 characters in the \"Country\" field.");$("input#Country").focus();return false;}
		if (!(/^[a-z A-Z]{2,}$/.test($("input#Country").val())))
		{alert("Please enter only letter and whitespace characters in the \"Country\" field.");$("input#Country").focus();return false;}
		
		if ($("input#Phone").val()=="")
		{alert("Please enter a value for the \"Phone\" field.");$("input#Phone").focus();return false;}
		if ($("input#Phone").val().length<10)
		{alert("Please enter at least 10 characters in the \"Phone\" field.");$("input#Phone").focus();return false;}
		if ($("input#Phone").val().length>15)
		{alert("Please enter at most 15 characters in the \"Phone\" field.");$("input#Phone").focus();return false;}
		if (!(/[0-9( )-]{10,}/.test($("input#Phone").val())))
		{alert("Please enter only digit and \"-\" characters in the \"Phone\" field.");$("input#Phone").focus();return false;}
		
		if ($("textarea#Comments").val()=="")
		{alert("Please enter a value for the \"Comments\" field.");$("textarea#Comments").focus();return false;}
		if ($("textarea#Comments").val().length<10)
		{alert("Please enter at least 10 characters in the \"Comments\" field.");$("textarea#Comments").focus();return false;}
		if ($("textarea#Comments").val().length>255)
		{alert("Please enter at most 255 characters in the \"Comments\" field.");$("textarea#Comments").focus();return false;}
		if ($("textarea#Comments").val()=="")
		{alert("Please enter a value for the \"Comments\" field.");$("textarea#Comments").focus();return false;}
		
	});


});



function killErrors(){
	return true;
}
window.onerror = killErrors;