var LoanAmount_1,					LoanAmount_2,					LoanAmount_3,					LoanAmount_4;
var PriceOfHome_1,					PriceOfHome_2,					PriceOfHome_3,					PriceOfHome_4;
var DownPayment_1,					DownPayment_2,					DownPayment_3,					DownPayment_4;
var ClosingCost_1,					ClosingCost_2,					ClosingCost_3,					ClosingCost_4;
var DownPaymentPercent_1,			DownPaymentPercent_2,			DownPaymentPercent_3,			DownPaymentPercent_4;
var TotalMonthlyPayment_1,			TotalMonthlyPayment_2,			TotalMonthlyPayment_3,			TotalMonthlyPayment_4;
var MonthlyPrincipalInterest_1,		MonthlyPrincipalInterest_2,		MonthlyPrincipalInterest_3,		MonthlyPrincipalInterest_4;
var TaxAndInsurance_1,				TaxAndInsurance_2,				TaxAndInsurance_3,				TaxAndInsurance_4;
var MortgageInsurance_1,			MortgageInsurance_2,			MortgageInsurance_3,			MortgageInsurance_4;
var InterestRateFactor_1,			InterestRateFactor_2,			InterestRateFactor_3,			InterestRateFactor_4;
var APR_1,							APR_2,							APR_3,							APR_4;
var JumboRate_1,					JumboRate_2,					JumboRate_3,					JumboRate_4;
var JumboRateFactor_1,				JumboRateFactor_2,				JumboRateFactor_3,				JumboRateFactor_4;
var ConformingRate_1,				ConformingRate_2,				ConformingRate_3,				ConformingRate_4;
var ConformingRateFactor_1,			ConformingRateFactor_2,			ConformingRateFactor_3,			ConformingRateFactor_4;
var	InterestRate_1,					InterestRate_2,					InterestRate_3,					InterestRate_4
var DownPaymentPercent,				PercentLTV,						JumboMinimumLimit;
var MonthlyIncome,					CashForClosing,					MonthlyDebts;
var PercentClosingCost;			

function PerformValidation(){
	document.all["calculatorDiv"].style.visibility="hidden";

	objMonthlyIncome		= 	document.all["monthlyIncomeTextBox"];
	objCashForClosing		= 	document.all["CashForClosingTextbox"];
	objMonthlyDebts			= 	document.all["monthlyDebtsTextbox"];
	objTermLoan				=	document.all["termDropDownList"];

	objErrMonthlyIncome		=	document.all["errMonthlyIncome"];
	objErrCashForClosing	=	document.all["errCashForClosing"];
	objErrMonthlyDebts		=	document.all["errMonthlyDebts"];

	if (!ValidatePrice(objMonthlyIncome, objErrMonthlyIncome, "Monthly Income", 2000))	return false;
	if (objCashForClosing.value.length > 0){
		if (!ValidatePrice(objCashForClosing, objErrCashForClosing, "Cash Closing", 2000))	return false;
		CashForClosing			=	parseFloat(GetPrice(objCashForClosing.value));														
	}
	else{
		CashForClosing			=	0;
		objErrCashForClosing.style.visibility="hidden";
	}
	
	if (!ValidatePrice(objMonthlyDebts, objErrMonthlyDebts, "Monthly Debts", 0))		return false;

	MonthlyIncome			=	parseFloat(GetPrice(objMonthlyIncome.value));
	MonthlyDebts			=	parseFloat(GetPrice(objMonthlyDebts.value));
	DownPaymentPercent		=	10;//Hard-coded for now. Should be dropdownlist driven.
	PercentLTV				=	100 - DownPaymentPercent;

	if (!ValidateMonthlyDebt(objErrMonthlyDebts))	return false;
	if(!IsDataQualified())							return false;

	PerformCalculation();
	
	SetCalculatedValues(1);
	SetCalculatedValues(2);
	SetCalculatedValues(3);
	SetCalculatedValues(4);
	
	document.all["calculatorDiv"].style.visibility="visible";
}

function SetCommas(intValue)
{
	commaValue="";
	intValue = intValue + "";
	var intPeriodIndex, valuePrePeriod, valuePostPeriod;
	intPeriodIndex	= intValue.indexOf(".");
	intLength		= intValue.length;


	if(intPeriodIndex != -1){
		valuePrePeriod	= intValue.substring(0,intPeriodIndex)
		valuePostPeriod = intValue.substring(intPeriodIndex+1, intLength)
		if(valuePostPeriod.length == 1)
			valuePostPeriod = valuePostPeriod + "0";
	}
	else{
		valuePrePeriod	= intValue;
		valuePostPeriod = "00";
	}

	if(valuePostPeriod.length > 2)
		valuePostPeriod = valuePostPeriod.substring(0,2);

	intLength = valuePrePeriod.length;
	for ( i = intLength -3; i > 0; i=i-3)
	{
		commaValue		= ","+valuePrePeriod.substring(i)+commaValue;
		valuePrePeriod	= valuePrePeriod.substring(0,i);
	}
	commaValue =  valuePrePeriod+commaValue;
	commaValue = commaValue + "." + valuePostPeriod;
	return(commaValue);
}

