var XML_KRAJE = null;
//pridanie udalosti na reality form
$(function () {
    //skryjeme specialne casti
    var typ = $('#realityForm select[@name="typ_nehnutelnosti"]').val();
    if (typ.indexOf('1') == 0) {
        $('#realityForm .spDom, #realityForm .spPozemok').hide();
        $('#realityForm .spByt').show();
    }
    else if (typ.indexOf('2') == 0) {
        $('#realityForm .spByt, #realityForm .spPozemok').hide();
        $('#realityForm .spDom').show();
    }
    else if (typ.indexOf('0') == 0) {
        $('#realityForm .spByt, #realityForm .spDom').hide();
        $('#realityForm .spPozemok').show();
    }
    else {
        $('#realityForm .spByt, #realityForm .spDom, #realityForm .spPozemok').hide();
    }
    //pridame udalost na zmenu typu nehnutelnosti
    $('#realityForm select[@name="typ_nehnutelnosti"]').change(function () {
        var typ = $(this).val();
        if (typ.indexOf('1') == 0) {
            $('#realityForm .spDom, #realityForm .spPozemok').hide();
            $('#realityForm .spByt').show();
        }
        else if (typ.indexOf('2') == 0) {
            $('#realityForm .spByt, #realityForm .spPozemok').hide();
            $('#realityForm .spDom').show();
        }
        else if (typ.indexOf('0') == 0) {
            $('#realityForm .spByt, #realityForm .spDom').hide();
            $('#realityForm .spPozemok').show();
        }
        else {
            $('#realityForm .spByt, #realityForm .spDom, #realityForm .spPozemok').hide();
        }
    });
    //zmena zaujmu
    var zaujem = $('#realityForm input:radio[@name="zaujem"]');
    var zaujem_val = zaujem[0].checked ? zaujem[0].value : (zaujem[1].checked ? zaujem[1].value : (zaujem[2].checked ? zaujem[2].value : ''));
    if (zaujem_val == 'prenajom') {
        $('#realityForm .spPrenajom').show();
    }
    else {
        $('#realityForm .spPrenajom').hide();
    }
    $('#realityForm input:radio[@name="zaujem"]').change(function () {
        var zaujem = $(this).val();
        if (zaujem == 'prenajom') {
            $('#realityForm .spPrenajom').show();
        }
        else {
            $('#realityForm .spPrenajom').hide();
        }
    });
    //zmena krajov - okresov atd...
    $('#realityForm select[@name="kraj"]').change(function () {
        var kraj = $(this).val();
        $.getJSON(
            '/kraje.php',
            {
                'kraj': kraj
            },
            function (okresy) {
                realityForm_changeOkresy(okresy);
                realityForm_changeObce([]);
            }
        )
    });
    $('#realityForm select[@name="okres"]').change(function () {
        var okres = $(this).val();
        $.getJSON(
            '/kraje.php',
            {
                'kraj': $('#realityForm select[@name="kraj"]').val(),
                'okres': okres
            },
            function (obce) {
                realityForm_changeObce(obce);
            }
        )
    });
});

function realityForm_changeOkresy(okresy) {
    var s = $('#realityForm select[@name="okres"]')[0];
    for (var i = 1 ; i < s.options.length ; i++) {
        s.options[i] = null;
    }
    s.options.length = 1;
    for (var j = 0 ; j < okresy.length ; j++) {
        s.options[j+1] = new Option(okresy[j],okresy[j]);
    }
}

function realityForm_changeObce(obce) {
    var s = $('#realityForm select[@name="obec"]')[0];
    for (var i = 1 ; i < s.options.length ; i++) {
        s.options[i] = null;
    }
    s.options.length = 1;
    for (var j = 0 ; j < obce.length ; j++) {
        s.options[j+1] = new Option(obce[j],obce[j]);
    }
}