// begin the script to calculate the mortgage

var newWindow;
function makeNewWindow() {
    newWindow = window.open("", "", "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=auto,resizable=yes,width=375,height=225")
    }
function roundtodinero(n)
{
    dinero = n * 100;
    dinero = Math.round(dinero);
    strdinero = "" + dinero;
    len = strdinero.length;
    return strdinero.substring(0, len - 2) + "." + strdinero.substring(len - 2, len);
}
function monthly(principal, years, apr)
{
    rate = apr / 12;
    payments = years *12;
    return roundtodinero(principal * rate / (1 - (1 / Math.pow(1 + rate, payments))));
}
function monthlyamortization2(principal, years, apr)
{
    var interestpayment; var principalpayment; var i;
    var payments = years * 12;
    var monthlyinterest = apr /12;
    var monthlypayment = monthly(principal, years, apr);
    var rndprincipal = roundtodinero(principal);
    var monthpayment = monthly(principal, years, apr);
    var totinterest = 0; var totpayments= 0;
    var rndintpayment;
    var rndprncppayment;
    var rndprincipal;
    var dinero1, dinero2, dinero3, strdinero1, strdinero2, strdinero3, len1, len2, len3;

makeNewWindow();
    
newWindow.document.write(" <html><head><link rel=\"stylesheet\" href=\"main.css\"></head><body bgcolor=\"#ffffff\"> ");
newWindow.document.write(" <table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"340\"> ");
newWindow.document.write(" <tr><td colspan=\"2\" class=\"mortgagecalc_head\">Mortgage Payments</td></tr> ");
newWindow.document.write(" <tr><td width=\"170\" class=\"mortgagecalc_info\"><b>Total Loan Amount:</b></td> ");
newWindow.document.write(" <td width=\"170\" class=\"mortgagecalc_info\">$" + (rndprincipal) + "</td></tr> ");
newWindow.document.write(" <tr bgcolor=\"#e5e5e5\"><td width=\"170\" class=\"mortgagecalc_info\"><b>APR:</b></td> ");
newWindow.document.write(" <td width=\"170\" class=\"mortgagecalc_info\">" + (apr1) + "%</td></tr> ");
newWindow.document.write(" <tr><td width=\"170\" class=\"mortgagecalc_info\"><b>Mortgage Length:</b></td> ");
newWindow.document.write(" <td width=\"170\" class=\"mortgagecalc_info\"> " + years + " years</td></tr> ");
newWindow.document.write(" <tr bgcolor=\"#e5e5e5\"><td width=\"170\" class=\"mortgagecalc_info\"><b>Monthly Payment:</b></td> ");
newWindow.document.write(" <td width=\"170\" class=\"mortgagecalc_info\">$" + (monthpayment) + "</td></tr> ");

for(i = 1; i <= payments; i++)
{
    interestpayment = principal * monthlyinterest;
    dinero1 = interestpayment * 100;
    dinero1 = Math.round(dinero1);
    strdinero1 = "" + dinero1;
    len1 = strdinero1.length;
    rndintpayment= strdinero1.substring(0, len1 - 2) + "." + strdinero1.substring(len1 - 2, len1);
    principalpayment = monthlypayment - interestpayment;
    dinero2 = principalpayment * 100;
    dinero2 = Math.round(dinero2);
    strdinero2 = "" + dinero2;
    len2 = strdinero2.length;
    rndprncppayment= strdinero2.substring(0, len2 - 2) + "." + strdinero2.substring(len2 - 2, len2);
    principal -= principalpayment;
    dinero3 = principal * 100;
    dinero3 = Math.round(dinero3);
    strdinero3 = "" + dinero3;
    len3 = strdinero3.length;
    rndprincipal= strdinero3.substring(0, len3 - 2) + "." + strdinero3.substring(len3 - 2, len3);
    totinterest= totinterest + interestpayment;
    totpayments= totpayments + interestpayment + principalpayment;
}
    dinero3 = totinterest * 100;
    dinero3 = Math.round(dinero3);
    strdinero3 = "" + dinero3;
    len3 = strdinero3.length;
    totinterest= strdinero3.substring(0, len3 - 2) + "." + strdinero3.substring(len3 - 2, len3);
    dinero3 = totpayments * 100;
    dinero3 = Math.round(dinero3);
    strdinero3 = "" + dinero3;
    len3 = strdinero3.length;
    totpayments= strdinero3.substring(0, len3 - 2) + "." + strdinero3.substring(len3 - 2, len3);

newWindow.document.write(" <tr><td width=\"170\" class=\"mortgagecalc_info\"><b>Total Interest Paid:</b></td> ");
newWindow.document.write(" <td width=\"170\" class=\"mortgagecalc_info\">$" + eval(totinterest) + "</td></tr> ");
newWindow.document.write(" <tr bgcolor=\"#e5e5e5\"><td width=\"170\" class=\"mortgagecalc_info\"><b>Mortgage Total Cost:</b></td> ");
newWindow.document.write(" <td width=\"170\" class=\"mortgagecalc_info\">$" + eval(totpayments) + "</td></tr> ");
newWindow.document.write(" <tr><td class=\"mortgagecalc_info\" colspan=\"2\" align=\"center\"><a href=\"javascript:window.close();\">close this window</a></td></tr></table></body></html> ");


newWindow.document.close();
}
function payment(form)
{
    if((form.principal.value.length != 0) && (form.apr.value.length != 0) && (form.years.value.length != 0))
    {
        principal = eval(form.principal.value);
        apr1= eval(form.apr.value);
        apr= eval(form.apr.value) / 100.0;
        years = eval(form.years.value);
        if (years == 0.0)
        {
            alert("You have not specified a year value for the mortgage term, therefore there is no monthly payment value to calculate.");
        }
        else
        {
            monthlyamortization2(principal, years, apr);
        }
    }
    else
    {
        alert("Please fill in all of the areas for an accurate calculation.");
    }
}
// -->