/*******************************************************************************************************************************************************************************************************************************************
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_B       = new Array( 20000,   50000,   50000); //[Kč]
var maxVyseUveru_B       = new Array(500000, 2000000, 1000000); //[Kč]
var minSplatek_B         = new Array(     6,       6,       6); //[-]
var maxSplatek_B         = new Array(    72,     120,     120); //[-]
var urokovaSazba_B       = new Array(  11.9,     8.9,    10.9); //[%]
var RPSNposkytnutiAbs_B  = new Array(     0,       0,       0); //[Kč]
var RPSNposkytnutimin_B  = new Array(   500,     500,     500); //[Kč]
var RPSNposkytnutimax_B  = new Array(  5000,    5000,    5000); //[Kč]
var RPSNposkytnutiPerc_B = new Array(   1.0,     1.0,     1.0); //[%]
var RPSNsprava_B         = new Array(    99,     150,     150); //[Kč]

var prvneEditovano_B = true;
var numberSeparator_B = ' '; //pri zmene nutno zmenit i oddelovac v podmince if (txt.value.replace(/[' ']/g, '')

document.getElementById('B_vyse_splatky').onkeypress = onParameterChange_B;
function onParameterChange_B(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_B() {
    var B_typ_uveru = document.getElementById('B_typ_uveru');
    var typ = parseInt(B_typ_uveru.options[B_typ_uveru.selectedIndex].value, 10);
    var B_splatek = document.getElementById('B_splatek');
    
    onKeyUp_B('B_vyse_splatky');
    document.getElementById('spnVarovaniB').style.display = 'none';
    
    for (var i = B_splatek.options.length - 1; i >= 0; i--)
        B_splatek.remove(i);
    for (var i = minSplatek_B[typ]; i <= maxSplatek_B[typ]; i++) {
        var o = document.createElement('option');
        o.text = i.toString();
        try {
            B_splatek.add(o, null);
        } catch(ex) {
            B_splatek.add(o);
        }
    }
}

function onKeyUp_B(txtName) {
    if (prvneEditovano_B == true)
        return;
    
    validujPole_B(txtName);
}

function onBlur_B(txtName) {
    var txt = document.getElementById(txtName);
    var val = parseMyInt_B(txt.value);
    txt.value = (!isNaN(val) ? formatInt_B(val, numberSeparator_B) : '');

    prvneEditovano_B = false;
    
    onKeyUp_B(txtName);
}

function poskytnutifce_B(valVyseUveru, typ) {
    var poskytnuti = RPSNposkytnutiAbs_B[typ] + valVyseUveru * RPSNposkytnutiPerc_B[typ] / 100.0;
    if (poskytnuti < RPSNposkytnutimin_B[typ])
        poskytnuti = RPSNposkytnutimin_B[typ];
    else if (poskytnuti > RPSNposkytnutimax_B[typ])
        poskytnuti = RPSNposkytnutimax_B[typ];
    
    return poskytnuti;
}

function spocitatB(valVyseSplatky, typ) {
    var B_splatek = document.getElementById('B_splatek');
    var valB_splatek = parseInt(B_splatek.options[B_splatek.selectedIndex].text, 10);

    var r = (urokovaSazba_B[typ] / 100.0) / 12.0;
    
    var valVyseUveruVcetnePoplatku = valVyseSplatky * ( 1 - 1 / Math.pow((1 + r), valB_splatek) ) / r;
    var valVyseUveru = (valVyseUveruVcetnePoplatku - RPSNposkytnutiAbs_B[typ]) / (1 + RPSNposkytnutiPerc_B[typ] / 100);

    var poskytnuti = valVyseUveruVcetnePoplatku - valVyseUveru;
    if (poskytnuti < RPSNposkytnutimin_B[typ])
        poskytnuti = RPSNposkytnutimin_B[typ];
    else if (poskytnuti > RPSNposkytnutimax_B[typ])
        poskytnuti = RPSNposkytnutimax_B[typ];

    valVyseUveru = valVyseUveruVcetnePoplatku - poskytnuti;
    
    valVyseUveru = Math.round(valVyseUveru / 500) * 500;
    
    var B_typ_uveru = document.getElementById('B_typ_uveru');
    document.getElementById('spnVarovaniB').style.display = 'none';
    if (valVyseUveru < minVyseUveru_B[typ]) {
        valVyseUveru = 0;
        document.getElementById('spnVarovaniB').style.display = 'inline';
        document.getElementById('spnVarovaniB').innerHTML = B_typ_uveru.options[B_typ_uveru.selectedIndex].text + ' je poskytována v rozmezí<br />' + formatInt_B(minVyseUveru_B[typ], numberSeparator_B) + ' Kč až ' + formatInt_B(maxVyseUveru_B[typ], numberSeparator_B) + ' Kč. Vypočtená výše úvěru ze zadaných údajů toto nesplňuje.<br />Zadejte, prosím, požadovanou výši anuitní splátky znovu nebo upravte délku splácení.<br />';
    } else if (valVyseUveru > maxVyseUveru_B[typ]) {
        valVyseUveru = maxVyseUveru_B[typ];
        poskytnuti = poskytnutifce_B(valVyseUveru, typ);
        var valVyse_splatky = (valVyseUveru + poskytnuti) * r / ( 1 - 1 / Math.pow( (1 + r), valB_splatek ) );
        valVyse_splatky = Math.ceil(valVyse_splatky);
        document.getElementById('B_vyse_splatky').value = formatInt_B(Math.round(valVyse_splatky), numberSeparator_B);
    }

    document.getElementById('B_vyse_uveru').value = formatInt_B(Math.round(valVyseUveru), numberSeparator_B);
    document.getElementById('B_urokova_sazba').value = urokovaSazba_B[typ].toFixed(1).replace(/[.]/g, ',');
}

function validujPole_B(txtName) {
    var txt = document.getElementById(txtName);
    var val = parseMyInt_B(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 onSpocitatB() {
    prvneEditovano_B = false;

    if (!validujPole_B('B_vyse_splatky'))
        return;

    var B_typ_uveru = document.getElementById('B_typ_uveru');
    var typ = parseInt(B_typ_uveru.options[B_typ_uveru.selectedIndex].value, 10);
    
    var B_vyse_splatky = document.getElementById('B_vyse_splatky');
    var valVyseSplatky = parseMyInt_B(B_vyse_splatky.value);
    
    spocitatB(valVyseSplatky, typ);
}

function formatInt_B(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_B(val) {
    return parseInt(val.replace(/[^0-9]/g, ''), 10);
}

