// The prequalValidation.js file contains additional
// business logic scripting needed for the 
// Prequalification.aspx and PrequalificationBilling.aspx files

var cells1 = document.getElementsByName('RefiLabel');
var cells2 = document.getElementsByName('RefiOptions');
var cells3 = document.getElementsByName('SellingHouseLabel');
var cells4 = document.getElementsByName('SellingHouseOptions');
var cells5 = document.getElementsByName('CreditSummary1');
var cells6 = document.getElementsByName('CreditSummary2');
var cells7 = document.getElementsByName('CreditSummary3');
var cells8 = document.getElementsByName('CreditSummary4');
var cells9 = document.getElementsByName('CoApplicant1');
var cells10 = document.getElementsByName('CoApplicant2');
var cells11 = document.getElementsByName('CoApplicant3');
var cells12 = document.getElementsByName('CoApplicant4');
var cells13 = document.getElementsByName('CoApplicantIncome1');
var cells14 = document.getElementsByName('CoApplicantIncome2');
var cells15 = document.getElementsByName('PurchaseHomePrice1');
var cells16 = document.getElementsByName('PurchaseHomePrice2');

// Setup additional default page UI features
function configurePageLoad()
{
	toggleRefiOptionsViaLoanPurpose();
	toggleSellingHouseOptions();
	toggleOwnRentalProperty();
	toggleCreditSummaryViaQualificationType();
	toggleCoApplicant();
}

		
// toggle Refinance Options
function toggleRefiOptionsViaLoanPurpose()
{
	var oDDL = document.getElementById('ddlLoanType');
	var curText = oDDL.options[oDDL.selectedIndex].text;
	var chk1 = document.getElementById('chkSellingHouse');
	
	if (curText == "Refinance")
	{
		//cells1[0].style.display = 'block';
		//cells2[0].style.display = 'block';
		//cells3[0].style.display = 'block';
		//cells4[0].style.display = 'block';
		//cells15[0].style.display = 'none';
		//cells16[0].style.display = 'none';
	}
	else
	{
		//cells1[0].style.display = 'none';
		//cells2[0].style.display = 'none';
		//cells15[0].style.display = 'block';
		//cells16[0].style.display = 'block';
		
		if (chk1.checked != true)
		{
			//cells3[0].style.display = 'none';
			//cells4[0].style.display = 'none';
		}
	}
}
	
// toggle Selling A House options
function toggleSellingHouseOptions()
{
	var chk1 = document.getElementById('chkSellingHouse');
	var chk2 = document.getElementById('chkSellingHouseWithAnAgent');
	var chk3 = document.getElementById('chkSellingCurrentHouse');
	var txt1 = document.getElementById('txtSellingHousePrice');
	var txt2 = document.getElementById('txtSellingHouseMtgValue');
	var oDDL = document.getElementById('ddlLoanType');
	var curText = oDDL.options[oDDL.selectedIndex].text;
	
	if (chk1.checked == true)
	{
		chk2.disabled = false;
		chk3.disabled = false;
		
		//cells3[0].style.display = 'block';
		//cells4[0].style.display = 'block';
	}
	else
	{
		chk2.disabled = true;
		chk2.checked = false;
		chk3.disabled = true;
		chk3.checked = false;
		txt1.value = "0";
		txt2.value = "0";
		
		if (curText != "Refinance")
		{
			//cells3[0].style.display = 'none';
			//cells4[0].style.display = 'none';
		}
	}
}

// toggle Do you own rental property
function toggleOwnRentalProperty()
{
	var frm = document.Form1;
	var txt1 = document.getElementById('txtRentalUnitsNumber');
	var txt2 = document.getElementById('txtTotRents');
	var txt3 = document.getElementById('txtTotPmts');
	
	if (frm.chkRentalProperty.checked)
	{
		txt1.disabled = false;
		txt2.disabled = false;
		txt3.disabled = false;
	}
	else
	{
		txt1.disabled = true;
		txt2.disabled = true;
		txt3.disabled = true;
		txt1.value = "";
		txt2.value = "";
		txt3.value = "";
	}
}

