/*
     $Revision$
     $Id$
*/

$(function ()
{ // initialisatie
  // NB: niveau is niet altijd aanwezig
  var isTaakKenmerk = ( (typeof niveau == "undefined") ? false : (niveau == "T" || niveau == "E" || niveau == "C") );
  if (isTaakKenmerk)
  {
    if (niveau == "E")
    {
      $("#taskdiscipline").prev().text(" "); // Op taskdiscipline (ctr_discipline) wil ik geen multiselect!
      $("#taskdiscipline").change(change_taskdiscipline);
    }
    else if (niveau == "C")
    {
      $("#task_show").attr("sgonChange", "change_task");
    }
    else if (niveau == "T")
    {
      $("#taskdiscipline").prev().text(" "); // Op taskdiscipline (ctr_discipline) wil ik geen multiselect!
      $("#taskdiscipline").change(change_taskdiscipline);
      $("#task_show").attr("sgonChange", "change_task");
      if (mode == "search")
      {
        if (typeof sgtask != "undefined")
          sgtask.reloadUrlAdd([{urlParam: "ctrdisc_key_arr", field: "taskdiscipline" }]);
      }
    }
  }
  else
  {
    $("#objectdiscipline_show").attr("sgonchange", "change_object_discipline");  // mld_stdmelding gebruikt INS_DISCIPLINE (suggestbox) als foreign.
    $("#objectdiscipline").change(function() {change_object_discipline(); });
    $("#objectgroup").change(function() {change_object_group(); });

    var srtinstallatie_key = $("#objectkey").val();
    if (!srtinstallatie_key)
    {
        if (mode == "search")
        {
          fill_discipline(-1);
          change_object_discipline(); // Initialisatie listboxen bij search.
        }
    }
    else
    {
      var discipline_key = $("#objectdiscipline").val();
      var srtgroep_key = $("#objectgroup").val();
      var srtdeel_key = $("#objecttype").val();

      fill_discipline(discipline_key);
      if (typeof niveau === "undefined")
      {
        if (srtdeel_key > 0 )
        {
          var niveau = "S";
        }
        else if (srtgroep_key > 0)
        {
          var niveau = "G";
        }
        else
        {
          var niveau = "D";
        }
      }

      if (niveau == "D")
      {
          fill_srtgroep(discipline_key);
          $("#objecttype").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_taskdiscipline()
{
  if ((mode == "edit" || mode == "search") && niveau == "T")
  {
    sgtask.setValue(-1, "");
    sgtask.CheckJustOne();
  }
}

function change_task()
{
  var srtcontrole_key = sgtask.getValue();
  if (srtcontrole_key > 0)
  {
    $.getJSON("../ins/get_ins_info_ajax.asp",
              { req_info: "ins_srtcontrole",
                srtcontrole_key: srtcontrole_key
              },
              process_task_info);
  }
}

function process_task_info(data, textStatus)
{ // only if disc suggest is present
  if ($("#taskdiscipline"))
  {
    // Zet de taskdiscipline (ctrdiscipline) indien niet ingevuld
    var d_disc_key = data.disc_key;
    var f_disc_key = $("#taskdiscipline").val();
    if (f_disc_key <= 0 || (f_disc_key != d_disc_key))
    {
      //has_mld_issuetype_key = true;
      $("#taskdiscipline").val(d_disc_key)
    }
  }
}

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

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

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

    var discipline_key = $("#objectdiscipline").val();
    var srtgroep_key = $("#objectgroup").val();
    fill_srtdeel(discipline_key, srtgroep_key);
}

function fill_discipline(discipline_key)
{
    $.getJSON("../ins/get_ins_info_ajax.asp",
                { req_info: "ins_srtinstallatie",
                  niveau: "D",
                  install_key: -1,
                  init_key: discipline_key,
                  available: srtinstallatieselector_available
                },
              process_info);
}

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 = null;
    switch(data.niveau)
    {
        case "D": select = $("#objectdiscipline"); break;
        case "G": select = $("#objectgroup"); break;
        case "S": select = $("#objecttype"); break;
    }

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

    if (sel_key > -1)
        select.val(sel_key);
}

function pre_submit()
{ // NB: niveau is niet altijd aanwezig
  var isTaakKenmerk = ( (typeof niveau == "undefined") ? false : (niveau == "T" || niveau == "E" || niveau == "C") );
  if (isTaakKenmerk)
  {
    var disc_key = $("#taskdiscipline").val();
    var srtcontrole_key = $("#task").val();

    if (disc_key) // discipline_key en srtcontrole_key zijn readonly, dan srtinstallatiekey laten staan.
    {
      if (srtcontrole_key > -1)
      {
        $("#level").val("C");
        $("#objectkey").val(srtcontrole_key);
      }
      else
      {
        $("#level").val(disc_key > 0? "E" : "T");
        $("#objectkey").val(disc_key);
      }
    }
  }
  else
  {
    var discipline_key = $("#objectdiscipline").val();
    var srtgroep_key = $("#objectgroup").val();
    var srtdeel_key = $("#objecttype").val();

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

  return true;
}