function calc_home_affordability() {
	f = document.forms.defaultForm;
	// check values
	if (f.yearly_income.value=="") { alert("Enter Yearly Income!"); f.yearly_income.focus(); return; }
	if (f.yearly_taxes.value=="") { alert("Enter Yearly Taxes!"); f.yearly_taxes.focus(); return; }
	if (f.yearly_insurance.value=="") { alert("Enter Yearly Insurance!"); f.yearly_insurance.focus(); return; }
	if (f.monthly_debt.value=="") { alert("Enter Monthly Debt!"); f.monthly_debt.focus(); return; }
	if (f.interest_rate.value=="") { alert("Enter Interest Rate!"); f.interest_rate.focus(); return; }
	if (f.term.selectedIndex==0) { alert("Choose a term!"); f.term.focus(); return; }

	I = f.yearly_income.value / 12;
	T = f.yearly_taxes.value / 12;
	S = f.yearly_insurance.value / 12;
	D = f.monthly_debt.value;
	i = f.interest_rate.value / 100;
	p = f.term.options[f.term.selectedIndex].value;
	
	M = Math.pow(1 + (i/12), p) - 1;
	M = M / ((i/12)*Math.pow(1 + (i/12), p));
	
	var res1 = (0.28*(I - T - S)) * M;
	var res2 = (0.36*(I - T - S - D)) * M;
	
	document.all("spanResult").innerText = "$"+nicely_formatted(res1>res2?res2:res1);
	window.document.getElementById('TDresults').style.display = 'inline';
}

function calc_salary_requirement() {
	
	f = document.forms.defaultForm;
	// check values
	if (f.loan_amount.value=="") { alert("Enter Loan Amount!"); f.loan_amount.focus(); return; }
	if (f.yearly_taxes.value=="") { alert("Enter Yearly Taxes!"); f.yearly_taxes.focus(); return; }
	if (f.yearly_insurance.value=="") { alert("Enter Yearly Insurance!"); f.yearly_insurance.focus(); return; }
	if (f.monthly_debt.value=="") { alert("Enter Monthly Debt!"); f.monthly_debt.focus(); return; }
	if (f.interest_rate.value=="") { alert("Enter Interest Rate!"); f.interest_rate.focus(); return; }
	if (f.term.selectedIndex==0) { alert("Choose a term!"); f.term.focus(); return; }
	
	L = f.loan_amount.value;
	T = f.yearly_taxes.value / 12;
	S = f.yearly_insurance.value / 12;
	D = f.monthly_debt.value;
	i = f.interest_rate.value / 100;
	p = f.term.options[f.term.selectedIndex].value;
	
	U = L*(i/12)* Math.pow(1+(i/12),p);
	d = Math.pow(1+(i/12),p)-1;
	
	res1 = (((U / d)/0.28)+T/1+S/1)*12;
	res2 = (((U / d)/0.36)+T/1+S/1+D/1)*12;
	
	document.all("spanResult").innerText = "$"+nicely_formatted(res1>res2?res1:res2);
	window.document.getElementById('TDresults').style.display = 'inline';
}

function calc_payment() {
	f = document.forms.defaultForm;
	// check values
	if (f.loan_amount.value=="") { alert("Enter Loan Amount!"); f.loan_amount.focus(); return; }
	//if (f.yearly_taxes.value=="") { alert("Enter Yearly Taxes!"); f.yearly_taxes.focus(); return; }
	//if (f.yearly_insurance.value=="") { alert("Enter Yearly Insurance!"); f.yearly_insurance.focus(); return; }
	if (f.interest_rate.value=="") { alert("Enter Interest Rate!"); f.interest_rate.focus(); return; }
	if (f.term.selectedIndex==0) { alert("Choose a term!"); f.term.focus(); return; }

	L = f.loan_amount.value;
	T = f.yearly_taxes.value/12;
	S = f.yearly_insurance.value/12;
	i = f.interest_rate.value / 100;
	p = f.term.options[f.term.selectedIndex].value;
	
	var res = (L*(i/12)*(Math.pow(1 + (i/12),p)))/(Math.pow(1 + (i/12),p)-1)+T+S;
	document.all("spanResult").innerText = "$"+nicely_formatted(res);
	window.document.getElementById('TDresults').style.display = 'inline';
}

