

function validate_form(form)
{
	if (!validateBusinessNameInput(form.business_name)) return false;
	if (!validateStreetAddressInput(form.address)) return false;
	if (!validateCityInput(form.city)) return false;
	if (!validateSelectbox(form.state, "Please select the State.")) return false;
	if (!validateZipCodeInput(form.zip)) return false;
	if (!validateFirstNameInput(form.first_name)) return false;
	if (!validateLastNameInput(form.last_name)) return false;
	if (!validateEmailInput(form.business_email)) return false;

	return true;
}
function validationAlert(strAlert, hField) {
	// Displays an alert, focuses a form field, and returns false.
	alert(strAlert);
	try { hField.focus(); } catch(ex) {}
	return false;
}

function validateBusinessNameInput(hInput) {
	if (hInput.value.length == 0) { return validationAlert(ErrorMsg.EMPTY_BUSINESSNAME, hInput); }
	if (/[^A-Za-z -]/.test(hInput.value)) { return validationAlert(ErrorMsg.INVALID_BUSINESSNAME, hInput); }
	return true;
}

function validateFirstNameInput(hInput) {
	if (hInput.value.length == 0) { return validationAlert(ErrorMsg.EMPTY_FIRSTNAME, hInput); }
	if (/[^A-Za-z -]/.test(hInput.value)) { return validationAlert(ErrorMsg.INVALID_FIRSTNAME, hInput); }
	return true;
}

function validateLastNameInput(hInput) {
	if (hInput.value.length == 0) { return validationAlert(ErrorMsg.EMPTY_LASTNAME, hInput); }
	if (/[^A-Za-z -]/.test(hInput.value)) { return validationAlert(ErrorMsg.INVALID_LASTNAME, hInput); }
	return true;
}

function validateStreetAddressInput(hInput) {
	if (hInput.value.length == 0) { return validationAlert(ErrorMsg.EMPTY_ADDRESS, hInput); }
	if (hInput.value.length < 3) { return validationAlert(ErrorMsg.INVALID_ADDRESS, hInput); }
	if (hInput.value.replace(/[^0-9]/g, "").length < 1) { return validationAlert(ErrorMsg.INVALID_ADDRESS, hInput); }
	if (hInput.value.replace(/[^A-Za-z]/g, "").length < 1) { return validationAlert(ErrorMsg.INVALID_ADDRESS, hInput); }
	return true;
}


function validateEmailInput(hInput) {
	hInput.value = trimString(hInput.value);
	if (hInput.value.length == 0) { return validationAlert(ErrorMsg.EMPTY_EMAIL, hInput); }
	if (!isValidEmail(hInput.value)) { return validationAlert(ErrorMsg.INVALID_EMAIL, hInput); }
	return true;
}

function isValidEmail(strEmail) {
	var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
	if (strEmail.length < 5) { return false; }
	if (!strEmail.match(re)) { return false; }
	return true;
}

function trimString(strIn) {
	if (/^\s/.test(strIn)) { strIn = strIn.replace(/^\s{1,}/, ""); }
	if (/\s$/.test(strIn)) { strIn = strIn.replace(/\s{1,}$/, ""); }
	return strIn;
}
function validateCityInput(hInput) {
	if (hInput.value.length == 0) { return validationAlert(ErrorMsg.EMPTY_CITY, hInput); }
	if (hInput.value.length < 2) { return validationAlert(ErrorMsg.INVALID_CITY, hInput); }
	if (/[^A-Za-z -]/.test(hInput.value)) { return validationAlert(ErrorMsg.INVALID_CITY, hInput); }
	return true;
}

function validateZipCodeInput(hInput) {
	if (hInput.value.length == 0) { return validationAlert(ErrorMsg.EMPTY_ZIPCODE, hInput); }
	if (hInput.value.length < 5) { return validationAlert(ErrorMsg.INVALID_ZIPCODE, hInput); }
	if (/[^0-9]/.test(hInput.value)) { return validationAlert(ErrorMsg.INVALID2_ZIPCODE, hInput); }
	return true;
}