function ValidateMonthlyDebt(errSpanObj){
	if(MonthlyIncome < MonthlyDebts){
		errSpanObj.innerText		=	"Monthly Income should exceed Monthly Debts.";
		errSpanObj.style.visibility = "visible";
		objMonthlyDebts.focus();
		return false;
	}
	if(MonthlyIncome - MonthlyDebts < 1500){
		errSpanObj.innerText		=	"Monthly Debt is too high.";
		errSpanObj.style.visibility = "visible";
		objMonthlyDebts.focus();
		return false;
	}
	return true;
}

function IsDataQualified(){
	value = (CalculatePrincipalInterestTaxInsurance() - MonthlyDebts);
	if(value < 100){
		errQualifiedData.innerText="We were not able to calculate any results based on your input";
		errQualifiedData.style.visibility="visible";
		return(false);
	}
	errQualifiedData.style.visibility="hidden";
	return(true);
}

function ValidatePrice(objTxt, errSpanObj, errMsg, minimumAmount)
{
	var objValue;
	reg	= /^\$?([1-9]{1}[0-9]{0,2}(\,[0-9]{3})*(\.[0-9]{0,2})?|[1-9]{1}[0-9]{0,}(\.[0-9]{0,2})?|0(\.[0-9]{0,2})?|(\.[0-9]{1,2})?)$/;

	if((objTxt.value == 0) || (objTxt.value.search(reg) == -1)){
		errSpanObj.innerText		=	"Please enter a valid "+errMsg;
		errSpanObj.style.visibility = "visible";
		objTxt.focus();
		return false;
	}
	else{
		objValue = GetPrice(objTxt.value);
		if(objValue >= minimumAmount){
			errSpanObj.style.visibility = "hidden";
			return true;
		}
		else{
			errSpanObj.innerText		=	"The amount you have entered is too low";
			errSpanObj.style.visibility = "visible";
			objTxt.focus();
			return false;						
		}
	}
}

function GetPrice(value)
{
	value = value+"";
	value = value.split(",").join("");
	value = value.split("$").join("");
	return (value);
}

function GetPercent(intValue){

	var intPeriodIndex, valuePrePeriod, valuePostPeriod;
	intValue = intValue + "";
	intPeriodIndex	= intValue.indexOf(".");
	intLength		= intValue.length;

	if(intPeriodIndex != -1){
		valuePrePeriod	= intValue.substring(0,intPeriodIndex)
		valuePostPeriod = intValue.substring(intPeriodIndex+1, intLength)
		if(valuePostPeriod.length == 1)
			valuePostPeriod = valuePostPeriod + "0";
	}
	else{
		valuePrePeriod	= intValue;
		valuePostPeriod = "00";
	}

	if(valuePostPeriod.length > 2)
		valuePostPeriod = valuePostPeriod.substring(0,2);

	return(valuePrePeriod+"."+valuePostPeriod+" %");
}

