/*
    $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_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)
      {
        alert(L("lcl_obj_value_to_low"));
        obj.focus();
        inslist.action = 'javascript:0';
        return false;
      }
      else if (!isNaN(sk_nmin) && new_val < sk_nmin)
      {
        alert(L("lcl_obj_value_below_nmin"));
        obj.focus();
        inslist.action = 'javascript:0';
        return false;
      }
      else if (!isNaN(sk_nmax) && new_val > sk_nmax)
      {
        alert(L("lcl_obj_value_below_nmax"));
        obj.focus();
        inslist.action = 'javascript:0';
        return false;
      }
      inslist.action = 'ins_search_list.asp';
      return true;
    }
    alert(L("lcl_obj_bad_value"));
    obj.focus();
    inslist.action = 'javascript:0';
    return false;
  }
  else
  {
    // yet, only numerical kenmerktypes are supported
    // 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)
{
  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)
{
  var insKeyString = getKeyString(rowArray);
  if (isMulti || confirm(L("lcl_ins_del_txt_deel")))
  {
     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 insEditInsp(row)
{
  $(row).addClass('dirty');
  var insKeyString = row.getAttribute("ROWKEY");
  var rowdata = eval("(" + row.getAttribute("ROWDATA") + ")");
  if (rowdata)
  {
    srtcontrolekey = rowdata.insSrtControleKey;
    //deellsrtcontkey = rowdata.insDeelSrtContrKey;
    var url = "appl/ins/ins_edit_inspect.asp?ins_keys=" + insKeyString + "&srtcont_keys=" + srtcontrolekey// + "&dlsrtcont_keys=" + deellsrtcontkey;
    FcltMgr.openDetail(url, L("lcl_change"));
  }
}

function insPlan(rowArray)
{
  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 url = "appl/ins/ins_edit_inspect.asp?plan=1&ins_keys=" + insKeyString + "&srtcont_keys=" + srtcontrolekeyArray;
  FcltMgr.openDetail(url, L("lcl_ins_schedule"));
}

function insStart(rowArray, isMulti)
{
  if (isMulti || confirm(L("lcl_ins_starting")))
  {
    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_inspect_start.asp"
          , data
          , FcltCallbackRefresh
          , "json"
          );
  }
}

function insClose(rowArray)
{
  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 url = "appl/ins/ins_edit_inspect.asp?close=1&ins_keys=" + insKeyString + "&srtcont_keys=" + srtcontrolekeyArray;
  FcltMgr.openDetail(url, L("lcl_ins_close"));
}

function insFinish(rowArray, isMulti)
{
  if (isMulti || confirm(L("lcl_ins_finishing")))
  {
    var insKeyString = getKeyString(rowArray);
    var deelsrtcontrkeyArray = new Array();
    for (var i = 0; i < rowArray.length; i++)
    {
      rowdata = eval('(' + rowArray[i].getAttribute("ROWDATA") + ')');
      deelsrtcontrkeyArray[i] = rowdata.insDeelSrtContrKey;
    }

    var data = { ins_keys: insKeyString,
                 dlsrtcont_key_arr: deelsrtcontrkeyArray.join(",")
               };
    protectRequest.dataToken(data);
    $.post( "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});
}
