function validate(form) {
	try { $LoanPurposeIDSelected =  form.LoanPurposeID.value; } catch(ex){$LoanPurposeIDSelected=0;};
	join_phonenum(form);
	if (!validateSelectbox(form.LoanPurposeID, "Please select the Loan Purpose.")) return false;
	if (!validateSelectbox(form.PropertyValueAmount, "Please select the Estimated Home Value.")) return false;
	if ($LoanPurposeIDSelected != 4 && !validateSelectbox(form.CurrentMortgageBalanceAmount, "Please select the First Mortgage Balance.")) return false;
	if ($LoanPurposeIDSelected != 4 && !validateSelectbox(form.CurrentMortgageRate, "Please select the Interest Rate.")) return false;
	if ($LoanPurposeIDSelected != 4 && !validateSelectbox(form.AdditionalCash, "Please select how much you want to borrow.")) return false;	
	if ($LoanPurposeIDSelected == 4 && !validateSelectbox(form.DownPaymentAmount, "Please select how much you want to down.")) return false;	
	if ($LoanPurposeIDSelected == 4 && !validateSelectbox(form.AgentFound, "Please select whether you are currently working with an agent.")) return false;	
	if ($LoanPurposeIDSelected == 4 && !validateSelectbox(form.FoundHome, "Please select whether you have found a home.")) return false;	
	if (!validateSelectbox(form.PropertyTypeID, "Please your property type.")) return false;	
	if (!validateFirstNameInput(form.FirstName)) return false;
	if (!validateLastNameInput(form.LastName)) 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.Zipcode)) return false;
	if (!validateEmailInput(form.EmailAddress)) return false;
	if (!validatePrimaryPhoneInput(form.PrimaryPhone)) {try { form.PrimaryPhone.focus(); } catch(ex) {}; return false;}
	//if (!validateSecondaryPhoneInput(form.SEC_PHON)) {try { form.SEC_1.focus(); } catch(ex) {}; return false;}
	if (!validateSelectbox(form.CreditRatingID, "Please select your credit rating.")) 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 join_phonenum(frm)  // phone number join
{
  frm.PrimaryPhone.value = frm.HomePhoneAreaCode.value + frm.HomePhonePrefix.value + frm.HomePhoneSuffix.value
}

function validateSelectbox(hSelectbox, strAlert) {
	if (hSelectbox.value.length == 0) {
		return validationAlert(strAlert, hSelectbox);
	}
	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 getParsedPhoneStr(strIn) {
	strIn = strIn.replace(/\.|-| |\(|\)/g,"");
	return strIn;
}

function validatePrimaryPhoneInput(hInput) {
	if (hInput.value.length == 0) { return validationAlert(ErrorMsg.EMPTY_PRI_PHONE, hInput); }
	if (!isValidEntirePhone(hInput.value)) { return validationAlert(ErrorMsg.INVALID_PRI_PHONE, hInput); }
	return true;
}

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 validateSecondaryPhoneInput(hInput) {
	if (hInput.value.length == 0) { return true; }
	if (!isValidEntirePhone(hInput.value)) { return validationAlert(ErrorMsg.INVALID_SEC_PHONE, hInput); }
	return true;
}

function isValidEntirePhone(strPhone) {
	var bparsedStr = trimString(strPhone);
	var parsedStr = getParsedPhoneStr(bparsedStr);
	if (parsedStr.length < 10) { return false; }
	if (parsedStr.length > 17) { return false; }
	if (/^[01]/.test(parsedStr)) { return false; }
	var npa = parsedStr.substring(0,3);
	if (!isValidPhoneNPA(npa)) {
		return false;
	}
	var nxx = parsedStr.substring(3,6);
	if (!isValidPhoneNXX(nxx)) {
		return false;
	}	
	return true;
}

function isValidPhoneNPA(strNPA) {
	if (strNPA.length < 3) { return false; }
	if (/[^0-9]/.test(strNPA)) { return false; }
	if (/^[01]/.test(strNPA)) { return false; }
	if ("200,222,300,333,400,444,500,555,600,666,700,777,900,999".indexOf(strNPA) != -1) { return false; }
	return true;
}

function isValidPhoneNXX(strNXX) {
	if (strNXX.length < 3) { return false; }
	if (/[^0-9]/.test(strNXX)) { return false; }
	if (/^[01]/.test(strNXX)) { return false; }
	return true;
}

var ErrorMsg = new Object();
	ErrorMsg.VAR1 = "%VAR1%";
	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.";