/*
     $Revision$
     $Id$

     File:        cnt_edit_scope.js
*/

var highlitedRowObj = -1;
var highlitedRowObjSort = -1;
var highlitedRowLoc = -1;

function cnt_submit()
{
  document.activeElement.blur(); // trigger laatste onChanges
  if (!validateForm("u2"))
      return false;
  document.forms.u2.submit(); //TODO zie alg!
}

function cnt_cancel()
{
  FcltMgr.closeDetail(window, { cancel: true } );
}

function updateBld(pautfunction)
{
  var locatie_key_arr = new Array();
  $('#locs :selected').each(function(i, selected)
                            {
                              locatie_key_arr[i] = $(selected).val();
                            });
  $("#selectblddiv").load("../Shared/loadGebouw.asp",
                          { autfunction: + pautfunction,
                            autlevel: PRSreadlevel,
                            terrein: 1,
                            locatie_key_arr: (locatie_key_arr.length != 0 ? locatie_key_arr : [-1])  // -1 is ook een geldige waarde
                          });
}

function showObjDetail(evt)
{
  evt = evt || window.event;
  var obj = evt.srcElement || evt.target;
  var obj_str = "";
  var obj_key = obj.value;
  if (obj_key && obj_key > 0 && $("#objs").length)
  {
    var url = "appl/INS/ins_show_deel.asp?ins_key=" + obj_key + "&urole=bo";
    $('#objs :selected').each(function(i, selected)
                              {
                                if ($(selected).val() == obj_key)
                                  obj_str = $(selected).text();
                              });
    FcltMgr.openDetail(url, L("lcl_cnt_object") + " " + obj_str);
  }
}

function updateRoomObj()
{
  $("#objs").load("../cnt/loadRuimteObjecten.asp",
                  { locatiekey: $("#locatiekey").val(),
                    gebouwkey: $("#gebouwkey").val(),
                    bttype: $("#bttype").val(),
                    verdiepingkey: $("#verdiepingkey").val(),
                    ruimtekey: $("#ruimtekey").val(),
                    prs_key: $("#sName_key").val(),
                    dept_key: $("#sDept_key").val()
                  });
}

function onAfdelingChange()
{ // on change of select box persons or organisation it empties place select boxes
  emptyPlace();
  if ($("#sDept_key").val() > 0 && $("#sDept_key").val() != $("#prsafd_key").val())
    emptyPersoon();
}

function process_afdeling(data, textStatus)
{
    if (data)
    {
      // setValue(key, txt, doonChange, checkExist, extraParam, lastTry)
      sgDept.setValue(data.afd_key, data.afdeling, false, true);
      updateRoomObj();
    }
}

function onPersoonChange()
{ // on change of select box persons or organisation it empties place select boxes
  emptyPlace();

  if ($("#prsafd_key").val() > 0)
    $.getJSON("./get_cnt_info_ajax.asp",
              { req_info: "afdeling",
                afd_key: $("#prsafd_key").val() },
              process_afdeling);
}

function onPlaatsChange()
{ // on change of place select boxes it empties person and organisation select box
  emptyAfdeling();
  emptyPersoon();
  updateRoomObj();
}

function emptyPersoon()
{
  sgPerson.setValue(-1, "");
}

function emptyAfdeling()
{
  sgDept.setValue(-1, "");
}

function emptyPlace()
{ // on change of select box persons or organisation it empties place select boxes
  if ($("#locatie").length)
  {
    // setValue(key, txt, doonChange, checkExist, extraParam, lastTry)
    sgLoc.setValue(-1, "");
    sgBld.setValue(-1, "");
    sgFlr.setValue(-1, "");
    sgRoom.setValue(-1, "");
  }

  updateRoomObj();
}

function updateGroup(pautfunction)
{
  var disc_key_arr = new Array();
  $('#disc :selected').each(function(i, selected)
                            {
                              disc_key_arr[i] = $(selected).val();
                            });
  if (disc_key_arr.length != 0)
    $("#srtgroup").load("../Shared/loadGroup.asp",
                        { autfunction: pautfunction,
                          disc_key_arr: (disc_key_arr.length != 0 ? disc_key_arr.join(",") : ""),
                          module: "ins",
                          multi: 1,
                          size: 10,
                          selectfirstone: 1 },
                        function ()
                        { // Laad de items
                          updateSrtDeel(pautfunction);
                        });
  else
    $("#srtgroup").empty();
}

function updateSrtDeel(pautfunction)
{
  var group_key_arr = new Array();
  $('#srtgroup :selected').each(function(i, selected)
                             {
                               group_key_arr[i] = $(selected).val();
                             });
  if (group_key_arr.length != 0)
    $("#srtdeelmulti").load("../Shared/loadDeel.asp",
                            { autfunction: pautfunction,
                              srtgroup_key_arr: (group_key_arr != 0 ? group_key_arr.join(",") : ""),
                              module: "ins",
                              multi: 1,
                              size: 10,
                              selectfirstone: 1
                            });
  else
    $("#srtdeelmulti").empty();
}

