getParam = function(arg) {
	q = location.search;
	if (q.indexOf(arg) >= 0) {
		var pntr = q.indexOf(arg) + arg.length + 1;
		if (q.indexOf("&", pntr) >= 0) {
			return q.substring(pntr, q.indexOf("&", pntr));
		} else {
			return q.substring(pntr, q.length);
		}
	} else {
		return null;
	}
}
function LoanCalc() { 
	form = document.myform 
	
	UnfmtLoanAmount = xreplace(document.getElementById('LoanAmount').value,',','')
	LoanAmount1= UnfmtLoanAmount * 1.015
	LoanAmount2= LoanAmount1.toFixed(2)
	document.getElementById('LoanAmountMIP').innerHTML = LoanAmount2
	
	DownPayment= "0" 
	AnnualInterestRate = form.InterestRate.value/100 
	Years= form.NumberOfYears.value 
	MonthRate=AnnualInterestRate/12
	
	NumPayments = Years*12
	document.getElementById('NumberOfPayments').innerHTML=NumPayments 
	
	Prin=LoanAmount2-DownPayment 
	
	var base = 1;
  	var mbase = 1 + MonthRate;
  	for (i=0; i<Years * 12; i++)
  	{
    		base = base * mbase
  	}
  	
	MonthPI=Math.floor(((Prin*MonthRate)/(1-(1/base)))*Math.pow(10,2) + 0.5)/Math.pow(10,2);
		
	
	//MonthPI=Math.floor((Prin*MonthRate)/(1-Math.pow((1+MonthRate),(-1*NumPayments)))*100)/100 
	document.getElementById('MonthlyPI').innerHTML = MonthPI.toFixed(2)
	
	UnfmtYrlyTaxes = xreplace(document.getElementById('YrlyTaxes').value,',','')
	MonthTaxes = UnfmtYrlyTaxes / 12
	document.getElementById('MonthlyTaxes').innerHTML = MonthTaxes.toFixed(2)

	UnfmtYrlyIns = xreplace(document.getElementById('YrlyIns').value,',','')
	MonthIns = UnfmtYrlyIns / 12
	document.getElementById('MonthlyIns').innerHTML = MonthIns.toFixed(2)

	MonthMI = (LoanAmount2 * 0.005) / 12
	if (Years == 15) { MonthMI = 0; }
	document.getElementById('MonthlyMI').innerHTML = MonthMI.toFixed(2)

	MonthPmt = MonthPI  + MonthTaxes + MonthIns + MonthMI
	document.getElementById('MonthlyPmt').innerHTML = MonthPmt.toFixed(2)

	var LoanToValue = (UnfmtLoanAmount / xreplace(form.EstPropValue.value,',','')) * 100
	document.getElementById('LTV').innerHTML = LoanToValue.toFixed(2)

	AnnualCombinedInc = xreplace(form.AnnualCombinedInc.value,',','')

	var HousingExpRatio = (MonthPmt / (AnnualCombinedInc / 12)) * 100 
	document.getElementById('HousingExpRatio').innerHTML = HousingExpRatio.toFixed(2)
				
	MthlyNonHouseExp = (xreplace(form.MthlyNonHouseExp.value,',','') * 1)
	MthlyExp = MthlyNonHouseExp + MonthPmt
	var TotalRatio = (MthlyExp / (AnnualCombinedInc / 12)) * 100
	document.getElementById('TotalRatio').innerHTML = TotalRatio.toFixed(2)

	formatOUT2(document.getElementById('LoanAmountMIP'));
	formatOUT2(document.getElementById('MonthlyPI'));
	formatOUT2(document.getElementById('MonthlyIns'));
	formatOUT2(document.getElementById('MonthlyTaxes'));
	formatOUT2(document.getElementById('MonthlyMI'));
	formatOUT2(document.getElementById('MonthlyPmt'));
	
}

