/*
     $Revision$
     $Id$
*/

//function OrderItem(srtdeel_key, amount, dis_key, price, eenheid, veelvoud, received, retour, rowIndex, item_key)
function OrderItem(prs_key, prs_name, dept_name, rowIndex)
{
  this.prs_key = prs_key;
  this.prs_name = prs_name;
  this.dept_name = dept_name;
  this.rowIndex = rowIndex;

  this.row = null;
}

function PrsRow(prs_key, prs_text, lastRow)
{
  this.prs_key = prs_key;
  this.prs_text = prs_text;
  this.lastRow = lastRow;

  this.row = null;
}

// Array of currently ordered items
var selectedItems = new Array();
var nrdeleted = 0;  // aantal regels die tijdens invoer van een bestelling zijn verwijderd

var prsRows = new Array();

var hook_url = "";

function AddItem(pprs_key, pprs_name, pdept_name)
{
  receiveInfo = false;
  if (!isNaN(pprs_key) && pprs_key > 0) receiveInfo = true;

  if (this.dis_desc == null) dis_desc = "";

  table = document.getElementById("sel_items");

  if (!receiveInfo && document.getElementById("person").value <= 0)
  {
    FcltMgr.alert(L("lcl_shared_choose_item_first"));
    return;
  }

  if (!receiveInfo) {
    prs_key = $("#person").val();
    prs_name = $("#person_show").val();
    dept_name = $("#dept").val();
  }
  else {
    prs_key = pprs_key;
    prs_name = pprs_name;
    dept_name = pdept_name;
  }

  var table = document.getElementById("sel_items")
  var rows = document.getElementById('sel_items').getElementsByTagName('tr')
  var tablelength = rows.length;
  var rowIndex = rows[rows.length-1].rowIndex;  // Pak de laatste rowIndex van de tabel
  var next_rowIndex = ++rowIndex;

  // Om het niet te gek te maken beperken we vooralsnog maar eens tot vijf FE vervangers max
  if (tablelength - 1 >= 5) {
    FcltMgr.alert(L("lcl_prs_substitute_max5"));
    return;
  }

  var registerOK = RegisterItem(prs_key, prs_name, dept_name, next_rowIndex, true);

  if (registerOK) CreateRow(prs_key, prs_name, dept_name, next_rowIndex, true);
}

function setInnerText(innerObject, innerValue)
{
	if (typeof innerObject.innerText != 'undefined')
	  innerObject.innerText = innerValue
	else
		innerObject.textContent = innerValue;
  return innerObject;
}

function CreateRow(prs_key, prs_name, dept_name, rowIndex, editable)
{
  // insert white line
  table = document.getElementById("sel_items");

  // insert data rows
  tr = table.insertRow((rowIndex == -1) ? (-1) : (rowIndex));
  var id_key = prs_key;
  selectedItems[" " + id_key].row = tr;

  // Create drop button
  cell = tr.insertCell(-1);
  cell.className = "result";
  cell.align = "center";
  cell.innerHTML = "<i class='fa fa-fw fa-trash' OnClick=CancelOrder('"
                 + id_key + "') style=cursor:pointer></i>";

  // Create order prs_name field
  cell = tr.insertCell(-1);
  cell.className = "result";
  // innerHTML opent een htmlparser die overbodige spaties en line breaks verwijderd in IE, daarom met innerText oplossen
  // Input velden moeten wel met innerHTML of outerHTML anders wordt het text
  cell = setInnerText(cell, prs_name);
  cell.title = prs_name;
  cell.align = 'left';

  // Create order dept_name field
  cell = tr.insertCell(-1);
  cell.className = "result";
  cell = setInnerText(cell, dept_name);
  cell.title = dept_name;
  cell.align = 'left';
}

//function RegisterItem(srtdeel_key, amount, dis_key, price, eenheid, veelvoud, received, retour, dis_desc, editable, item_key)
function RegisterItem(prs_key, prs_name, dept_name, rowIndex, editable)
{
  // Indien al in de lijst toegevoegd, dan melding geven en return
  if (selectedItems[" " + prs_key] != null) {
    FcltMgr.alert(L("lcl_prs_substitute_inlist"));
    return false;
  }

  selectedItems[" " + prs_key] = new OrderItem(prs_key, prs_name, dept_name, rowIndex);
  return true;
}

function CancelOrder(id_key)
{
  FcltMgr.confirm(L("lcl_prs_cancelSubstitute"), function() {
      if (selectedItems[" " + id_key] != null)
      {
          tr = selectedItems[" " + id_key].row;
          rowIndex = tr.rowIndex;
          document.getElementById("sel_items").deleteRow(rowIndex);
          nrdeleted++;
          selectedItems[" " + id_key] = null;
      }
  });
}

function SaveSubstitutes()
{
  // Form substitutes_string of prs_keys
  var substitutes_string = "";
  for (i in selectedItems)
  {
    if (selectedItems[i] != null)
    {
      prs_key = selectedItems[i].prs_key;
      if (!isNaN(prs_key) && prs_key > 0) {
        substitutes_string += prs_key + ",";
      }
    }
  }

  substitutes_string = substitutes_string.substring(0, substitutes_string.length - 1);
  document.forms.u2.substitutes_string.value = substitutes_string;

  window.document.u2.submit();
  return true;
}



