﻿// JavaScript Mortgage Calculator

function Calculate(form, f) {
    var x, y, incm, intRate = 0;

    intRate = form.rate.value / 100 / 12;

    p = form.term.value * 12;

    // IF EXITING THE DOWN PAYMENT AMOUNT FIELD 
    // CALC THE DOWN PAYMENT PERCENTAGE & LOAN AMOUNT 
    if (f == "down") {
        form.pdown.value = (form.down.value / form.price.value) * 100;
        form.loan.value = form.price.value - form.down.value;
    }

    // IF EXITING THE PERCENTAGE DOWNPAYMENT FIELD 
    // CALC THE DOWN PAYMENT VALUE & LOAN AMOUNT   
    if (f == "pdown") {
        form.down.value = (form.price.value * form.pdown.value) / 100;
        form.loan.value = form.price.value - form.down.value;
    }

    if (f == "loan") {
        form.down.value = form.price.value - form.loan.value;
        form.pdown.value = (form.down.value / form.price.value) * 100;
    }

    form.down.value = (form.price.value * form.pdown.value) / 100;
    form.loan.value = form.price.value - form.down.value;

    x = form.loan.value * intRate * Math.pow(1 + intRate, p);

    y = Math.pow(1 + intRate, p) - 1;

    form.pni.value = parseInt(x / y);

    form.taxes.value = parseInt(form.price.value * form.taxrate.value / 1200);

    form.ins.value = parseInt(form.price.value * form.insrate.value / 1200);

    //		DO PMI CALCULATIONS
    form.pmirate.value = 0;
    if (form.pdown.value < 20) {
        form.pmirate.value = .34;
    }

    if (form.pdown.value < 15) {
        form.pmirate.value = .52;
    }

    if (form.pdown.value < 10) {
        form.pmirate.value = .78;
    }

    form.pmi.value = parseInt(form.loan.value * form.pmirate.value / 1200);

    form.piti.value = parseInt(form.pni.value) + parseInt(form.taxes.value) + parseInt(form.ins.value) + parseInt(form.pmi.value);

    incm = form.inc.value / 12;

    form.fr.value = (form.piti.value / incm) * 100;

    form.bk.value = ((parseInt(form.piti.value) + parseInt(form.debt.value)) / incm) * 100;

    //	if ((parseInt(form.fr.value) <= 28) && (parseInt(form.bk.value) <= 36)) {
    //		form.qualtext.value = "YES!, you qualify!";
    //	} else {
    //		form.qualtext.value = "You don't qualify, reduce debt or PITI.";
    //	}
}

function Instructs(form, f) {
    var text = "No Hint available for this field!";
    if (form.pni.value == "") {
        Calculate(form);
    }
    switch (f) {
        case "price": 
            {
                text = "Enter the purchase price of the home in dollars.";
                break;
            }
        case "pdown": 
            {
                text = "Enter the percent that you will put down, usually 5, 10, or 20%.";
                break;
            }
        case "loan": 
            {
                text = "This is the amount borrowed from the lender (financed amount).";
                break;
            }
        case "down": 
            {
                text = "This is the down payment amount, not including closing costs.";
                break;
            }
        case "rate": 
            {
                text = "Enter the interest rate for the loan as a percent.";
                break;
            }
        case "term": 
            {
                text = "Enter the term in years for the loan, usually 15 or 30.";
                break;
            }
        case "pni": 
            {
                text = "This is the total principal and interest portion of your monthly payment (P&I).";
                break;
            }
        case "taxrate": 
            {
                text = "This is the total percentage per year of state, county, and city taxes, as a percentage of the purchase price.";
                break;
            }
        case "insrate": 
            {
                text = "This is Homeowner's Insurance rate per year, as a percentage of the loan amount.";
                break;
            }
        case "ins": 
            {
                text = "This is the total amount of Homeowner's Insurance per month.";
                break;
            }
        case "pmirate": 
            {
                text = "Private Mortgage Insurance is required on loans over 80%. It is a percentage per year of the loan amount.";
                break;
            }
        case "pmi": 
            {
                text = "This is the Private Mortgage Insurance per month.";
                break;
            }
        case "piti": 
            {
                text = "This is the total monthly payment, PITI, Principal + Interest + Taxes + Insurance";
                break;
            }
        case "inc": 
            {
                text = "Enter your total combined annual gross income.";
                break;
            }
        case "taxes": 
            {
                text = "This is the total taxes per month.";
                break;
            }
        case "debt": 
            {
                text = "Enter your total monthly debt, car loans, credit cards, student loans, etc.";
                break;
            }
        case "fr": 
            {
                text = "This is the ratio of the PITI to your monthly income. A ratio of 28% or less is usually requird by most lenders";
                break;
            }
        case "bk": 
            {
                text = "The ratio of the (PITI+debt) to your monthly income. A ratio of 36% or less is usually required by most lenders";
                break;
            }
        default:
            {
                text = "This field displays information about the field that currently has the cursor in it.";
                break;
            }
    }
    form.hint.value = text;
}

