﻿//--------------------------------------------------------
// Common Functions
//--------------------------------------------------------
function getServerControl(strControlName) {
    var control;
    for (var i = 0; i < document.forms[0].elements.length; i++) {

        if (document.forms[0].elements[i].name.indexOf('$') == -1) {
            if (document.forms[0].elements[i].name == strControlName)
                control = document.forms[0].elements[i];
        }
        else {
            if (StringEndsWith(document.forms[0].elements[i].name, "$" + strControlName))
                control = document.forms[0].elements[i];
        }
    }
    return control;
}
function StringEndsWith(strText, strEndsWith) {
    return strText.substring(strText.length - strEndsWith.length) == strEndsWith;
}

function checkZeroInFront(field, alertFlag) {
    if (field.value.substring(0, 1) == '0') {
        if (alertFlag) {
            alert("First Character must not be 0");
            field.focus();
        }
        return false;
    }
    return true;
}
function getFrontOfDot(aString) {
    var blnIsNegative = false;
    var fromStr, toStr;
    fromStr = aString;
    toStr = "";

    if (fromStr.substr(0, 1) == "-") {
        blnIsNegative = true;
        fromStr = fromStr.substr(1, fromStr.length - 1);
    }

    while (fromStr != "") {
        if (fromStr.length <= 3) {
            toStr = fromStr + toStr;
            fromStr = "";
        }
        else {
            if (fromStr.substr(0, 1) != "-") toStr = "," + fromStr.substr((fromStr.length - 3), 3) + toStr;
            else toStr = fromStr.substr((fromStr.length - 3), 3) + toStr
            fromStr = fromStr.substr(0, (fromStr.length - 3))
        }
    }
    if (blnIsNegative)
        return '-' + (toStr);
    else
        return (toStr);
}
function getBackOfDot(aString) {
    if (aString.length == 1) {
        return (aString + "0");
    }
    else if (aString.length == 2) {
        return (aString);
    }
    else if (aString.length == 0) {
        return ("00")
    }
    else {
        return (aString)
    }
}
function getFormattedNumber(aNum) {
    var tempNum, tempStr, re, sindex, frontStr, backStr;
    // Round to two decimal places
    tempNum = roundDeci(aNum, 2);
    // Search for decimal point & split the string into two parts
    tempStr = tempNum.toString();
    re = /\./;
    sindex = tempStr.search(re);
    if (sindex != -1) {
        frontStr = tempStr.slice(0, sindex);
        backStr = tempStr.slice(sindex + 1);
    }
    else {
        frontStr = tempStr;
        backStr = "0";
    }
    // Handle where frontStr is blank. (Netscape sometime gives .xx e.g. "0.33" will show ".33")
    if (frontStr == "") frontStr = "0";
    return (getFrontOfDot(frontStr) + "." + getBackOfDot(backStr));
}
function remComma(aString) {
    re = new RegExp(",", 'g');
    return aString.replace(re, "");
}
function getNumber(aString) {
    var tempStr, tempStr1;
    tempStr = remComma(aString); // remove commas if there are any.
    if (parseFloat(tempStr) == tempStr) return parseFloat(tempStr)
    else return NaN; // return the number
}
function validNumber(aTextBox, alertFlag) {
    var tempStr, tempNum;
    tempStr = aTextBox.value;
    tempNum = getNumber(tempStr);
    if (isNaN(tempNum) || !isFinite(tempNum)) {
        if (alertFlag) {
            alert(aTextBox.value + " is not a valid number.");
            aTextBox.focus();
        }
        return false;
    }
    else return true;
}
function checkNegative(field, fieldn, alertFlag) {
    if (remComma(field.value) < 0) {
        if (alertFlag) {
            alert(fieldn + " must be positive.");
            field.focus();
        }
        return false;
    }
    return true;
}
function checkRange(field, fieldn, range1, range2, alertFlag) {
    if (field.value < range1) {
        if (alertFlag) {
            alert(fieldn + " must be more than " + range1)
            field.focus()
        }
        return false
    }
    if (field.value > range2) {
        if (alertFlag) {
            alert(fieldn + " must be less than " + range2)
            field.focus()
        }
        return false
    }
    return true
}
function checkRange1(field, fieldn, range1, range2, alertFlag, alertMsg) {
    if (field.value < range1) {
        if (alertFlag) {
            alert(alertMsg)
            field.focus()
        }
        return false
    }
    if (field.value > range2) {
        if (alertFlag) {
            alert(alertMsg)
            field.focus()
        }
        return false
    }
    return true
}
function roundDeci(aNum, deci) {
    // Rounds to 'deci' decimal places
    var tempNum = 0;
    if (!isFinite(aNum)) return (0);
    if (!isFinite(tempNum)) return (0);
    tempNum = aNum;
    for (var i = deci + 1; i >= deci; i--) {
        tempNum = tempNum * Math.pow(10, i);
        if (isFinite(tempNum) == false) {
            alert("Number is too big or too small for rounding.");
            return (0);
        }
        tempNum = Math.round(tempNum);
        tempNum = tempNum / Math.pow(10, i);
        //Debug: alert("i="+i+"; tempNum="+tempNum+";");
    }
    return (tempNum);
}

