/*
     $Revision$
     $Id$
*/

$(function ()
{   // initialisatie
    $("#disciplinetype").change( change_disciplinetype );
    $("#discipline").change( change_discipline );
    $("#autoorder").change( onChangeAutoorder );
    $("#autoorderamount").change( onChangeAutoorderAmount );
    $("#accepttime").change( onChangeAccepttime );
    $("#criticalaccept").change( onChangeCriticalAccepttime );
    $("#highpriorityaccept").change( onChangeHighprioAccepttime );
    $("#lowpriorityaccept").change( onChangeLowprioAccepttime );
    $("#criticaltime").change( onChangeCriticaltime );
    $("#highprioritytime").change( onChangeHighprioritytime );
    $("#lowprioritytime").change( onChangeLowprioritytime );
    $("#plannable").change( onChangePlannable );

    onChangeAccepttime();
    onChangeCriticalAccepttime();
    onChangeHighprioAccepttime();
    onChangeLowprioAccepttime();
    onChangeCriticaltime();
    onChangeHighprioritytime();
    onChangeLowprioritytime();
    onChangePlannable();

    var srtdisc_key = (typeof $("#disciplinetype").val() == "undefined" ? (typeof disciplinetype_key == "undefined" ? -1 : disciplinetype_key) : $("#disciplinetype").val() );
    var disc_key    = (typeof $("#discipline").val()     == "undefined" ? (typeof discipline_key     == "undefined" ? -1 : discipline_key)     : $("#discipline").val() );

    var init_srtdisc_key = (srtdisc_key > -1 ? srtdisc_key : 0);  // defaultwaarde 0 zorgt ervoor dat eerst de vakgroeptype moet worden gekozen voordat er een vakgroep kan worden geselecteerd.
    var init_disc_key = (disc_key ? disc_key : -1);

    fill_disciplineselector(init_srtdisc_key, init_disc_key);
    fill_issuetypeselector(0, 0);
});

function onChangeAccepttime()
{
    $("#accepttime_eenheid").prop("disabled", isNaN(parseFloat($("#accepttime").val())));
}
function onChangeCriticalAccepttime()
{
    $("#criticalaccept_eenheid").prop("disabled", isNaN(parseFloat($("#criticalaccept").val())));
}
function onChangeHighprioAccepttime()
{
    $("#highpriorityaccept_eenheid").prop("disabled", isNaN(parseFloat($("#highpriorityaccept").val())));
}
function onChangeLowprioAccepttime()
{
    $("#lowpriorityaccept_eenheid").prop("disabled", isNaN(parseFloat($("#lowpriorityaccept").val())));
}
function onChangeCriticaltime()
{
    $("#criticaltime_eenheid").prop("disabled", isNaN(parseFloat($("#criticaltime").val())));
}
function onChangeHighprioritytime()
{
    $("#highprioritytime_eenheid").prop("disabled", isNaN(parseFloat($("#highprioritytime").val())));
}
function onChangeLowprioritytime()
{
    $("#lowprioritytime_eenheid").prop("disabled", isNaN(parseFloat($("#lowprioritytime").val())));
}
function onChangePlannable()
{
    switch ($("#plannable").val())
    {
        case "0": $("#plannablefrontend").off("click");
                  break;
        case "1": $("#plannablefrontend").on("click", _ => { return false; });
                  $("#plannablefrontend").prop("checked", true);
                  break;
        case "2": $("#plannablefrontend").on("click", _ => { return false; });
                  $("#plannablefrontend").prop("checked", false);
                  break;
    }
}


function change_disciplinetype()
{
    var srtdisc_key = $("#disciplinetype").val();

    if (srtdisc_key != -1)
        fill_disciplineselector(srtdisc_key, -1);
    else
        process_stdminfo({niveau:"D",lov:[]});
    process_stdminfo({niveau:"M",lov:[]});
}

function change_discipline()
{
    var srtdisc_key = $("#disciplinetype").val();
    var disc_key = $("#discipline").val();

    if (disc_key != -1)
        fill_issuetypeselector(srtdisc_key, disc_key);
    else
        process_stdminfo({niveau:"M",lov:[]});
}

function onChangeAutoorder()
{
  if ($("#autoorder").prop("checked"))
    $("#autoorderamount").val("0");
  else
    $("#autoorderamount").val("");
}

function onChangeAutoorderAmount()
{
  if (!$("#autoorder").prop("checked"))
    $("#autoorderamount").val("");
  else if ($("#autoorderamount").val() == 0)
    $("#autoorderamount").val("0")
}

function fill_disciplineselector(srtdisc_key, disc_key)
{
    $.getJSON( "../mld/get_mld_info_ajax.asp"
             , { req_info: "mld_stdmelding_list"
               , niveau: "D"
               , parent_key: srtdisc_key
               , init_key: disc_key
               }
             , process_stdminfo
             );
}

function fill_issuetypeselector(srtdisc_key, disc_key)
{
    $.getJSON( "../mld/get_mld_info_ajax.asp"
             , { req_info: "mld_stdmelding_list"
               , niveau: "M"
               , parent_key: srtdisc_key
               , child_key: disc_key
               , init_key: -1
               }
             , process_stdminfo
             );
}

function process_stdminfo(data)
{
    var select;
    switch(data.niveau)
    {
        case "D":  select = $("#discipline"); break;
        case "M":  select = $("#issuetype"); break;
    }
    var add_data = "";
    var sel_key = [];
    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];
            add_data = ' srtdisc_key="' + opt.atr.srtdisc_key + '"'
                     + ' disc_key="'    + opt.atr.disc_key + '"';
            new_lov += '<option value="' + opt.key + '"' + (opt.sel?" selected":"") + add_data + '>'+ opt.oms + '</option>';
            if (opt.sel)
                sel_key.push(opt.key);
        }
        select.prop("disabled", false);
        if (select.prop("multiple"))
            select.prop("size", Math.min(data.lov.length+1, 8));
    }
    else
    {
        select.prop("disabled", true);
    }
    select.empty().append(new_lov);
    if (sel_key.length)
        select.val(sel_key);
}