// email,digit,other fields validate
function isEmail(strEmail){
	var patrn = /^[\w\-\.]+@[\w\-\.]+(\.\w+)+$/;
	return patrn.test(strEmail);
}
function isValidDigit(s) 
{ 
	var patrn=/^[0-9]{1,20}$/; 
	if (!patrn.exec(s)) return false;
	return true;
} 
function isValidInput(s)
{
	var patrn=/^([a-zA-Z0-9]|[., -#]){1,}$/; 
	if (!patrn.exec(s)) return false;
	return true;
}


$(document).ready(function(){

	// shipping validate
	$("#shipping_usa_fm").submit(function(){
		if ($.trim($("input:checked[name='ShipMethod']").val())=="")
		{alert("Please select a shipping method.");return false;}
	});
	
	// credit card info validate
	$("#confirmorder_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 ($("select#CCType").val()=="1")// Visa	13 or 16 digit
		{
			if (!($("input#CCNumber").val().length==13 || $("input#CCNumber").val().length==16) || !isValidDigit($("input#CCNumber").val()))
			{alert("Please enter 13 or 16 digit in the \"Credit Card Number\" field.");$("input#CCNumber").focus();return false;}
		}
		else if ($("select#CCType").val()=="2")// MasterCard	16 digit
		{
			if ($("input#CCNumber").val().length!=16 || !isValidDigit($("input#CCNumber").val()))
			{alert("Please enter 16 digit in the \"Credit Card Number\" field.");$("input#CCNumber").focus();return false;}
		}
		else if ($("select#CCType").val()=="3")// AMEX	15 digit
		{
			if ($("input#CCNumber").val().length!=15 || !isValidDigit($("input#CCNumber").val()))
			{alert("Please enter 15 digit in the \"Credit Card Number\" field.");$("input#CCNumber").focus();return false;}
		}
		else
		{}
		
		if ($("input#CSC").val().length<3 || !isValidDigit($("input#CSC").val()))
		{alert("Please enter at least 3 digit in the \"Security Code\" field.");$("input#CSC").focus();return false;}

	});
	
});


// user validate
$(document).ready(function(){

	// user login from (#userlogin_fm) -- validate login -- login.asp
	$("#userlogin_fm input#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;}
	});
	
	
	
	// newuser form (#newuser_fm) login area	--	validate login
	$("#newuser_fm input#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;}
	
		window.location.href="/login.asp?UserName="+$("input#LoginEmail").val()+"&Password="+$("input#LoginPassword").val();
	});
	
	
	// newuser from (#newuser_fm) newuser area	--	validate reg
	// edituser from (#edituser_fm) edituser area	--	validate edit
	$("#newuser_fm input#btn_reg, #edituser_fm input#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()))
		{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 (!isValidInput($("input#City").val()))
		{alert("Please enter only letter, digit, whitespace and \"-.,\" characters in the \"City\" field.");$("input#City").focus();return false;}
		//if (!(/^[a-zA-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>10)
		{alert("Please enter at most 10 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 in the \"Zip Code\" field.");$("input#PostalCode").focus();return false;}
		
		if ($("select#Country").val()=="USA")
		{
			if ($("input#PostalCode").val().length!=5 || !isValidDigit($("input#PostalCode").val()))
			{alert("Please enter 5 digit 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()))
		{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 (!isValidInput($("input#ShipCity").val()))
		{alert("Please enter only letter, digit, whitespace and \"-.,\" characters in the \"Shipping City\" field.");$("input#ShipCity").focus();return false;}
		//if (!(/^[a-zA-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>10)
		{alert("Please enter at most 10 characters in the \"Shipping Zip Code\" field.");$("input#ShipPostalCode").focus();return false;}
		if (!(/^[a-z0-9A-Z]{2,}$/.test($("input#ShipPostalCode").val())))
		{alert("Please enter only letter, digit in the \"Shipping Zip Code\" field.");$("input#ShipPostalCode").focus();return false;}
		
		if ($("select#ShipCountry").val()=="USA")
		{
			if ($("input#ShipPostalCode").val().length!=5 || !isValidDigit($("input#ShipPostalCode").val()))
			{alert("Please enter 5 digit in the \"Shipping Zip Code\" field.");$("input#ShipPostalCode").focus();return false;}
		}
		
	});
	
	// 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());
		}
	});


});