// toggle Credit Summary panel and labels
function toggleCreditSummaryViaQualificationType()
{
	var lbl = document.getElementById('lblSocSecRequired');
	var lbl2 = document.getElementById('lblCoAppSocSecRequired');
	
	for (var i = 0; i < document.Form1.radQualificationType.length; i++)
	{
		if (document.Form1.radQualificationType[i].checked)
		{
			var radVal = document.Form1.radQualificationType[i].value;
		}
	}

	if (radVal == "PrequalificationWithNoCreditAndNoWarranty")
	{
		//cells5[0].style.display = 'block';
		//cells6[0].style.display = 'block';
		//cells7[0].style.display = 'block';
		//cells8[0].style.display = 'block';
		lbl.innerText = "(optional)";
		lbl2.innerText = "(optional)";
	}
	else
	{
		//cells5[0].style.display = 'none';
		//cells6[0].style.display = 'none';
		//cells7[0].style.display = 'none';
		//cells8[0].style.display = 'none';
		lbl.innerText = "(required)";
		lbl2.innerText = "(required)";
	}
}

function toggleCoApplicant()
{
	var chk1 = document.getElementById('chkIndicateCoApplicant');
	
	if (chk1.checked)
	{
	    document.getElementById('CoApplicant1').style.display = 'block';
	    document.getElementById('CoApplicant2').style.display = 'block';
	    document.getElementById('CoApplicant3').style.display = 'block';
	    document.getElementById('CoApplicant4').style.display = 'block';
    }
    else
    {
	    document.getElementById('CoApplicant1').style.display = 'none';
	    document.getElementById('CoApplicant2').style.display = 'none';
	    document.getElementById('CoApplicant3').style.display = 'none';
	    document.getElementById('CoApplicant4').style.display = 'none';
    }
}