//--------------------------------------------------------
// Calculate Val (Popup)
//--------------------------------------------------------
function calctotal() {
    if (!validNumber(getServerControl('cv1'), false)) return;
    if (!checkNegative(getServerControl('cv1'), "", false)) return;
    if (!validNumber(getServerControl('cv2'), false)) return;
    if (!checkNegative(getServerControl('cv2'), "", false)) return;
    if (!validNumber(getServerControl('cv3'), false)) return;
    if (!checkNegative(getServerControl('cv3'), "", false)) return;
    if (!validNumber(getServerControl('cv4'), false)) return;
    if (!checkNegative(getServerControl('cv4'), "", false)) return;
    if (!validNumber(getServerControl('cv5'), false)) return;
    if (!checkNegative(getServerControl('cv5'), "", false)) return;
    if (!validNumber(getServerControl('cv6'), false)) return;
    if (!checkNegative(getServerControl('cv6'), "", false)) return;
    cv1 = remComma(getServerControl('cv1').value);
    cv2 = remComma(getServerControl('cv2').value);
    cv3 = remComma(getServerControl('cv3').value);
    cv4 = remComma(getServerControl('cv4').value);
    cv5 = remComma(getServerControl('cv5').value);
    cv6 = remComma(getServerControl('cv6').value);
    gctotal = parseFloat(cv1) + parseFloat(cv2) + parseFloat(cv3) + parseFloat(cv4) + parseFloat(cv5) + parseFloat(cv6);
    getServerControl('curr_total').value = getFormattedNumber(gctotal);
}
function calftotal() {
    if (!validNumber(getServerControl('fv1'), false)) return;
    if (!checkNegative(getServerControl('fv1'), "", false)) return;
    if (!validNumber(getServerControl('fv2'), false)) return;
    if (!checkNegative(getServerControl('fv2'), "", false)) return;
    if (!validNumber(getServerControl('fv3'), false)) return;
    if (!checkNegative(getServerControl('fv3'), "", false)) return;
    if (!validNumber(getServerControl('fv4'), false)) return;
    if (!checkNegative(getServerControl('fv4'), "", false)) return;
    if (!validNumber(getServerControl('fv5'), false)) return;
    if (!checkNegative(getServerControl('fv5'), "", false)) return;
    if (!validNumber(getServerControl('fv6'), false)) return;
    if (!checkNegative(getServerControl('fv6'), "", false)) return;
    fv1 = remComma(getServerControl('fv1').value);
    fv2 = remComma(getServerControl('fv2').value);
    fv3 = remComma(getServerControl('fv3').value);
    fv4 = remComma(getServerControl('fv4').value);
    fv5 = remComma(getServerControl('fv5').value);
    fv6 = remComma(getServerControl('fv6').value);
    gftotal = parseFloat(fv1) + parseFloat(fv2) + parseFloat(fv3) + parseFloat(fv4) + parseFloat(fv5) + parseFloat(fv6);
    getServerControl('future_total').value = getFormattedNumber(gftotal);
}
function xvalue1() {
    if (!checkRange(getServerControl('rr1'), "", 0, 100, false)) return;
    if (!checkNegative(getServerControl('cv1'), "", false)) return;
    colYears = remComma(getServerControl('colYears').value);
    cv1 = remComma(getServerControl('cv1').value);
    rr1 = remComma(getServerControl('rr1').value);
    xanswer1 = parseFloat(cv1) * Math.pow(1 + parseFloat(rr1) / 100, colYears);
    getServerControl('fv1').value = getFormattedNumber(xanswer1);
}
function xvalue2() {
    if (!checkRange(getServerControl('rr2'), "", 0, 100, false)) return;
    if (!checkNegative(getServerControl('cv2'), "", false)) return;
    colYears = remComma(getServerControl('colYears').value);
    cv2 = remComma(getServerControl('cv2').value);
    rr2 = remComma(getServerControl('rr2').value);
    xanswer2 = parseFloat(cv2) * Math.pow(1 + parseFloat(rr2) / 100, colYears);
    getServerControl('fv2').value = getFormattedNumber(xanswer2);
}
function xvalue3() {
    if (!checkRange(getServerControl('rr3'), "", 0, 100, false)) return;
    if (!checkNegative(getServerControl('cv3'), "", false)) return;
    colYears = remComma(getServerControl('colYears').value);
    cv3 = remComma(getServerControl('cv3').value);
    rr3 = remComma(getServerControl('rr3').value);
    xanswer3 = parseFloat(cv3) * Math.pow(1 + parseFloat(rr3) / 100, colYears);
    getServerControl('fv3').value = getFormattedNumber(xanswer3);
}
function xvalue4() {
    if (!checkRange(getServerControl('rr4'), "", 0, 100, false)) return;
    if (!checkNegative(getServerControl('cv4'), "", false)) return;
    colYears = remComma(getServerControl('colYears').value);
    cv4 = remComma(getServerControl('cv4').value);
    rr4 = remComma(getServerControl('rr4').value);
    xanswer4 = parseFloat(cv4) * Math.pow(1 + parseFloat(rr4) / 100, colYears);
    getServerControl('fv4').value = getFormattedNumber(xanswer4);
}
function xvalue5() {
    if (!checkRange(getServerControl('rr5'), "", 0, 100, false)) return;
    if (!checkNegative(getServerControl('cv5'), "", false)) return;
    colYears = remComma(getServerControl('colYears').value);
    cv5 = remComma(getServerControl('cv5').value);
    rr5 = remComma(getServerControl('rr5').value);
    xanswer5 = parseFloat(cv5) * Math.pow(1 + parseFloat(rr5) / 100, colYears);
    getServerControl('fv5').value = getFormattedNumber(xanswer5);
}
function xvalue6() {
    if (!checkNegative(getServerControl('cv6'), "", false)) return;
    if (!checkRange(getServerControl('rr6'), "", 0, 100, false)) return;
    colYears = remComma(getServerControl('colYears').value);
    cv6 = remComma(getServerControl('cv6').value);
    rr6 = remComma(getServerControl('rr6').value);
    xanswer6 = parseFloat(cv6) * Math.pow(1 + parseFloat(rr6) / 100, colYears);
    getServerControl('fv6').value = getFormattedNumber(xanswer6);
}


