/*
     $Revision$
     $Id$
*/

var has_mld_issuetype_key = false;

$(function ()
{ // initialisatie
  var isOpdrachtKenmerk = (niveau == "P" || niveau == "O");
  has_mld_issuetype_key = false;

  if ($("#issuepropertytype").length > 0)
    $("#issuepropertytype").change(change_issuepropertytype);

  if (!isOpdrachtKenmerk)
  {
    if (niveau == "T")
    {
      $("#disciplinetype").prev().text(" "); // Op disciplinetype wil ik geen multiselect!
      $("#disciplinetype").change(change_disciplinetype);
    }
    else if (niveau == "D")
    {
      $("#discipline_show").attr("sgonChange", "change_discipline");
    }
    else if (niveau == "S")
    {
      $("#mld_issuetype_show").attr("sgonChange", "change_mld_issuetype");
    }
    else if (niveau == "M")
    {
      $("#disciplinetype").prev().text(" "); // Op disciplinetype wil ik geen multiselect!
      $("#disciplinetype").change(change_disciplinetype);
      $("#discipline_show").attr("sgonChange", "change_discipline");
      $("#mld_issuetype_show").attr("sgonChange", "change_mld_issuetype");
      if (mode == "search")
      {
        if (typeof sgdiscipline != "undefined")
          sgdiscipline.reloadUrlAdd([{urlParam: "srtdisc_key", field: "disciplinetype" }]);
        if (typeof sgmld_issuetype != "undefined")
          sgmld_issuetype.reloadUrlAdd([{urlParam: "disc_key", field: "discipline" }]);
      }
    }
  }
  else
  {
    if (!$("#ordertype").length)
    { // Voeg een hidden field toe voor het veld waarop gefilterd is.
      $("<input>").attr({type: "hidden", id: "ordertype", name: "ordertype", value: ordertype_key}).appendTo("form");
    }
  }

  if ($("#issuepropertytype").length > 0)
    change_issuepropertytype();
});

function change_issuepropertytype()
{
  var isOpdrachtKenmerk = (niveau == "P" || niveau == "O");
  var ktype = $("#issuepropertytype option:selected").attr("mld_srtkenmerk_kenmerktype");
  var ktype_lbl_default  = (ktype == "l" || ktype == "b" ? L("mgt_kenmerk_icon") : L("mgt_kenmerk_default"));
  var ktype_lbl_readonly = (ktype == "l" || ktype == "b" ? L("mgt_kenmerk_collapsed") : (isOpdrachtKenmerk ? L("mgt_kenmerk_toonbaar") : L("lcl_mld_kenmerk_readonly") ));
  $("label[for*='default']").html(ktype_lbl_default);
  $("label[for*='readonly']").html(ktype_lbl_readonly);

  if (ktype == "b" || ktype == "l") {
    $("#treatmentteam").closest("tr").show();
    $("textarea#default").closest("tr").hide();
    $("textarea#default").prop('disabled', true);

    if(!$(".iconpicker").length)
      $.getJSON("../mld/get_mld_info_ajax.asp",
        { req_info: "get_icon_picker" },
        setIconPicker);
  }
  else {
    $(".symbol-upload").off("click", iface.upload_symbol);
    $(".symbol-delete").off("click", iface.remove_symbol);
    $(".symbol-iconpicker").off("click", iface.iconpicker);
    $(".iconpicker").closest("tr").remove();
    $("#treatmentteam").closest("tr").hide();
    $("textarea#default").closest("tr").show();
    $("textarea#default").prop('disabled', false);
  }

  if (!isOpdrachtKenmerk) {
    if ($.inArray(ktype, ['R', 'r', 'S']) === -1) {
      $("#in_bord").prop("disabled", true);
      $("#in_bord").closest("div").hide();
    } else {
      $("#in_bord").prop("disabled", false)
                  .prop("checked", ktype !== "S");
      $("#in_bord").closest("div").show();

    }
  }
  FcltMgr.resized();
}

function setIconPicker(data) {
  $("#default").closest("tr").after($(data.iconPicker));
  $(".symbol-upload").on("click", iface.upload_symbol);
  $(".symbol-delete").on("click", iface.remove_symbol);
  $(".symbol-iconpicker").on("click", iface.iconpicker);
  FcltMgr.resized();
}