function validatePrequalForm()
{
	var frm = document.Form1;
	var chk1 = document.getElementById('chkIndicateCoApplicant');
	var sErr = "";
	var radVal = "";
	var bln = false;
	
	// REQUIRED FIELDS
	// qualification type
	for (var i = 0; i < frm.radQualificationType.length; i++)
	{
		if (frm.radQualificationType[i].checked)
		{
			radVal = frm.radQualificationType[i].value;
		}
	}
	if (radVal == "") sErr += "Qualification Type is required.\r\n";
	
	// qualification type 1 w/low loan amount
	if ((document.Form1.radQualificationType[0].checked) && (parseInt(frm.txt1stTDLoanAmt.value)*1000 < 125000))
	{
		sErr += "Qualification Type 1 requires a loan amount of at least $125,000.\r\n";
	}
	
	// loan purpose type
	if (frm.ddlLoanType.value == '') sErr += "Loan Purpose Type is required.\r\n";
	
	// occupancy type
	if (frm.ddlOccupancyType.selectedIndex == 0) sErr += "Occupancy Type is required.\r\n";

	// num of units
	//if (frm.ddlNumOfUnits.selectedIndex == 0) sErr += "Num of Units is required.\r\n";

	// refinance purpose
	if  (frm.ddlLoanType.options[document.Form1.ddlLoanType.selectedIndex].text == "Refinance")
	{
		if (frm.ddlRefinancePurpose.selectedIndex == 0) sErr += "Refinance Purpose is required.\r\n";	
	}
	
	// property type
	if (frm.ddlPropertyType.value == '') sErr += "Property Type is required.\r\n";
	
	// applicant first and last name
	if (trim(frm.txtApplicantFirstName.value) == "") sErr += "Applicant First Name is required.\r\n";
	if (trim(frm.txtApplicantLastName.value) == "") sErr += "Applicant Last Name is required.\r\n";
	
	// applicant current address
	if (trim(frm.txtCurrentStreetNumber.value) == "") sErr += "Applicant Current Street # is required.\r\n";
	if (trim(frm.txtCurrentStreetName.value) == "") sErr += "Applicant Current Street Name is required.\r\n";
	if (trim(frm.txtCurrentCity.value) == "") sErr += "Applicant Current City is required.\r\n";
	if (frm.ddlCurrentState.selectedIndex == 0) sErr += "Applicant Current State is required.\r\n";
	if (trim(frm.txtCurrentZip.value) == "") sErr += "Applicant Current Zip is required.\r\n";
	
	// applicant email
	if (!isEmail(trim(frm.txtApplicantEmail.value))) sErr += "A Valid Applicant Email Address is required.\r\n";
	
	// applicant income
	if ((trim(frm.txtApplicantMonthlyIncome.value) == "") || (trim(frm.txtApplicantMonthlyIncome.value) == "0")) sErr += "Applicant Monthly Income is required.\r\n";
	
	//beg new
	// ltv <= 100
	if (trim(frm.txt1stTDLoanAmt.value) > trim(frm.txtExpectedNewHomePrice.value))  sErr += "Loan Amount must be less than property value.\r\n";
	//end new
	
	// 1st td loan amount
	if ((trim(frm.txt1stTDLoanAmt.value) == "") || (trim(frm.txt1stTDLoanAmt.value) == "0") || (trim(frm.txt1stTDLoanAmt.value) == "0.00")) sErr += "1st TD Loan Amount is required.\r\n";
	
	// amortization type
	bln = false;
	//for (i = 0; i < frm.lbAmortization.length; i++)
	//{
	//	if (frm.lbAmortization[i].selected)
	//	{
	//		bln = true;
	//	}
//	}
//	if (bln == false) sErr += "Amortization is required.\r\n";
	
	// loan program
//	bln = false;
//	for (i = 0; i < frm.lbLoanProgram.length; i++)
//	{
//		if (frm.lbLoanProgram[i].selected)
//		{
//			bln = true;
//		}
//	}
//	if (bln == false) sErr += "Loan Program is required.\r\n";
	
	// EDIT CHECKS
	// agent email
	if (trim(frm.txtAgentEmail.value) != "")
	{
		if (!isEmail(trim(frm.txtAgentEmail.value))) sErr += "A Valid Agent Email Address is required.\r\n";
	}
	
	if (trim(frm.ddlReferredFrom.value) == "")
	{
		sErr += "Please indicate how you heard about us.\r\n";
	}
	
	
	// agent phone
	if ((trim(frm.txtAgentAreaCode.value) != "") || (trim(frm.txtAgentPhone1.value) != "") || (trim(frm.txtAgentPhone2.value) != ""))
	{
		if (!isValidPhoneNumber(trim(frm.txtAgentAreaCode.value), trim(frm.txtAgentPhone1.value), trim(frm.txtAgentPhone2.value)))
		{
			sErr += "A Valid Agent Phone Number is required.\r\n";
		}
	}
	
	// applicant home phone number - not required, but if partial phone number entered, full phone number required
	if ((trim(frm.txtApplicantHomePhoneAreaCode.value) != "") || (trim(frm.txtApplicantHomePhone1.value) != "") || (trim(frm.txtApplicantHomePhone2.value) != ""))
	{
		if (!isValidPhoneNumber(trim(frm.txtApplicantHomePhoneAreaCode.value), trim(frm.txtApplicantHomePhone1.value), trim(frm.txtApplicantHomePhone2.value)))
		{
			sErr += "A Valid Applicant Home Phone Number is required.\r\n";
		}
	}
	
	// applicant work phone - not required, but if partial phone number entered, full phone number required
	if ((trim(frm.txtApplicantWorkPhoneAreaCode.value) != "") || (trim(frm.txtApplicantWorkPhone1.value) != "") || (trim(frm.txtApplicantWorkPhone2.value) != ""))
	{
		if (!isValidPhoneNumber(trim(frm.txtApplicantWorkPhoneAreaCode.value), trim(frm.txtApplicantWorkPhone1.value), trim(frm.txtApplicantWorkPhone2.value)))
		{
			sErr += "A Valid Applicant Work Phone Number is required.\r\n";
		}
	}
	
	// applicant social security number - if Qualification type calls for credit check, then required.  Else, optional.
	// if optional and data entered, then require validation.
	if ((radVal == "PrequalificationWithCreditAndWarranty") || (radVal == "PrequalificationWithCreditAndNoWarranty"))
	{
		if (!isValidSocialSecurityNumber(trim(frm.txtApplicantSocSecNumber1.value), trim(frm.txtApplicantSocSecNumber2.value), trim(frm.txtApplicantSocSecNumber3.value)))
		{
			sErr += "A Valid Applicant Social Security Number is required.\r\n";
		}
		// coapplicant social security number
		if (chk1.checked)
		{
			if (!isValidSocialSecurityNumber(trim(frm.txtCoApplicantSocSecNumber1.value), trim(frm.txtCoApplicantSocSecNumber2.value), trim(frm.txtCoApplicantSocSecNumber3.value)))
			{
				sErr += "A Valid CoApplicant Social Security Number is required.\r\n";
			}
		}
	}
	else
	{
	// applicant social security number
		if ((trim(frm.txtApplicantSocSecNumber1.value) != "") || (trim(frm.txtApplicantSocSecNumber2.value) != "") || (trim(frm.txtApplicantSocSecNumber3.value) != ""))
		{
			if (!isValidSocialSecurityNumber(trim(frm.txtApplicantSocSecNumber1.value), trim(frm.txtApplicantSocSecNumber2.value), trim(frm.txtApplicantSocSecNumber3.value)))
			{
				sErr += "A Valid Applicant Social Security Number is required.\r\n";
			}
		}
	}
	
	if (chk1.checked)
	{
		// coapplicant first and last name
		if (trim(frm.txtCoAppFirstName.value) == "") sErr += "CoApplicant First Name is required.\r\n";
		if (trim(frm.txtCoAppLastName.value) == "") sErr += "CoApplicant Last Name is required.\r\n";
		
		// coapplicant current address
		if (trim(frm.txtCoAppCurrentStreetNumber.value) == "") sErr += "CoApplicant Current Street # is required.\r\n";
		if (trim(frm.txtCoAppCurrentStreetName.value) == "") sErr += "CoApplicant Current Street Name is required.\r\n";
		if (trim(frm.txtCoAppCurrentCity.value) == "") sErr += "CoApplicant Current City is required.\r\n";
		if (frm.ddlCoAppCurrentState.selectedIndex == 0) sErr += "CoApplicant Current State is required.\r\n";
		if (trim(frm.txtCoAppCurrentZip.value) == "") sErr += "CoApplicant Current Zip is required.\r\n";
		
		// applicant income
	if ((trim(frm.txtCoApplicantMonthlyIncome.value) == "") || (trim(frm.txtCoApplicantMonthlyIncome.value) == "0")) sErr += "CoApplicant Monthly Income is required.\r\n";
	}
	
	// coapplicant work phone - not required, but if partial phone number entered, full phone number required
	if ((trim(frm.txtCoApplicantWorkPhoneAreaCode.value) != "") || (trim(frm.txtCoApplicantWorkPhone1.value) != "") || (trim(frm.txtCoApplicantWorkPhone2.value) != ""))
	{
		if (!isValidPhoneNumber(trim(frm.txtCoApplicantWorkPhoneAreaCode.value), trim(frm.txtCoApplicantWorkPhone1.value), trim(frm.txtCoApplicantWorkPhone2.value)))
		{
			sErr += "A Valid CoApplicant Work Phone Number is required.\r\n";
		}
	}
	
	// coapplicant email - not required, but if partial email entered, valid email address required
	if (trim(frm.txtCoApplicantEmail.value) != "")
	{
		if (!isEmail(trim(frm.txtCoApplicantEmail.value))) sErr += "A Valid CoApplicant Email Address is required.\r\n";
	}
		
	// selling a house
	if ((frm.chkSellingHouse.checked) || (frm.ddlLoanType.options[document.Form1.ddlLoanType.selectedIndex].text == "Refinance"))
	{
		if ((trim(frm.txtSellingHousePrice.value) == "") || (trim(frm.txtSellingHousePrice.value) == "0")) sErr += "Selling Price or Estimated Value is required.\r\n";
	}
		
	// if Prequalification With No Credit And No Warranty, then credit score required
	var bln = false;
	if (radVal == "PrequalificationWithNoCreditAndNoWarranty")
	{
		for (var i = 0; i < frm.radCreditScore.length; i++)
		{
			if (frm.radCreditScore[i].checked)
			{
				bln = true
			}
		}
		if (!bln) sErr += "Credit History is required.\r\n";
	}
	
	// if Loan Purpose Type == Purchase, then Expected New Home Price required
	if (frm.ddlLoanType.options[document.Form1.ddlLoanType.selectedIndex].text == "Purchase")
	{
		if ((trim(frm.txtExpectedNewHomePrice.value) == "") || (trim(frm.txtExpectedNewHomePrice.value) == "0") || (trim(frm.txtExpectedNewHomePrice.value) == "0.00")) sErr += "Expected New Home Price is required.\r\n";
	}
	
	
	// display error message
	if (sErr.length > 0)
	{
		sErr = "Prequalification Validation errors:\r\n\r\n" + sErr;
		alert(sErr);
		return false;
	}

	return true;
}

