/*
* Monthly.js is a JavaScript program for computing monthly payments
* using the formula: M = P * ( J / (1 - (1 + J) ^ -N))
*  M is the monthly payment, P is the principle amount, 
*     J is monthly interest in decimal form = I / (12 x 100), and 
*     N is number of months over which loan is amortized
*/


function computePayments()
{
	var mPayment = 0.0;
	var P = 0.0;
	var J = 0.0;
	var N = 0.0;
	var iN = 0.0;
	
	P = document.monthlypmt.principle.value;
	J = document.monthlypmt.rate.value / 1200;
	N = document.monthlypmt.termmonths.value;
	iN = Math.pow( 1 + J, 0 - N);
	
	mPayment = P * (J / (1 - iN));
	mPayment = Math.round(100 * mPayment)/100;
	document.monthlypmt.mpay.value=mPayment;
}


function buildCalculator()
{
    document.write('<center>\n');
		document.write('<p><b><i>Enter principle amount being borrowed without commas, the interest rate percentage, ');
		document.write('and the number of months to repay then click the Compute button to see ');
		document.write('what the monthly payment would be.</i></b><p>');
    document.write('  <form name=monthlypmt action="" method=post>\n');
		document.write('<table align="center" summary="Monthly Payment Calculator" border="0">');
		document.write('<tr><th colspan="2">Monthly Payment Calculator</th></tr>\n');
		document.write('<tr><td align="right">Principle Amount:</td><td><b>$</b>');
		document.write('<input type=text align=right name=principle value=""/></td></tr>\n');
		document.write('<tr><td align="right">Interest Rate:</td><td>');
		document.write('<input type=text align=right name=rate value=""/><b>%</b></td></tr>\n');
		document.write('<tr><td align="right">Number of Months:</td><td>');
		document.write('<input type=text align=right name=termmonths value=""/></td></tr>\n');
		document.write('<tr><td align="right">');
		document.write('<input type=button value="Compute Payments" alt="Compute"'); 		
		document.write('onClick="computePayments()"/>Monthly Payment:</td>\n');
		document.write('<td><input type=text readonly="true" name=mpay value=""/></td></tr>\n');
		document.write('<tr><td colspan="2">By <a href="http://stratiotes.romanhords.com/"'     
			+ 'alt="Author homepage" title="Author homepage">'
     + "Stratiotes' Place</a>");
		document.write('</table></form></center>\n');
}