function PerformCalculation(){
	
	LoanAmount_1				= CalculateLoanAmount(JumboRateFactor_1,ConformingRateFactor_1);
	LoanAmount_2				= CalculateLoanAmount(JumboRateFactor_2,ConformingRateFactor_2);
	LoanAmount_3				= CalculateLoanAmount(JumboRateFactor_3,ConformingRateFactor_3);
	LoanAmount_4				= CalculateLoanAmount(JumboRateFactor_4,ConformingRateFactor_4);
	
	ClosingCost_1				= CalculateTotalClosingCost(LoanAmount_1);
	ClosingCost_2				= CalculateTotalClosingCost(LoanAmount_2);
	ClosingCost_3				= CalculateTotalClosingCost(LoanAmount_3);
	ClosingCost_4				= CalculateTotalClosingCost(LoanAmount_4);

	InterestRateFactor_1		= CalculateInterestRateFactor(LoanAmount_1, JumboRateFactor_1, ConformingRateFactor_1);
	InterestRateFactor_2		= CalculateInterestRateFactor(LoanAmount_2, JumboRateFactor_2, ConformingRateFactor_2);
	InterestRateFactor_3		= CalculateInterestRateFactor(LoanAmount_3, JumboRateFactor_3, ConformingRateFactor_3);
	InterestRateFactor_4		= CalculateInterestRateFactor(LoanAmount_4, JumboRateFactor_4, ConformingRateFactor_4);
	
	InterestRate_1				= CalculateInterestRate(LoanAmount_1, JumboRate_1, ConformingRate_1);
	InterestRate_2				= CalculateInterestRate(LoanAmount_2, JumboRate_2, ConformingRate_2);
	InterestRate_3				= CalculateInterestRate(LoanAmount_3, JumboRate_3, ConformingRate_3);
	InterestRate_4				= CalculateInterestRate(LoanAmount_4, JumboRate_4, ConformingRate_4);

	DownPayment_1				= CalculateDownPayment(ClosingCost_1);
	DownPayment_2				= CalculateDownPayment(ClosingCost_2);
	DownPayment_3				= CalculateDownPayment(ClosingCost_3);
	DownPayment_4				= CalculateDownPayment(ClosingCost_4);

	PriceOfHome_1				= CalculatePriceOfHome(LoanAmount_1,DownPayment_1);
	PriceOfHome_2				= CalculatePriceOfHome(LoanAmount_2,DownPayment_2);
	PriceOfHome_3				= CalculatePriceOfHome(LoanAmount_3,DownPayment_3);
	PriceOfHome_4				= CalculatePriceOfHome(LoanAmount_4,DownPayment_4);
	
	DownPaymentPercent_1		= CalculateDownPaymentPercent(DownPayment_1,PriceOfHome_1);
	DownPaymentPercent_2		= CalculateDownPaymentPercent(DownPayment_2,PriceOfHome_2);
	DownPaymentPercent_3		= CalculateDownPaymentPercent(DownPayment_3,PriceOfHome_3);
	DownPaymentPercent_4		= CalculateDownPaymentPercent(DownPayment_4,PriceOfHome_4);
	
	MonthlyPrincipalInterest_1	= CalculateMonthlyPrincipalInterest(PriceOfHome_1,DownPayment_1,InterestRateFactor_1);
	MonthlyPrincipalInterest_2	= CalculateMonthlyPrincipalInterest(PriceOfHome_2,DownPayment_2,InterestRateFactor_2);
	MonthlyPrincipalInterest_3	= CalculateMonthlyPrincipalInterest(PriceOfHome_3,DownPayment_3,InterestRateFactor_3);
	MonthlyPrincipalInterest_4	= CalculateMonthlyPrincipalInterest(PriceOfHome_4,DownPayment_4,InterestRateFactor_4);

	TaxAndInsurance_1			= CalculateTaxInsurance(PriceOfHome_1);
	TaxAndInsurance_2			= CalculateTaxInsurance(PriceOfHome_2);
	TaxAndInsurance_3			= CalculateTaxInsurance(PriceOfHome_3);
	TaxAndInsurance_4			= CalculateTaxInsurance(PriceOfHome_4);

	MortgageInsurance_1			= CalulateMortageInsurance(LoanAmount_1);
	MortgageInsurance_2			= CalulateMortageInsurance(LoanAmount_2);
	MortgageInsurance_3			= CalulateMortageInsurance(LoanAmount_3);
	MortgageInsurance_4			= CalulateMortageInsurance(LoanAmount_4);

	TotalMonthlyPayment_1		= MonthlyPrincipalInterest_1 + TaxAndInsurance_1 + MortgageInsurance_1;
	TotalMonthlyPayment_2		= MonthlyPrincipalInterest_2 + TaxAndInsurance_2 + MortgageInsurance_2;
	TotalMonthlyPayment_3		= MonthlyPrincipalInterest_3 + TaxAndInsurance_3 + MortgageInsurance_3;
	TotalMonthlyPayment_4		= MonthlyPrincipalInterest_4 + TaxAndInsurance_4 + MortgageInsurance_4;

	APR_1						= CalculateAPR(MonthlyPrincipalInterest_1,LoanAmount_1,ClosingCost_1);
	APR_2						= CalculateAPR(MonthlyPrincipalInterest_2,LoanAmount_2,ClosingCost_2);
	APR_3						= CalculateAPR(MonthlyPrincipalInterest_3,LoanAmount_3,ClosingCost_3);
	APR_4						= CalculateAPR(MonthlyPrincipalInterest_4,LoanAmount_4,ClosingCost_4);
	
}