function validateBillingForm()
{
	var frm = document.Form1;
	var sErr = "";
	
	// REQUIRED FIELDS
	// billing name
	if (trim(frm.txtBillingFirstName.value) == "") sErr += "First Name is required.\r\n";
	if (trim(frm.txtBillingLastName.value) == "") sErr += "Last Name is required.\r\n";
	
	// billing address
	if (trim(frm.txtBillingAddress1.value) == "") sErr += "Address 1 is required.\r\n";
	if (trim(frm.txtBillingCity.value) == "") sErr += "City is required.\r\n";
	if (frm.ddlBillingState.selectedIndex == 0) sErr += "State is required.\r\n";
	if (trim(frm.txtBillingPostalCode.value) == "") sErr += "Zip is required.\r\n";
	
	// email
	if (!isEmail(trim(frm.txtBillingEmail.value))) sErr += "A Valid Email Address is required.\r\n";
	
	// phone	
	if (!isValidPhoneNumber(trim(frm.txtBillingPhoneAreaCode.value), trim(frm.txtBillingPhone1.value), trim(frm.txtBillingPhone2.value)))
	{
		sErr += "A Valid Phone Number is required.\r\n";
	}
	 
	// credit card
	if (trim(frm.txtBillingCreditCardNumber.value) == "") 
	{
		sErr += "Credit Card Number is required.\r\n";
	}
	else
	{
		if (!isCreditCard(trim(frm.txtBillingCreditCardNumber.value))) sErr += "A Valid Credit Card Number is required.\r\n";
	}
	if (frm.ddlBillingCreditCardType.selectedIndex == 0) sErr += "Credit Card Type is required.\r\n";
	if (frm.ddlBillingMonthExp.selectedIndex == 0) sErr += "Credit Card Expiration Month is required.\r\n";
	
	// credit card year
	var regexYear = /^(19|20)\d\d/;
	var sYear = trim(frm.txtBillingYearExp.value);
	if (!regexYear.test(sYear)) sErr += "A valid Credit Card Expiration Year is required.\r\n";
	
	if (sErr.length > 0)
	{
		sErr = "Prequalification Billing errors:\r\n\r\n" + sErr;
		alert(sErr);
		return false;
	}

	return true;
}