//--------------------------------------------------------
// Personal?
//--------------------------------------------------------
function personaladd() {
    if (!validNumber(getServerControl('lumsumInvest'), false)) return;
    if (!checkNegative(getServerControl('lumsumInvest'), "", false)) return;
    if (!validNumber(getServerControl('levelInvest'), false)) return;
    if (!checkNegative(getServerControl('levelInvest'), "", false)) return;
    var investRate = parseFloat(getServerControl('investRate').value);
    var colYears = parseFloat(getServerControl('colYears').value);
    lumsumInvest = remComma(getServerControl('lumsumInvest').value);
    futurebalance = remComma(getServerControl('futurebalance').value);
    FVlumsum = parseFloat(lumsumInvest) * Math.pow((1 + parseFloat(investRate)), parseFloat(colYears));
    getServerControl('FVlumsum').value = getFormattedNumber(FVlumsum);
    FVmonInvest = parseFloat(remComma(getServerControl('levelInvest').value)) * (Math.pow(1 + investRate / 12, colYears * 12) - 1) / (investRate / 12) * (1 + investRate / 12);
    getServerControl('FVmonInvest').value = getFormattedNumber(FVmonInvest);
    FVlumsum = remComma(getServerControl('FVlumsum').value);
    FVmonInvest = remComma(getServerControl('FVmonInvest').value);
    total = parseFloat(FVlumsum) + parseFloat(FVmonInvest);
    getServerControl('total').value = getFormattedNumber(total);
    futurebalance = remComma(getServerControl('futurebalance').value);
    total = remComma(getServerControl('total').value);
    balFutureAmt = parseFloat(futurebalance) - parseFloat(total);
    getServerControl('balFutureAmt').value = getFormattedNumber(balFutureAmt);
    balFutureAmt = remComma(getServerControl('balFutureAmt').value);
    lumsumInvest = remComma(getServerControl('lumsumInvest').value);
    total = remComma(getServerControl('total').value);
    shortfallLumsum = balFutureAmt / Math.pow(1 + investRate, colYears);
    getServerControl('shortfallLumsum').value = getFormattedNumber(shortfallLumsum);
    balFutureAmt = remComma(getServerControl('balFutureAmt').value);
    shortfallLevelInvest = parseFloat(balFutureAmt) / ((Math.pow(1 + investRate / 12, colYears * 12) - 1) / (investRate / 12) * (1 + investRate / 12));
    getServerControl('shortfallLevelInvest').value = getFormattedNumber(shortfallLevelInvest);
}

//--------------------------------------------------------
// EPF
//--------------------------------------------------------
function autocal() {

    if (!validNumber(getServerControl("EPFbalance"), false)) return;
    if (!checkNegative(getServerControl("EPFbalance"), "", false)) return;
    if (!validNumber(getServerControl("EPFann"), false)) return;
    if (!checkNegative(getServerControl("EPFann"), "", false)) return;
    if (!validNumber(getServerControl("EPFannReturn"), false)) return;
    if (!checkNegative(getServerControl("EPFannReturn"), "", false)) return;
    if (!validNumber(getServerControl("salaryRate"), false)) return;
    if (!checkNegative(getServerControl("salaryRate"), "", false)) return;
    if (!validNumber(getServerControl("colYears"), false)) return;
    if (!checkNegative(getServerControl("colYears"), "", false)) return;
    var EPFannReturn = parseFloat(getServerControl("EPFannReturn").value) / 100;
    var salaryRate = parseFloat(getServerControl("salaryRate").value) / 100;
    var colYears = parseFloat(getServerControl("colYears").value);
    var EPFbalance = parseFloat(remComma(getServerControl("EPFbalance").value)) * 0.3;
    var EPFann = parseFloat(remComma(getServerControl("EPFann").value)) * 0.3;
    var ret = (EPFbalance + EPFann) * EPFannReturn;
    for (i = 0; i < colYears; i++) {
        var bal = EPFbalance + EPFann + ret;
        EPFbalance = parseFloat(bal);
        EPFann = EPFann * (1 + salaryRate);
        ret = (EPFbalance + EPFann) * EPFannReturn;
    }
    getServerControl("futureValue").value = getFormattedNumber(bal);
}

//--------------------------------------------------------
// Education
//--------------------------------------------------------
function totFutureVal() {

    var colYears = getServerControl("colYears_txt").value;
    var investRate = getServerControl("investRate_txt").value;

    if (!validNumber(getServerControl('FutureEdufund'), false)) return;
    if (!validNumber(getServerControl('futureEPFval'), false)) return;
    if (!validNumber(getServerControl('fval'), false)) return;
    if (!checkNegative(getServerControl('FutureEdufund'), "", false)) return;
    if (!checkNegative(getServerControl('fval'), "", false)) return;
    FutureEdufund = (remComma(getServerControl('FutureEdufund').value));
    futureEPFval = (remComma(getServerControl('futureEPFval').value));
    fval = (remComma(getServerControl('fval').value));
    futurebalance = (parseFloat(FutureEdufund) - parseFloat(futureEPFval)) - parseFloat(fval);
    getServerControl('futurebalance').value = getFormattedNumber(futurebalance);
    currLumsum = futurebalance / Math.pow((1 + parseFloat(investRate)), parseFloat(colYears));
    getServerControl('currLumsum').value = getFormattedNumber(currLumsum);
    currMonthInvest = futurebalance / (((((Math.pow((1 + (investRate / 12)), (colYears * 12))) - 1) / (investRate / 12))) * (1 + (investRate / 12)))
    getServerControl('currMonthInvest').value = getFormattedNumber(currMonthInvest);
}