function calc_payment_side() {
	f = document.forms.sideForm;
	// check values
	if (f.loan_amount.value=="") { alert("Enter Loan Amount!"); f.loan_amount.focus(); return; }
	//if (f.yearly_taxes.value=="") { alert("Enter Yearly Taxes!"); f.yearly_taxes.focus(); return; }
	//if (f.yearly_insurance.value=="") { alert("Enter Yearly Insurance!"); f.yearly_insurance.focus(); return; }
	if (f.interest_rate.value=="") { alert("Enter Interest Rate!"); f.interest_rate.focus(); return; }
	if (f.term.selectedIndex==0) { alert("Choose a term!"); f.term.focus(); return; }

	L = f.loan_amount.value;
	i = f.interest_rate.value / 100;
	p = f.term.options[f.term.selectedIndex].value;
	
	var res = (L*(i/12)*(Math.pow(1 + (i/12),p)))/(Math.pow(1 + (i/12),p)-1);
	document.all("spanResultSide").innerText = "$"+nicely_formatted(res);
	window.document.getElementById('TDresultsSide').style.display = 'inline';
}

function calc_buying_vs_renting() {
	f = document.forms.calculator_buyingrenting;
	// check values
	if (f.current_rent.value=="") { alert("Enter Current Rate!"); f.current_rent.focus(); return; }
	if (f.home_price.value=="") { alert("Enter Purchase Price of Home!"); f.home_price.focus(); return; }
	if (f.down_payment.value=="") { alert("Enter Down Payment!"); f.down_payment.focus(); return; }
	if (f.loan_term.selectedIndex==0) { alert("Choose a term!"); f.loan_term.focus(); return; }
	if (f.interest_rate.value=="") { alert("Enter Interest Term!"); f.interest_rate.focus(); return; }
	if (f.years_until_sale.value=="") { alert("Enter Years Until Home Sale!"); f.years_until_sale.focus(); return; }
	if (f.year_tax.value=="") { alert("Enter Yearly Taxes!"); f.year_tax.focus(); return; }
	if (f.yearly_insurance.value=="") { alert("Enter Yearly Insurance!"); f.yearly_insurance.focus(); return; }
	if (f.appreciation_rate.value=="") { alert("Enter Appreciation Rate For Home!"); f.appreciation_rate.focus(); return; }
	if (f.increase_rate.value=="") { alert("Enter Increase Rate for Rent!"); f.increase_rate.focus(); return; }
	

	L = (f.home_price.value - f.down_payment.value);
	S = f.yearly_insurance.value/12;
	i = f.interest_rate.value / 100;
	p = f.loan_term.options[f.loan_term.selectedIndex].value;
	TA = f.year_tax.value/100;
	
	Vin = f.home_price.value;
	A = f.appreciation_rate.value/100;
	T = f.years_until_sale.value;
	R = f.current_rent.value;
	A1 = f.increase_rate.value/100;
	
	var P = (L*(i/12)*(Math.pow(1 + (i/12),p)))/(Math.pow(1 + (i/12),p)-1);
	var equity = (Vin * Math.pow((1 + A), T)) - (L - ((P - ((i/12) * L)) * Math.pow(1 + (i / 12), 12 * T)));
	var propTax = (((Vin * Math.pow((1 + A), T)) + Vin/1) / 2) * TA * T;
	var taxSave = ((P - ((P - (L * (i / 12))) * Math.pow(1 + (i / 12), (12 * T))))) * 0.28;
	var monM = (L*(i/12)*(Math.pow(1 + (i/12),p)))/(Math.pow(1 + (i/12),p)-1)+TA/1+S/1;
	TR = Math.pow((1 + A1), T);
	Rf = R * TR;
	monRT = ((R/1)+Rf);
	var monR = monRT / 2;
	var ins = S * T;
	
	var res = ((monR * T * 12) + (equity + taxSave)) - ((monM * T * 12) + propTax + ins) ;
	document.all("spanResult").innerText = "$"+nicely_formatted(res);
	 
	window.document.getElementById('TDresults').style.display = 'inline';
}