function Validate() {

	document.getElementById('Result1').style.display = "none";
	document.getElementById('Result2').style.display = "none";

	var ErrorMsg = "";
	UnfmtLoanAmount = xreplace(document.getElementById('LoanAmount').value,',','')
	UnfmtLoanAmount1 = UnfmtLoanAmount * 1
	UnfmtLoanAmount2 = UnfmtLoanAmount1.toFixed(0)
	MaxLoanAmount1 = document.myform.MaxLoanAmount.value
	MaxLoanAmount2 = MaxLoanAmount1 * 1;
	//window.alert(UnfmtLoanAmount2);
	
	if(document.myform.State.selectedIndex <= 0) {
		ErrorMsg = ErrorMsg + "Property State is not selected<br>";
		document.getElementById('MaxLoanAmount').value = "0";
	}
	if(document.myform.County.selectedIndex <= 0) {
		ErrorMsg = ErrorMsg + "Property County is not selected<br>";
		document.getElementById('MaxLoanAmount').value = "0";
	}
	if (UnfmtLoanAmount2 > MaxLoanAmount2 && MaxLoanAmount2 > 0) {
		var MaxLnAmt = number_format(document.myform.MaxLoanAmount.value);
		ErrorMsg = ErrorMsg + "Loan Amount exceeds the limit of $" + MaxLnAmt + "<br>";
	}
	
	if (UnfmtLoanAmount2 < 10000) {
		ErrorMsg = ErrorMsg + "Loan Amount entered is less than the minimum loan amount required<br>";
	} 
	if (UnfmtLoanAmount2 == 0) {
		ErrorMsg = ErrorMsg + "Loan Amount is equal to 0 (zero).  You must enter a valid loan amount<br>";
	}
	
	
	if (ErrorMsg != "") {
		ErrorMsg = "ERROR<br>" + ErrorMsg;
		document.getElementById("ErrorMsg").innerHTML = ErrorMsg;
		
		document.getElementById('LTV').innerHTML = "";
		document.getElementById('HousingExpRatio').innerHTML = "";
		document.getElementById('TotalRatio').innerHTML = "";
		document.getElementById('NumberOfPayments').innerHTML = "";
		document.getElementById('LoanAmountMIP').innerHTML = "";
		document.getElementById('MonthlyPI').innerHTML = "";
		document.getElementById('MonthlyIns').innerHTML = "";
		document.getElementById('MonthlyTaxes').innerHTML = "";
		document.getElementById('MonthlyMI').innerHTML = "";
		document.getElementById('MonthlyPmt').innerHTML = "";
		document.getElementById("Result1").style.display = "none";
		document.getElementById('Msg1').innerHTML = "";
		document.getElementById("Result2").style.display = "none";
		document.getElementById('Msg2').innerHTML = "";
				
			
		return false;
	} else {
		document.getElementById("ErrorMsg").innerHTML = "";
		LoanCalc();
		Qualify();
		return true;
	}
	
}

