/*
     $Revision$
     $Id$

     File:               fac_typeopdrfaq.js
*/

var rowIndex = -1;

function CreateRow(typeopdr_key, typeopdr)
{
  rowIndex++;
  var table = $("#typeopdrfaqtable>tbody")[0];

  // Insert data rows
  tr = table.insertRow(-1);
  tr.myData = { typeopdr_key: typeopdr_key };
  tr.value = rowIndex;

  // Create drop button
  cell = tr.insertCell(-1);
  cell.className = "result";
  cell.align = "center";
  cell.innerHTML = "<span OnClick='delRow(this)' style='cursor:pointer'>" + I("fa-trash-alt") + "</span>";

  // 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

  // Opdrachttype omschrijving veld
  cell = tr.insertCell(-1);
  cell.className = "result";
  cell.innerHTML = typeopdr;
  cell.align = "left";
  cell.style.whiteSpace = "nowrap";

  // Data velden
  cell = tr.insertCell(-1);
  cell.className = "result";
  cell.innerHTML = "<input type='hidden' id='typeopdr" + rowIndex + "' name='typeopdr" + rowIndex + "' value='" + typeopdr_key + "'>";
  cell.align = 'left';

  document.getElementById("nrRows").value = parseInt(rowIndex) + 1;
}

function delRow(img)
{
  // Verwijder de aangeklikte afdeling uit de lijst (de regel)
  FcltMgr.confirm(L("lcl_faq_del_typeopdr"), function() {
      var tr = $(img).closest("tr")[0];
      if (tr.myData.typeopdr_key != null) {
          document.getElementById("typeopdrfaqtable").deleteRow(tr.rowIndex);
          FcltMgr.resized(window);
      }
  });
}

function fac_submit()
{
  var data = $("[name=u2]").serialize();
  $.post($("form[name=u2]")[0].action,
         data,
         FcltCallbackAndThenAlways(fac_submit_callback),
         "json");
  return true; // disabled button ook
}

function fac_submit_callback(json)
{
  if (json.success)
  {
    window.parent.FcltMgr.reload();
    FcltMgr.closeDetail(window, json);
  }
}

function fac_add()
{
  $("#opdrtype option:selected").each(function () {
    fac_check_and_add($(this).val(), $(this).text());
  });
}

function fac_check_and_add(typeopdr_val, typeopdr_show_val)
{
    var exist = false;

    $("#typeopdrfaqtable tr").each(function(i, value)
                              {
                                if (this.myData
                                   && (typeopdr_val == this.myData.typeopdr_key)
                                   )
                                {
                                  exist = true;
                                }
                              });
    if (!exist)
    {
      CreateRow( typeopdr_val, typeopdr_show_val );
      FcltMgr.resized(window);
    }
}


