function try_recalculate() {
    el_cena = document.forms.kalk.kwota1;
    if(chk_isnum(el_cena)) {
        set_params();
        recalculate(el_cena.value);
    }
}

function chk_isnum(ob) {
    raw = ob.value;
//     raw = raw.toString().replace(/\s/g,'');
//     alert(raw);
    if(raw == "") {
        clear_fields();
    }

    val = parseFloat(raw);
    if(isNaN(raw)) {
        if(isNaN(val)) {
            ob.value = "";
            return false;
        } else {
            ob.value = val;
            return false;
        }
    }
//     ob.value = format_currency(raw);
    if(val > 0)
        return true;
    else
        return false;
}
function clear_fields() {
    frm = document.forms.kalk;
    flds = new Array("podatek_ccp",
            "taksa_not",
            "taksa_not_vat",
            "agencja_prow",
            "agencja_prow_vat",
            "wpis_do_ksiegi",
            "wniosek_do_wydzialu",
            "wniosek_do_wydzialu_vat",
            "oplaty_razem",
            "suma");

    for(i=0; i<flds.length; i++) {
//         document.writeln(flds[i]);
//         alert(flds[i]);
        if(el = document.getElementById([flds[i]])) {
            el.innerHTML = "&nbsp;";
        }

//         else
//             alert(flds[i]);
    }

}
function recalculate(cena) {
    frm = document.forms.kalk;

    prow_calculated = Math.round((this.prowizja_agencja * cena )) / 100;
    p_ccp_calculated = Math.round((this.podatek_czynnosci * cena )) / 100;

    prowizja_agencja_vat_calculated = Math.round((this.prowizja_agencja_vat * prow_calculated )) / 100;

    if ((cena > 0) && (cena <= 5000)) {
        taksa_notarialna_calculated = 200;
        if(this.taksa_model == 2) {
            taksa_notarialna_calculated /= 2;
        }
    } else {
        if ((cena > 5000) && (cena <= 15000))
                taksa_notarialna_calculated = (200 + ((cena - 5000)*0.03));
        if ((cena > 15000) && (cena <= 30000))
                taksa_notarialna_calculated = (500 + ((cena - 15000)*0.02));
        if ((cena > 30000) && (cena <= 60000))
                taksa_notarialna_calculated = (800 + ((cena - 30000)*0.01));
        if ((cena > 60000) && (cena <= 1000000))
                taksa_notarialna_calculated = (1100 + ((cena - 60000)*0.005));
        if (cena > 1000000)
                taksa_notarialna_calculated = (5800 + ((cena - 1000000)*0.0025));

        if(this.taksa_model == 1) {
            taksa_notarialna_calculated /= 2;
        }
    }

    taksa_notarialna_calculated = Math.round(taksa_notarialna_calculated * 100) / 100;
    taksa_not_vat_calculated = Math.round((parseFloat(this.taksa_notarialna_vat) * parseFloat(taksa_notarialna_calculated))) / 100;

    el = document.getElementById("wpis_do_ksiegi");
    el.innerHTML = this.wpis_do_ksiegi_current > 0 ? format_currency(this.wpis_do_ksiegi_current) : " ------- ";
    el = document.getElementById("wniosek_do_wydzialu");
    el.innerHTML = this.wniosek_do_wydzialu_current > 0 ? format_currency(this.wniosek_do_wydzialu_current) : " ------- ";

    if(this.wpis_do_ksiegi_current > 0 && this.wniosek_do_wydzialu_current > 0) {
        wniosek_do_wydzialu_vat_calculated = Math.round(parseFloat(this.wniosek_do_wydzialu_vat) * parseFloat(this.wniosek_do_wydzialu_current)) / 100;
        el = document.getElementById("wniosek_do_wydzialu");
        el.innerHTML = format_currency(wniosek_do_wydzialu_vat_calculated);
    } else {
        wniosek_do_wydzialu_vat_calculated = 0;
        print_currency("wniosek_do_wydzialu_vat", format_currency(wniosek_do_wydzialu_vat_calculated));
    }

    oplaty_calculated = p_ccp_calculated + taksa_notarialna_calculated + taksa_not_vat_calculated + prow_calculated + prowizja_agencja_vat_calculated + this.wpis_do_ksiegi_current + this.wniosek_do_wydzialu_current + wniosek_do_wydzialu_vat_calculated;
    suma_calculated = Math.round((parseFloat(oplaty_calculated) + parseFloat(cena)) * 100) / 100;

    print_currency("podatek_ccp", p_ccp_calculated);
    print_currency("taksa_not", taksa_notarialna_calculated);
    print_currency("taksa_not_vat", taksa_not_vat_calculated);
    print_currency("agencja_prow", prow_calculated);
    print_currency("agencja_prow_vat", prowizja_agencja_vat_calculated);
    print_currency("oplaty_razem", oplaty_calculated);
    print_currency("suma", suma_calculated);

}

function roundup(v) {
    return Math.round(v * 100) / 100;
}

function print_currency(field, val) {
    frm = document.forms.kalk;

    val = this.format_currency(val);
//     return (((sign)?'':'-') + '$' + num + '.' + cents);

    if(el = document.getElementById(field)) {
        el.innerHTML = val;
    }
//     frm[field].value = val;
}
function format_currency(val) {
    val = val.toString().replace(/\$|\,/g,'');

    if(isNaN(val))
    val = "0";
    val = Math.floor(val*100+0.50000000001);
    cents = val%100;
    val = Math.floor(val/100).toString();
    if(cents<10)
    cents = "0" + cents;
    for (var i = 0; i < Math.floor((val.length-(1+i))/3); i++)
    val = val.substring(0,val.length-(4*i+3))+' '+
    val.substring(val.length-(4*i+3));
    return val + '.' + cents;
}

function set_params() {
    frm = document.forms.kalk;
    if(frm.typ[0].checked) { // spóldzielcze lub własnościowe
        this.taksa_model = 1;
        if(frm.ks_wiecz.checked) {
            this.wpis_do_ksiegi_current = this.wpis_do_ksiegi;
            this.wniosek_do_wydzialu_current = this.wniosek_do_wydzialu;
        } else {
            this.wpis_do_ksiegi_current = 0;
            this.wniosek_do_wydzialu_current = 0;
        }
    } else {
        this.taksa_model = 2;
        this.wpis_do_ksiegi_current = this.wpis_do_ksiegi;
        this.wniosek_do_wydzialu_current = this.wniosek_do_wydzialu;
    }
}