//--------------------------------------------------------
// Retirement
//--------------------------------------------------------
function totRetireVal() {
    futureAmtReq = remComma(getServerControl('futureAmtReq').value);
    futureEPF = remComma(getServerControl('futureEPF').value);
    rval = remComma(getServerControl('rval').value);
    invRateUR = parseFloat(getServerControl('invRateUR_txt').value);
    YrsToRet = parseFloat(getServerControl('YrsToRet_txt').value);
    futureAmtBalance = parseFloat(futureAmtReq) - parseFloat(futureEPF) - parseFloat(rval);
    getServerControl('futureAmtBalance').value = getFormattedNumber(futureAmtBalance);
    lumpSum1 = futureAmtBalance / Math.pow(1 + invRateUR, YrsToRet);
    getServerControl('lumpSum1').value = getFormattedNumber(lumpSum1);
    currLevelinvest = futureAmtBalance / (((((Math.pow(1 + invRateUR / 12, YrsToRet * 12)) - 1) / (invRateUR / 12))) * (1 + invRateUR / 12));
    getServerControl('currLevelinvest').value = getFormattedNumber(currLevelinvest);

}
function personalretireadd() {
    if (!validNumber(getServerControl('lumsumInvest'), false)) return;
    if (!checkNegative(getServerControl('lumsumInvest'), "", false)) return;
    if (!validNumber(getServerControl('levelInvest'), false)) return;
    if (!checkNegative(getServerControl('levelInvest'), "", false)) return;
    var invRateUR = parseFloat(getServerControl('invRateUR').value);
    var retAge = getServerControl('retAge').value;
    lumsumInvest = remComma(getServerControl('lumsumInvest').value);
    futurebalance = remComma(getServerControl('futurebalance').value);
    FVlumsum = parseFloat(lumsumInvest) * Math.pow(1 + parseFloat(invRateUR), parseFloat(retAge));
    getServerControl('FVlumsum').value = getFormattedNumber(FVlumsum);
    FVmonInvest = parseFloat(getServerControl('levelInvest').value) * ((((Math.pow((1 + (invRateUR / 12)), (retAge * 12))) - 1) / (invRateUR / 12) * (1 + (invRateUR / 12))));
    getServerControl('FVmonInvest').value = getFormattedNumber(FVmonInvest);
    FVlumsum = remComma(getServerControl('FVlumsum').value);
    FVmonInvest = remComma(getServerControl('FVmonInvest').value);
    total = parseFloat(FVlumsum) + parseFloat(FVmonInvest);
    getServerControl('total').value = getFormattedNumber(total);
    futurebalance = remComma(getServerControl('futurebalance').value);
    total = remComma(getServerControl('total').value);
    balFutureAmt = parseFloat(futurebalance) - parseFloat(total);
    getServerControl('balFutureAmt').value = getFormattedNumber(balFutureAmt);
    balFutureAmt = remComma(getServerControl('balFutureAmt').value);
    lumsumInvest = remComma(getServerControl('lumsumInvest').value);
    total = remComma(getServerControl('total').value);
    shortfallLumsum = balFutureAmt / Math.pow(1 + invRateUR, retAge);
    getServerControl('shortfallLumsum').value = getFormattedNumber(shortfallLumsum);
    balFutureAmt = remComma(getServerControl('balFutureAmt').value);
    shortfallLevelInvest = parseFloat(balFutureAmt) / ((((Math.pow((1 + (invRateUR / 12)), (retAge * 12))) - 1) / (invRateUR / 12) * (1 + (invRateUR / 12))));
    getServerControl('shortfallLevelInvest').value = getFormattedNumber(shortfallLevelInvest);
}