function Qualify() {
		
		var MessageGood = "";
		var MessageBad = "";
		var DTIValid = true;
		var LTVValid = true;
		var PurchaseMaxLTV = 97.75;
		var RefinanceMaxLTV0 = 95; //Max LTV for 0x30 1-2 Family w/CashOut
		var RefinanceMaxLTV1 = 85; //Max LTV for 1x30 and/or 3-4 Family
		var RefinanceMaxLTV2 = 97.75; //Max LTV for 0x30 1-2 Family w/o CashOut
		var LTV = document.getElementById('LTV').innerHTML;
		var DTI = document.getElementById('TotalRatio').innerHTML;
		var eLoanPurpose = document.myform.LoanPurpose;
		var eMortgageHistory = document.myform.MortgageHistory;
		var eNumUnits = document.myform.NumUnits;
				
		DTI = parseFloat(DTI);
		
		if (DTI <= 50) {
			MessageGood = "Scenario Qualified<br>";
			DTIValid = true;
		}
		if (DTI >50 && DTI <= 55) {
			MessageBad = "Compensating Factors Needed, contact your AE<br>"
			DTIValid = false;
		}
		if (DTI > 55) {
			MessageBad = "Scenario not qualified, contact your AE to discuss<br>"
			DTIValid = false;
		}
		
		var Purpose = eLoanPurpose.options[eLoanPurpose.selectedIndex].value;
		var MtgHist = eMortgageHistory.options[eMortgageHistory.selectedIndex].value;
		var NumUnits = eNumUnits.options[eNumUnits.selectedIndex].text;

		LTV = parseFloat(LTV);
		PurchaseMaxLTV = parseFloat(PurchaseMaxLTV);
		RefinanceMaxLTV0 = parseFloat(RefinanceMaxLTV0);
		RefinanceMaxLTV1 = parseFloat(RefinanceMaxLTV1);
		RefinanceMaxLTV2 = parseFloat(RefinanceMaxLTV2);
		NumUnits = parseFloat(NumUnits);
		
		if (Purpose == "Purchase" && LTV <= PurchaseMaxLTV) {
			LTVValid = true;
		}
		if (Purpose == "Purchase" && LTV > PurchaseMaxLTV) {
			MessageBad = MessageBad + "LTV exceeds Max LTV of " + PurchaseMaxLTV + "%<br>";
			LTVValid = false;
		}
		//////////////////////////////////////////////////////////////////////
		if (Purpose == "Refinance" && LTV <= RefinanceMaxLTV0 && MtgHist == "0x30" && NumUnits >= 1 && NumUnits <= 2) {
			LTVValid = true;
		}
		if (Purpose == "Refinance" && LTV > RefinanceMaxLTV0 && MtgHist == "0x30" && NumUnits >= 1 && NumUnits <= 2) {
			MessageBad = MessageBad + "LTV exceeds Max LTV of " + RefinanceMaxLTV0 + "%<br>";
			LTVValid = false;
		}
		//////////////////////////////////////////////////////////////////////
		if (Purpose == "Refinance" && LTV <= RefinanceMaxLTV1 && (MtgHist == "1x30" || NumUnits == 3 || NumUnits == 4)) {
			LTVValid = true;
		}
		if (Purpose == "Refinance" && LTV > RefinanceMaxLTV1 &&  (MtgHist == "1x30" || NumUnits == 3 || NumUnits == 4)) {
			MessageBad = MessageBad + "LTV exceeds Max LTV of " + RefinanceMaxLTV1 + "%<br>";
			LTVValid = false;
		}
		//////////////////////////////////////////////////////////////////////
		if (Purpose == "RefinanceRateTerm" && LTV <= RefinanceMaxLTV2 && MtgHist == "0x30" && NumUnits >= 1 && NumUnits <= 2) {
			LTVValid = true;
		}
		if (Purpose == "RefinanceRateTerm" && LTV > RefinanceMaxLTV2 && MtgHist == "0x30" && NumUnits >= 1 && NumUnits <= 2) {
			MessageBad = MessageBad + "LTV exceeds Max LTV of " + RefinanceMaxLTV2 + "%<br>";
			LTVValid = false;
		}
		//////////////////////////////////////////////////////////////////////
		if (Purpose == "RefinanceRateTerm" && LTV <= RefinanceMaxLTV1 && (MtgHist == "1x30" || NumUnits == 3 || NumUnits == 4)) {
			LTVValid = true;
		}
		if (Purpose == "RefinanceRateTerm" && LTV > RefinanceMaxLTV1 &&  (MtgHist == "1x30" || NumUnits == 3 || NumUnits == 4)) {
			MessageBad = MessageBad + "LTV exceeds Max LTV of " + RefinanceMaxLTV1 + "%<br>";
			LTVValid = false;
		}
		
		
		if (MtgHist == "Worse") {
			MessageBad = "Verifyable explanation needed to qualify, contact your AE to discuss your options!";
		}


		if (MessageGood != "") {
			document.getElementById("Msg1").innerHTML = MessageGood;
			document.getElementById("Result1").style.display = "block";
			document.getElementById("Result2").style.display = "none";
		}
		if (MessageBad != "") {
			document.getElementById("Msg2").innerHTML = MessageBad;
			document.getElementById("Result1").style.display = "none";
			document.getElementById("Result2").style.display = "block";
		}
		
}