function calc_fixedadj() {
	f = document.forms.calculator_fixedadj;
	// check values
	if (f.loan_amount.value=="") { alert("Enter Loan Amount!"); f.loan_amount.focus(); return; }
	if (f.term.selectedIndex==0) { alert("Choose a term!"); f.term.focus(); return; }
	if (f.years.value=="") { alert("Enter Years You Expect To Live In The House!"); f.years.focus(); return; }
	if (f.fixed_interest_rate.value=="") { alert("Enter Fixed Interest Rate!"); f.fixed_interest_rate.focus(); return; }
	if (f.interest_rate.value=="") { alert("Enter Initial Adjustable Rate!"); f.interest_rate.focus(); return; }
	if (f.yearls_first.value=="") { alert("Enter Years Before 1st Adjustment!"); f.yearls_first.focus(); return; }
	if (f.yearls_between.value=="") { alert("Enter Years Between Subsequent Adjustments!"); f.yearls_between.focus(); return; }
	if (f.max_inc.value=="") { alert("Enter Maximum Increase Per Period!"); f.max_inc.focus(); return; }
	if (f.max_cap.value=="") { alert("Enter Maximum Total Interest Rate!"); f.max_cap.focus(); return; }
	
	L = f.loan_amount.value;
	iF = f.fixed_interest_rate.value / 100;
	iA = f.interest_rate.value / 100;
	p = f.term.options[f.term.selectedIndex].value;
	cap = f.max_cap.value / 100;
	max = f.max_inc.value / 100;
	period = f.yearls_between.value;
	
	var Fixed = (L * (iF / 12) * Math.pow(1 + (iF / 12), p)) / (Math.pow(1 + (iF / 12), p) - 1);
	var AdjL = (L * (iA / 12) * Math.pow(1 + (iA / 12), p)) / (Math.pow(1 + (iA / 12), p) - 1);
	
	var AdgH = 0;

	for(count = 1; count <= p /12; count = count + period/1)  {
		AdgH = (L * (iA / 12) * Math.pow(1 + (iA / 12), (12 * period))) / (Math.pow(1 + (iA / 12), (12 * period)) - 1);
		if((iA/1) + max > cap)
			iA = cap;
		else
			iA+= max/1;
	}
	
	document.all("spanResultFixed").innerText = "$" + nicely_formatted(Fixed);
	document.all("spanResultAdjustable").innerText = "Best: " + "$" + nicely_formatted(AdjL) + ", Worst: " + "$" + nicely_formatted(AdgH) ;
	window.document.getElementById('TDresults').style.display = 'inline';
}

function calc_1530() {
	f = document.forms.calculator_1530;
	// check values
	if (f.loan_amount.value=="") { alert("Enter Loan Amount!"); f.loan_amount.focus(); return; }
	if (f.yearly_taxes.value=="") { alert("Enter Yearly Taxes!"); f.yearly_taxes.focus(); return; }
	if (f.yearly_insurance.value=="") { alert("Enter Yearly Insurance!"); f.yearly_insurance.focus(); return; }
	if (f.interest_rate.value=="") { alert("Enter Interest Rate!"); f.interest_rate.focus(); return; }

	L = f.loan_amount.value;
	T = f.yearly_taxes.value/12;
	S = f.yearly_insurance.value/12;
	i = f.interest_rate.value / 100;
	
	var res1 = (L*(i/12)*(Math.pow(1 + (i/12),(15 * 12))))/(Math.pow(1 + (i/12),(15 * 12))-1)+T+S;
	var res2 = (L*(i/12)*(Math.pow(1 + (i/12),(30 * 12))))/(Math.pow(1 + (i/12),(30 * 12))-1)+T+S;
	var res3 = (res2 * 12 * 30) - (res1 * 12 * 15);
	
	document.all("spanResult30").innerText = "$" + nicely_formatted(res2);
	document.all("spanResult15").innerText = "$" + nicely_formatted(res1);
	document.all("spanResultSave").innerText = "$" + nicely_formatted(res3);
	window.document.getElementById('TDresults').style.display = 'inline';
}

