/* ####### GENERAL CONVERSION FUNCTIONS ####### */

function trimString(strIn) {
	if (/^\s/.test(strIn)) { strIn = strIn.replace(/^\s{1,}/, ""); }
	if (/\s$/.test(strIn)) { strIn = strIn.replace(/\s{1,}$/, ""); }
	return strIn;
}
function getIntegerString(strIn) {
	return strIn.replace(/[^0-9]/g, "");
}
function getInteger(vNum) {
	vNum = getIntegerString(vNum.toString());
	if (vNum == "") { vNum = 0; }
	return Number(vNum);
}
function getDecimalString(strIn) {
	strIn = strIn.replace(/[^0-9\.]/g, "");
	var iPoint = strIn.indexOf(".");
	if (iPoint > -1) {
		strIn = strIn.substring(0, iPoint + 1) + getIntegerString(strIn.substring((iPoint + 1), strIn.length));
	}
	return strIn;
}
function getParsedPhoneStr(strIn) {
	strIn = strIn.replace(/\.|-| |\(|\)/g,"");
	return strIn;
}

/* ###### GENERAL HELPER FUNCTIONS ######  */
function addCommasToNumString(strIn) {
	var arrTemp = strIn.split("");
	var i = strIn.length - 4;
	var iPoint = strIn.indexOf(".");
	if (iPoint > -1) { i -= (strIn.length - iPoint); }
	for (i; i >= 0; i-=3) { arrTemp[i] += ","; }
	return arrTemp.join("");
}

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


function isValueInSelectbox(hSelectbox, strValue) {
	for (var i=0; i < hSelectbox.options.length; i++) {
		if (hSelectbox.options[i].value == strValue) {
			return true;
		}
	}
	return false;
}

function isNotHiddenFormField(form, strName) {
	// Tells you if a non-hidden field exists in a form.
	var type;
	var field = form.elements[strName];
	if (field && (typeof(field) == "object")) {
		try {
			type = field.getAttribute("type");
		} catch(ex) {
			// probably a radio button group (no attribute)
			return true;
		}
		if ((type) && (type.toLowerCase() == "hidden")) {
			return false;
		}
		return true;
	}
	return false;
}

function getFormFieldValue(hField) {
	if (!hField) { return undefined; }
	try {
		if (hField.type) {
			if (hField.type == "radio") {
				return getRadioValue(hField.form.elements[hField.name]);
			} else if (hField.type == "select-multiple") {
				return getMultipleSelectBoxValues(hField);
			} else {
				return hField.value;
			}
		}
	} catch (ex) {}
	try {
		if (hField.length && hField[0] && (hField[0].type == "radio")) {
			return getRadioValue(hField[0].form.elements[hField[0].name]);
		}
	} catch(ex) {}
	return undefined;
}

function getRadioValue(hRadioGroup) {
	// Gets the selected value of a radio button group. If no radio button is selected, returns an empty string.
	for (var i=0; i < hRadioGroup.length; i++) {
		if (hRadioGroup[i].checked) { return hRadioGroup[i].value; }
	}
	return "";
}

function getMultipleSelectBoxValues(hSelect) {
	var i, option, arrSelected = new Array();
	while (hSelect.selectedIndex >= 0) {
		arrSelected[arrSelected.length] = hSelect.selectedIndex;
		hSelect.options[hSelect.selectedIndex].selected = false;
	}
	for (i=0; i < arrSelected.length; i++) {
		option = hSelect.options[arrSelected[i]];
		option.selected = true;
		arrSelected[i] = option.value;
	}
	return arrSelected;
}


/* ####### STRING VALIDATION ####### */

function isValidFirstName(strName) {
	if (/[^A-Za-z -]/.test(strName)) { return false; }
	return true;
}

function isValidLastName(strName) {
	if (/[^A-Za-z -]/.test(strName)) { return false; }
	return true;
}

function isValidStreetAddress(strAddress) {
	if (strAddress.length < 3) { return false; }
	if (strAddress.replace(/[^0-9]/g, "").length < 1) { return false; }
	if (strAddress.replace(/[^A-Za-z]/g, "").length < 1) { return false; }
	return true;
}

