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(){
if($("#edituser_fm").length > 0 || $("#newuser_fm").length > 0)
{
	$("#edituser_fm, #newuser_fm").submit(function(){
		//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 (!(/^([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 and whitespace characters in the \"City\" field.");$("input#City").focus();return false;}
		
		if ($("input#PostalCode").val()=="")
		{alert("Please enter a value for the \"Zip / Postal Code\" field.");$("input#PostalCode").focus();return false;}
		if ($("input#PostalCode").val().length<5)
		{alert("Please enter at least 5 characters in the \"Zip / Postal Code\" field.");$("input#PostalCode").focus();return false;}
		if ($("input#PostalCode").val().length>15)
		{alert("Please enter at most 15 characters in the \"Zip / Postal Code\" field.");$("input#PostalCode").focus();return false;}

		if ($("select#Country").val()=="USA" || $("select#Country").val()=="United States")
		{
			if (!(/^([0-9]|[ -]){2,}$/.test($("input#PostalCode").val())))
			{alert("Please enter only digit, whitespace and \"-\" characters in the \"Zip / Postal 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 / Postal Code\" field.");$("input#PostalCode").focus();return false;}
		}

		if ($("select#Country").val()=="USA" || $("select#Country").val()=="United States" || $("select#Country").val()=="Canada")
		{
			if ($("input#State").val()=="")
			{alert("Please enter a value for the \"State/Province\" field.");$("input#State").focus();return false;}
			if ($("input#State").val().length<2)
			{alert("Please enter at least 2 characters in the \"State/Province\" field.");$("input#State").focus();return false;}
			if ($("input#State").val().length>25)
			{alert("Please enter at most 25 characters in the \"State/Province\" 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/Province\" field.");$("input#State").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 (!(/^([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;}

		if ($("input#ShipPostalCode").val()=="")
		{alert("Please enter a value for the \"Shipping Zip / Postal Code\" field.");$("input#ShipPostalCode").focus();return false;}
		if ($("input#ShipPostalCode").val().length<5)
		{alert("Please enter at least 5 characters in the \"Shipping Zip / Postal Code\" field.");$("input#ShipPostalCode").focus();return false;}
		if ($("input#ShipPostalCode").val().length>15)
		{alert("Please enter at most 15 characters in the \"Shipping Zip / Postal Code\" field.");$("input#ShipPostalCode").focus();return false;}
		
		if ($("select#ShipCountry").val()=="USA" || $("select#ShipCountry").val()=="United States")
		{
			if (!(/^([0-9]|[ -]){2,}$/.test($("input#ShipPostalCode").val())))
			{alert("Please enter only digit, whitespace and \"-\" characters in the \"Shipping Zip / Postal 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 / Postal Code\" field.");$("input#ShipPostalCode").focus();return false;}
		}
		
		if ($("select#ShipCountry").val()=="USA" || $("select#ShipCountry").val()=="United States" || $("select#ShipCountry").val()=="Canada")
		{
			if ($("input#ShipState").val()=="")
			{alert("Please enter a value for the \"Shipping State/Province\" field.");$("input#ShipState").focus();return false;}
			if ($("input#ShipState").val().length<2)
			{alert("Please enter at least 2 characters in the \"Shipping State/Province\" field.");$("input#ShipState").focus();return false;}
			if ($("input#ShipState").val().length>25)
			{alert("Please enter at most 25 characters in the \"Shipping State/Province\" field.");$("input#ShipState").focus();return false;}
			if (!(/^[a-zA-Z]{2,}$/.test($("input#ShipState").val())))
			{alert("Please enter only letter in the \"Shipping State/Province\" field.");$("input#ShipState").focus();return false;}
		}

		//email and password
		if ($("input#Email").val()=="")
		{alert("Please enter a value for the \"Email Address\" 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;}
		
	});
}
});
