/*
     $Revision$
     $Id$
*/
function updateDisc(purole, pautfunction)
{
  return new Promise((resolve, reject) => {
    if (!$("#discmulti").length) {
      reject();
    }

    var lsrtdisc_key_arr = new Array();
    $("#srtdiscmulti :selected").each(function(i, selected)
                                  {
                                    lsrtdisc_key_arr[i] = $(selected).val();
                                  });
    $("#discmulti").load("../Shared/loadDisc.asp?urole=" + purole
                                                        + "&autfunction=" + pautfunction
                                                        + "&srtdisc_key_arr=" + lsrtdisc_key_arr.join(","),
                         updateStdm(purole, pautfunction).then(resolve));
  });
}

function updateStdm(purole, pautfunction)
{
  return new Promise((resolve, reject) => {
    if (!$("#sm").length) {
      reject();
    }

    var lsrtdisc_key_arr = new Array();
    var ldisc_key_arr = new Array();
    $('#srtdiscmulti :selected').each(function(i, selected)
                                    {
                                      lsrtdisc_key_arr[i] = $(selected).val();
                                    });
    $("#discmulti :selected").each(function(i, selected)
                                  {
                                    ldisc_key_arr[i] = $(selected).val();
                                  });
    $("#sm").load("../Shared/loadStdmelding.asp?urole=" + purole
                + "&autfunction=" + pautfunction
                + (lsrtdisc_key_arr.length != 0? "&srtdisc_key_arr=" + lsrtdisc_key_arr.join(",") : "")
                + (ldisc_key_arr.length != 0? "&disc_key_arr=" + ldisc_key_arr.join(",") : "")
                , $("#mldbord-columns").length && updateBoardColumns(purole, pautfunction).then(resolve));
  });
}

function updateBoardColumns(purole, pautfunction) {
  if (!window.finishBeforeSubmit) {
    window.finishBeforeSubmit = [];
  }
  const boardColumnsPromise = new Promise((resolve, reject) => {
    if (!$("#mldbord-columns").length) {
      reject();
    }
    var lsrtdisc_key_arr = new Array();
    var ldisc_key_arr = new Array();
    var lstdm_key_arr = new Array();
    var lcol_key = $("#mldbord-columns").val() || 0;
    $('#srtdiscmulti :selected').each((i, selected) => {
      lsrtdisc_key_arr[i] = $(selected).val();
    });
    $("#discmulti :selected").each((i, selected) => {
      ldisc_key_arr[i] = $(selected).val();
    });
    $("#sm :selected").each((i, selected) => {
      lstdm_key_arr[i] = $(selected).val();
    });
    $("#mldbord-columns").load("../MLD/loadBoardColumns.asp?urole=" + purole
                            + "&autfunction=" + pautfunction
                            + "&colKey=" + lcol_key
                            + "&srtdisc_key_arr=" + lsrtdisc_key_arr.join(",")
                            + "&disc_key_arr=" + ldisc_key_arr.join(",")
                            + "&stdm_key_arr=" + lstdm_key_arr.join(","),
                            resolve);
  });
  window.finishBeforeSubmit.push(boardColumnsPromise);
  return boardColumnsPromise;
}

function onChangeKpgroep()
{
  // sgKp.setValue($("#kp").val(), $("#kp_show").val(), true, true, null, true);
  // Regel hierboven werkt niet goed omdat CheckExist een get call veroorzaakt om te kijken of waarde bestaat.
  // In de tussentijd verstoort CheckJustOne dit process met een $.getJSON()
  // Dus gewoon Groep leegmaken
  if (sgKp.isMulti())
    // Multi select "Kostenplaats" herladen met de juiste waarden"
    sgKp.updateMultiSuggest();
  else
  {
    sgKp.setValue(-1, "", true, false, null, true);
    sgKp.CheckJustOne();
  }
}

function updateInsSrtGroup(pautfunction, psrtgroup, psrtdeel_arr)
{
  var ldis_key = $('#insdisc :selected').val();
  $("#inssrtgroup").load("../Shared/loadGroup.asp?" + serializeObj(
                         { autfunction:  pautfunction,
                           disc_key_arr: ldis_key,
                           module: "ins",
                           emptyOption: "",
                           psrtgroup: (psrtgroup? psrtgroup : []) }),
                         function ()
                         {
                           // Laad de items
                           // Selecteer eventueel de initiële items die meegegeven kunnen worden
                           updateInsSrtDeel(pautfunction, psrtdeel_arr);
                         });
}

function updateInsSrtDeel(pautfunction, psrtdeel_arr)
{
  var lsrtgroup = $('#inssrtgroup :selected').val();
  $("#inssrtdeelmulti").load("../Shared/loadSrtDeel.asp?" + serializeObj(
                             { autfunction :  pautfunction,
                               srtgroup_key_arr: lsrtgroup,
                               module: "ins",
                               psrtdeel_str: (psrtdeel_arr? psrtdeel_arr : []),
                               emptyOption: "",
                               multi: 1
                             })
                       );
}