//--------------------------------------------------------
// Home
//--------------------------------------------------------
function totHomeVal() {
    if (!validNumber(getServerControl("futureEPFval"), false)) return;
    if (!checkNegative(getServerControl("futureEPFval"), "", false)) return;
    if (!validNumber(getServerControl("hval"), false)) return;
    if (!checkNegative(getServerControl("hval"), "", false)) return;
    total = remComma(getServerControl("total").value);
    hval = remComma(getServerControl("hval").value);
    futureEPFval = remComma(getServerControl("futureEPFval").value);
    Homeyrs = parseFloat(getServerControl("Homeyrs_txt").value);
    investRate = parseFloat(getServerControl("investRate_txt").value);
    futurebalance = parseFloat(total) - parseFloat(futureEPFval) - parseFloat(hval);
    if (futurebalance < 0)
        getServerControl("button1").disabled = true
    else
        getServerControl("button1").disabled = false
    getServerControl("futurebalance").value = getFormattedNumber(futurebalance);
    currLumsum = futurebalance / Math.pow((1 + investRate), (Homeyrs));
    getServerControl("currLumsum").value = getFormattedNumber(currLumsum);
    currMonthInvest = futurebalance / ((Math.pow(1 + investRate / 12, Homeyrs * 12) - 1) / (investRate / 12) * (1 + investRate / 12))
    getServerControl("currMonthInvest").value = getFormattedNumber(currMonthInvest);
}
function autohomecal() {
    if (!validNumber(getServerControl('EPFbalance'), false)) return;
    if (!checkNegative(getServerControl('EPFbalance'), "", false)) return;
    if (!validNumber(getServerControl('EPFann'), false)) return;
    if (!checkNegative(getServerControl('EPFann'), "", false)) return;
    if (!validNumber(getServerControl('EPFannReturn'), false)) return;
    if (!checkNegative(getServerControl('EPFannReturn'), "", false)) return;
    if (!validNumber(getServerControl('salaryRate'), false)) return;
    if (!checkNegative(getServerControl('salaryRate'), "", false)) return;
    var EPFannReturn = parseFloat(getServerControl('EPFannReturn').value) / 100;
    var salaryRate = parseFloat(getServerControl('salaryRate').value) / 100;
    var Homeyrs = getServerControl('Homeyrs').value;
    var EPFbalance = parseFloat(remComma(getServerControl('EPFbalance').value)) * 0.3;
    var EPFann = parseFloat(remComma(getServerControl('EPFann').value)) * 0.3;
    var ret = (parseFloat(EPFbalance) + parseFloat(EPFann)) * EPFannReturn;
    for (i = 0; i < Homeyrs; i++) {
        var bal = parseFloat(EPFbalance) + parseFloat(EPFann) + parseFloat(ret);
        var bal1 = parseFloat(bal);
        EPFbalance = parseFloat(bal1);
        EPFann = parseFloat(EPFann) * (1 + parseFloat(salaryRate));
        ret = (parseFloat(EPFbalance) + parseFloat(EPFann)) * parseFloat(EPFannReturn);
    }
    getServerControl('futureValue').value = getFormattedNumber(bal);
}
function personalhomeadd() {
    if (!validNumber(getServerControl('lumsumInvest'), false)) return;
    if (!checkNegative(getServerControl('lumsumInvest'), "", false)) return;
    if (!validNumber(getServerControl('levelInvest'), false)) return;
    if (!checkNegative(getServerControl('levelInvest'), "", false)) return;
    var investRate = parseFloat(getServerControl('investRate').value);
    var Homeyrs = parseFloat(getServerControl('Homeyrs').value);
    lumsumInvest = remComma(getServerControl('lumsumInvest').value);
    futurebalance = remComma(getServerControl('futurebalance').value);
    FVlumsum = parseFloat(lumsumInvest) * Math.pow(1 + investRate, Homeyrs);
    getServerControl('FVlumsum').value = getFormattedNumber(FVlumsum);
    FVmonInvest = parseFloat(myform.levelInvest.value) * ((Math.pow(1 + investRate / 12, Homeyrs * 12) - 1) / (investRate / 12) * (1 + investRate / 12));
    getServerControl('FVmonInvest').value = getFormattedNumber(FVmonInvest);
    FVlumsum = remComma(getServerControl('FVlumsum').value);
    FVmonInvest = remComma(getServerControl('FVmonInvest').value);
    total = parseFloat(FVlumsum) + parseFloat(FVmonInvest);
    getServerControl('total').value = getFormattedNumber(total);
    futurebalance = remComma(getServerControl('futurebalance').value);
    total = remComma(getServerControl('total').value);
    balFutureAmt = parseFloat(futurebalance) - parseFloat(total);
    getServerControl('balFutureAmt').value = getFormattedNumber(balFutureAmt);
    balFutureAmt = remComma(getServerControl('balFutureAmt').value);
    lumsumInvest = remComma(getServerControl('lumsumInvest').value);
    total = remComma(getServerControl('total').value);
    shortfallLumsum = balFutureAmt / Math.pow(1 + investRate, Homeyrs);
    getServerControl('shortfallLumsum').value = getFormattedNumber(shortfallLumsum);
    balFutureAmt = remComma(getServerControl('balFutureAmt').value);
    shortfallLevelInvest = parseFloat(balFutureAmt) / ((Math.pow(1 + investRate / 12, Homeyrs * 12) - 1) / (investRate / 12) * (1 + investRate / 12));
    getServerControl('shortfallLevelInvest').value = getFormattedNumber(shortfallLevelInvest);
}


//--------------------------------------------------------
// Other Finance
//--------------------------------------------------------
function totPlan1() {
    
    FutureAmtReq1 = (remComma(getServerControl('FutureAmtReq1').value));
    FutureEarmark1 = (remComma(getServerControl('FutureEarmark1').value));

    totGoal1 = getNumber(FutureAmtReq1) - getNumber(FutureEarmark1);
    getServerControl('futurebalance1').value = getFormattedNumber(totGoal1);

    Yrsgoal1 = parseFloat(getServerControl('Yrsgoal1_txt').value);
    futurebalance1 = (remComma(getServerControl('futurebalance1').value));
    infRateRet = parseFloat(getServerControl('infRateRet_txt').value);

    currLumsum1 = parseFloat(futurebalance1) / Math.pow((1 + (infRateRet)), (Yrsgoal1));
    getServerControl('currLumsum1').value = getFormattedNumber(currLumsum1);
    if (infRateRet == 0) currMonthInvest1 = 0;
    else currMonthInvest1 = parseFloat(futurebalance1) / (((Math.pow((1 + (infRateRet / 12)), (Yrsgoal1 * 12)) - 1) / (infRateRet / 12)) * (1 + (infRateRet / 12)));
    getServerControl('currMonthInvest1').value = getFormattedNumber(currMonthInvest1);

    currLumsum1 = (remComma(getServerControl('currLumsum1').value));
    currLumsum2 = (remComma(getServerControl('currLumsum2').value));
    currMonthInvest1 = (remComma(getServerControl('currMonthInvest1').value));
    currMonthInvest2 = (remComma(getServerControl('currMonthInvest2').value));

    totcurrLumsum = getNumber(currLumsum1) + getNumber(currLumsum2);
    getServerControl('totcurrLumsum').value = getFormattedNumber(totcurrLumsum);

    totcurrMonthInvest = getNumber(currMonthInvest1) + getNumber(currMonthInvest2);
    getServerControl('totcurrMonthInvest').value = getFormattedNumber(totcurrMonthInvest);
}

