/*
    $Revision$
    $Id$

    File:           INS/ins_list.js
*/

// FE functions
function insHistory(row)
{
  $(row).addClass('dirty');
  var ins_key = row.getAttribute("ROWKEY");
  var insData = eval('(' + row.getAttribute("ROWDATA") + ')');
  if (insData)
  {
    var insDeelDescr = insData.insDeelDescr;
    var insKenmerkDeelKey = insData.insKenmerkDeelKey;
    var insKenmerkKey = insData.insKenmerkKey;

    var url = "appl/ins/ins_history.asp?urole=" + urole
                  + "&insdeelkey=" + ins_key
                  + "&inskenmerkdeelkey=" + insKenmerkDeelKey
                  + "&inskenmerkkey=" + insKenmerkKey
                  + "&insdeeldescr=" + insDeelDescr
                  + "&consumption_reg=1";
    FcltMgr.openDetail(url, insDeelDescr);
  }
}

function insSave(rowArray)
{
  var insKeyString = getKeyString(rowArray);

  if (insKeyString != "")
  {
    var data = $("#inslist").serialize();
    protectRequest.dataToken(data);
    $.post("../ins/ins_save_use.asp",
           data,
           FcltCallbackRefresh,
           "json");
  }
}

function checkEnter(evt)
{ // e is event object passed from function invocation
  evt = evt || window.event;
  var characterCode = evt.keyCode; // literal character code will be stored in this variable, character code is contained in IE's keyCode property

  if (characterCode == 13) // if generated character code is equal to ascii 13 (if enter key)
    return false;
  else
    return true;
}

function checkInput(evt)
{
  evt = evt || window.event;
  var obj = evt.srcElement || evt.target;
  var new_val = obj.value;
  var sk_kenmerktype = obj.value1;
  //var sk_lengte = parseInt(obj.value2, 10);
  var sk_nmin = parseInt(obj.value3, 10);
  var sk_nmax = parseInt(obj.value4, 10);

  if (sk_kenmerktype == 'N')
  {
    if (new_val == "" ||
      new_val >= 0 ||
      (!isNaN(new_val) && isGoodNumber(new_val,true,true,-1,-1) && parseInt(new_val,10) >= 0))
    {
      i = obj.id.substring(3);
      var last_value = parseInt(document.getElementById("last_val" + i).value,10);
      if (new_val == "") new_val = last_value;
      if (new_val < last_value)
      {
        FcltMgr.alert(L("lcl_obj_value_to_low"));
        obj.focus();
        inslist.action = 'javascript:0';
        return false;
      }
      else if (!isNaN(sk_nmin) && new_val < sk_nmin)
      {
        FcltMgr.alert(L("lcl_obj_value_below_nmin"));
        obj.focus();
        inslist.action = 'javascript:0';
        return false;
      }
      else if (!isNaN(sk_nmax) && new_val > sk_nmax)
      {
        FcltMgr.alert(L("lcl_obj_value_below_nmax"));
        obj.focus();
        inslist.action = 'javascript:0';
        return false;
      }
      inslist.action = 'ins_search_list.asp';
      return true;
    }
    FcltMgr.alert(L("lcl_obj_bad_value"));
    obj.focus();
    inslist.action = 'javascript:0';
    return false;
  }
  else
  {
    // yet, only numerical kenmerktypes are supported
    // FcltMgr.alert(L("lcl_obj_only_num"));
    obj.value = "";
  }
}

// FO en BO functions
function insEdit1(row)
{
    insEdit([row]);
}

// insEdit wordt allen nog maar aangeroepen voor wijzigen van 1 object.
// Voor uitlenen en innemen worden de functies insCheckIn() en insCheckOut() gebruikt.
function insEdit(row)
{
  $(row).addClass('dirty');
  var ins_key = row.getAttribute("ROWKEY");

  if (ins_key > 0)
  {
    var url = 'appl/ins/ins_deel.asp?ins_key=' + ins_key
            + '&urole=' + urole;
    FcltMgr.openDetail(url, "", { reuse: true });
  }
}