function validateSelectbox(hSelectbox, strAlert) {
	if (hSelectbox.value.length == 0) {
		return validationAlert(strAlert, hSelectbox);
	}
	return true;
}


var ErrorMsg = new Object();
	ErrorMsg.VAR1 = "%VAR1%";
	ErrorMsg.EMPTY_BUSINESSNAME = "Please enter your Business Name.";
	ErrorMsg.INVALID_BUSINESSNAME = "Your Business Name may only contain letters, hyphens, or spaces. Please update your entry.";
	ErrorMsg.EMPTY_FIRSTNAME = "Please enter your First Name.";
	ErrorMsg.INVALID_FIRSTNAME = "Your First Name may only contain letters, hyphens, or spaces. Please update your entry.";
	ErrorMsg.EMPTY_LASTNAME = "Please enter your Last Name.";
	ErrorMsg.INVALID_LASTNAME = "Your Last Name may only contain letters, hyphens, or spaces. Please update your entry.";
	ErrorMsg.EMPTY_ADDRESS = "Please enter your Address.";
	ErrorMsg.INVALID_ADDRESS = "Please enter a valid Address.";
	ErrorMsg.EMPTY_CITY = "Please enter your City.";
	ErrorMsg.INVALID_CITY = "Your City may only contain letters, hyphens, or spaces. Please update your entry.";
	ErrorMsg.UNSELECTED_STATE = "Please select your State.";
	ErrorMsg.EMPTY_ZIPCODE = "Please enter your Zip Code.";
	ErrorMsg.INVALID_ZIPCODE = "Your Zip Code must be at least five numbers. Please update your entry.";
	ErrorMsg.INVALID2_ZIPCODE = "Your Zip Code may only contain numbers. Please update your entry.";
	ErrorMsg.EMPTY_PRI_PHONE = "Please enter your Primary Phone Number";
	ErrorMsg.INVALID_PRI_PHONE = "Please enter your valid Primary Phone Number";
	ErrorMsg.INVALID_SEC_PHONE = "Please enter your valid Secondary Phone Number";
	ErrorMsg.EMPTY_PHONE_NPA = "Please enter your " + ErrorMsg.VAR1 + " Phone Number Area Code";
	ErrorMsg.INVALID_PHONE_NPA = "Your " + ErrorMsg.VAR1 + " Phone Number Area Code must be a valid three-digit area code. Please update your entry.";
	ErrorMsg.EMPTY_PHONE_NXX = "Please enter the first three digits of your " + ErrorMsg.VAR1 + " Phone Number.";
	ErrorMsg.INVALID_PHONE_NXX = "Your " + ErrorMsg.VAR1 + " Phone Number must begin with three numbers. Please update your entry.";
	ErrorMsg.INVALID2_PHONE_NXX = "Your " + ErrorMsg.VAR1 + " Phone Number may not begin with a \"1\" or a \"0\". Please update your entry.";
	ErrorMsg.EMPTY_PHONE_STATION = "Please enter the last four digits of your " + ErrorMsg.VAR1 + " Phone Number.";
	ErrorMsg.INVALID_PHONE_STATION = "Your " + ErrorMsg.VAR1 + " Phone Number must end with four numbers. Please update your entry.";
	ErrorMsg.EMPTY_EMAIL = "Please enter your Email Address.";
	ErrorMsg.INVALID_EMAIL = "You must enter a valid Email Address. Please update your entry.";
  ErrorMsg.UNSELECTED_BEST_CALL_TIME = "Please select Best Time to Call.";
  ErrorMsg.EMPTY_PREMATCH_NPA = "Please enter the area code where you currently live.";
  ErrorMsg.INVALID_PREMATCH_NPA = "Please enter the area code where you currently live. No 800 or 888 numbers please.";
  ErrorMsg.RESTRICTED_PREMATCH_NPA = "Please enter the area code where you currently live. No 800 or 888 numbers please.";