function ResetPage() {

		document.getElementById('LTV').innerHTML = "";
		document.getElementById('HousingExpRatio').innerHTML = "";
		document.getElementById('TotalRatio').innerHTML = "";
		document.getElementById('NumberOfPayments').innerHTML = "";
		document.getElementById('LoanAmountMIP').innerHTML = "";
		document.getElementById('MonthlyPI').innerHTML = "";
		document.getElementById('MonthlyIns').innerHTML = "";
		document.getElementById('MonthlyTaxes').innerHTML = "";
		document.getElementById('MonthlyMI').innerHTML = "";
		document.getElementById('MonthlyPmt').innerHTML = "";

		var sel1 = document.getElementById('State');
		sel1.selectedIndex = 0;
		sel1.onchange();
		sel1.focus();

		document.getElementById('ErrorMsg').innerHTML = "";

		document.myform.reset();
	
}

function getWeek(year,month,day) {

		year = year*1;
		month = month*1;
		day = day*1;

		var a = Math.floor((14-(month))/12);
		var y = year+4800-a;
		var m = (month)+(12*a)-3;
		var jd = day + Math.floor(((153*m)+2)/5) + 
			(365*y) + Math.floor(y/4) - Math.floor(y/100) + 
			Math.floor(y/400) - 32045;

		var d4 = (jd+31741-(jd%7))%146097%36524%1461;
		var L = Math.floor(d4/1460);
		var d1 = ((d4-L)%365)+L;

		NumberOfWeek = Math.floor(d1/7) + 1;

		return NumberOfWeek;

}
		
function CalcIncome(PeriodEnding, YTDIncome, NTMth1, NTMth2) {

		var DateParts = PeriodEnding.split("/");

		var psYear = DateParts[2];
		var psMonth = DateParts[0];
		var psDay = DateParts[1];
		var WeekNumber = getWeek(psYear, psMonth, psDay);

		var y2dIncome = YTDIncome*1;

		var weeklyIncome = y2dIncome/WeekNumber;
		weeklyIncome = weeklyIncome.toFixed(2);

		var yearlyIncome = weeklyIncome*52;
		yearlyIncome = yearlyIncome.toFixed(2);
			
		var monthlyIncome = yearlyIncome/12;
		monthlyIncome = monthlyIncome.toFixed(2);

		var NonTaxMthInc1 = NTMth1 * 1;
		var NonTaxMthInc2 = NTMth2 * 1;
		NonTaxMthInc1 = NonTaxMthInc1 * 1.25;
		NonTaxMthInc2 = NonTaxMthInc2 * 1.25;
		NonTaxYrInc1 = NonTaxMthInc1*12;
		NonTaxYrInc2 = NonTaxMthInc2*12;
		NonTaxWkInc1 = NonTaxYrInc1/52;
		NonTaxWkInc2 = NonTaxYrInc2/52;
		

		if (isNaN(WeekNumber)) {
			WeekNumber = 0;
		}
		if (isNaN(monthlyIncome)) {
			monthlyIncome = 0;
		}
		if (isNaN(weeklyIncome)) {
			weeklyIncome = 0;
		}
		if (isNaN(yearlyIncome)) {
			yearlyIncome = 0;
		}
		if (isNaN(NonTaxMthInc1)) {
			NonTaxMthInc1 = 0;
		}
		if (isNaN(NonTaxMthInc2)) {
			NonTaxMthInc2 = 0;
		}
				
		var myData = new Array()
		
		myData[0] = WeekNumber;
		myData[1] = (weeklyIncome*1) + (NonTaxWkInc1*1) + (NonTaxWkInc2*1);
		myData[2] = (monthlyIncome*1) + (NonTaxMthInc1*1) + (NonTaxMthInc2*1);
		myData[3] = (yearlyIncome*1) + (NonTaxYrInc1*1) + (NonTaxYrInc2*1);
		
		return myData;
		
}

