var digits = "0123456789";
var lLetters = "abcdefghijklmnopqrstuvwxyz"
var uLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
var alphanum = lLetters + uLetters + digits;
var whitespace = " \t\n\r ";

var ProvinceDelimiter = "|";
var Provinces = new Array();
Provinces["US"] = "AL|AK|AS|AZ|AR|CA|CO|CT|DE|DC|FM|FL|GA|GU|HI|ID|IL|IN|IA|KS|KY|LA|ME|MH|MD|MA|MI|MN|MS|MO|MT|NE|NV|NH|NJ|NM|NY|NC|ND|MP|OH|OK|OR|PW|PA|PR|RI|SC|SD|TN|TX|UT|VT|VI|VA|WA|WV|WI|WY|AE|AA|AE|AE|AP";
Provinces["CA"] = "AB|BC|MB|NB|NL|NT|NS|NU|ON|PE|QC|SK|YT";

function isEmpty(s) {
	return ((s == null) || (s.length == 0))
}

function isWhitespace(s) {
	var i;
	if (isEmpty(s)) return true;
		for (i = 0; i < s.length; i++)
		{   
			var c = s.charAt(i);
			if (whitespace.indexOf(c) == -1) return false;
		}
	return true;
}

function StripIn (s, bag) {
	var i;
	var returnString = "";
	for (i = 0; i < s.length; i++) {   
		var c = s.charAt(i);
		if (bag.indexOf(c) == -1) returnString += c;
	}
	return returnString;
}

function StripNotIn (s, bag) {
	var i;
	var returnString = "";
	for (i = 0; i < s.length; i++) {
		 var c = s.charAt(i);
		 if (bag.indexOf(c) != -1) returnString += c;
	}
	return returnString;
}

function isLetter (c) {
	return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) )
}

function isDigit (c) {
	return ((c >= "0") && (c <= "9"))
}

function isLetterOrDigit (c) {
	return (isLetter(c) || isDigit(c))
}

function isInteger (s) {
	var i;
	if (isEmpty(s)) return false;
	for (i = 0; i < s.length; i++) {
		var c = s.charAt(i);
		if (!isDigit(c)) return false;
	}
	return true;
}

function AlphaNumeric(s) {
	var i;
	if (isEmpty(s)) return false;
	for (i = 0; i < s.length; i++) {
		var c = s.charAt(i);
		if (!isLetterorDigit(c)) return false;
	}
	return true;
}

function isLength(s, lMin, lMax) {
	if ((s.length >= lMin) && (s.length <= lMax)) return true;
	return false;
}

function isProvinceCode(sCode, sCountry) {
	if (Provinces[sCountry] != null) {
		if (!isLength(sCode, 2, 2)) return false;
		return ((Provinces[sCountry].indexOf(sCode) != -1) && (sCode.indexOf(ProvinceDelimiter) == -1) && (isLength(sCode,2,2)));
	}
	return true;
}

function isZipCode(sZip, sCountry) {
	if (sCountry=="US") return isUSZipCode(sZip);
	if (sCountry=="CA") return isCAZipCode(sZip);
	return true
}

function isUSZipCode(sZip) {
	return (isInteger(sZip) && ((sZip.length==5) || (sZip.length==9)));
}

function isCAZipCode(sZip) {
	var re = new RegExp();
	re = /^[a-zA-z]\d[a-zA-z]( |-)?\d[a-zA-z]\d$/;
	return re.test(sZip);
}

function isEmail (s) {
	if (isEmpty(s)) return false;
	if (isWhitespace(s)) return false;
	var i = 1;
	var sLength = s.length;
	while ((i < sLength) && (s.charAt(i) != "@")) {i++}
	if ((i >= sLength) || (s.charAt(i) != "@")) return false;
	else i += 2;
	while ((i < sLength) && (s.charAt(i) != ".")) { i++ }
	if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
	else return true;
}

function isURL(s) {
	if (isWhitespace(s)) return false;
	return true;
}

function warnInvalid (theField, s) {
	alert(s);
	theField.focus();
	theField.select();
	return false;
}

function IsNumeric(sText)

{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
   }


