// JavaScript Document

function validateForm()
{
	if (document.forms[0].amount.value == "")
	{
		self.alert("You must Calculate A Total before proceeding!");
		document.forms[0].amount.focus();
		return false;
	}
	
/* JA: ability to checkout disabled when no items selected
	if (!(checkTotalQtyZero(document.myform)))
	{
		return false;
	}
*/
	
	if (document.forms[0].billTo_firstName.value == "")
	{
		self.alert("Billing Info: \nPlease enter a value for First Name!");
		document.forms[0].billTo_firstName.focus();
		return false;
	}
	
	if (document.forms[0].billTo_lastName.value == "")
	{
		self.alert("Billing Info: \nPlease enter a value for Last Name!");
		document.forms[0].billTo_lastName.focus();
		return false;
	}
	
	if (document.forms[0].billTo_street1.value == "")
	{
		self.alert("Billing Info: \nPlease enter a value for Address 1!");
		document.forms[0].billTo_street1.focus();
		return false; 
	}
	 
	if (document.forms[0].billTo_city.value == "")
	{
		self.alert("Billing Info: \nPlease enter a value for City!");
		document.forms[0].billTo_city.focus();
		return false;
	}
	
	if (document.forms[0].billTo_state.value == "")
	{
		self.alert("Billing Info: \nPlease enter a value for State/Province!");
		document.forms[0].billTo_state.focus();
		return false; 
	}
	
	if (document.forms[0].billTo_postalCode.value == "")
	{
		self.alert("Billing Info: \nPlease enter a value for Zip!");
		document.forms[0].billTo_postalCode.focus();
		return false; 
	}
	
	if(!(validateZipCode(document.forms[0].billTo_postalCode))){
		self.alert("Billing Info: \nPlease enter a valid Postal Code in the format: #####-#### or #####")
		document.forms[0].billTo_postalCode.focus();
		return false;
	}
	
	if (document.forms[0].billTo_country.value == "")
	{
		self.alert("Billing Info: \nPlease enter a value for Country!");
		document.forms[0].billTo_country.focus();
		return false; 
	}
	
	if (document.forms[0].billTo_email.value == "")
	{
		self.alert("Billing Info: \nPlease enter a value for Email!");
		document.forms[0].billTo_email.focus();
		return false; 
	}
	
	/*if(!(validateEmail(document.forms[0].billTo_email))){
		self.alert("Billing Info: \nYour billing email address appears to be invalid. \nPlease re-enter.");
		document.forms[0].billTo_email.focus();
		return false;
	}*/
	
	if (document.forms[0].billTo_phoneNumber.value == "")
	{
		self.alert("Billing Info: \nPlease enter a value for Phone Number!");
		document.forms[0].billTo_phoneNumber.focus();
		return false; 
	}
	
	if(!(validatePhone(document.forms[0].billTo_phoneNumber)))
	{
		self.alert("Billing Info: \nPlease format your telephone number as: ###-###-####")
		document.forms[0].billTo_phoneNumber.focus(); 
		return false;
	}
	
	if(document.forms[0].shipTo_same_as_billTo.checked==false) 
	{
		return validateShippingInfo();
		
	}
}

function validatePhone(oTextBox)
{
	var chars = "12345678910-";
	var arrChars = chars.split();
	var sText = "";
	var sChar = "";
	var r;
	sText = oTextBox.value;
	//var re = new RegExp("[1234567890-]") ///\d{3}(-)\d{3}(-)\d{4}/)
	var re = new RegExp(/^\(?\d{3}\)?\s|-\d{3}-\d{4}$/); ///\d{3}(-)\d{3}(-)\d{4}/)
	
	return re.test(sText);
}
function validateZipCode(oTextBox)
{
	/*sText = oTextBox.value;
	//var re = new RegExp("[1234567890-]") ///\d{3}(-)\d{3}(-)\d{4}/)
	var reUsa = new RegExp(/(^\d{5}$)|(^\d{5}-\d{4}$)/);
	var reCanada = new RegExp(/(^\d{5}$)|(^\d{5}-\d{4}$)/);
	return (reUsa.test(sText)||reCanada.test(sText));*/
	return true;
}

function validateShippingInfo()
{	
	if (document.forms[0].shipTo_firstName.value == "")
	{
		self.alert("Shipping Info: \nPlease enter a value for First Name!");
		document.forms[0].shipTo_firstName.focus();
		return false;
	}
	
	if (document.forms[0].shipTo_lastName.value == "")
	{
		self.alert("Shipping Info: \nPlease enter a value for Last Name!");
		document.forms[0].shipTo_lastName.focus();
		return false;
	}
	
	if (document.forms[0].shipTo_street1.value == "")
	{
		self.alert("Shipping Info: \nPlease enter a value for Address 1!");
		document.forms[0].shipTo_street1.focus();
		return false; 
	}
	 
	if (document.forms[0].shipTo_city.value == "")
	{
		self.alert("Shipping Info: \nPlease enter a value for City!");
		document.forms[0].shipTo_city.focus();
		return false;
	}
	
	if (document.forms[0].shipTo_state.value == "")
	{
		self.alert("Shipping Info: \nPlease enter a value for State/Province!");
		document.forms[0].shipTo_state.focus();
		return false; 
	}
	
	if (document.forms[0].shipTo_postalCode.value == "")
	{
		self.alert("Shipping Info: \nPlease enter a value for Zip!");
		document.forms[0].shipTo_postalCode.focus();
		return false; 
	}
	
	if (document.forms[0].shipTo_country.value == "")
	{
		self.alert("Shipping Info: \nPlease enter a value for Country!");
		document.forms[0].shipTo_country.focus();
		return false; 
	}
	if(document.forms[0].shipTo_phoneNumber.value!=""){
		if(!(validatePhone(document.forms[0].sbipTo_phoneNumber)))
		{
			self.alert("Shipping Info: \nPlease format your telephone number as: ###-###-####")
			document.forms[0].shipTo_phoneNumber.focus(); 
			return false;
		}
	}
	
	if (document.forms[0].shipTo_email.value == "")
	{
		self.alert("Shipping Info: \nPlease enter a value for Email!");
		document.forms[0].shipTo_email.focus();
		return false; 
	}
}

function validateEmail(oTextBox) {
	var re;
	var sText = oTextBox.value;
	var re = new RegExp(/(^[a-z]([a-z_\.]*)@([a-z_\.]*)([.][a-z]{3})$)|(^[a-z]([a-z_\.]*)@([a-z_\.]*)(\.[a-z]{3})(\.[a-z]{2})*$)/i);
	return re.test(sText);
}