function totPlan2() {

    FutureAmtReq2 = (remComma(getServerControl('FutureAmtReq2').value));
    FutureEarmark2 = (remComma(getServerControl('FutureEarmark2').value));

    totGoal2 = getNumber(FutureAmtReq2) - getNumber(FutureEarmark2);
    getServerControl('futurebalance2').value = getFormattedNumber(totGoal2);

    Yrsgoal2 = parseFloat(getServerControl('Yrsgoal2_txt').value);
    futurebalance2 = (remComma(getServerControl('futurebalance2').value));
    infRateRet = parseFloat(getServerControl('infRateRet_txt').value);

    currLumsum2 = parseFloat(futurebalance2) / Math.pow((1 + (infRateRet)), (Yrsgoal2));
    getServerControl('currLumsum2').value = getFormattedNumber(currLumsum2);
    if (infRateRet == 0) 
        currMonthInvest2 = 0;
    else 
        currMonthInvest2 = parseFloat(futurebalance2) / (((Math.pow((1 + (infRateRet / 12)), (Yrsgoal2 * 12)) - 1) / (infRateRet / 12)) * (1 + (infRateRet / 12)));
    getServerControl('currMonthInvest2').value = getFormattedNumber(currMonthInvest2);

    currLumsum1 = (remComma(getServerControl('currLumsum1').value));
    currLumsum2 = (remComma(getServerControl('currLumsum2').value));
    currMonthInvest1 = (remComma(getServerControl('currMonthInvest1').value));
    currMonthInvest2 = (remComma(getServerControl('currMonthInvest2').value));

    totcurrLumsum = getNumber(currLumsum1) + getNumber(currLumsum2);
    getServerControl('totcurrLumsum').value = getFormattedNumber(totcurrLumsum);

    totcurrMonthInvest = getNumber(currMonthInvest1) + getNumber(currMonthInvest2);
    getServerControl('totcurrMonthInvest').value = getFormattedNumber(totcurrMonthInvest);
}

