/*******************************************************************************************************************************************************************************************************************************************
i Typ půjčky                           Min výše    Max výše       Min splátky   Max splátky   Úroková sazba   RPSN poskytnutí absolutní hodnota   RPSN poskytnutí min   RPSN poskytnutí max   RPSN poskytnutí procento   RPSN Měsíční správa
0 Rychlá půjčka                        20 000 Kč   500 000 Kč     6             72            11,9 %          0 Kč                                0 Kč                  0 Kč                  0,0 %                      99 Kč
1 Účelová půjčka na bydlení            50 000 Kč   2 000 000 Kč   6             120            8,9 %          0 Kč                                0 Kč                  0 Kč                  0,0 %                      150 Kč
2 Účelová půjčka na ostatní investice  50 000 Kč   1 000 000 Kč   6             120           10,9 %          0 Kč                                0 Kč                  0 Kč                  0,0 %                      150 Kč
*******************************************************************************************************************************************************************************************************************************************/
var minVyseUveru_A       = new Array( 20000,   50000,   50000); //[Kč]
var maxVyseUveru_A       = new Array(500000, 2000000, 1000000); //[Kč]
var minSplatek_A         = new Array(     6,       6,       6); //[-]
var maxSplatek_A         = new Array(    72,     120,     120); //[-]
var urokovaSazba_A       = new Array(  11.9,     8.9,    10.9); //[%]
var RPSNposkytnutiAbs_A  = new Array(     0,       0,       0); //[Kč]
var RPSNposkytnutimin_A  = new Array(   500,     500,     500); //[Kč]
var RPSNposkytnutimax_A  = new Array(  5000,    5000,    5000); //[Kč]
var RPSNposkytnutiPerc_A = new Array(   1.0,     1.0,     1.0); //[%]
var RPSNsprava_A         = new Array(    99,     150,     150); //[Kč]

var prvneEditovano_A = true;
var numberSeparator_A = ' '; //pri zmene nutno zmenit i oddelovac v podmince if (txt.value.replace(/[' ']/g, '')

window.onload = function() { on_typ_uveruChange_A(); }

document.getElementById('A_vyse_uveru').onkeypress = onParameterChange_A;
function onParameterChange_A(e) {
    if( !(typeof(event) == 'undefined' || event == null ) ) {
	    if (event.keyCode > 0 && String.fromCharCode(event.keyCode).length > 0 && event.keyCode < 48)
		    return false;
        else if (event.keyCode > 0 && !(event.keyCode <= 57))
		    return false;
    }
}

function on_typ_uveruChange_A() {
    var A_typ_uveru = document.getElementById('A_typ_uveru');
    var typ = parseInt(A_typ_uveru.options[A_typ_uveru.selectedIndex].value, 10);
    var A_splatek = document.getElementById('A_splatek');
    
    onKeyUp_A('A_vyse_uveru');
    
    for (var i = A_splatek.options.length - 1; i >= 0; i--)
        A_splatek.remove(i);
    for (var i = minSplatek_A[typ]; i <= maxSplatek_A[typ]; i++) {
        var o = document.createElement('option');
        o.text = i.toString();
        try {
            A_splatek.add(o, null);
        } catch(ex) {
            A_splatek.add(o);
        }
    }
}

function onKeyUp_A(txtName) {    
    if (prvneEditovano_A == true)
        return;
    
    validujPole_A(txtName);
    validujUver_A(false);
}

function onBlur_A(txtName) {
    var txt = document.getElementById(txtName);
    var val = parseMyInt_A(txt.value);
    txt.value = (!isNaN(val) ? formatInt_A(val, numberSeparator_A) : '');

    prvneEditovano_A = false;
    
    onKeyUp_A(txtName);
}

function iterace_A(typ, vyse_splatky, splatek, vyseUveru) {
    var dir = true;
    var ldir = true;
    var val = 0.1;
    var step = 0.1;
    var lr = krok_A(typ, val, vyse_splatky, splatek, vyseUveru);
    dir = ldir = (r > 0);
    for (var idx = 0; idx < 2000; idx++) {
        val += step * (dir ? 1 : -1);
        var r = krok_A(typ, val, vyse_splatky, splatek, vyseUveru);
        if (Math.abs(r) < 0.000001)
            return val;
        dir = (r > 0);
        if (dir != ldir)
            step /= 10;
        ldir = dir;
        lval = val;
    }
    return val;
}