function addressvalidate(myform)
{
	with (myform)
	{
		valid=0;
		errorMessage = '';
		
		txtFirstName.style.backgroundColor  =oclr;
		txtLastName.style.backgroundColor =oclr;
		txtAddress1.style.backgroundColor =oclr;
		txtCity.style.backgroundColor =oclr;
		txtProvince.style.backgroundColor =oclr;
		txtPostalCode.style.backgroundColor =oclr;
		txtPhone.style.backgroundColor =oclr;

		if (txtFirstName.value.length  == 0)
		{
		txtFirstName.style.backgroundColor =fclr;
		valid=1;
		errorMessage = errorMessage + 'Please enter a FIRST NAME.<br>';
		}
		if (txtLastName.value.length  == 0)
		{
		txtLastName.style.backgroundColor =fclr;
		valid=1;
		errorMessage = errorMessage + 'Please enter a LAST NAME.<br>';
		}
		if (txtAddress1.value.length  == 0)
		{
		txtAddress1.style.backgroundColor =fclr;
		valid=1;
		errorMessage = errorMessage + 'Please enter an ADDRESS.<br>';
		}	   
		if (txtCity.value.length  == 0)
		{
		txtCity.style.backgroundColor =fclr;
		valid=1;
		errorMessage = errorMessage + 'Please enter a CITY.<br>';
		}	
		if (txtProvince.value.length  == 0)
		{
		txtProvince.style.backgroundColor =fclr;
		valid=1;
		errorMessage = errorMessage + 'Please enter a STATE/PROVINCE.<br>';
		}	
		if (selCountry.value.length  == 0)
		{
		selCountry.style.backgroundColor =fclr;
		valid=1;
		}	   
		if (txtPostalCode.value.length  < 4)
		{
		txtPostalCode.style.backgroundColor =fclr;
		valid=1;
		errorMessage = errorMessage + 'Please enter a valid ZIP/POSTAL CODE.<br>';
		}	
		   
		if (txtPhone.value.length  == 0)
		{
		
		txtPhone.style.backgroundColor = fclr;
		valid=1;
		errorMessage = errorMessage + 'Please enter a PHONE NUMBER.<br>';
		
		}
		
		if (IsNumeric(txtPhone.value)) {
		} else {
		
			txtPhone.style.backgroundColor = fclr;
			valid=1;
			errorMessage = errorMessage + 'Please enter a valid PHONE NUMBER.<br>';
		
		}
		
		chkcountry= selCountry[selCountry.selectedIndex].value;   
		chkcountry= chkcountry.toUpperCase();   		   
		
	}	
	
	


	if (valid == 1)
	{

			document.getElementById("errormsg").style.color=errclr;
			document.getElementById("errormsg").innerHTML='IMPORTANT MESSAGE! There is a small problem with your order. Please see message in red below.<br>' + errorMessage;
			//moved the 2 if clauses to display later

			//document.location.hash="errormsgposition";

			return false;
	

	}	

	temp1=chkstate(chkcountry, myform);
	
	if (temp1 != '0') {
		document.getElementById("errormsg").style.color=errclr;
		document.getElementById("errormsg").innerHTML='IMPORTANT MESSAGE! There is a small problem with your order. Please see message in red below.<br>' + temp1;
		return false;
	}
	
	
	// Check for changes in shipping level on continue so as to throw informational popup.
	

	return true;
}

function chkstate(country, myform)
{
	with (myform)
	{
		if (country == "US")
		{
			if (!isProvinceCode(txtProvince.value.toUpperCase(), country)) 
				return  "The STATE entered is invalid or missing. Please use a two-letter state abbreviation without periods, for example, NY not N.Y.";
			txtPostalCode.value = StripNotIn(txtPostalCode.value, digits);
			if (!isUSZipCode(txtPostalCode.value))
				return "The ZIP CODE entered is invalid or missing. Please only enter a five digit number.";
			if (txtPostalCode.value.length == 9)
			{
				txtPostalCode.value = txtPostalCode.value.slice(0,5) + "-" + txtPostalCode.value.slice(5)
			}
		}
		if (country == "CA")
		{
			if (!isProvinceCode(txtProvince.value.toUpperCase(), country))
				return  "The PROVINCE entered is invalid or missing. Please use a two-letter province abbreviation without periods, for example, BC not B.C. for British Colombia.";
			txtPostalCode.value = StripNotIn(txtPostalCode.value, alphanum + " -");
			txtPostalCode.value = txtPostalCode.value.toUpperCase();
			if (!isCAZipCode(txtPostalCode.value)) 
				return "The CANADIAN POSTAL CODE entered is invalid or missing. Please use this format when entering: A#A-#A#.";
		}
		
		
	}
	return 0;
}