var HTMLOutput
var HTMLAmortization
var paymentInterest;
var paymentPrincipal;
var currentBalance=0;

function calcMonthlyPayment(P, n, i)
{
var m = P*i*Math.pow((1 + i), n)/(Math.pow((1 + i), n) - 1);
return Math.round(m *100)/100;   // round up
}

function calcMortgage()
{
	if (checkRequired("calc"))
	{
var P=document.getElementById("principal_loan").value;
var n=document.getElementById("amortization_length").value*12
var i=(document.getElementById("annual_interest").value/100)/360*30
i=(Math.round(i*1000000000)/1000000000);
var monthlyPayment=calcMonthlyPayment(P, n, i);

	HTMLOutput="<table>";
	HTMLOutput+="<tr><td><b>Your monthly payments will be:</b></td><td>$" + monthlyPayment + "</td></tr>";
	HTMLOutput+="</table>";
	HTMLOutput+="<br><font size=1>To view the amortization table click on 'Show Table'</font><Br><br>";
	HTMLOutput+="<input type=button value='Show Table' onClick=showAmortizationTable();>";	
	
	document.getElementById("result").innerHTML=HTMLOutput;
	
	createAmortizationTable(P, n, i, monthlyPayment)

}
}

function showAmortizationTable()
{
	msgWindow=window.open("","msgWindow","toolbar=no,status=no,menubar=yes,scrollbars=yes,width=400,height=500,left=20, top=20");
	msgWindow.document.open();
	msgWindow.document.writeln(HTMLAmortization);
	msgWindow.document.close();
}

function createAmortizationTable(P, n, i, monthlyPayment)
{
var InterestPaid=0;

currentBalance=P;
HTMLAmortization="<center><input type='button' onclick='javascript:window.close();' value='Close Window'><br><h3>Mortgage Amortization Table</h3><table border=0>"
HTMLAmortization+="<tr><td><b>Principal Loan Balance: </td><td>$" + addComma(P) + "</td></tr>";
HTMLAmortization+="<tr><td><b>Annual Interest Rate: </td><td>" + document.getElementById("annual_interest").value + " %" + "</td></tr>";
HTMLAmortization+="<tr><td><b>Amortization Length: </td><td>" + n + " Years" + "</td></tr>";
HTMLAmortization+="</table><br><br>";
HTMLAmortization+="<table cellpadding=2 border=1 style='border-collapse:collapse'><tr><td><b>Payment #</b></td>"

HTMLAmortization+="<td align=center><b>Principal</b></td>"
HTMLAmortization+="<td align=center><b>Interest</b></td>"
HTMLAmortization+="<td align=center><b>Balance</b></td></tr>"

var paymentNumber=1;
for (paymentNumber=1; paymentNumber<=n; paymentNumber++)
{

	//calculate interest payment

	paymentInterest=[currentBalance * i]
	paymentInterest=Math.round(paymentInterest * 100)/100;
	if (paymentNumber<n)
	{
	paymentPrincipal=[monthlyPayment-paymentInterest];
	currentBalance-=paymentPrincipal;
	currentBalance=Math.round(currentBalance * 100) / 100;
	}
	else
	{
	paymentPrincipal=currentBalance;
	currentBalance-=paymentPrincipal;	
	}


	HTMLAmortization+="<tr><td align=center>" + paymentNumber + "</td>"
	HTMLAmortization+="<td align=center>$" + addComma(Math.round(paymentPrincipal * 100)/100) + "</td>"
	HTMLAmortization+="<td align=center>$" + addComma(paymentInterest) + "</td>"
	HTMLAmortization+="<td align=center>$" + addComma(currentBalance) + "</td></tr>"
}

HTMLAmortization+="</table>";
HTMLAmortization+="</table></center><br>These results are approximate and are intended for your information"+
" only; they are not an endorsement or offering. The accuracy of the results is not guaranteed by"+
" this bank and is only meant to be an approximate guideline."

//document.getElementById("result").innerHTML=HTMLOutput;

}