﻿function spread(theName) {
    if (document.all[theName].style.display == "none") {
        document.all[theName].style.display = "";

    }
    else {
        document.all[theName].style.display = "none";

    }
}

function currency(anynum) {

    //-- Returns passed number as string in $000,000.00 format.
    anynum = eval(anynum)
    workNum = Math.abs((Math.round(anynum * 100) / 100)); workStr = "" + workNum
    if (workStr.indexOf(".") == -1) { workStr += ".00" }
    dStr = workStr.substr(0, workStr.indexOf(".")); dNum = dStr - 0
    pStr = workStr.substr(workStr.indexOf("."))
    while (pStr.length < 3) { pStr += "0" }

    //--- Adds comma in 1,000 thousands place.
    if (dNum >= 1000) {
        dLen = dStr.length
        dStr = parseInt("" + (dNum / 1000)) + "," + dStr.substring(dLen - 3, dLen)
    }

    //-- Adds comma in 1,000,000 millions place.
    if (dNum >= 1000000) {
        dLen = dStr.length
        dStr = parseInt("" + (dNum / 1000000)) + "," + dStr.substring(dLen - 7, dLen)
    }
    retval = dStr + pStr
    //-- Put numbers in parentheses if negative.
    if (anynum < 0) { retval = "(" + retval + ")" }
    return "$ " + retval
}

function getData(finddata) {
    if (parseInt(finddata.principal.value) <= 0 || finddata.principal.value == "") {
        alert("請輸入本金/本金不能是負數");
        finddata.principal.focus();
        return false;
    }
    if (parseInt(finddata.intrate.value) <= 0 || finddata.intrate.value == "") {
        alert("請輸入利率/利率不能是負數");
        finddata.intrate.focus();
        return false;
    }
    if (parseInt(finddata.tenor.value) <= 0 || finddata.tenor.value == "") {
        alert("請輸入期數/期數不能是負數");
        finddata.tenor.focus();
        return false;
    }

    months = finddata.tenor.value;
    principle = finddata.principal.value;
    iRate = finddata.intrate.value / 1200;
    monthPay = ((principle * iRate) / (1 - Math.pow(1 + iRate, (months * -1))) * 100) / 100;

    // display the result
    finddata.instalment.value = currency(Math.round(monthPay));
    finddata.totalpay.value = currency(Math.round(months * monthPay));
    finddata.totalint.value = currency(Math.round(months * monthPay - principle));
}