function change_disciplinetype()
{
  if ((mode == "edit" || mode == "search") && niveau == "M")
  {
    sgdiscipline.setValue(-1, "");
    sgdiscipline.CheckJustOne();
    sgmld_issuetype.setValue(-1, "");
    sgmld_issuetype.CheckJustOne();
  }
  change_discipline();
}

function change_discipline()
{
  var disc_key = $("#discipline").val();
  if (mode == "search" && niveau == "M")
  {
    if (sgmld_issuetype.getValue() == -1)
    {
      sgmld_issuetype.setValue(-1, "", true, false, null, true);
      sgmld_issuetype.CheckJustOne();
    }
  }
  if (disc_key > 0)
  {
    if (typeof sgmld_issuetype != "undefined")
    {
      if (!has_mld_issuetype_key)
      {
        sgmld_issuetype.setValue(-1, "");
        sgmld_issuetype.CheckJustOne();
      }
    }
    $.getJSON("../mld/get_mld_info_ajax.asp",
              { req_info: "mld_discipline",
                mld_ins_discipline_key: disc_key,
                caller_key: -1
              },
              process_disc_info);
  }
  change_mld_issuetype();
}

function process_disc_info(data, textStatus)
{ // Zet disciplinetype indien niet ingevuld
  var d_srtdisc_key = data.ins_srtdiscipline_key;
  var f_srtdisc_key = $("#disciplinetype").val();
  if (f_srtdisc_key <= 0 || (f_srtdisc_key != d_srtdisc_key))
  {
    $("#disciplinetype").val(d_srtdisc_key);
  }
  has_mld_issuetype_key = false;
}

function change_mld_issuetype()
{
  var stdmelding_key = sgmld_issuetype.getValue();
  if (stdmelding_key > 0)
  {
    if ($("#level").find("option[value='N']").length === 0)
    { // In mode=search moet er een level=N worden toegevoegd om kenmerken van alle levels bij een stdmelding te tonen. 
      $("#level").append($("<option>", {value: "N", text: L("lcl_mld_kenmerk_all")}));
    }
    $.getJSON("../mld/get_mld_info_ajax.asp",
              { req_info: "mld_stdmelding",
                mld_stdmelding_key: stdmelding_key,
                caller_key: -1
              },
              process_stdm_info);
  }
  else
  { // Verwijder level=N als er geen stdmelding gekozen is.
    $("#level").find("option[value='N']").remove();
  }
}

function process_stdm_info(data, textStatus)
{ // only if disc suggest is present
  if (typeof sgdiscipline != "undefined")
  {
    // Zet de discipline indien niet ingevuld
    var d_disc_key = data.ins_discipline_key;
    var f_disc_key = $("#discipline").val();
    if (f_disc_key <= 0 || (f_disc_key != d_disc_key))
    {
      has_mld_issuetype_key = true;
      var disc_naam = (data.ins_srtdiscipline_prefix != null ? data.ins_srtdiscipline_prefix + "-" : "")
                    + data.discipline_omschrijving;
      sgdiscipline.setValue(data.ins_discipline_key
                          , disc_naam
                          , true  /*doonChange*/
                          , false /*checkExist*/
                          );
    }
  }
}

function pre_submit()
{
  if (niveau == "O")
  {
    var ordertype = $("#ordertype").val();
    $("#level").val(ordertype > 0? "O" : "P");
  }
  else if (niveau == "M" && (is_clone == 1 || kenmerk_key == -1) )
  {
    var disciplinetype = $("#disciplinetype").val();
    var discipline = $("#discipline").val();
    var mld_issuetype = $("#mld_issuetype").val();
    if (mld_issuetype > 0)
    {
      $("#level").val("S");
      $("#issuetype").val(mld_issuetype);
    }
    else if (discipline > 0)
    {
      $("#level").val("D");
      $("#issuetype").val(discipline);
    }
    else
    {
      $("#level").val(disciplinetype > 0? "T" : "A");
      $("#issuetype").val(disciplinetype); // Als discipline "-1" is dan geldt het kenmerk voor alle objecten.
    }
  }

  return true;
}