function MakeMoneyCase(fP) {
    removeStringList(fP, "0123456789.", false);
    var sTmp = parseFloat(fP.value)
    if (isNaN(sTmp)) {
        sTmp= "";
        fP.value = '';
    } else {
        fP.value = FormatMoneyStr(sTmp);
    }
}
function removeStringList(StringField, charList, bStrip) {
    // eg. removeStringList('ABC123','1234567890',true) will strip all AlphaNumeric = 'ABC'
    var sOutString = '';
    for (iPos=0;iPos < StringField.value.length;iPos++) {
        if (bStrip)    {
            if (charList.indexOf(StringField.value.charAt(iPos))==-1)
                sOutString = sOutString + StringField.value.charAt(iPos);
        } else {
            if (charList.indexOf(StringField.value.charAt(iPos))>-1)
                sOutString = sOutString + StringField.value.charAt(iPos);
        }
    }
    StringField.value = sOutString;
}
function FormatMoneyStr(sM) {
    sOut = "";
    if (sM+"" == "undefined" || sM+"" == "null")
        return null;
    sM += "";
    idx = sM.indexOf(".");
    if (idx < 0) {
        sOut = sM;
    } else {
        sOut = sM.substring(0, idx + 3);
        if (sOut.length < (idx + 3))
            sOut += "0";
    }
    return sOut;
}
function comp(x) { // general entry point for all cases
        // convert all entry fields into variables
        pv = Math.abs(parseFloat(x.pv.value))*-1;
        fv = parseFloat(x.fv.value);
        np = parseFloat(x.np.value);
        pmt = parseFloat(x.pmt.value);
        //var numPeriods;
        if (x.period.value == "week") { numPeriods = 52; }
        if (x.period.value == "fortnight") { numPeriods = 26; }
        if (x.period.value == "month") { numPeriods = 12; }
        ir = ((x.ir.value/numPeriods) / 100);
        if(ir == 0) {
                if(pmt != 0) {
                        np = - (fv + pv)/pmt;
                } else {
                        alert("Divide by zero error.");
                }
        } else {
                np = Math.log((-fv * ir + pmt)/(pmt + ir * pv))/ Math.log(1 + ir);
        }
        if(np == 0) {
                alert("Can't compute Number of Periods for the present values.");
        } else if(np > 0) {
                x.np.value = Math.ceil(np) + " (" + x.period.value + "s)";
        } else {
                alert("Using data entered, you will never pay off this loan. Either lower the principal amount, lower the interest rate, or increase the payment amount.");
        }
}

