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

function init_srtinstallatie()
{
    $("#article_catalogue").change(function() {change_article_catalogue(); });
    $("#article_group").change(function() {change_article_group(); });


    var srtinstallatie_key = $("#catalogue_key").val();
    if (!srtinstallatie_key)
    {
        change_article_catalogue();
        change_article_group(); // Initialisatie listboxen bij search.
    }
    else
    {
        var discipline_key = $("#article_catalogue").val();
        var srtgroep_key = $("#article_group").val();
        var srtdeel_key = $("#article").val();
        var niveau = $("#level").val();
        if (niveau == "D")
        {
            fill_srtgroep(discipline_key);
            $("#article").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_article_catalogue(srtgroep_key)
{
    process_info({niveau:"G",lov:[]});
    process_info({niveau:"S",lov:[]});

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

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

    var discipline_key = $("#article_catalogue").val();
    var srtgroep_key = $("#article_group").val();
    fill_srtdeel(discipline_key, srtgroep_key);
}

function fill_srtgroep(discipline_key, srtgroep_key)
{
    if (discipline_key > 0)
    {
        $.getJSON("../bes/get_item_info_ajax.asp",
                    {
                      req_info: "bes_srtinstallatie",
                      niveau: "G",
                      install_key: discipline_key,
                      init_key: srtgroep_key
                    },
                  process_info);
    }
}

function fill_srtdeel(discipline_key, srtgroep_key, srtdeel_key)
{
    if (srtgroep_key > 0)
    {
        $.getJSON("../bes/get_item_info_ajax.asp",
                    { req_info: "bes_srtinstallatie",
                      niveau: "S",
                      install_key: srtgroep_key,
                      init_key: srtdeel_key
                    },
                  process_info);
    }
}

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

    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 bes_kenmerk_key = $("#id").val();
    var discipline_key = $("#article_catalogue").val();
    var srtgroep_key = $("#article_group").val();
    var srtdeel_key = $("#article").val();

    if (bes_kenmerk_key)
    {
        if (srtdeel_key != -1)
        {
            $("#level").val("S");
            $("#catalogue_key").val(srtdeel_key);
        }
        else if (srtgroep_key != -1)
        {
            $("#level").val("G");
            $("#catalogue_key").val(srtgroep_key);
        }
        else
        {
            $("#level").val("D");
            $("#catalogue_key").val(discipline_key);
        }
    }
    return true;
}