function SetCalculatedValues(colIndex){
	document.all["priceOfHome_Col"+colIndex].innerText			= SetCommas(eval("PriceOfHome_"+colIndex));
	document.all["loanAmount_Col"+colIndex].innerText			= SetCommas(eval("LoanAmount_"+colIndex));
	document.all["downPayment_Col"+colIndex].innerText			= SetCommas(eval("DownPayment_"+colIndex));
	document.all["downPaymentPercent_Col"+colIndex].innerText	= GetPercent(eval("DownPaymentPercent_"+colIndex));				
	document.all["closingCost_Col"+colIndex].innerText			= SetCommas(eval("ClosingCost_"+colIndex));
	document.all["monthlyPayment_Col"+colIndex].innerText		= SetCommas(eval("TotalMonthlyPayment_"+colIndex));
	document.all["principalInterest_Col"+colIndex].innerText	= SetCommas(eval("MonthlyPrincipalInterest_"+colIndex));
	document.all["TaxInsurance_Col"+colIndex].innerText			= SetCommas(eval("TaxAndInsurance_"+colIndex));
	document.all["mortgageInsurance_Col"+colIndex].innerText	= SetCommas(eval("MortgageInsurance_"+colIndex));
	document.all["rate_Col"+colIndex].innerText					= GetPercent(eval("InterestRate_"+colIndex));
	document.all["APR_Col"+colIndex].innerText					= GetPercent(eval("APR_"+colIndex));
}

function CalculateDownPaymentPercent(downPayment, priceOfHome){
	value = (downPayment*100)/priceOfHome;
	return(value);
}

function CalculateTaxInsurance(priceOfHome){
	value = (priceOfHome * 1.25)/(100 * 12);
	return(value);
}

function CalculateHomeOwnersInsurance(priceOfHome){
	value = (priceOfHome * 0.35)/(100 * 12);
	return(value);
}

function CalculatePriceOfHome(loanAmount, downPayment){
	value = loanAmount + downPayment;
	return(value);
}

function CalculateInterestRateFactor(loanAmount, jumboRateFactor, conformingRateFactor){
	if(loanAmount > JumboMinimumLimit)
		return(jumboRateFactor);
	else
		return(conformingRateFactor);
}

function CalculateInterestRate(loanAmount, jumboRate, conformingRate){
	if(loanAmount > JumboMinimumLimit)
		return(jumboRate);
	else
		return(conformingRate);
}

function CalulateMortageInsurance(loanAmount){
	return((loanAmount * CalculateMortgageInsuranceFixedRate())/(100 * 12))
}

function CalculateMortgageInsuranceFixedRate(){

	if(PercentLTV <= 80){
		return(0);
	}
	if(PercentLTV <= 85){
		return(0.30);
	}
	if(PercentLTV <= 90){
		return(0.50);
	}
	else
	{
		return(0.75);
	}
}

function CalculateMortgageInsuranceAdjustableRate(){
	if(PercentLTV <= 80){
		return(0);
	}
	if(PercentLTV <= 85){
		return(0.30);
	}
	if(PercentLTV <= 90){
		return(0.58);
	}
	else{
		return(0.85);
	}
}

function CalculateMonthlyPrincipalInterest(priceOfHome, downPayment, interestRateFactor){
	value = ((priceOfHome - downPayment) * interestRateFactor)/1000;
	return(value);
}

function CalculteDebtToIncomeRatio(){
	if(PercentLTV <= 80){
		return(0.42);
	}
	if(PercentLTV <= 85){
		return(0.4);
	}
	if(PercentLTV <= 90){
		return(0.38);
	}
	if(PercentLTV <= 95){
		return(0.36);
	}
	else{
		return(0.34);
	}
}

function CalculatePrincipalInterestTaxInsurance(){
	value = MonthlyIncome * CalculteDebtToIncomeRatio();
	return(value);
}

function CalculateAPR(paymentInterest, loanAmount, closingCost){
	value = (paymentInterest * 12 * 100)/(loanAmount + closingCost);
	return(value);
}

function CalculateLoanAmount(jumboRate, conformingRate){

	conformingLoan	= CalculateConformingLoanAmount(conformingRate);
	jumboLoan		= CalculateJumboLoanAmount(jumboRate);

	if(conformingLoan > JumboMinimumLimit){
		return(jumboLoan);
	}
	else{
		return(conformingLoan);
	}
}

function CalculateConformingLoanAmount(conformingRate){
	value = ((CalculatePrincipalInterestTaxInsurance() - MonthlyDebts) * 1000)/conformingRate;
	return(value);
}

function CalculateJumboLoanAmount(jumboRate){
	value = ((CalculatePrincipalInterestTaxInsurance() - MonthlyDebts) * 1000)/jumboRate;
	return(value);
}

function CalculatePartialClosingCost1(loanAmount){
	value = loanAmount * PercentClosingCost;
	return(value);
}

function CalculatePartialClosingCost2(loanAmount){				
	value = ((CashForClosing - CalculatePartialClosingCost1(loanAmount)) * PercentClosingCost);
	if(value < 0) value = 0;								
	return(value);
}

function CalculateTotalClosingCost(loanAmount){
	value = CalculatePartialClosingCost1(loanAmount) + CalculatePartialClosingCost2(loanAmount);
	return(value);
}

function CalculateDownPayment(totalClosingCost){
	value = CashForClosing - totalClosingCost;
	if(value < 0) value = 0;
	return(value);
}