//--------------------------------------------------------
// Insurance
//--------------------------------------------------------
function calTotInsurance() {
    demiseAmtReceive = remComma(getServerControl('demiseAmtReceive').value);
    demiseAmtPay = remComma(getServerControl('demiseAmtPay').value);
    totInsuAmt = parseFloat(demiseAmtReceive) - parseFloat(demiseAmtPay);
    getServerControl('netAmtDue').value = getFormattedNumber(totInsuAmt);
}
function totInsuinc() {
    if (!validNumber(getServerControl('Income'), false)) return;
    if (!checkNegative(getServerControl('Income'), "", false)) return;
    income = remComma(getServerControl('Income').value);
    addtotIncIn = parseFloat(income)
    getinsuCashin();
}
function totInsuout() {
    if (!validNumber(getServerControl('Investments'), false)) return;
    if (!checkNegative(getServerControl('Investments'), "", false)) return;
    if (!validNumber(getServerControl('Living'), false)) return;
    if (!checkNegative(getServerControl('Living'), "", false)) return;
    if (!validNumber(getServerControl('Transport'), false)) return;
    if (!checkNegative(getServerControl('Transport'), "", false)) return;
    if (!validNumber(getServerControl('LoanRepay'), false)) return;
    if (!checkNegative(getServerControl('LoanRepay'), "", false)) return;
    if (!validNumber(getServerControl('Insurance'), false)) return;
    if (!checkNegative(getServerControl('Insurance'), "", false)) return;
    if (!validNumber(getServerControl('OtherExp'), false)) return;
    if (!checkNegative(getServerControl('OtherExp'), "", false)) return;
    Investments = remComma(getServerControl('Investments').value);
    Living = remComma(getServerControl('Living').value);
    Transport = remComma(getServerControl('Transport').value);
    LoanRepay = remComma(getServerControl('LoanRepay').value);
    Insurance = remComma(getServerControl('Insurance').value);
    OtherExp = remComma(getServerControl('OtherExp').value);
    addtotInsuOut = parseFloat(Investments) + parseFloat(Living) + parseFloat(Transport) +
		            parseFloat(LoanRepay) + parseFloat(Insurance) + parseFloat(OtherExp)
    getServerControl('totalExpen').value = getFormattedNumber(addtotInsuOut);
    getinsuCashin();
}
function getinsuCashin() {
    total = remComma(getServerControl('Income').value);
    totalExpen = remComma(getServerControl('totalExpen').value);
    insuTotal = parseFloat(total) - parseFloat(totalExpen);
    getServerControl('netInOut').value = getFormattedNumber(insuTotal);
}
function totReceive() {
    if (!validNumber(getServerControl('cashEquivalent'), false)) return;
    if (!checkNegative(getServerControl('cashEquivalent'), "", false)) return;
    if (!validNumber(getServerControl('security'), false)) return;
    if (!checkNegative(getServerControl('security'), "", false)) return;
    if (!validNumber(getServerControl('unitTrust'), false)) return;
    if (!checkNegative(getServerControl('unitTrust'), "", false)) return;
    if (!validNumber(getServerControl('investProp'), false)) return;
    if (!checkNegative(getServerControl('investProp'), "", false)) return;
    if (!validNumber(getServerControl('EPF'), false)) return;
    if (!checkNegative(getServerControl('EPF'), "", false)) return;
    if (!validNumber(getServerControl('perLifeInsu'), false)) return;
    if (!checkNegative(getServerControl('perLifeInsu'), "", false)) return;
    if (!validNumber(getServerControl('compLife'), false)) return;
    if (!checkNegative(getServerControl('compLife'), "", false)) return;
    if (!validNumber(getServerControl('otherMoni'), false)) return;
    if (!checkNegative(getServerControl('otherMoni'), "", false)) return;
    cashEquivalent = remComma(getServerControl('cashEquivalent').value);
    security = remComma(getServerControl('security').value);
    unitTrust = remComma(getServerControl('unitTrust').value);
    investProp = remComma(getServerControl('investProp').value);
    EPF = remComma(getServerControl('EPF').value);
    perLifeInsu = remComma(getServerControl('perLifeInsu').value);
    compLife = remComma(getServerControl('compLife').value);
    otherMoni = remComma(getServerControl('otherMoni').value);
    totInsuReceive = parseFloat(cashEquivalent) + parseFloat(security) +
		                parseFloat(unitTrust) + parseFloat(investProp) + parseFloat(EPF) +
		                parseFloat(perLifeInsu) + parseFloat(compLife) + parseFloat(otherMoni);
    getServerControl('totAmtReceive').value = getFormattedNumber(totInsuReceive);
}
function totPay() {
    if (!validNumber(getServerControl('amtDue'), false)) return;
    if (!checkNegative(getServerControl('amtDue'), "", false)) return;
    if (!validNumber(getServerControl('funeralCost'), false)) return;
    if (!checkNegative(getServerControl('funeralCost'), "", false)) return;
    if (!validNumber(getServerControl('adminCost'), false)) return;
    if (!checkNegative(getServerControl('adminCost'), "", false)) return;
    if (!validNumber(getServerControl('taxes'), false)) return;
    if (!checkNegative(getServerControl('taxes'), "", false)) return;
    if (!validNumber(getServerControl('otherPay'), false)) return;
    if (!checkNegative(getServerControl('otherPay'), "", false)) return;
    amtDue = remComma(getServerControl('amtDue').value);
    funeralCost = remComma(getServerControl('funeralCost').value);
    adminCost = remComma(getServerControl('adminCost').value);
    taxes = remComma(getServerControl('taxes').value);
    otherPay = remComma(getServerControl('otherPay').value);
    totInsuPay = parseFloat(amtDue) + parseFloat(funeralCost) + parseFloat(adminCost) + parseFloat(taxes) + parseFloat(otherPay);
    getServerControl('AmtPayDemise').value = getFormattedNumber(totInsuPay);
}
function cal1() {
    if (!validNumber(getServerControl('currAge'), false)) return
    if (!checkNegative(getServerControl('currAge'), "", false)) return
    if (!checkZeroInFront(getServerControl('currAge'), false)) return
    if (!validNumber(getServerControl('EPFbalance'), false)) return
    if (!checkNegative(getServerControl('EPFbalance'), "", false)) return
    if (!checkZeroInFront(getServerControl('EPFbalance'), "", false)) {
        getServerControl('currInvBalance').value = "0.00"
        getServerControl('acct1').value = "0.00"
        getServerControl('.acct2').value = "0.00"
        getServerControl('acct3').value = "0.00"
        return
    }
    if (!validNumber(getServerControl('monEPFContribute'), false)) return
    if (!checkNegative(getServerControl('monEPFContribute'), "", false)) return
    var EPFbalance = remComma(getServerControl('EPFbalance').value)
    amt1 = parseFloat(EPFbalance) * 0.7
    getServerControl('acct1').value = getFormattedNumber(amt1)
    amt2 = parseFloat(EPFbalance) * 0.3
    getServerControl('acct2').value = getFormattedNumber(amt2)
    currInvBalance = (amt1 - 50000) * 0.2
    getServerControl('currInvBalance').value = getFormattedNumber(currInvBalance)
}

