/*
     $Revision$
     $Id$

     File:               fac_stdmeldingfaq.js
*/

var rowIndex = -1;
var multiFAQ = true;

function onClickItem(tr)
{
  // Wordt aangeroepen na één klik op een item
  $(tr).toggleClass("selected");
}

function onDblClickItem(tr)
{
    $(tr).closest("table").find("tr").removeClass("selected"); // Alle andere regels deselecteren
    $(tr).addClass("selected");
    window.parent.doSubmit();
}

function setMultiFAQ(aantal)
{
	multiFAQ = (aantal != 1);
}

function CreateRow(disc_key, stdm_key, vakgroep, stdmelding)
{
  rowIndex++;
  var table = $("#stdmeldingfaqtable>tbody")[0];

  // Insert data rows
  tr = table.insertRow(-1);
  tr.myData = { disc_key: disc_key
              , stdm_key: stdm_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

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

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

  // Data velden
  cell = tr.insertCell(-1);
  cell.className = "result";
  cell.innerHTML = "<input type='hidden' id='disc" + rowIndex + "' name='disc" + rowIndex + "' value='" + disc_key + "'>"
                 + "<input type='hidden' id='stdm" + rowIndex + "' name='stdm" + rowIndex + "' value='" + stdm_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_stdmelding"), function() {
      var tr = $(img).closest("tr")[0];
      if (tr.myData.disc_key != null) {
          document.getElementById("stdmeldingfaqtable").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()
{
  if ($("select#disc_show").length)
  { // add multiselect disc records
    $("#disc_show option:selected").each(function () {
      fac_check_and_add($(this).val(), "", $(this).text(), "");
    });
  }
  else
  { // single discipline is present
    if ($("#disc").val() > 0)
    { // single discipline has a value
      if ($("select#stdm_show").length)
      { // add multiselect disc + call combi records
        $("#stdm_show option:selected").each(function () {
          fac_check_and_add($("#disc").val(), $(this).val(), $("#disc_show").val(), $(this).text());
        });
      }
      else if ($("input#stdm_show").length)
      { // single call is present
        if ($("#stdm").val() > 0)
        { // add 1 disc + call combi record
          fac_check_and_add($("#disc").val(), $("#stdm").val(), $("#disc_show").val(), $("#stdm_show").val());
        }
        else
        { // add 1 disc record without call
          fac_check_and_add($("#disc").val(), "", $("#disc_show").val(), "");
        }
      }
    }
  }
}
function fac_check_and_add(disc_val, stdm_val, disc_show_val, stdm_show_val)
{
    var exist = false;

    $("#stdmeldingfaqtable tr").each(function(i, value)
                              {
                                if (this.myData
                                   && (disc_val == this.myData.disc_key)
                                   && (stdm_val == this.myData.stdm_key)
                                   )
                                {
                                  exist = true;
                                }
                              });
    if (!exist)
    {
      CreateRow( disc_val
               , stdm_val
               , disc_show_val
               , stdm_show_val
               );
      FcltMgr.resized(window);
    }
}