function removeObj()
{
  if (highlitedRowObj > 0)
  {
    deletingRow = "row" + highlitedRowObj;
    index = document.getElementById(deletingRow).rowIndex;
    document.getElementById("cntobjscope_list").deleteRow(index);
    highlitedRowObj = -1;
  }
}

function removeObjSort()
{
  if (highlitedRowObjSort > 0)
  {
    deletingRow = "row" + highlitedRowObjSort;
    index = document.getElementById(deletingRow).rowIndex;
    document.getElementById("cntobjsortscope_list").deleteRow(index);
    highlitedRowObjSort = -1;
  }
}

function removeLoc()
{
  if (highlitedRowLoc > 0)
  {
    deletingRow = "row" + highlitedRowLoc;
    index = document.getElementById(deletingRow).rowIndex;
    document.getElementById("cntlocscope_list").deleteRow(index);
    highlitedRowLoc = -1;
  }
}

function removeAllObj()
{
  document.getElementById("objpass").innerHTML = "<TABLE border=0 cellpadding=0 width=100% cellspacing=1 id='cntobjscope_list'></TABLE>";
  document.getElementById("objcnt").value = 0;
}

function removeAllObjSort()
{
  document.getElementById("ospass").innerHTML = "<TABLE border=0 cellpadding=0 width=100% cellspacing=1 id='cntobjsortscope_list'></TABLE>";
  document.getElementById("oscnt").value = 0;
}

function removeAllLoc()
{
  document.getElementById("locpass").innerHTML = "<TABLE border=0 cellpadding=0 width=100% cellspacing=1 id='cntlocscope_list'></TABLE>";
  document.getElementById("loccnt").value = 0;
}

function addObj()
{
  var object_key_str = new Array();
  var object_text_str = new Array();
  //var gebouw_loctext_str = new Array();
  $('#objs :selected').each(function(i, selected)
                            {
                              object_key_str[i] = $(selected).val();
                              object_text_str[i] = $(selected).text();
                              //gebouw_loctext_str[i] = $(selected)[0].loc_text;
                            });

  for (var i = 0; i < object_key_str.length; i++)
  {
    if (!itemObjExist( document.getElementById("cntobjscope_list"), object_key_str[i])) // && object_key_str[i] != -1)
    {
      insertObjRow(object_text_str[i], object_key_str[i]);
    }
  }
}

function addObjSort()
{
  var sorts_key_str = new Array();
  var sorts_text_str = new Array();
  $('#srtdeelmulti :selected').each(function(i, selected)
                             {
                               sorts_key_str[i] = $(selected).val();
                               sorts_text_str[i] = $(selected).text();
                             });
  for (var i = 0; i < sorts_key_str.length; i++)
  {
    if (!itemObjSortExist(document.getElementById("cntobjsortscope_list"), sorts_key_str[i]))
    {
      insertObjSortRow(sorts_text_str[i], sorts_key_str[i]);
    }
  }
}

function addLoc()
{
  var gebouw_key_str = new Array();
  var gebouw_text_str = new Array();
  var gebouw_loctext_str = new Array();
  var gebouw_bttype_str = new Array();
  $('#blds :selected').each(function(i, selected)
                            {
                              gebouw_key_str[i] = $(selected).val();
                              gebouw_text_str[i] = $(selected).text();
                              gebouw_loctext_str[i] = $(selected)[0].getAttribute("loc_text");
                              gebouw_bttype_str[i] = $(selected)[0].getAttribute("bttype");
                            });
  var gebouw_str = "" + gebouw_key_str;

  if (gebouw_str == "-1")
  { // Building is not selected so insert the locations
    var locatie_key_str = new Array();
    var locatie_text_str = new Array();
    $('#locs :selected').each(function(i, selected)
                              {
                                locatie_key_str[i] = $(selected).val();
                                locatie_text_str[i] = $(selected).text();
                              });

    for (var i = 0; i < locatie_key_str.length; i++)
    {
      if (!itemLocExist(document.getElementById("cntlocscope_list"), locatie_key_str[i]))
      {
        insertLocRow(locatie_text_str[i], locatie_key_str[i], "L");
      }
    }
  }
  else
  {
    for (var i = 0; i < gebouw_key_str.length; i++)
    {
      if (!itemBldExist( document.getElementById("cntlocscope_list"), gebouw_loctext_str[i], gebouw_text_str[i], gebouw_bttype_str[i]) && gebouw_key_str[i] != -1)
      {
        insertLocRow(gebouw_loctext_str[i] + " (" + gebouw_text_str[i] + ")", gebouw_key_str[i], gebouw_bttype_str[i]);
      }
    }
    //TODO: Wellicht zijn er nog locaties geselecteerd waarvan geen gebouwen zijn geselecteerd dan worden ze nu niet overgezet
    //      Moeten die wel overgezet worden? Discussie!
  }
}

function itemObjExist(tbl, key)
{
  if (!tbl.rows)
    return true;

  for (var i = 0; i < tbl.rows.length; i++)
  {
    var j = parseInt(tbl.rows[i].id.substr(3), 10) - 1000;
    var objkey = $("#kd" + j).val();
    if (objkey == key)
      return true;
  }
  return false;
}