function calc_loan() {
	f = document.forms.calculator_loan;
	// check values
	if (f.loan_amount.value=="") { alert("Enter Loan Amount!"); f.loan_amount.focus(); return; }
	if (f.yearly_taxes.value=="") { alert("Enter Yearly Taxes!"); f.yearly_taxes.focus(); return; }
	if (f.yearly_insurance.value=="") { alert("Enter Yearly Insurance!"); f.yearly_insurance.focus(); return; }
	if (f.interest_rate1.value=="") { alert("Enter Interest Rate!"); f.interest_rate1.focus(); return; }
	if (f.term1.selectedIndex==0) { alert("Choose a term!"); f.term1.focus(); return; }
	if (f.interest_rate2.value=="") { alert("Enter Interest Rate!"); f.interest_rate2.focus(); return; }
	if (f.term2.selectedIndex==0) { alert("Choose a term!"); f.term2.focus(); return; }

	L = f.loan_amount.value;
	T = f.yearly_taxes.value/12;
	S = f.yearly_insurance.value/12;
	i1 = f.interest_rate1.value / 100;
	p1 = f.term1.options[f.term1.selectedIndex].value;
	i2 = f.interest_rate2.value / 100;
	p2 = f.term2.options[f.term2.selectedIndex].value;
	
	var res1 = (L*(i1/12)*(Math.pow(1 + (i1/12),p1)))/(Math.pow(1 + (i1/12),p1)-1)+T+S;
	var res2 = (L*(i2/12)*(Math.pow(1 + (i2/12),p2)))/(Math.pow(1 + (i2/12),p2)-1)+T+S;
	var res3 = res1 * p1;
	var res4 = res1 * p2;
	
	document.all("spanResultI").innerText = "$" + nicely_formatted(res1);
	document.all("spanResultII").innerText = "$" + nicely_formatted(res2);
	document.all("spanResultTI").innerText = "$" + nicely_formatted(res3);
	document.all("spanResultTII").innerText = "$" + nicely_formatted(res4);
	window.document.getElementById('TDresults').style.display = 'inline';
}

function calc_closing() {
	f = document.forms.calculator_closing;
	// check values
	if (f.down_payment.value=="") { alert("Enter down payment!"); f.down_payment.focus(); return; }
	if (f.loan_orig.value=="") { alert("Enter origination fee!"); f.loan_orig.focus(); return; }
	if (f.app_fee.value=="") { alert("Enter appraisal fee!"); f.app_fee.focus(); return; }
	if (f.credit.value=="") { alert("Enter credit report fee!"); f.credit.focus(); return; }
	if (f.tax_service.value=="") { alert("Enter tax service fee!"); f.tax_service.focus(); return; }
	if (f.assumption.value=="") { alert("Enter assumption fee!"); f.assumption.focus(); return; }
	if (f.inspection.value=="") { alert("Enter inspection fee!"); f.inspection.focus(); return; }
	if (f.other.value=="") { alert("Enter other inspection fees!"); f.other.focus(); return; }
	if (f.title.value=="") { alert("Enter title insurance payment!"); f.title.focus(); return; }
	if (f.transfer.value=="") { alert("Enter city transfer tax!"); f.transfer.focus(); return; }
	if (f.hazard.value=="") { alert("Enter hazard insurance reserve!"); f.hazard.focus(); return; }
	if (f.points.value=="") { alert("Enter pre-paid points!"); f.points.focus(); return; }
	if (f.mortgage_insurance.value=="") { alert("Enter mortgage insurance!"); f.mortgage_insurance.focus(); return; }
	if (f.tax_impounds.value=="") { alert("Enter tax impounds!"); f.tax_impounds.focus(); return; }
	if (f.escrow.value=="") { alert("Enter escrow fee!"); f.escrow.focus(); return; }
	
	var res = f.down_payment.value/1 + f.loan_orig.value/1 + f.app_fee.value/1 + f.credit.value/1 + f.tax_service.value/1;
	res+= f.assumption.value/1 + f.inspection.value/1 + f.other.value/1 + f.title.value/1 + f.transfer.value/1;
	res+= f.hazard.value/1 + f.points.value/1 + f.mortgage_insurance.value/1 + f.tax_impounds.value/1 + f.escrow.value/1;
	
	document.all("spanResult").innerText = "$" + nicely_formatted(res);
	window.document.getElementById('TDresults').style.display = 'inline';
}

