// 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;
}


jQuery(document).ready(function(){

	// shipping validate
	jQuery("#shipping_usa_fm").submit(function(){
		if ($.trim(jQuery("input:checked[name='ShipMethod']").val())=="")
		{alert("Please select a shipping method.");return false;}
	});
	
	// credit card info validate
	jQuery("#confirmorder_fm").submit(function(){

		if (jQuery("input#FirstName").val()=="")
		{alert("Please enter a value for the \"First Name\" field.");jQuery("input#FirstName").focus();return false;}
		if (jQuery("input#FirstName").val().length<2)
		{alert("Please enter at least 2 characters in the \"First Name\" field.");jQuery("input#FirstName").focus();return false;}
		if (jQuery("input#FirstName").val().length>50)
		{alert("Please enter at most 50 characters in the \"First Name\" field.");jQuery("input#FirstName").focus();return false;}
		if (!isValidInput(jQuery("input#FirstName").val()))
		{alert("Please enter only letter, whitespace and \"-.,\" characters in the \"First Name\" field.");jQuery("input#FirstName").focus();return false;}
		
		if (jQuery("input#LastName").val()=="")
		{alert("Please enter a value for the \"Last Name\" field.");jQuery("input#LastName").focus();return false;}
		if (jQuery("input#LastName").val().length<2)
		{alert("Please enter at least 2 characters in the \"Last Name\" field.");jQuery("input#LastName").focus();return false;}
		if (jQuery("input#LastName").val().length>50)
		{alert("Please enter at most 50 characters in the \"Last Name\" field.");jQuery("input#LastName").focus();return false;}
		if (!isValidInput(jQuery("input#LastName").val()))
		{alert("Please enter only letter, whitespace and \"-.,\" characters in the \"Last Name\" field.");jQuery("input#LastName").focus();return false;}
		
		if (jQuery("select#CCType").val()=="1")// Visa	13 or 16 digit
		{
			if (!(jQuery("input#CCNumber").val().length==13 || jQuery("input#CCNumber").val().length==16) || !isValidDigit(jQuery("input#CCNumber").val()))
			{alert("Please enter 13 or 16 digit in the \"Credit Card Number\" field.");jQuery("input#CCNumber").focus();return false;}
		}
		else if (jQuery("select#CCType").val()=="2")// MasterCard	16 digit
		{
			if (jQuery("input#CCNumber").val().length!=16 || !isValidDigit(jQuery("input#CCNumber").val()))
			{alert("Please enter 16 digit in the \"Credit Card Number\" field.");jQuery("input#CCNumber").focus();return false;}
		}
		else if (jQuery("select#CCType").val()=="3")// AMEX	15 digit
		{
			if (jQuery("input#CCNumber").val().length!=15 || !isValidDigit(jQuery("input#CCNumber").val()))
			{alert("Please enter 15 digit in the \"Credit Card Number\" field.");jQuery("input#CCNumber").focus();return false;}
		}
		else
		{}
		
		if (jQuery("input#CSC").val().length<3 || !isValidDigit(jQuery("input#CSC").val()))
		{alert("Please enter at least 3 digit in the \"Security Code\" field.");jQuery("input#CSC").focus();return false;}

	});
	
});