function isValidCity(strCity) {
	if (strCity.length < 2) { return false; }
	if (/[^A-Za-z -]/.test(strCity)) { return false; }
	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;
}

function isValidEmail(strEmail) {
	var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/

	if (strEmail.length < 5) { return false; }

	if (!strEmail.match(re)) { return false; }
	
	return true;
}

/* ####### GENERIC FORM VALIDATORS ####### */

function validateInput(hInput, minLength, inputDescription) {
	if (hInput.value.length == 0) {
		return validationAlert("Please enter " + inputDescription + ".", hInput);
	} else if (hInput.value.length < minLength) {
		return validationAlert("Please re-enter " + inputDescription + ".\n(The information you entered is incomplete.)", hInput);
	}
	return true;
}

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

function validateCheckbox(hCheckbox, strAlert) {
	if (hCheckbox.checked == false) {
		return validationAlert(strAlert, hCheckbox);
	}
	return true;
}
function validateComparison(hInput, strCompare, inputDescription, extraDescription) {
	if (hInput.value != strCompare) {
		return validationAlert("Please re-enter " + inputDescription + " " + extraDescription + ".", hInput);
	}
	return true;
}

function validateNumbersOnly(hInput, strAlert) {
	if (/[^0-9]/.test(hInput.value)) {
		return validationAlert(strAlert, hInput);
	}
	return true;
}

function validateIntegerInput(hInput, minLength, inputDescription) {
	if (!validateInput(hInput, minLength, inputDescription)) { return false; }
	if (!validateNumbersOnly(hInput, inputDescription)) { return false; }
	return true;
}


/* ####### FORM VALIDATION STRING CONSTANTS ####### */

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 = "Your Address must contain letters and numbers. Please update your entry.";
	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.EMPTY_STREET_NUMBER = "Please enter your Street Number.";
	ErrorMsg.EMPTY_STREET_NAME = "Please enter your Street Name.";
    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.";

/* ####### SPECIFIC-USE FORM VALIDATORS ####### */