function calc_down() {
	f = document.forms.calculator_down;
	// check values
	if (f.home_value.value=="") { alert("Enter House Value!"); f.home_value.focus(); return; }
	if (f.interest_rate.value=="") { alert("Enter Interest Rate!"); f.interest_rate.focus(); return; }
	if (f.term.selectedIndex==0) { alert("Choose a term!"); f.term.focus(); return; }
	if (f.down1.value=="") { alert("Enter down payment I!"); f.down1.focus(); return; }
	if (f.down2.value=="") { alert("Enter down payment II!"); f.down2.focus(); return; }
	
	i = f.interest_rate.value / 100;
	p = f.term.options[f.term.selectedIndex].value;
	L1 = f.home_value.value - f.down1.value;
	L2 = f.home_value.value - f.down2.value;
	
	var res1 = (L1*(i/12)*(Math.pow(1 + (i/12),p)))/(Math.pow(1 + (i/12),p)-1);
	var res2 = (L2*(i/12)*(Math.pow(1 + (i/12),p)))/(Math.pow(1 + (i/12),p)-1);
	
	document.all("spanResultI").innerText = "$"+nicely_formatted(res1);
	document.all("spanResultII").innerText = "$"+nicely_formatted(res2);
	window.document.getElementById('TDresults').style.display = 'inline';
}

function calc_refinance() {
	f = document.forms.calculator_refinancing;
	// check values
	if (f.current_balance.value=="") { alert("Enter current balance!"); f.current_balance.focus(); return; }
	if (f.current_term.selectedIndex==0) { alert("Choose current term!"); f.current_term.focus(); return; }
	if (f.remaining.value=="") { alert("Enter remaining payments!"); f.remaining.focus(); return; }
	if (f.current_interest_rate.value=="") { alert("Enter current rate!"); f.current_interest_rate.focus(); return; }
	if (f.new_amount.value=="") { alert("Enter new loan amount!"); f.new_amount.focus(); return; }
	if (f.new_term.selectedIndex==0) { alert("Choose new term!"); f.new_term.focus(); return; }
	if (f.new_interest_rate.value=="") { alert("Enter new interest rate!"); f.new_interest_rate.focus(); return; }

	i1 = f.current_interest_rate.value / 100;
	i2 = f.current_interest_rate.value / 100;
	p1 = f.remaining.value;
	p2 = f.new_term.options[f.new_term.selectedIndex].value;
	L1 = f.current_balance.value;
	L2 = f.new_amount.value;
	
	var res1 = (L1*(i1/12)*(Math.pow(1 + (i1/12),p1)))/(Math.pow(1 + (i1/12),p1)-1);
	var res2 = (L2*(i2/12)*(Math.pow(1 + (i2/12),p2)))/(Math.pow(1 + (i2/12),p2)-1);
	var res3 = res1 * p1;
	var res4 = res2 * p2;
	
	document.all("spanResultL").innerText = "$"+nicely_formatted(res1);
	document.all("spanResultR").innerText = "$"+nicely_formatted(res2);
	document.all("spanResultTL").innerText = "$"+nicely_formatted(res3);
	document.all("spanResultTR").innerText = "$"+nicely_formatted(res4);
	
	window.document.getElementById('TDresults').style.display = 'inline';
}