function insDelete(rowArray, isMulti)
{
  FcltMgr.confirm(L("lcl_ins_del_txt_deel"), { autoconfirm: isMulti }, function() {
      var insKeyString = getKeyString(rowArray);
      var data = {
          key: insKeyString,
          level: "D"
      };
      protectRequest.dataToken(data);
      $.post("../ins/ins_delete.asp",
          data,
          FcltCallbackRefresh,
          "json");
  });
}

function refreshall(data)
{
  if (data.refresh)
    window.location.reload();
}

//TODO: werkt voor single items. Nu nog voor multi?
function insCheckIn(row)
{
  $(row).addClass('dirty');
  var ins_key = row.getAttribute("ROWKEY");
  FcltMgr.openDetail("appl/INS/ins_deel_free.asp?ins_key=" + ins_key, L("lcl_ins_insinn"));
}

function insCheckOut(row)
{
  $(row).addClass('dirty');
  var ins_key = row.getAttribute("ROWKEY");
  FcltMgr.openDetail("appl/INS/ins_deel_free.asp?ins_key=" + ins_key, L("lcl_ins_lend_out"));
}

// Uitleen flexkenmerken bewerken.
function insAdapt(row)
{
  $(row).addClass('dirty');
  var ins_key = row.getAttribute("ROWKEY");
  FcltMgr.openDetail("appl/INS/ins_deel_free.asp?adapt=1&ins_key=" + ins_key, L("lcl_ins_adapt"));
}

// Een nieuw item uitgeven
function insCheckOutNewItem(prskey, prstype)
{
  FcltMgr.openDetail("appl/INS/ins_deel_free.asp?" + (prstype == "C"? "cps_key=" : "prs_key=") + prskey, L("lcl_ins_lend_out"));
}

function insMultiEdit(rowArray)
{
  var insKeyString = getKeyString(rowArray);
  var subject = L("lcl_ins_multi_edit");
  var url = "../ins/ins_edit_deel_multi.asp?ins_keys=" + insKeyString + "&urole=" + urole;
  FcltMgr.openModalDetail(url, subject, {callback: FcltCallbackRefresh});
}

function insShowInsp(row)
{
  $(row).addClass('dirty');
  var inskey = row.getAttribute("ROWKEY");
  var rowdata = eval("(" + row.getAttribute("ROWDATA") + ")");
  if (rowdata)
  {
    var srtcontrolekey = rowdata.insSrtControleKey;
    var deellsrtcontkey = rowdata.insDeelSrtContrKey;
    var url = "appl/ins/ins_inspect.asp?ins_keys=" + inskey + "&srtcont_keys=" + srtcontrolekey + "&deelsrtcont_key=" + deellsrtcontkey;
    FcltMgr.openDetail(url, L("lcl_change"));
  }
}

function insPlan(rowArray)
{
  var insKeyString = getKeyString(rowArray);
  var srtcontrolekeyArray = new Array();
  var deelsrtcontrkeyArray = new Array();
  for (var i = 0; i < rowArray.length; i++)
  {
    rowdata = eval('(' + rowArray[i].getAttribute("ROWDATA") + ')');
    srtcontrolekeyArray[i] = rowdata.insSrtControleKey;
    // Als je wilt inplannen is er nog geen inspectie record (ins_deelsrtcontrole_key). De insDeelSrtContrKey is dan -2.
    deelsrtcontrkeyArray[i] = rowdata.insDeelSrtContrKey;
  }

  // Als je vanuit de lijst 1 inspectie inplant kun je na afloop naar de show mode van deze inspectie (waarvan er net een inspectie record is aangemaakt).
  // Vooralsnog nog niet gedaan.
  //if (rowArray.length == 1)
  //  var url = "appl/ins/ins_inspect.asp?plan=1&ins_keys=" + insKeyString + "&srtcont_keys=" + srtcontrolekeyArray.join(",") + "&deelsrtcont_keys=" + deelsrtcontrkeyArray.join(",");
  //else
    var url = "appl/ins/ins_edit_inspect.asp?plan=1&ins_keys=" + insKeyString + "&srtcont_keys=" + srtcontrolekeyArray.join(",") + "&deelsrtcont_keys=" + deelsrtcontrkeyArray.join(",");
  FcltMgr.openDetail(url, L("lcl_ins_schedule"));
}