//--------------------------------------------------------
//Networth
//--------------------------------------------------------
function totAsset() {
    if (!validNumber(getServerControl('cash'), false)) return
    if (!checkNegative(getServerControl('cash'), "", false)) return
    if (!validNumber(getServerControl('equivalents'), false)) return
    if (!checkNegative(getServerControl('equivalents'), "", false)) return
    if (!validNumber(getServerControl('equities'), false)) return
    if (!checkNegative(getServerControl('equities'), "", false)) return
    if (!validNumber(getServerControl('uTrust'), false)) return
    if (!checkNegative(getServerControl('uTrust'), "", false)) return
    if (!validNumber(getServerControl('epfPlan'), false)) return
    if (!checkNegative(getServerControl('epfPlan'), "", false)) return
    if (!validNumber(getServerControl('property'), false)) return
    if (!checkNegative(getServerControl('property'), "", false)) return
    if (!validNumber(getServerControl('miscell'), false)) return
    if (!checkNegative(getServerControl('miscell'), "", false)) return
    cash = remComma(getServerControl('cash').value);
    equivalents = remComma(getServerControl('equivalents').value);
    equities = remComma(getServerControl('equities').value);
    uTrust = remComma(getServerControl('uTrust').value);
    epfPlan = remComma(getServerControl('epfPlan').value);
    property = remComma(getServerControl('property').value);
    miscell = remComma(getServerControl('miscell').value);
    addTotAsset = parseFloat(cash) + parseFloat(equivalents) + parseFloat(equities) +
		           parseFloat(uTrust) + parseFloat(epfPlan) + parseFloat(property) + parseFloat(miscell);
    getServerControl('totAssets').value = getFormattedNumber(parseFloat(addTotAsset));
    getnetWorth();
    RMValue();
}
function totLiab() {
    if (!validNumber(getServerControl('creCard'), false)) return
    if (!checkNegative(getServerControl('creCard'), "", false)) return
    if (!validNumber(getServerControl('perLoan'), false)) return
    if (!checkNegative(getServerControl('perLoan'), "", false)) return
    if (!validNumber(getServerControl('carLoan'), false)) return
    if (!checkNegative(getServerControl('carLoan'), "", false)) return
    if (!validNumber(getServerControl('othLoan'), false)) return
    if (!checkNegative(getServerControl('othLoan'), "", false)) return
    creCard = remComma(getServerControl('creCard').value);
    perLoan = remComma(getServerControl('perLoan').value);
    carLoan = remComma(getServerControl('carLoan').value);
    othLoan = remComma(getServerControl('othLoan').value);
    addTotLiab = parseFloat(creCard) + parseFloat(perLoan) + parseFloat(carLoan) + parseFloat(othLoan);
    getServerControl('totLiabily').value = getFormattedNumber(parseFloat(addTotLiab));
    getnetWorth();
    RMValue();
}

function RMValue() {
    getServerControl('RMnetworth').value = getServerControl('netWorth').value;
    getServerControl('RMAssets').value = getServerControl('totAssets').value;
    getServerControl('RMLiabili').value = getServerControl('totLiabily').value;
}
function getnetWorth() {
    intTotal = getNumber(getServerControl('totAssets').value) - getNumber(getServerControl('totLiabily').value);
    getServerControl('netWorth').value = getFormattedNumber(intTotal);
}
function getnetCashin() {
    inoutTotal = getNumber(getServerControl('totCash').value) - getNumber(getServerControl('totCashout').value);
    getServerControl('netCashin').value = getFormattedNumber(inoutTotal);
}
function totIncIn() {
    if (!validNumber(getServerControl('comIncome'), false)) return;
    if (!validNumber(getServerControl('invIncome'), false)) return;
    if (!validNumber(getServerControl('pensionPay'), false)) return;
    if (!validNumber(getServerControl('othIncome'), false)) return;
    if (!checkNegative(getServerControl('comIncome'), "", false)) return;
    if (!checkNegative(getServerControl('invIncome'), "", false)) return;
    if (!checkNegative(getServerControl('pensionPay'), "", false)) return;
    if (!checkNegative(getServerControl('othIncome'), "", false)) return;
    comIncome = (remComma(getServerControl('comIncome').value));
    invIncome = (remComma(getServerControl('invIncome').value));
    pensionPay = (remComma(getServerControl('pensionPay').value));
    othIncome = (remComma(getServerControl('othIncome').value));
    addtotIncIn = parseFloat(comIncome) + parseFloat(invIncome) + parseFloat(pensionPay) + parseFloat(othIncome);
    getServerControl('totCash').value = getFormattedNumber(addtotIncIn);
    getnetCashin();
}
function totIncOut() {
    if (!validNumber(getServerControl('outinvestment'), false)) return;
    if (!validNumber(getServerControl('houseExp'), false)) return;
    if (!validNumber(getServerControl('loanRepay'), false)) return;
    if (!validNumber(getServerControl('insPremium'), false)) return;
    if (!validNumber(getServerControl('taxLiabily'), false)) return;
    if (!validNumber(getServerControl('others'), false)) return;
    if (!checkNegative(getServerControl('outinvestment'), "", false)) return;
    if (!checkNegative(getServerControl('houseExp'), "", false)) return;
    if (!checkNegative(getServerControl('loanRepay'), "", false)) return;
    if (!checkNegative(getServerControl('insPremium'), "", false)) return;
    if (!checkNegative(getServerControl('taxLiabily'), "", false)) return;
    if (!checkNegative(getServerControl('others'), "", false)) return;
    outinvestment = (remComma(getServerControl('outinvestment').value));
    houseExp = (remComma(getServerControl('houseExp').value));
    loanRepay = (remComma(getServerControl('loanRepay').value));
    insPremium = (remComma(getServerControl('insPremium').value));
    taxLiabily = (remComma(getServerControl('taxLiabily').value));
    others = (remComma(getServerControl('others').value));
    addtotIncOut = parseFloat(outinvestment) + parseFloat(houseExp) + parseFloat(loanRepay) + parseFloat(insPremium) + parseFloat(taxLiabily) + parseFloat(others);
    getServerControl('totCashout').value = getFormattedNumber(addtotIncOut);
    getnetCashin();
}