function krok_A(typ, valIRR, vyse_splatky, splatek, vyseUveru) {
    var sprava = RPSNsprava_A[typ];
    
    var valNPV = 0;
    for (var idx = 1; idx <= splatek + 1; idx++) {
        valNPV += (idx == 1 ? - vyseUveru : vyse_splatky + sprava) / Math.pow(1.0 + valIRR, idx);
    }
    return valNPV;
}

function poskytnutifce_A(valVyseUveru, typ) {
    var poskytnuti = RPSNposkytnutiAbs_A[typ] + valVyseUveru * RPSNposkytnutiPerc_A[typ] / 100.0;
    if (poskytnuti < RPSNposkytnutimin_A[typ])
        poskytnuti = RPSNposkytnutimin_A[typ];
    else if (poskytnuti > RPSNposkytnutimax_A[typ])
        poskytnuti = RPSNposkytnutimax_A[typ];
    
    return poskytnuti;
}

function spocitatA(valVyseUveru, typ) {
    var A_splatek = document.getElementById('A_splatek');
    var valA_splatek = parseInt(A_splatek.options[A_splatek.selectedIndex].text, 10);

    var r = (urokovaSazba_A[typ] / 100.0) / 12.0;
    var poskytnuti = poskytnutifce_A(valVyseUveru, typ);
    var valVyse_splatky = (valVyseUveru + poskytnuti) * r / ( 1 - 1 / Math.pow( (1 + r), valA_splatek ) );
    var valIRR = iterace_A(typ, valVyse_splatky, valA_splatek, valVyseUveru);
    var valRPSN = (Math.pow( 1.0 + valIRR, 12) - 1.0) * 100;
    
    valVyse_splatky = Math.ceil(valVyse_splatky);
    
    document.getElementById('A_vyse_splatky').value = formatInt_A(Math.round(valVyse_splatky), numberSeparator_A);
    document.getElementById('A_urokova_sazba').value = urokovaSazba_A[typ].toFixed(1).replace(/[.]/g, ',');
    document.getElementById('A_rpsn').value = valRPSN.toFixed(1).replace(/[.]/g, ',');
}

function validujPole_A(txtName) {
    var txt = document.getElementById(txtName);
    var val = parseMyInt_A(txt.value);
    var valid = document.getElementById('valid' + txtName);
    
    if (txt.value.replace(/[' ']/g, '') == val) {
        valid.style.display = 'none';
        return true;
    } else {
        valid.style.display = 'inline';
        return false;
    }
}

function validujUver_A(provedVypocet) {
    if ( provedVypocet && !validujPole_A('A_vyse_uveru') )
        return;
    
    var A_typ_uveru = document.getElementById('A_typ_uveru');
    var typ = parseInt(A_typ_uveru.options[A_typ_uveru.selectedIndex].value, 10);
    
    var A_vyse_uveru = document.getElementById('A_vyse_uveru');
    var valVyseUveru = parseMyInt_A(A_vyse_uveru.value);
    
    document.getElementById('spnVarovaniA').style.display = 'none';
    if (valVyseUveru < minVyseUveru_A[typ] || valVyseUveru > maxVyseUveru_A[typ]) {
        document.getElementById('spnVarovaniA').style.display = 'inline';
        document.getElementById('spnVarovaniA').innerHTML = A_typ_uveru.options[A_typ_uveru.selectedIndex].text + ' je poskytována v rozmezí<br />' + formatInt_A(minVyseUveru_A[typ], numberSeparator_A) + ' Kč až ' + formatInt_A(maxVyseUveru_A[typ], numberSeparator_A) + ' Kč.<br />Zadejte, prosím, požadovanou výši úvěru znovu.<br />';
        return;
    }
    
    if (provedVypocet)
        spocitatA(valVyseUveru, typ);
}

function onSpocitatA() {
    prvneEditovano_A = false;
    validujUver_A(true);
}

function formatInt_A(num, sprtr) {
    var val = parseInt(num, 10).toFixed(0);
    
    if      (val.length > 9) val = val.substring(0, val.length - 9) + sprtr + val.substring(val.length - 9, val.length - 6) + sprtr + val.substring(val.length - 6, val.length - 3) + sprtr + val.substring(val.length - 3);
    else if (val.length > 6) val = val.substring(0, val.length - 6) + sprtr + val.substring(val.length - 6, val.length - 3) + sprtr + val.substring(val.length - 3);
    else if (val.length > 3) val = val.substring(0, val.length - 3) + sprtr + val.substring(val.length - 3);
    
    return val;
}

function parseMyInt_A(val) {
    return parseInt(val.replace(/[^0-9]/g, ''), 10);
}

