// Star-Motoring Loan Calculator - Copyright 2010 TheStar Publication
//<!-- loan calculator -->
function calcRound(num) {

    result = Math.floor(num) + ".";

    var cents = 100 * (num - Math.floor(num)) + 0.5;

    result += Math.floor(cents / 10);
    result += Math.floor(cents % 10);

    return (result)

}

function cal() {
    var myindex
    var Month
    var tDownpayment
    var tIntrate
    var tcprice

    myindex = document.getElementById("periods").selectedIndex;
    Month = document.getElementById("periods").options[myindex].value;
    tDownpayment = document.getElementById("downpayment").value;
    tIntrate = document.getElementById("intrate").value;
    tcprice = document.getElementById("cprice").value;

    year = Month / 12;
    loanprice = tcprice - tDownpayment;
    cInstallment = (((loanprice * (tIntrate / 100)) * year) + loanprice) / Month;
    document.getElementById("installment").value = calcRound(cInstallment);

    if (tDownpayment == "") {
        document.getElementById("downpayment").focus();
        document.getElementById("downpayment").select();
    }

    check_dpayment()
}

function check_dpayment() {
    if (document.getElementById("downpayment").value != "") {
        margin = (document.getElementById("cprice").value - document.getElementById("downpayment").value) / document.getElementById("cprice").value
        if (margin > 0.9) {
            alert("Please enter a valid downpayment amount. \nThe minimum downpayment should be 10% of the car purchase price.")
            document.getElementById("downpayment").focus();
            document.getElementById("downpayment").select();
        }
    }
}

//<!-- end loan calculator -->
