<!-- hide from non-JavaScript browsers
function roundToPennies(n)
{
    pennies = n * 100;
    pennies = Math.round(pennies);
    strPennies = new String(pennies);
    len = strPennies.length;
    return strPennies.substring(0,len-2)+ "." + strPennies.substring(len-2, len);
}
function Monthly(principal, years, apr)
{
    rate = apr / 12;
    payments = years * 12;
    return roundToPennies(principal * rate / (1-(1 / Math.pow(1 + rate, payments))));
}
function MonthlyAmortization(principal, years, apr)
{
    payments = years * 12;
    monthlyInterest = apr / 12;
    monthlyPayment = Monthly(principal, years, apr);
    document.write("<CENTER>");
    document.write("<H1>Loan Amortization Table</H1>");
    document.write("<HR>");
    document.write("<TABLE BORDER=1 CELLPADDING=4 cellspacing=0>");
    document.write("<TR>");
    
    document.write("<TH COLSPAN=4>");
    document.write("$" + roundToPennies(principal)+"<BR>");
    document.write(" at Interest Rate: " + roundToPennies(apr*100) + "% <BR>");
    document.write(" over " + years + " years.<BR>");
    document.write("Monthly payment: $" + Monthly(principal, years, apr));
    document.write("</TH>");
    
    document.write("</TR>");
    document.write("<TR>");
    document.write("<TD>"+"Month"+ "</TD>");
    document.write("<TD>"+"Interest"+ "</TD>");
    document.write("<TD>"+"Principal"+ "</TD>");
    document.write("<TD>"+"Balance"+ "</TD>");
    document.write("</TR>");

    for(i = 1; i <= payments; i++)
    {
        document.write("<TR>");
        document.write("<TD>" + i + "</TD>");
        interestPayment = principal * monthlyInterest;
        document.write("<TD>$" + roundToPennies(interestPayment) +"</TD>");
        principalPayment = monthlyPayment - interestPayment;
        document.write("<TD>$" + roundToPennies(principalPayment) +"</TD>");
        principal -= principalPayment;
        document.write("<TD>$" + roundToPennies(principal) + "</TD>");
        document.write("</TD>");
    }
    document.write("</TABLE>");
    document.write("</CENTER>");
}
function compute(form)
{
    if((form.principal.value.length != 0) &&
        (form.apr.value.length != 0) &&
        (form.years.value.length != 0))
    {
        principal = eval(form.principal.value);
        apr = eval(form.apr.value) / 100.0;
        years = eval(form.years.value);
        if(years == 0.0)
        {
            alert(
            "You have no monthly payment, since the number of years is zero.");
        }
        else
        {
            MonthlyAmortization(principal, years, apr);
        }
    }
    else
    {
        alert("You must fill in all the fields.");
    }
}