// user validate
jQuery(document).ready(function(){

	// user login from (#userlogin_fm) -- validate login -- login.asp
	jQuery("#userlogin_fm input#btn_login").click(function(){
		if (jQuery("input#UserName").val()=="")
		{alert("Please enter your email address.");jQuery("input#UserName").focus();return false;}
		if (!isEmail(jQuery("input#UserName").val()))
		{alert("Please enter a valid email address.");jQuery("input#UserName").focus();return false;}
		
		if (jQuery("input#Password").val()=="")
		{alert("Please enter your password.");jQuery("input#Password").focus();return false;}
		if (jQuery("input#Password").val().length<5)
		{alert("Password requires a minimum 5 characters");jQuery("input#Password").focus();return false;}
		if (!isValidInput(jQuery("input#Password").val()))
		{alert("Please enter only letter, digit, whitespace and \"-.\" characters in the \"Password\" field.");jQuery("input#Password").focus();return false;}
	});
	
	
	
	// newuser form (#newuser_fm) login area	--	validate login
	jQuery("#newuser_fm input#btn_login").click(function(){
		if (jQuery("input#LoginEmail").val()=="")
		{alert("Please enter your email address.");jQuery("input#LoginEmail").focus();return false;}
		if (!isEmail(jQuery("input#LoginEmail").val()))
		{alert("Please enter a valid email address.");jQuery("input#LoginEmail").focus();return false;}
		
		if (jQuery("input#LoginPassword").val()=="")
		{alert("Please enter your password.");jQuery("input#LoginPassword").focus();return false;}
		if (jQuery("input#LoginPassword").val().length<5)
		{alert("Password requires a minimum 5 characters");jQuery("input#LoginPassword").focus();return false;}
		if (!isValidInput(jQuery("input#LoginPassword").val()))
		{alert("Please enter only letter, digit, whitespace and \"-.\" characters in the \"Password\" field.");jQuery("input#LoginPassword").focus();return false;}
	
		window.location.href="/login.asp?UserName="+jQuery("input#LoginEmail").val()+"&Password="+jQuery("input#LoginPassword").val();
	});
	
	
	// newuser from (#newuser_fm) newuser area	--	validate reg
	// edituser from (#edituser_fm) edituser area	--	validate edit
	jQuery("#newuser_fm input#btn_reg, #edituser_fm input#btn_edit").click(function(){
		//email and password
		if (jQuery("input#Email").val()=="")
		{alert("Please enter a value for the \"Email\" field.");jQuery("input#Email").focus();return false;}
		if (!isEmail(jQuery("input#Email").val()))
		{alert("Please enter a valid email address");jQuery("input#Email").focus();return false;}

		if (jQuery("input#Password").val()=="")
		{alert("Please enter a value for the \"Password\" field.");jQuery("input#Password").focus();return false;}
		if (jQuery("input#Password").val().length<5)
		{alert("Please enter at least 5 characters in the \"Password\" field.");jQuery("input#Password").focus();return false;}
		if (jQuery("input#Password").val().length>50)
		{alert("Please enter at most 50 characters in the \"Password\" field.");jQuery("input#Password").focus();return false;}
		if (!isValidInput(jQuery("input#Password").val()))
		{alert("Please enter only letter, digit, whitespace and \"-.\" characters in the \"Password\" field.");jQuery("input#Password").focus();return false;}
		if (jQuery("input#Password2").val()!=jQuery("input#Password").val())
		{alert("Please re-enter your password so that it matches.");jQuery("input#Password2").focus();return false;}
		
		
		//billing information
		if (jQuery("input#FirstName").val()=="")
		{alert("Please enter a value for the \"First Name\" field.");jQuery("input#FirstName").focus();return false;}
		if (jQuery("input#FirstName").val().length<2)
		{alert("Please enter at least 2 characters in the \"First Name\" field.");jQuery("input#FirstName").focus();return false;}
		if (jQuery("input#FirstName").val().length>50)
		{alert("Please enter at most 50 characters in the \"First Name\" field.");jQuery("input#FirstName").focus();return false;}
		if (!isValidInput(jQuery("input#FirstName").val()))
		{alert("Please enter only letter, whitespace and \"-.\" characters in the \"First Name\" field.");jQuery("input#FirstName").focus();return false;}
		
		if (jQuery("input#LastName").val()=="")
		{alert("Please enter a value for the \"Last Name\" field.");jQuery("input#LastName").focus();return false;}
		if (jQuery("input#LastName").val().length<2)
		{alert("Please enter at least 2 characters in the \"Last Name\" field.");jQuery("input#LastName").focus();return false;}
		if (jQuery("input#LastName").val().length>50)
		{alert("Please enter at most 50 characters in the \"Last Name\" field.");jQuery("input#LastName").focus();return false;}
		if (!isValidInput(jQuery("input#LastName").val()))
		{alert("Please enter only letter, whitespace and \"-.\" characters in the \"Last Name\" field.");jQuery("input#LastName").focus();return false;}
		
		if (jQuery("input#Address1").val()=="")
		{alert("Please enter a value for the \"Address\" field.");jQuery("input#Address1").focus();return false;}
		if (jQuery("input#Address1").val().length<5)
		{alert("Please enter at least 5 characters in the \"Address\" field.");jQuery("input#Address1").focus();return false;}
		if (jQuery("input#Address1").val().length>50)
		{alert("Please enter at most 50 characters in the \"Address\" field.");jQuery("input#Address1").focus();return false;}
		if (!isValidInput(jQuery("input#Address1").val()))
		{alert("Please enter only letter, digit, whitespace and \"-.,\" characters in the \"Address\" field.");jQuery("input#Address1").focus();return false;}
		
		if (jQuery("input#City").val()=="")
		{alert("Please enter a value for the \"City\" field.");jQuery("input#City").focus();return false;}
		if (jQuery("input#City").val().length<2)
		{alert("Please enter at least 2 characters in the \"City\" field.");jQuery("input#City").focus();return false;}
		if (jQuery("input#City").val().length>25)
		{alert("Please enter at most 25 characters in the \"City\" field.");jQuery("input#City").focus();return false;}
		if (!isValidInput(jQuery("input#City").val()))
		{alert("Please enter only letter, digit, whitespace and \"-.,\" characters in the \"City\" field.");jQuery("input#City").focus();return false;}
		//if (!(/^[a-zA-Z]{2,}$/.test(jQuery("input#City").val())))
		//{alert("Please enter only letter in the \"City\" field.");jQuery("input#City").focus();return false;}
		
		//--	when state field is input box
		//if (jQuery("input#State").val()=="")
		//{alert("Please enter a value for the \"State\" field.");jQuery("input#State").focus();return false;}
		//if (jQuery("input#State").val().length<2)
		//{alert("Please enter at least 2 characters in the \"State\" field.");jQuery("input#State").focus();return false;}
		//if (jQuery("input#State").val().length>25)
		//{alert("Please enter at most 25 characters in the \"State\" field.");jQuery("input#State").focus();return false;}
		//if (!(/^[a-zA-Z]{2,}$/.test(jQuery("input#State").val())))
		//{alert("Please enter only letter in the \"State\" field.");jQuery("input#State").focus();return false;}
		
		if (jQuery("select#State").val()=="")
		{alert("Please select an option for the \"State\" field.");jQuery("select#State").focus();return false;}
		
		if (jQuery("input#PostalCode").val()=="")
		{alert("Please enter a value for the \"Zip Code\" field.");jQuery("input#PostalCode").focus();return false;}
		if (jQuery("input#PostalCode").val().length<5)
		{alert("Please enter at least 5 characters in the \"Zip Code\" field.");jQuery("input#PostalCode").focus();return false;}
		if (jQuery("input#PostalCode").val().length>10)
		{alert("Please enter at most 10 characters in the \"Zip Code\" field.");jQuery("input#PostalCode").focus();return false;}
		if (!(/^[a-z0-9A-Z]{2,}$/.test(jQuery("input#PostalCode").val())))
		{alert("Please enter only letter, digit in the \"Zip Code\" field.");jQuery("input#PostalCode").focus();return false;}
		
		if (jQuery("select#Country").val()=="USA")
		{
			if (jQuery("input#PostalCode").val().length!=5 || !isValidDigit(jQuery("input#PostalCode").val()))
			{alert("Please enter 5 digit in the \"Zip Code\" field.");jQuery("input#PostalCode").focus();return false;}
		}
		
		if (jQuery("input#Phone").val()=="")
		{alert("Please enter a value for the \"Phone\" field.");jQuery("input#Phone").focus();return false;}
		if (jQuery("input#Phone").val().length<10)
		{alert("Please enter at least 10 characters in the \"Phone\" field.");jQuery("input#Phone").focus();return false;}
		if (jQuery("input#Phone").val().length>15)
		{alert("Please enter at most 15 characters in the \"Phone\" field.");jQuery("input#Phone").focus();return false;}
		if (!(/[0-9( )-]{10,}/.test(jQuery("input#Phone").val())))
		{alert("Please enter only digit and \"-\" characters in the \"Phone\" field.");jQuery("input#Phone").focus();return false;}
		
		
		//shipping information
		if (jQuery("input#ShipFirstName").val()=="")
		{alert("Please enter a value for the \"Shipping First Name\" field.");jQuery("input#ShipFirstName").focus();return false;}
		if (jQuery("input#ShipFirstName").val().length<2)
		{alert("Please enter at least 2 characters in the \"Shipping First Name\" field.");jQuery("input#ShipFirstName").focus();return false;}
		if (jQuery("input#ShipFirstName").val().length>50)
		{alert("Please enter at most 50 characters in the \"Shipping First Name\" field.");jQuery("input#ShipFirstName").focus();return false;}
		if (!isValidInput(jQuery("input#ShipFirstName").val()))
		{alert("Please enter only letter, whitespace and \"-.\" characters in the \"Shipping First Name\" field.");jQuery("input#ShipFirstName").focus();return false;}
		
		if (jQuery("input#ShipLastName").val()=="")
		{alert("Please enter a value for the \"Shipping Last Name\" field.");jQuery("input#ShipLastName").focus();return false;}
		if (jQuery("input#ShipLastName").val().length<2)
		{alert("Please enter at least 2 characters in the \"Shipping Last Name\" field.");jQuery("input#ShipLastName").focus();return false;}
		if (jQuery("input#ShipLastName").val().length>50)
		{alert("Please enter at most 50 characters in the \"Shipping Last Name\" field.");jQuery("input#ShipLastName").focus();return false;}
		if (!isValidInput(jQuery("input#ShipLastName").val()))
		{alert("Please enter only letter, whitespace and \"-.\" characters in the \"Shipping Last Name\" field.");jQuery("input#ShipLastName").focus();return false;}

		if (jQuery("input#ShipAddress1").val()=="")
		{alert("Please enter a value for the \"Shipping Address\" field.");jQuery("input#ShipAddress1").focus();return false;}
		if (jQuery("input#ShipAddress1").val().length<5)
		{alert("Please enter at least 5 characters in the \"Shipping Address\" field.");jQuery("input#ShipAddress1").focus();return false;}
		if (jQuery("input#ShipAddress1").val().length>50)
		{alert("Please enter at most 50 characters in the \"Shipping Address\" field.");jQuery("input#ShipAddress1").focus();return false;}
		if (!isValidInput(jQuery("input#ShipAddress1").val()))
		{alert("Please enter only letter, digit, whitespace and \"-.,\" characters in the \"Shipping Address\" field.");jQuery("input#ShipAddress1").focus();return false;}

		if (jQuery("input#ShipCity").val()=="")
		{alert("Please enter a value for the \"Shipping City\" field.");jQuery("input#ShipCity").focus();return false;}
		if (jQuery("input#ShipCity").val().length<2)
		{alert("Please enter at least 2 characters in the \"Shipping City\" field.");jQuery("input#ShipCity").focus();return false;}
		if (jQuery("input#ShipCity").val().length>25)
		{alert("Please enter at most 25 characters in the \"Shipping City\" field.");jQuery("input#ShipCity").focus();return false;}
		if (!isValidInput(jQuery("input#ShipCity").val()))
		{alert("Please enter only letter, digit, whitespace and \"-.,\" characters in the \"Shipping City\" field.");jQuery("input#ShipCity").focus();return false;}
		//if (!(/^[a-zA-Z]{2,}$/.test(jQuery("input#ShipCity").val())))
		//{alert("Please enter only letter in the \"Shipping City\" field.");jQuery("input#ShipCity").focus();return false;}

		//--	when shipstate field is input box
		//if (jQuery("input#ShipState").val()=="")
		//{alert("Please enter a value for the \"Shipping State\" field.");jQuery("input#ShipState").focus();return false;}
		//if (jQuery("input#ShipState").val().length<2)
		//{alert("Please enter at least 2 characters in the \"Shipping State\" field.");jQuery("input#ShipState").focus();return false;}
		//if (jQuery("input#ShipState").val().length>25)
		//{alert("Please enter at most 25 characters in the \"Shipping State\" field.");jQuery("input#ShipState").focus();return false;}
		//if (!(/^[a-zA-Z]{2,}$/.test(jQuery("input#ShipState").val())))
		//{alert("Please enter only letter in the \"Shipping State\" field.");jQuery("input#ShipState").focus();return false;}
		
		if (jQuery("select#ShipState").val()=="")
		{alert("Please select an option for the \"Shipping State\" field.");jQuery("select#ShipState").focus();return false;}
		
		if (jQuery("input#ShipPostalCode").val()=="")
		{alert("Please enter a value for the \"Shipping Zip Code\" field.");jQuery("input#ShipPostalCode").focus();return false;}
		if (jQuery("input#ShipPostalCode").val().length<5)
		{alert("Please enter at least 5 characters in the \"Shipping Zip Code\" field.");jQuery("input#ShipPostalCode").focus();return false;}
		if (jQuery("input#ShipPostalCode").val().length>10)
		{alert("Please enter at most 10 characters in the \"Shipping Zip Code\" field.");jQuery("input#ShipPostalCode").focus();return false;}
		if (!(/^[a-z0-9A-Z]{2,}$/.test(jQuery("input#ShipPostalCode").val())))
		{alert("Please enter only letter, digit in the \"Shipping Zip Code\" field.");jQuery("input#ShipPostalCode").focus();return false;}
		
		if (jQuery("select#ShipCountry").val()=="USA")
		{
			if (jQuery("input#ShipPostalCode").val().length!=5 || !isValidDigit(jQuery("input#ShipPostalCode").val()))
			{alert("Please enter 5 digit in the \"Shipping Zip Code\" field.");jQuery("input#ShipPostalCode").focus();return false;}
		}
		
	});
	
	// edituser form (#edituser_fm) -- state selected option fix
	jQuery("#edituser_fm Select#State").val(jQuery("#edituser_fm Select#State").attr("title"));
	jQuery("#edituser_fm Select#ShipState").val(jQuery("#edituser_fm Select#ShipState").attr("title"));
	
	// state or country change events
	jQuery("Select#State,Select#ShipState").change(function(){
		var obj=jQuery(this).attr("id").replace("State","")+"Country";
		if(jQuery(this).children("option:selected").is(".canda"))
		{jQuery("#"+obj).val("Canada");}
		else
		{jQuery("#"+obj).val("USA");}
	});
	
	jQuery("Select#Country,Select#ShipCountry").change(function(){
		var obj=jQuery(this).attr("id").replace("Country","")+"State";
		if(jQuery(this).val()=="USA")
		{jQuery("#"+obj).val("AL");}
		else
		{jQuery("#"+obj).val("AB");}
	});

	// shipping same as billing
	jQuery("input#FillFields").click(function(){
		if (jQuery("input#FillFields:checked").val()=="on")
		{
			jQuery("input#ShipFirstName").val(jQuery("input#FirstName").val());
			jQuery("input#ShipLastName").val(jQuery("input#LastName").val());
			jQuery("input#ShipAddress1").val(jQuery("input#Address1").val());
			jQuery("input#ShipAddress2").val(jQuery("input#Address2").val());
			jQuery("input#ShipCity").val(jQuery("input#City").val());
			jQuery("input#ShipPostalCode").val(jQuery("input#PostalCode").val());
			jQuery("select#ShipState").val(jQuery("select#State").val());
			jQuery("select#ShipCountry").val(jQuery("select#Country").val());
		}
	});


});