function validatePrequalificationDocuments(obj)
{
	var frm = document.Form1;
	var sErr = "";
	
	// excel files
	if (obj == "EXCEL") if (frm.ddlExcelFiles.selectedIndex == 0) sErr += "Please select an Excel file to retrieve.\r\n";
	if (obj == "PDF") if (frm.ddlPDFFiles.selectedIndex == 0) sErr += "Please select a PDF file to retrieve.\r\n";
	if (obj == "CREDIT") if (frm.ddlCreditFiles.selectedIndex == 0) sErr += "Please select a PDF file to retrieve.\r\n";
	if (obj == "MISMO") if (frm.ddlMISMOFiles.selectedIndex == 0) sErr += "Please select a MISMO file to retrieve.\r\n";
	
	if (sErr.length > 0)
	{
		sErr = "Prequalification Document error:\r\n\r\n" + sErr;
		alert(sErr);
		return false;
	}
	
	return true;
}

function showCCC()
{
	var sMsg = "Prequalification Billing\r\n\r\n";
	sMsg += "Credit Card Code:\r\n";
	sMsg += "The Credit Card Code is a three- or four-digit security code that is printed\r\n";
	sMsg += "on the back of credit cards in reverse italics in the card's signature panel.\r\n";
	sMsg += "For American Express cards, the code is on the front.";

	alert(sMsg);
}