function validateFirstNameInput(hInput) {
	if (hInput.value.length == 0) { return validationAlert(ErrorMsg.EMPTY_FIRSTNAME, hInput); }
	if (!isValidFirstName(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 (!isValidLastName(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 (!isValidStreetAddress(hInput.value)) { return validationAlert(ErrorMsg.INVALID_ADDRESS, hInput); }
	return true;
}

function validateStreetNumberInput(hInput) {
	if (hInput.value.length == 0) { return validationAlert(ErrorMsg.EMPTY_STREET_NUMBER, hInput); }
	return true;
}

function validateStreetNameInput(hInput) {
	if (hInput.value.length == 0) { return validationAlert(ErrorMsg.EMPTY_STREET_NAME, hInput); }
	return true;
}

function validateCityInput(hInput) {
	if (hInput.value.length == 0) { return validationAlert(ErrorMsg.EMPTY_CITY, hInput); }
	if (!isValidCity(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 validatePropZipCodeInput(hInput) {
	if (hInput.value.length == 0) { return true; }
	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 validatePrematchNPAInput(hInput) {
	if (hInput.value.length == 0) { return validationAlert(ErrorMsg.EMPTY_PREMATCH_NPA, hInput); }
	if (hInput.value.length < 3) { return validationAlert(ErrorMsg.INVALID_PREMATCH_NPA, hInput); }
	if (!isValidPhoneNPA(hInput.value)) { return validationAlert(ErrorMsg.INVALID_PREMATCH_NPA, hInput); }
	if ((hInput.value == "800") || (hInput.value == "888")) { return validationAlert(ErrorMsg.RESTRICTED_PREMATCH_NPA, hInput); }
	return true;
}

function validatePhoneNPAInput(hInput, strPhoneType) {
	if (hInput.value.length == 0) { return validationAlert(ErrorMsg.EMPTY_PHONE_NPA.replace(ErrorMsg.VAR1, strPhoneType), hInput); }
	if (hInput.value.length < 3) { return validationAlert(ErrorMsg.INVALID_PHONE_NPA.replace(ErrorMsg.VAR1, strPhoneType), hInput); }
	if (!isValidPhoneNPA(hInput.value)) { return validationAlert(ErrorMsg.INVALID_PHONE_NPA.replace(ErrorMsg.VAR1, strPhoneType), hInput); }
	return true;
}

function validatePhoneNXXInput(hInput, strPhoneType) {
	if (hInput.value.length == 0) { return validationAlert(ErrorMsg.EMPTY_PHONE_NXX.replace(ErrorMsg.VAR1, strPhoneType), hInput); }
	if (hInput.value.length < 3) { return validationAlert(ErrorMsg.INVALID_PHONE_NXX.replace(ErrorMsg.VAR1, strPhoneType), hInput); }
	if (/[^0-9]/.test(hInput.value)) { return validationAlert(ErrorMsg.INVALID_PHONE_NXX.replace(ErrorMsg.VAR1, strPhoneType), hInput); }
	if (/^[01]/.test(hInput.value)) { return validationAlert(ErrorMsg.INVALID2_PHONE_NXX.replace(ErrorMsg.VAR1, strPhoneType), hInput); }
	return true;
}

function validatePhoneStationInput(hInput, strPhoneType) {
	if (hInput.value.length == 0) { return validationAlert(ErrorMsg.EMPTY_PHONE_STATION.replace(ErrorMsg.VAR1, strPhoneType), hInput); }
	if (hInput.value.length < 4) { return validationAlert(ErrorMsg.INVALID_PHONE_STATION.replace(ErrorMsg.VAR1, strPhoneType), hInput); }
	if (/[^0-9]/.test(hInput.value)) { return validationAlert(ErrorMsg.INVALID_PHONE_STATION.replace(ErrorMsg.VAR1, strPhoneType), 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 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 validateSecondaryPhoneInput(hInput) {
	if (hInput.value.length == 0) { return true; }
	if (!isValidEntirePhone(hInput.value)) { return validationAlert(ErrorMsg.INVALID_SEC_PHONE, hInput); }
	return true;
}
	
function validatePhoneInput(hInput) {
	if (!isValidEntirePhone(hInput.value)) { return validationAlert("Phone number is required, and must be the correct format.", hInput); }
	return true;
}

/* ####### DYNAMIC FORM FIELD FUNCTIONS ####### */


function initOtherField(hHidden, hSelectbox, hTextbox, strOtherFormBlock) {
	// Initialize an "other value" selectbox/textbox combo (on page load).
	hSelectbox.value = hHidden.value;
	if (hSelectbox.value == hHidden.value) {
		hTextbox.value = "";
	} else {
		hSelectbox.value = "other";
		hTextbox.value = hHidden.value;
	}
	toggleOtherField(hSelectbox, strOtherFormBlock);
}

function toggleOtherField(hSelectbox, strOtherFormBlock) {
	// Shows another form block if an option with value "other" is selected in a SELECT form element.
	if (hSelectbox.value == "other") {
		showElement(strOtherFormBlock);
	} else {
		hideElement(strOtherFormBlock);
	}
}

function focusOtherField(hSelectbox, hTextbox) {
	// Focuses the appropriate field in a selectbox/textbox combo (for "other value" form validation).
	if (hSelectbox.value != "other") {
		hSelectbox.focus();
	} else {
		hTextbox.focus();
	}
}

function focusFirstEmptyField(form) {
	var hField;
	for (var i=0; i < form.elements.length; i++) {
		hField = form.elements[i];
		try {
			if (isNotHiddenFormField(form, hField.name) && (getFormFieldValue(hField) == "")) {
				hField.focus();
				return;
			}
		} catch(ex) {}
	}
}

function toggleElementBasedOnField(element, field, listOfValues) {
	// Toggles the visibility of the specified element, checking the value of the specified field against
	// the list of specified values.  If any values in the list match the field value, the field is made
	// visible--otherwise, the field is hidden
	var fieldValue = getFormFieldValue(field);
	var show = false;
	for (var i=0;i<listOfValues.length;i++) {
		if (listOfValues[i] == fieldValue) {
			show = true;
			break;
		}
	}
	
	if (show) {
		showElement(element);
	}
	else {
		hideElement(element);
	}
}

function setFieldNumbers(nextNum) {
	if (arguments.length < 2) { return nextNum; }
	var element;
	for (var i=1; i < arguments.length; i++) {
		element = document.getElementById(arguments[i]);
		if (!element) { continue; }
		try {
			element.innerHTML = nextNum;
			nextNum++;
		} catch(ex) {}
	}
	return nextNum;
}


function validateForm(form) {

	var fieldName;
	/* validate only the fields defined in the form */
	for (var i=0; i < form.elements.length; i++) {
		
		fieldName = form.elements[i].name.toUpperCase();

		if (fieldName == "EST_VAL") {
			if (!validateSelectbox(form.elements[i], "Please select the Estimated Property Value.")) {
        		return false;
			}else if(form.elements[i].value<80000){
				return validationAlert("Minimum Estimated Value must be at least $80,000.", form.elements[i]);
			}
		} else if (fieldName == "PROP_ZIP") {
			if (!validatePropZipCodeInput(form.elements[i])) {
        		return false;
			}
		} else if (fieldName == "FNAME") {
			if (!validateFirstNameInput(form.elements[i])) {
				return false;
			}
		} else if (fieldName == "LNAME") {
			if (!validateLastNameInput(form.elements[i])) {
				return false;
			}
		} else if (fieldName == "ZIP") {
			if (!validateZipCodeInput(form.elements[i])) {
				return false;
			}
		} else if (fieldName == "EMAIL") {
			if (!validateEmailInput(form.elements[i])) {
				return false;
			}
		} else if (fieldName == "PRI_PHON" || fieldName == "PRI_PHONE") {
			if (!validatePrimaryPhoneInput(form.elements[i])) {
				return false;
			}
		} else if (fieldName == "SEC_PHON" || fieldName == "SEC_PHONE") {
			if (!validateSecondaryPhoneInput(form.elements[i])) {
				return false;
			}
		} else if (fieldName == "ADDRESS") {
			if (!validateStreetAddressInput(form.elements[i])) {
				return false;
			}
		} else if (fieldName == "CITY") {
			if (!validateCityInput(form.elements[i])) {
				return false;
			}
		} else if (fieldName == "STATE") {
			if (!validateSelectbox(form.elements[i], "Please select the State.")) {
				return false;
			}
		} else if (fieldName == "PROP_ST") {
			
			var productElement = form.PRODUCT;

			if (productElement != null && productElement.type == "select-one" && productElement.options[productElement.selectedIndex].value == "PP_NEWHOME") {
				if (!validateSelectbox(form.elements[i], "Please select the Property State.")) {
					return false;
				}
			} 
		} else if (fieldName == "BAL_ONE") {

			var productElement = form.PRODUCT;
			
			if (productElement != null && ((productElement.type == "select-one" && productElement.options[productElement.selectedIndex].value != "PP_NEWHOME") || (productElement.type == "hidden" && productElement.value != "PP_NEWHOME"))) {
			
				if (!validateSelectbox(form.elements[i], "Please select the Mortgage Balance.")) {
					return false;
				}
			}
		} else if (fieldName == "MTG_ONE_INT") {
			
			var productElement = form.PRODUCT;
			
			if (productElement != null && ((productElement.type == "select-one" && productElement.options[productElement.selectedIndex].value != "PP_NEWHOME") || (productElement.type == "hidden" && productElement.value != "PP_NEWHOME"))) {
			
				if (!validateSelectbox(form.elements[i], "Please select the Interest Rate.")) {
					return false;
				}
			}
		} else if (fieldName == "CRED_GRADE") {
			if (!validateSelectbox(form.elements[i], "Please select the Credit Profile.")) {
				return false;
			}
		} else if (fieldName == "LOAN_TYPE") {
			if (!validateSelectbox(form.elements[i], "Please select the Rate Type.")) {
				return false;
			}
		} else if (fieldName == "PROP_DESC") {
			if (!validateSelectbox(form.elements[i], "Please select the Property Description.")) {
				return false;
			}
		} else if (fieldName == "PREF_CALLTIME") {
			if (!validateSelectbox(form.elements[i], "Please select the Best Contact Time.")) {
				return false;
			}
		} else if (fieldName == "PROP_PURP") {
			if (!validateSelectbox(form.elements[i], "Please select the Purpose of Property.")) {
				return false;
			}
		} else if (fieldName == "DOWN_PMT") {
		
			var productElement = form.PRODUCT;
			
			if (productElement != null && ((productElement.type == "select-one" && productElement.options[productElement.selectedIndex].value == "PP_NEWHOME") || (productElement.type == "hidden" && productElement.value != "PP_NEWHOME"))) {
				if (!validateSelectbox(form.elements[i], "Please select Your Down Payment amount.")) {
					return false;
				}
			}
        	} else if (fieldName == "PAYDAY_LOAN_VAL") {
        		if (!validateSelectbox(form.elements[i], "Please select the Desired Loan Amount.")) { return false; }
			
        	} else if (fieldName == "INCOME_SRC") {
        		if (!validateSelectbox(form.elements[i], "Please select the Source of Income.")) { return false; }

        	} else if (fieldName == "NET_MONTHLY_INCOME") {
        		/////
        		
		} else if (fieldName == "EMPLOYER") {
			if(!validateInput(form.elements[i], 2, "Employer")) { return false; }

        	} else if (fieldName == "JOB_TITLE") {
        		if(!validateInput(form.elements[i], 2, "Job Title")) { return false; }

        	} else if (fieldName == "WORK_ADDRESS") {
        		if (!validateStreetAddressInput(form.elements[i])) { return false; }

        	} else if (fieldName == "WORK_CITY") {
        		if (!validateCityInput(form.elements[i])) { return false; }

        	} else if (fieldName == "WORK_STATE") {
        		if (!validateSelectbox(form.elements[i], "Please select the State.")) { return false; }

        	} else if (fieldName == "WORK_ZIP") {
        		if (!validateZipCodeInput(form.elements[i])) { return false; }

        	} else if (fieldName == "WORK_PHONE") {
        		if (!validatePhoneInput(form.elements[i])) { return false; }

        	} else if (fieldName == "SPVR_NAME") {
        		if(!validateInput(form.elements[i], 2, "Supervisor Name")) { return false; }

        	} else if (fieldName == "SPVR_PHONE") {
        		if (!validatePhoneInput(form.elements[i])) { return false; }

        	} else if (fieldName == "HIRE_MONTH") {
        		if (!validateSelectbox(form.elements[i], "Please complete your Hire Date")) { return false; }
        		
        	} else if (fieldName == "HIRE_YEAR") {
        		if (!validateSelectbox(form.elements[i], "Please complete your Hire Date")) { return false; }
        		
        	} else if (fieldName == "PAY_CYCLE") {
        		if (!validateSelectbox(form.elements[i], "Please select your Pay Cycle.")) { return false; }

        	} else if (fieldName == "NEXT_PAY_MM1") {
        		if (!validateSelectbox(form.elements[i], "Please complete your Next Pay Date")) { return false; }
        		
        	} else if (fieldName == "NEXT_PAY_DD1") {
			if (!validateSelectbox(form.elements[i], "Please complete your Next Pay Date")) { return false; }
			
        	} else if (fieldName == "NEXT_PAY_YYYY1") {
			if (!validateSelectbox(form.elements[i], "Please complete your Next Pay Date")) { return false; }
			
        	} else if (fieldName == "NEXT_PAY_MM2") {
			if (!validateSelectbox(form.elements[i], "Please complete your Second Pay Date")) { return false; }
			
        	} else if (fieldName == "NEXT_PAY_DD2") {
			if (!validateSelectbox(form.elements[i], "Please complete your Second Pay Date")) { return false; }
			
        	} else if (fieldName == "NEXT_PAY_YYYY2") {
			if (!validateSelectbox(form.elements[i], "Please complete your Second Pay Date")) { return false; }
			
        	} else if (fieldName == "BANK_NAME") {
        		if(!validateInput(form.elements[i], 2, "Bank Name")) { return false; }
        		
        	} else if (fieldName == "BANK_ABA") {
        		/////

        	} else if (fieldName == "BANK_ACCOUNT") {
        		/////

        	} else if (fieldName == "BANK_PHONE") {
        		if (!validatePhoneInput(form.elements[i])) { return false; }

        	} else if (fieldName == "DEPOSIT_TYPE") {
        		if (!validateSelectbox(form.elements[i], "Please select your Deposit Type.")) { return false; }

        	} else if (fieldName == "BANK_ACCT_TYPE") {
        		if (!validateSelectbox(form.elements[i], "Please select your Bank Account Type.")) { return false; }

        	} else if (fieldName == "BANK_NSF") {

        	} else if (fieldName == "RESIDENCE_YEARS") {
        		if (!validateSelectbox(form.elements[i], "Please select how many years you have been at your current residence.")) { return false; }

        	} else if (fieldName == "RESIDENCE_MONTHS") {
        		if (!validateSelectbox(form.elements[i], "Please select how many months you have been at your current residence.")) { return false; }

        	} else if (fieldName == "RENT_OWN") {
        		if (!validateSelectbox(form.elements[i], "Please select if you rent or own.")) { return false; }

        	} else if (fieldName == "PRI_PHONE") {
        		if (!validatePrimaryPhoneInput(form.elements[i])) { return false; }

        	} else if (fieldName == "DRIV_LIC") {
        		if(!validateInput(form.elements[i], 2, "Drivers License")) { return false; }

        	} else if (fieldName == "DRIV_LIC_STATE") {
        		if (!validateSelectbox(form.elements[i], "Please select the state for your Drivers License.")) { return false; }

        	} else if (fieldName == "DOBMM") {
        		if (!validateSelectbox(form.elements[i], "Please complete your Date of Birth.")) { return false; }

        	} else if (fieldName == "DOBDD") {
        		if (!validateSelectbox(form.elements[i], "Please complete your Date of Birth.")) { return false; }

        	} else if (fieldName == "DOBYYYY") {
        		if (!validateSelectbox(form.elements[i], "Please complete your Date of Birth.")) { return false; }

        	} else if (fieldName == "REF_FNAME1") {
        		if(!validateInput(form.elements[i], 2, "Reference 1, First Name")) { return false; }

        	} else if (fieldName == "REF_LNAME1") {
        		if(!validateInput(form.elements[i], 2, "Reference 1, Last Name")) { return false; }

        	} else if (fieldName == "REF_PHONE1") {
        		if (!validatePhoneInput(form.elements[i])) { return false; }

        	} else if (fieldName == "REF_RELATIONSHIP1") {
        		if (!validateSelectbox(form.elements[i], "Please enter your relationship to Reference #1")) { return false; }

        	} else if (fieldName == "REF_FNAME2") {
        		if(!validateInput(form.elements[i], 2, "Reference 2, First Name")) { return false; }

        	} else if (fieldName == "REF_LNAME2") {
        		if(!validateInput(form.elements[i], 2, "Reference 2, Last Name")) { return false; }

        	} else if (fieldName == "REF_PHONE2") {
            		if (!validatePhoneInput(form.elements[i])) { return false; }

        	} else if (fieldName == "REF_RELATIONSHIP2") {
				if (!validateSelectbox(form.elements[i], "Please enter your relationship to Reference #2")) { return false; }
			} else if (fieldName == "INPUT") {
        		if (!validateCheckbox(form.elements[i], "To continue, you will need to read the terms and select the checkbox to certify this.")) { return false; }

        	}
	}
	
	if (!validateExtra(form)) { return false; }
	
	
	// remove elements from the dhtml when using dynamic display to avoid 
	// multiple values of the same element name
	if (form.PRODUCT != null && form.PRODUCT.options) {
		typeEle = form.PRODUCT;
		loanType = typeEle.options[typeEle.selectedIndex].value;
		if (loanType == 'PP_NEWHOME') {
			// remove all field from the GEN_MORT table
			var tableElem = document.getElementById("GEN_MORT");
			if (tableElem != null) {
				tableElem.parentElement.removeChild(tableElem);
			}
		} else {
			// remove all fields from the NEWHOME table
			var tableElem = document.getElementById("NEWHOME");
			if (tableElem != null) {
				tableElem.parentElement.removeChild(tableElem);
			}
		}
		
		return true;
	}		
}

// validate combined fields here.
function validateExtra(form) {

 //4. Both Next Pay Dates must be in the future 
 //5. Hire date must be in the past 
 
 now = new Date();
 
 npdd1 = findField(form, "NEXT_PAY_DD1");
 npdm1 = findField(form, "NEXT_PAY_MM1");
 npdy1 = findField(form, "NEXT_PAY_YYYY1");
 
 firstpaydate = new Date();
 firstpaydate.setDate(npdd1.value);
 firstpaydate.setMonth(npdm1.value-1);
 firstpaydate.setYear(npdy1.value);
 
 if(firstpaydate <= now) { alert("The next pay date is in the past."); return false; }
 
 npdd2 = findField(form, "NEXT_PAY_DD2");
 npdm2 = findField(form, "NEXT_PAY_MM2");
 npdy2 = findField(form, "NEXT_PAY_YYYY2");
 
 secondpaydate = new Date();
 secondpaydate.setDate(npdd2.value);
 secondpaydate.setMonth(npdm2.value-1);
 secondpaydate.setYear(npdy2.value);
 
 if(secondpaydate <= now) { alert("The second pay date is in the past."); return false; }
 
 hm = findField(form, "HIRE_MONTH");
 hy = findField(form, "HIRE_YEAR");
 
 hiredate = new Date();
 hiredate.setMonth(hm.value-1);
 hiredate.setYear(hy.value);
 
 if(hiredate > now) { alert("The hire date is in the future."); return false; }
 
 ssn1 = findField(form, "SSN1");
 ssn2 = findField(form, "SSN2");
 ssn3 = findField(form, "SSN3");

 if(
  (ssn1.value.length != 3 || /[^0-9]/.test(ssn1.value)) ||
  (ssn2.value.length != 2 || /[^0-9]/.test(ssn2.value)) ||
  (ssn3.value.length != 4 || /[^0-9]/.test(ssn3.value))
 ) {
  alert("Social Security Number is required.");
  return false;
 }

 return true;
}

function findField(form, fname) {

  for (var i=0; i < form.elements.length; i++) {			
    if (fname.toUpperCase() == form.elements[i].name.toUpperCase()) { return form.elements[i]; }
  }
}


// shows pertinent parts of the form based on the
// product value
function loanParams() {
	typeEle = document.forms[0].PRODUCT;
	loanType =  typeEle.options[typeEle.selectedIndex].value;

	if( document.getElementById ) {
		if (loanType == 'PP_NEWHOME') {
			// show option2
			document.getElementById('NEWHOME').style.display = "";
			//hide option 1
			document.getElementById('GEN_MORT').style.display = "none";
		} else {
			// show option1
			document.getElementById('GEN_MORT').style.display = "";
			// hide option2
			document.getElementById('NEWHOME').style.display = "none";
		}
	}
}

function loanParams2(select) {
	
	if( document.getElementById ) {
		if (select.value == 'PP_NEWHOME') {
			// show option2
			document.getElementById('NEWHOME').style.display = "";
			//hide option 1
			document.getElementById('GEN_MORT').style.display = "none";
		} else {
			// show option1
			document.getElementById('GEN_MORT').style.display = "";
			// hide option2
			document.getElementById('NEWHOME').style.display = "none";
		}
	}
}




