// zahl 
// Die Zahl, die formatiert werden soll 
// k 
//Die Anzahl der Nachkommastellen auf die die Zahl gerundet werden soll (default: 0) 
// bool fix (optional) 
// Dieser Parameter bestimmt, ob die formatierte Zahl eine feste Anzahl von Nachkommastellen haben soll.
// Ist der Wert true, dann werden evtl. fehlende Stellen mit einer Null aufgeüllt.

var rgbColor = '#FFFFCC';
var rgbColorWhite = '#FFFFFF';

var fInpSum = 0.0;
var fInpInterest = 0.0;
var nInpMonth = 0;
var nInpRest = 0;

function formatZahl(zahl, k, fix) {
    if (!k) k = 0;
    var neu = '';

    var dec_point = ',';
    var thousands_sep = '.';

    // Runden 
    var f = Math.pow(10, k);
    zahl = '' + parseInt(zahl * f + (.5 * (zahl > 0 ? 1 : -1))) / f;

    // Komma ermittlen 
    var idx = zahl.indexOf('.');

    // fehlende Nullen einfügen 
    if (fix) {
        zahl += (idx == -1 ? '.' : '')
         + f.toString().substring(1);
    }
    var sign = zahl < 0;
    if (sign) zahl = zahl.substring(1);
    idx = zahl.indexOf('.');

    // Nachkommastellen ermittlen 
    if (idx == -1) idx = zahl.length;
    else neu = dec_point + zahl.substr(idx + 1, k);


    while (idx > 0) {
        if (idx - 3 > 0)
            neu = thousands_sep + zahl.substring(idx - 3, idx) + neu;
        else
            neu = zahl.substring(0, idx) + neu;
        idx -= 3;
    }
    return (sign ? '-' : '') + neu;
} 

function CheckSingleInput(strInput) {

    var objstrInput = new String(strInput);
    objstrInput = objstrInput.replace(/\./g, "");
    objstrInput = objstrInput.replace(/,/g, ".");
    objstrInput = objstrInput.replace(/[\s\*-]/g, "");

    if (objstrInput.search(/^[0-9]*[.]?[0-9]+$/) == -1)
        return 0.0;

    var fInput = parseFloat(objstrInput);

    return fInput;
}

function CheckInputData() {

    var bResult = true;

    var obj = new Array(4)
    obj[0] = document.forms[0].inp_sum;
    obj[1] = document.forms[0].inp_interest;
    obj[2] = document.forms[0].inp_month;
    obj[3] = document.forms[0].inp_rest;

    for (var i = 3; i >= 0; i--) {
        var fInput = CheckSingleInput(obj[i].value);
        if (!fInput) {
            obj[i].style.backgroundColor = rgbColor;
            obj[i].focus();
            obj[i].select();
            bResult = false;
        }
        else {
            obj[i].style.backgroundColor = rgbColorWhite;
            if (i <= 1)
                obj[i].value = formatZahl(fInput, 2, true);
            else {
                var str = formatZahl(fInput, 0, true);
                obj[i].value = str.replace(",", "");
            }

            switch (i) {
                case 0:
                    fInpSum = fInput;
                    break;
                case 1:
                    fInpInterest = fInput;
                    break;
                case 2:
                    nInpMonth = fInput;
                    break;
                case 3:
                    nInpRest = fInput;
                    break;
            }
        }
    }
    
    return bResult;
}

function Calc() {
    if (!CheckInputData())
        return;

    // Berechnung
    var fRate = 0.0;
    var fInterest = 0.0;
    var fRest = 0.0;

    fInterest = (fInpInterest * ((nInpMonth + 1) * fInpSum)) / (24 * 100);
    fRate = (fInpSum + fInterest) / nInpMonth;
    fRest = (fInpSum + fInterest) - (nInpRest * fRate);

    // Ausgabe
    document.forms[0].out_rate.value = formatZahl(fRate, 2, true) + " EUR";

    document.forms[0].out_interest.value = formatZahl(fInterest, 2, true) + " EUR";

    document.forms[0].out_rest.value = formatZahl(fRest, 2, true) + " EUR";
    var str = formatZahl(nInpRest, 0, true);
    str = str.replace(",", "");
    document.forms[0].out_restmonth.value = "Restschuld nach " + str + " Monaten";
}