function itemObjSortExist(tbl, key)
{
  if (!tbl.rows)
    return true;

  for (var i = 0; i < tbl.rows.length; i++)
  {
    var j = parseInt(tbl.rows[i].id.substr(3), 10) - 1000;
    var srtkey = $("#skd" + j).val();
    if (srtkey == key)
      return true;
  }
  return false;
}

function itemLocExist(tbl, key)
{
  if (!tbl.rows)
    return true;

  for (var i = 0; i < tbl.rows.length; i++)
  {
    var j = tbl.rows[i].id.substr(3);
    var lockey = $("#akd" + j).val();
    if (lockey == key)
      return true;
  }
  return false;
}

function itemBldExist(tbl, loc_itm, bld_itm, bttype)
{
  if (!tbl.rows)
    return true;

  var locbld = loc_itm  + " (" + bld_itm + ")";
  for (var i = 0; i < tbl.rows.length; i++)
  {
    j = tbl.rows[i].id.substr(3);
    loctxt = $("#aks" + j).val();
    loctyp = $("#akt" + j).val();
    if (((loctxt == loc_itm) || (loctxt == locbld)) && (loctyp == bttype))
      return true;
  }
  return false;
}

function selectObjRow(rownr)
{
  if (highlitedRowObj > 0)
    $("#row" + highlitedRowObj).removeClass("highlite");
  $("#row" + rownr).addClass("highlite");
  highlitedRowObj = rownr;
}

function selectObjSortRow(rownr)
{
  if (highlitedRowObjSort > 0)
    $("#row" + highlitedRowObjSort).removeClass("highlite");
  $("#row" + rownr).addClass("highlite");
  highlitedRowObjSort = rownr;
}

function selectLocRow(rownr)
{
  if (highlitedRowLoc > 0)
    $("#row" + highlitedRowLoc).removeClass("highlite");
  $("#row" + rownr).addClass("highlite");
  highlitedRowLoc = rownr;
}

function insertObjRow(name, key)
{
  var j = parseInt($("#objcnt").val(), 10) + 1;
  html = "<tr id='row" + (j + 1000) + "' OnClick='javascript:selectObjRow(" + (j + 1000) + ");'>"
       + "<td class=selectable align=left id=obj>" + name + "</td>"
       + "<td><input type=hidden id=k" + j + " name=k" + j + " value=-1>"
       + "<input type=hidden id=kd" + j + " name=kd" + j + " value='" + key  + "'>"
       + "<input type=hidden id=ks" + j + " name=ks" + j + " value='" + name + "'>"
       + "</td></tr>";
  $("#cntobjscope_list").append(html);
  $("#objcnt").val(j);
}

function insertObjSortRow(name, key)
{
  var j = parseInt($("#oscnt").val(), 10) + 1;
  var html = "<tr id='row" + (j + 1000) + "' OnClick='javascript:selectObjSortRow(" + (j + 1000) + ");'>"
           + "<td class=selectable align=left id=obsort>" + name + "</td>"
           + "<td><input type=hidden id=sk" + j + " name=sk" + j + " value=-1>"
           + "<input type=hidden id=skd" + j + " name=skd" + j + " value='" + key  + "'>"
           + "<input type=hidden id=sks" + j + " name=sks" + j + " value='" + name + "'>"
           + "</td></tr>";
  $("#cntobjsortscope_list").append(html);
  $("#oscnt").val(j);
}

function insertLocRow(name, key, type)
{
  var j = parseInt($("#loccnt").val(), 10) + 1;
  var html = "<tr id='row" + j+ "' OnClick='javascript:selectLocRow(" + j + ");'>"
           + "<td class='selectable' align='left' id='loc'>" + name + "</td>"
           + "<td class='selectable'>"
           + "<input type='text' size='10' maxlength='12' id='akn" + j + "' name='akn" + j + "' onBlur='checkWeight(event);'>"
           + "<input type='hidden' id='ak"  + j + "' name='ak"  + j + "' value='-1'>"
           + "<input type='hidden' id='akd" + j + "' name='akd" + j + "' value='" + key  + "'>"
           + "<input type='hidden' id='aks" + j + "' name='aks" + j + "' value='" + name + "'>"
           + "<input type='hidden' id='akt" + j + "' name='akt" + j + "' value='" + type + "'>"
           + "</td></tr>";
  $("#cntlocscope_list").append(html);
  $("#loccnt").val(j);
}

function checkWeight(evt)
{
  evt = evt || window.event;
  var obj = evt.srcElement || evt.target;
  var ival = parseInt(obj.value,10);
  var retVal = false;
  if (( !isNaN(ival) && Math.abs(ival) < 1000000000) || obj.value =="")
    retVal = true;
  if ( !retVal )
  {
    FcltMgr.alert(L("lcl_cnt_invalid_kenmerk_value"), { fnafterclose: function () {obj.focus()} });
  }
  return retVal;
}

function RowData(cokey, sdkey)
{
  this.cokey = cokey;
  this.sdkey = sdkey; // soortdeel
}

function RowDataLoc(cpkey, lkey)
{
  this.cpkey = cpkey;
  this.lkey = lkey;
}
