$(document).ready(function () {
    setTimeout("init_srtinstallatie()", 100);
  });

function init_srtinstallatie()
{
    $("#object_discipline").change(function() {change_object_discipline(); });
    $("#object_group").change(function() {change_object_group(); });

    var srtinstallatie_key = $("#object_key").val();
    if (!srtinstallatie_key)
        change_object_discipline(); // Initialisatie listboxen bij search.
    else
    {
        var discipline_key = $("#object_discipline").val();
        var srtgroep_key = $("#object_group").val();
        var srtdeel_key = $("#object_type").val();
        var niveau = $("#level").val();
        if (niveau == "D")
        {
            fill_srtgroep(discipline_key);
            $("#object_type").prop("disabled", true);
        }
        if (niveau == "G")
        {
            fill_srtgroep(discipline_key, srtgroep_key);
            fill_srtdeel(discipline_key, srtgroep_key);
        }
        if (niveau == "S")
        {
            fill_srtgroep(discipline_key, srtgroep_key);
            fill_srtdeel(discipline_key, srtgroep_key, srtdeel_key);
        }
    }
}

function change_object_discipline(srtgroep_key)
{
    process_info({niveau:"G",lov:[]});
    process_info({niveau:"S",lov:[]});

    var discipline_key = $("#object_discipline").val();
    fill_srtgroep(discipline_key);
}

function change_object_group(srtdeel_key)
{
    process_info({niveau:"S",lov:[]});

    var discipline_key = $("#object_discipline").val();
    var srtgroep_key = $("#object_group").val();
    fill_srtdeel(discipline_key, srtgroep_key);
}

function fill_srtgroep(discipline_key, srtgroep_key)
{
    if (discipline_key > 0)
    {
        $.getJSON("../ins/get_ins_info_ajax.asp",
                    { req_info: "ins_srtinstallatie",
                      niveau: "G",
                      install_key: discipline_key,
                      init_key: srtgroep_key,
                      available: srtinstallatieselector_available
                    },
                  process_info);
    }
}

function fill_srtdeel(discipline_key, srtgroep_key, srtdeel_key)
{
    if (srtgroep_key > 0)
    {
        $.getJSON("../ins/get_ins_info_ajax.asp",
                    { req_info: "ins_srtinstallatie",
                      niveau: "S",
                      install_key: srtgroep_key,
                      init_key: srtdeel_key,
                      available: srtinstallatieselector_available
                    },
                  process_info);
    }
}

function process_info(data)
{
    var select = (data.niveau=="G" ? $("#object_group") : $("#object_type"));

    var sel_key = -1;
    var new_lov = '<option value="-1" ></option>';
    if (data.lov.length > 0)
    {
        for (i=0; i<data.lov.length;i++)
        {
            var opt = data.lov[i];
            new_lov += '<option value="' + opt.key + '"' + (opt.sel?"selected":"") + '>'+ opt.oms + '</option>';
            if (opt.sel) sel_key = opt.key;
        }
        select.prop("disabled", false);
    }
    else
    {
        select.prop("disabled", true);
    }
    select.empty().append(new_lov);
    if (sel_key > -1)
        select.val(sel_key);
}

function pre_submit()
{
    var discipline_key = $("#object_discipline").val();
    var srtgroep_key = $("#object_group").val();
    var srtdeel_key = $("#object_type").val();

    if (discipline_key) // discipline_key, srtgroep_key en srtdeel_key zijn readonly, dan srtinstallatiekey laten staan.
    {
        if (srtdeel_key != -1)
        {
            $("#level").val("S");
            $("#object_key").val(srtdeel_key);
        }
        else if (srtgroep_key != -1)
        {
            $("#level").val("G");
            $("#object_key").val(srtgroep_key);
        }
        else
        {
            $("#level").val("D");
            $("#object_key").val(discipline_key);
        }
    }

    return true;
}