function calc_points() {
	f = document.forms.calculator_points;
	// check values
	if (f.loan_amount.value=="") { alert("Enter Loan Amount!"); f.loan_amount.focus(); return; }
	if (f.yearly_taxes.value=="") { alert("Enter Yearly Taxes!"); f.yearly_taxes.focus(); return; }
	if (f.yearly_insurance.value=="") { alert("Enter Yearly Insurance!"); f.yearly_insurance.focus(); return; }
	if (f.interest_rate.value=="") { alert("Enter Interest Rate!"); f.interest_rate.focus(); return; }
	if (f.points.value=="") { alert("Enter points considered!"); f.points.focus(); return; }
	if (f.term.selectedIndex==0) { alert("Choose Loan term!"); f.term.focus(); return; }
	if (f.interest_rate_points.value=="") { alert("Enter Interest Rate With Points!"); f.interest_rate_points.focus(); return; }

	L = f.loan_amount.value;
	T = f.yearly_taxes.value/12;
	S = f.yearly_insurance.value/12;
	i = f.interest_rate.value / 100;
	iP = f.interest_rate_points.value / 100;
	po = f.points.value;
	t = f.term.options[f.term.selectedIndex].value;
	
	MP = (L*(i/12)*(Math.pow(1 + (i/12),t)))/(Math.pow(1 + (i/12),t)-1);
	MP1 = MP - (L * (i/12));
	TI1 = 0
	for(j = 1; j < t; j++)  {
		tmp = MP1 * Math.pow((1 + i/12), j-1);
		TI1+= MP - tmp;
	}
	
	MPP = (L*(iP/12)*(Math.pow(1 + (iP/12),t)))/(Math.pow(1 + (iP/12),t)-1);
	MPP1 = MPP - (L * (iP/12));
	TI1P = 0
	for(j = 1; j < t; j++)  {
		tmp = MPP1 * Math.pow((1 + iP/12), j-1);
		TI1P+= MPP - tmp;
	}
	
	var res = (TI1 - TI1P) - (L * po / 100);
	document.all("spanResult").innerText = "$"+nicely_formatted(res);
	window.document.getElementById('TDresults').style.display = 'inline';
}

function calc_tax() {
	f = document.forms.calculator_tax;
	// check values
	if (f.loan_amount.value=="") { alert("Enter Loan Amount!"); f.loan_amount.focus(); return; }
	if (f.interest_rate.value=="") { alert("Enter Interest Rate!"); f.interest_rate.focus(); return; }
	if (f.term.selectedIndex==0) { alert("Choose a term!"); f.term.focus(); return; }
	

	L = f.loan_amount.value;
	i = f.interest_rate.value / 100;
	t = f.term.options[f.term.selectedIndex].value;
	
	MP = (L*(i/12)*(Math.pow(1 + (i/12),t)))/(Math.pow(1 + (i/12),t)-1);
	TI1 = 0;
	
	var resT = "<Table border='1' cellspacing='1' cellpadding='2'>";
	tmp1 = 0/1;
	counter = 1/1;
	for(j = 1; j < t; j++)  {
		MP1 = (MP - (L * (i/12))) * Math.pow(1 + i/12, j-1);
		TI1+= MP - MP1;
		tmp1+= MP - MP1;
		if(j%12 == 0)  {
			resT+= "<TR><TD style='color: #004d84'>year number " + counter + "</TD><TD style='color: #004d84'>$" + nicely_formatted(tmp1* 0.28) + "</TD></TR>";
			tmp1 = 0/1;
			counter++;
		}
	}
	
	resT+= "</table>";
	var res = TI1 * 0.28;
	
	document.all("spanResult").innerText = "$"+nicely_formatted(res);
	document.all("spanResultTable").innerHTML = resT;
	window.document.getElementById('TDresults').style.display = 'inline';
}


// inserts commas etc.
function nicely_formatted(n) {
//	alert(n);
	n = n+"";
	var l = "", r = "", l1 = "";
	while (n.substr(0,1)!="." && n.length>0) {
		l += n.substr(0,1);
		n = n.substr(1,n.length-1);
	}
	if (n.length)
		r = n.substr(1,2);
	for (iaq=l.length-1; iaq>=0; iaq--) {
		l1 = l.substr(iaq,1)+l1;
		if ((l.length-iaq)%3==0 && iaq>0)
			l1 = ","+l1;
	}
	return l1+(r&&l1?".":"")+r;
}
