
Number.prototype.format = (function(decimals, dec_point, thousands_sep) {
    var number = this;

    var exponent = '';
    var numberstr = this.toString();
    var eindex = numberstr.indexOf('e');
    if (eindex > -1) {
        exponent = numberstr.substring(eindex);
        number = parseFloat(numberstr.substring(0, eindex));
    }

    if (decimals != null) {
        var temp = Math.pow(10, decimals);
        number = Math.round(number * temp) / temp;
    }

    var sign = number < 0 ? '-' : '';
    var integer = (number > 0 ? Math.floor(number) : Math.abs(Math.ceil(number))).toString();

    var fractional = number.toString().substring(integer.length + sign.length);
    dec_point = dec_point != null ? dec_point : '.';
    fractional = decimals != null && decimals > 0 || fractional.length > 1 ? (dec_point + fractional.substring(1)) : '';

    if (decimals != null && decimals > 0) {
        for (i = fractional.length - 1, z = decimals; i < z; ++i)
            fractional += '0';
    }

    thousands_sep = (thousands_sep != dec_point || fractional.length == 0) ? thousands_sep : null;

    if (thousands_sep != null && thousands_sep != '') {
        for (i = integer.length - 3; i > 0; i -= 3)
            integer = integer.substring(0, i) + thousands_sep + integer.substring(i);
    }

    return sign + integer + fractional + exponent;
});
var pstufe;

var recalc = function() {
    var totalsum = 0;
    $$('input.count-input').each(function(ci) {
        ci.value = ci.value.toInt() ? ci.value.toInt() : 0;
        if (ci.value > 93) {
            alert('Eine Leistung kann max. 93 mal pro Monat gebucht werden');
            ci.value = 93;
        }
        count = ci.value;
        id = ci.id.replace(/count_/g, '');
        singleprice = $('singleprice_' + id).value.toFloat();

        if (id == 2) {
            if (count < $('count_9').value) {
                alert('Die Leistung "Zuschlag bei Ganzk&#246;rperw&#228;sche" setzt immer die Leistung "Teilwaschen" voraus.');
                ci.value = $('count_9').value;
                recalc();
            }
        }

        price = (singleprice * count).round(2);
        totalsum += price;
        $('price_' + id).innerHTML = price.format(2, ',', '.');
    });

    totalsum = totalsum.round(2);
    $('total_sum').innerHTML = totalsum.format(2, ',', '.');

    $$('input.pstufe-input').each(function(el) { if (el.checked) pstufe = el.value })
    pamount = $('pamount_' + pstufe).value.toFloat();
    $('pstufe_amount').innerHTML = pamount.format(2, ',', '.');

    ownamount = totalsum - pamount;
    $('own_amount').innerHTML = Math.max(0, ownamount).format(2, ',', '.');
}
