// JavaScript Document

function tellUsMoreValidate(formName)
{
	var frm = document.getElementById(formName);
	var strReqMsg = "";
	var strValidationMsg = "";
	if (frm.businessName.value.replace(/\s+/g, "") == "")
		strReqMsg +='- Business Name\n';
	if (frm.contactName.value.replace(/\s+/g, "") == "")
		strReqMsg +='- Contact Name\n';
	if (frm.addr1.value.replace(/\s+/g, "") == "")
		strReqMsg +='- Address 1\n';
	if (frm.city.value.replace(/\s+/g, "") == "")
		strReqMsg +='- City\n';
	if (frm.state.value.replace(/\s+/g, "") == "")
		strReqMsg +='- State\n';
	if (frm.zip.value.replace(/\s+/g, "") == "")
		strReqMsg +='- Zip/Postal Code\n';
	if (frm.zip.value.length && ! isZip(frm.zip.value))
		strValidationMsg += "    - Zip code must be in the format xxxxx\n";
	if (frm.country.value.replace(/\s+/g, "") == "")
		strReqMsg +='- Country\n';
	if ((frm.country.value.replace(/\s+/g, "") != "US") && (frm.country.value.replace(/\s+/g, "") != "CA") && (frm.country.value.replace(/\s+/g, "") != ""))
	{
		if (frm.state.value.replace(/\s+/g, "") != "Other")
			strValidationMsg += "    - State must be \"Other\" for countries other than the US and Canada\n";
	}
	if (
	   (frm.phone1.value.replace(/\s+/g, "") == "") ||
	   (frm.phone2.value.replace(/\s+/g, "") == "") ||
	   (frm.phone3.value.replace(/\s+/g, "") == "") 
	   )
		strReqMsg +='- Phone Number\n';
	if (
	   (frm.phone1.value.length && ! isPhone1(frm.phone1.value.replace(/\s+/g, ""))) ||
	   (frm.phone2.value.length && ! isPhone1(frm.phone2.value.replace(/\s+/g, ""))) ||
	   (frm.phone3.value.length && ! isPhone2(frm.phone3.value.replace(/\s+/g, "")))
	   )
		strValidationMsg += "    - Phone Number must be in the format xxx xxx xxxx\n";
	/*if (frm.emailaddr.value.replace(/\s+/g, "") == "")
		strReqMsg +='- Email Address\n';
	if (frm.emailaddr.value.length && ! isEmail(frm.emailaddr.value))
		strValidationMsg += "    - Email Address must be a well-formed email address\n";*/
	if (strReqMsg.length || strValidationMsg.length)
	{
		var strDisplay = "";
		if(strReqMsg.length)
		{ strDisplay += "The following fields are required to be completed:\n\n" + strReqMsg; }
		if(strValidationMsg.length)
		{ strDisplay += "The following fields are not filled in correctly:\n\n" + strValidationMsg; }
		alert(strDisplay);
		return false;
	}
	else
	{
		return true;
	}		
}

function locateRetailerValidate(formName)
{
	var frm = document.getElementById(formName);
	var strReqMsg = "";
	var strValidationMsg = "";
	if (frm.contactName.value.replace(/\s+/g, "") == "")
		strReqMsg +='- Contact Name\n';
	/*if (frm.addr.value.replace(/\s+/g, "") == "")
		strReqMsg +='- Address\n';
	if (frm.city.value.replace(/\s+/g, "") == "")
		strReqMsg +='- City 1\n';
	if (frm.state.value.replace(/\s+/g, "") == "")
		strReqMsg +='- State\n';
	if (frm.zip.value.replace(/\s+/g, "") == "")
		strReqMsg +='- Zip\n';*/
	if (
	   (frm.phone1.value.replace(/\s+/g, "") == "") ||
	   (frm.phone2.value.replace(/\s+/g, "") == "") ||
	   (frm.phone3.value.replace(/\s+/g, "") == "") 
	   )
		strReqMsg +='- Phone Number\n';
	if (
	   (frm.phone1.value.length && ! isPhone1(frm.phone1.value.replace(/\s+/g, ""))) ||
	   (frm.phone2.value.length && ! isPhone1(frm.phone2.value.replace(/\s+/g, ""))) ||
	   (frm.phone3.value.length && ! isPhone2(frm.phone3.value.replace(/\s+/g, "")))
	   )
		strValidationMsg += "    - Phone Number must be in the format xxx xxx xxxx\n";
	/*if (frm.email.value.replace(/\s+/g, "") == "")
		strReqMsg +='- Email Address\n';
	if (frm.email.value.length && ! isEmail(frm.email.value))
		strValidationMsg += "    - Email Address must be a well-formed email address\n";*/
	if (frm.zip.value.length && ! isZip(frm.zip.value))
		strValidationMsg += "    - Zip code must be in the format xxxxx\n";	
	if (strReqMsg.length || strValidationMsg.length)
	{
		var strDisplay = "";
		if(strReqMsg.length)
		{ strDisplay += "The following fields are required to be completed:\n\n" + strReqMsg; }
		if(strValidationMsg.length)
		{ strDisplay += "The following fields are not filled in correctly:\n\n" + strValidationMsg; }
		alert(strDisplay);
		return false;
	}
	else
	{
		return true;
	}		
}

function isPhone1(strPhone)
{
	var blnIsPhone = true;
	// this piece must be 3 digits long
	if (strPhone.length < 3)
	{
		blnIsPhone=false;
		return blnIsPhone;
	}
	// all characters must be numbers	
	for(i = 0; i < strPhone.length; i++)
	{			
		if(isNaN(strPhone.charAt(i)))
			blnIsPhone = false;			
	}
	return blnIsPhone;
}

function isPhone2(strPhone)
{
	var blnIsPhone = true;
	// this piece must be 4 digits long
	if (strPhone.length < 4)
	{
		blnIsPhone=false;
		return blnIsPhone;
	}	
	// all characters must be numbers
	for(i = 0; i < strPhone.length; i++)
	{	
		if(isNaN(strPhone.charAt(i)))
			blnIsPhone = false;			
	}
	return blnIsPhone;
}

function isZip(strZip)
{
	var blnIsZip = true;
	// this piece must be at least 5 digits long
	if (strZip.length < 5)
	{
		blnIsZip=false;
		return blnIsZip;
	}	
	// all characters must be numbers
	for(i = 0; i < strZip.length; i++)
	{	
		if(isNaN(strZip.charAt(i)))
			blnIsZip = false;			
	}
	return blnIsZip;
}

function isEmail(strValue)
{
	// get location of @
	intAtSign = strValue.indexOf("@");

	// if no @ found or it is the first char, return false
	if(intAtSign < 1)
		return false;
	
	// else get the string of chars following the @
	else
		strAfterAtSign = strValue.substring(intAtSign + 1);
	
	// if there are less than 4 chars after the @, return false
	if(strAfterAtSign.length < 4)
		return false;
	
	// if there is no '.' after the @, return false
	else if(strAfterAtSign.indexOf(".") == -1)
		return false;

	// if the last char is '.', return false
	else if(strAfterAtSign.charAt(strAfterAtSign.length - 1) == ".")
		return false;
	
	// everything's good so return true
	else
		return true;
} 	