function CalcInc() {

		//Borrower Income
		var PeriodEnding1 = document.Form1.PeriodEnding1.value;
		var YTDIncome1 =  xreplace(document.Form1.YTDIncome1.value,',','');
		var NTMonthlyInc11 =  xreplace(document.Form1.NTMonthlyInc11.value,',','');
		var NTMonthlyInc12 =  xreplace(document.Form1.NTMonthlyInc12.value,',','');
				
		var Data1 = CalcIncome(PeriodEnding1, YTDIncome1, NTMonthlyInc11, NTMonthlyInc12);
				
		var psWeekNumber1 = Data1[0];
		var WeeklyIncome1 = Data1[1];
		var MonthlyIncome1 = Data1[2];
		var YearlyIncome1 = Data1[3];
		
		document.getElementById('psWeekNumber1').innerHTML = psWeekNumber1;
		document.getElementById('WeeklyIncome1').innerHTML = WeeklyIncome1.toFixed(2);
		document.getElementById('MonthlyIncome1').innerHTML = MonthlyIncome1.toFixed(2);
		document.getElementById('YearlyIncome1').innerHTML = YearlyIncome1.toFixed(2);
		
		formatOUT2(document.getElementById('WeeklyIncome1'));
		formatOUT2(document.getElementById('MonthlyIncome1'));
		formatOUT2(document.getElementById('YearlyIncome1'));
		
		//CoBorrower Income	
		var PeriodEnding2 = document.Form1.PeriodEnding2.value;
		var YTDIncome2 =  xreplace(document.Form1.YTDIncome2.value,',','');
		var NTMonthlyInc21 =  xreplace(document.Form1.NTMonthlyInc21.value,',','');
		var NTMonthlyInc22 =  xreplace(document.Form1.NTMonthlyInc22.value,',','');
				
		var Data2 = CalcIncome(PeriodEnding2, YTDIncome2, NTMonthlyInc21, NTMonthlyInc22);
		
		var psWeekNumber2 = Data2[0];
		var WeeklyIncome2 = Data2[1];
		var MonthlyIncome2 = Data2[2];
		var YearlyIncome2 = Data2[3];
		
		document.getElementById('psWeekNumber2').innerHTML = psWeekNumber2;
		document.getElementById('WeeklyIncome2').innerHTML = WeeklyIncome2.toFixed(2);
		document.getElementById('MonthlyIncome2').innerHTML = MonthlyIncome2.toFixed(2);
		document.getElementById('YearlyIncome2').innerHTML = YearlyIncome2.toFixed(2);
		
		formatOUT2(document.getElementById('WeeklyIncome2'));
		formatOUT2(document.getElementById('MonthlyIncome2'));
		formatOUT2(document.getElementById('YearlyIncome2'));
		
		// Process Combined Income
		var cmbWeeklyIncome = (Number(Data1[1]) + Number(Data2[1]));
		var cmbMonthlyIncome = (Number(Data1[2]) + Number(Data2[2]));
		var cmbYearlyIncome = (Number(Data1[3]) + Number(Data2[3]));
		
		document.getElementById('cmbWeeklyIncome').innerHTML = cmbWeeklyIncome.toFixed(2);
		document.getElementById('cmbMonthlyIncome').innerHTML = cmbMonthlyIncome.toFixed(2);
		document.getElementById('cmbYearlyIncome').innerHTML = cmbYearlyIncome.toFixed(2);
		
		formatOUT2(document.getElementById('cmbWeeklyIncome'));
		formatOUT2(document.getElementById('cmbMonthlyIncome'));
		formatOUT2(document.getElementById('cmbYearlyIncome'));
		
		
}

function CalcMonthly(YearlyIncElement, MonthlyIncElement) {
	
		if (isNaN(YearlyIncElement.value) == false) {
			var MonthlyInc = YearlyIncElement.value / 12;
			MonthlyIncElement.value = MonthlyInc.toFixed(2);
			formatOUT(MonthlyIncElement);
		} else {
			MonthlyIncElement.value = '';
		}
		
}