function insStart(rowArray, isMulti)
{
  FcltMgr.confirm(rowArray.length == 1 ? L("lcl_ins_starting") : L("lcl_ins_starting_m"), { autoconfirm: isMulti }, function() {
      var insKeyString = getKeyString(rowArray);
      var srtcontrolekeyArray = new Array();
      for (var i = 0; i < rowArray.length; i++) {
          rowdata = eval('(' + rowArray[i].getAttribute("ROWDATA") + ')');
          srtcontrolekeyArray[i] = rowdata.insSrtControleKey;
      }

      var data = {
          ins_keys: insKeyString,
          srtcont_key_arr: srtcontrolekeyArray.join(",")
      };
      protectRequest.dataToken(data);
      $.post("../ins/ins_inspect_start.asp", data, FcltCallbackRefresh, "json");
  });
}

function insClose(rowArray, isMulti, fromfinish)
{
  var insKeyString = getKeyString(rowArray);
  var srtcontrolekeyArray = new Array();
  var deelsrtcontrkeyArray = new Array();
  for (var i = 0; i < rowArray.length; i++)
  {
    rowdata = eval('(' + rowArray[i].getAttribute("ROWDATA") + ')');
    srtcontrolekeyArray[i] = rowdata.insSrtControleKey;
    deelsrtcontrkeyArray[i] = rowdata.insDeelSrtContrKey;
  }

  // Als je vanuit de lijst 1 inspectie sluit kun je na afloop naar de show mode van deze inspectie (ongeacht of ar al een inspectie record aanwezig was).
  // Vooralsnog nog niet gedaan (ins_inspect.asp dient dan ook nog iets aangepast te worden).
  //if (rowArray.length == 1)
  //  var url = "appl/ins/ins_inspect.asp?close=1&ins_keys=" + insKeyString + "&srtcont_keys=" + srtcontrolekeyArray.join(",") + "&deelsrtcont_key=" + deelsrtcontrkeyArray.join(",");
  //else
    var url = "appl/ins/ins_edit_inspect.asp?close=1&ins_keys=" + insKeyString
            + "&srtcont_keys=" + srtcontrolekeyArray.join(",")
            + "&deelsrtcont_keys=" + deelsrtcontrkeyArray.join(",")
            + (fromfinish
              ? "&fromfinish=1"
              : "");
  FcltMgr.openDetail(url, L("lcl_ins_stop"));
}

function insFinish(rowArray, isMulti)
{
  FcltMgr.confirm(rowArray.length == 1 ? L("lcl_ins_finishing") : L("lcl_ins_finishing_m"),
                  { autoconfirm: isMulti },
                  function()
                  {
                    var insKeyString = getKeyString(rowArray);
                    var deelsrtcontrkeyArray = new Array();
                    // Als er 1 inspectie is die niet Gereed gemeld(5) kan worden dan heb ik de Gereed meld gegevens nodig.
                    // Dan moet ik de gegevens invullen via insClose.
                    var mustClose = false;
                    for (var i = 0; i < rowArray.length; i++)
                    {
                      rowdata = eval('(' + rowArray[i].getAttribute("ROWDATA") + ')');
                      deelsrtcontrkeyArray[i] = rowdata.insDeelSrtContrKey;
                      isc_status = rowdata.ins_status;
                      mustClose = mustClose || (isc_status != 5);
                    }

                    if (mustClose)
                    { // Via insClose scherm de gegevens invullen.
                      insClose(rowArray, isMulti, true);
                    }
                    else
                    {
                      var data = { ins_keys: insKeyString,
                                   dlsrtcont_key_arr: deelsrtcontrkeyArray.join(",") };
                      protectRequest.dataToken(data);
                      $.post("../ins/ins_inspect_finish.asp", data, FcltCallbackRefresh, "json");
                    }
                  });
}

function insMultiMove(rowArray)
{
  var insKeyString = getKeyString(rowArray);
  var subject = L("lcl_ins_multi_move");
  var url = "../ins/ins_move_deel_multi.asp?ins_keys=" + insKeyString + "&urole=" + urole;
  FcltMgr.openModalDetail(url, subject, {callback: FcltCallbackRefresh});
}