function showWarranty()
{
	var sMsg = "ePrequal.com SureClose Warranty\r\n\r\n";
	sMsg += "Our industry unique Sure Close Warranty Statement provides you the security in\r\n";
	sMsg += "you the security in knowing when we say you are 'ePrequalified' for one or more loan programs, then we\r\n";
	sMsg += "have a high enough confidence in our preliminary findings to offer you this commitment. This is a conditional\r\n";
	sMsg += "statement which basically says 'If our finding indicate you are qualified to purchase a home under the\r\n";
	sMsg += "general terms suggested, and you go through the process and are turned down for financing and unable to\r\n";
	sMsg += "close escrow within 90 days, we will provide compensation to you of up to $1,000 towards re-imbursement of\r\n";
	sMsg += "actual out of pocket expenses including, but not limited to, appraisal fees, credit report fee, property\r\n";
	sMsg += "inspection fee, etc'.  Certain restrictions do apply.";
	
	alert(sMsg);
}

function showRefund()
{
	var sMsg = "Refund Policy\r\n\r\n";
	sMsg += "Our refund policy is simple. If for any reason you don't feel you have received the value from\r\n";
	sMsg += "this service that you expected, please notify us here within 5 days of purchasing your ePrequal.com service:\r\n";
	sMsg += "Refunds@ePrequal.com and state the reason for your dissatisfaction. We will process a full refund and credit\r\n";
	sMsg += "back your account within 10 days of receipt of the request. We value your business and your feedback.";
	
	alert(sMsg);
}

function trim(str)
{
	while (str.charAt(0) == (" "))
		{
			str = str.substring(1);
		}
	while (str.charAt(str.length - 1) == " ")
		{
			str = str.substring(0, str.length - 1);
		}
		
	return str;
}

function isValidPhoneNumber(sAreaCode, sPhone1, sPhone2)
{
	var regexPhone1 = /^\d\d\d/;
	var regexPhone2 = /^\d\d\d\d/;
	
	if ((!regexPhone1.test(sAreaCode)) || (!regexPhone1.test(sPhone1)) || (!regexPhone2.test(sPhone2)))
	{
		return false;
	}
	else
	{
		return true;
	}
}

function isValidSocialSecurityNumber(sNum1, sNum2, sNum3)
{
	var regexNum1 = /^\d\d\d/;
	var regexNum2 = /^\d\d/;
	var regexNum3 = /^\d\d\d\d/;
	
	if ((!regexNum1.test(sNum1)) || (!regexNum2.test(sNum2)) || (!regexNum3.test(sNum3)))
	{
		return false;
	}
	else
	{
		return true;
	}
}