/*
     $Revision$
     $Id$

     File:        opdr_list.js

*/
function opdr_edit(row, finish)
{
  var opdr_key = row.getAttribute("ROWKEY");
  var mldData = JSON.parse(row.getAttribute("ROWDATA"));
  if (mldData.act_key > -1)
  { // sjaboon
    var url = "appl/mld/mld_show_opdr.asp?urole=bo&template=1&opdr_key=" + opdr_key;
  }
  else
  {
    var url = "appl/mld/mld_opdr.asp?urole=" + urole + "&opdr_key=" + opdr_key + "&finish=" + (finish? 1 : 0);
  }
  FcltMgr.openDetail(url, { reuse: true });
}

function opdr_copy(row)
{
  var opdr_key = row.getAttribute("ROWKEY");
  var mldData = JSON.parse(row.getAttribute("ROWDATA"));
  if (mldData)
  {
    var mldKey = mldData.mldKey;
    var mldPrefix = mldData.mldPrefix;
    var mldVolgnr = mldData.mldVolgnr;
    var url = "appl/mld/mld_opdr.asp?urole=" + urole + "&opdr_key=" + opdr_key + "&opdr_copy=1";
    FcltMgr.openDetail(url, L("lcl_mld_copy") + " " + L("lcl_shared_order") + " " + mldPrefix + mldKey + "/" + mldVolgnr);
  }
}

function opdr_continue()
{
  FcltMgr.openDetail("appl/mld/mld_opdr.asp?urole=bo" + "&opdr_key=" + opdr_key + "&mld_key=" + mld_key + "&opdr_continue=1", L("lcl_mld_continuation") + " " + L("lcl_shared_order").toLowerCase());
}

function opdr_close_one(row)
{
    opdr_close([row]);
}

function opdr_close(rowArray, isMulti)
{
  var opdrKeyString = getKeyString(rowArray);
  var url = "../mld/opdr_close_confirm.asp?opdr_key=" + opdrKeyString;
  FcltMgr.openModalDetail(url, L("lcl_mld_opdr_close"), { callback: opdrCloseCallback });
}

function opdr_accept(rowArray, isMulti)
{
  var opdrKeyString = getKeyString(rowArray);
  if (rowArray.length == 1)
  {
    var url = "../mld/opdr_accept_confirm.asp?opdr_key=" + opdrKeyString;
    FcltMgr.openModalDetail(url, L("lcl_mld_opdr_accept").format(""), { callback: opdrCloseCallback });
  }
  else
  {
    var url = "../mld/opdr_accept_confirm.asp?opdr_key=" + opdrKeyString;
    FcltMgr.openModalDetail(url, L("lcl_accept"), { callback: opdrCloseCallback });
  }
}

function opdr_finish(rowArray, isMulti)
{
  var opdrKeyString = getKeyString(rowArray);
  if (rowArray.length == 1)
    opdr_edit(rowArray[0], true);
  else
  { // Multi
    var data = { opdr_key: opdrKeyString };
    protectRequest.dataToken(data);
    $.post("../mld/opdr_finish.asp",
           data,
           FcltCallbackRefresh,
           "json");
  }
}

function opdr_verwerk(rowArray, isMulti)
{
  var opdrKeyString = getKeyString(rowArray);
  var confirmtxt = (
    rowArray.length == 1
      ? L("lcl_shared_doaction1").format(L("lcl_mld_opdr_verwerk"))
      : L("lcl_shared_doaction").format(L("lcl_mld_opdr_verwerk"), rowArray.length)
  );
  FcltMgr.confirm(
    confirmtxt,
    { autoconfirm: isMulti }, // De multiactie vraagt zelf al om bevestiging
    function () {
      var url = "../mld/opdr_verwerk_save.asp";
      var data = {
        opdr_key: [opdrKeyString.split(",")]
      };
      protectRequest.dataToken(data);
      $.post(url, data, FcltCallbackRefresh, "json");
    }
  );
}

function opdr_add_invoice(row)
{
  var mldData = JSON.parse(row.getAttribute("ROWDATA"));
  if (mldData)
  {
    var mldKey = mldData.mldKey;
    var mldPrefix = mldData.mldPrefix;
    var mldVolgnr = mldData.mldVolgnr;
    var url = "appl/fin/fin_factuur.asp?referentie=" + mldPrefix + mldKey + "/" + mldVolgnr;
    FcltMgr.openDetail(url, (urole == "fe"? L("lcl_menu_fin_fe") : L("lcl_menu_fin_fo")));
  }
}

function opdr_list_invoice(row)
{
  var mldData = JSON.parse(row.getAttribute("ROWDATA"));
  if (mldData)
  {
    var mldKey = mldData.mldKey;
    var mldPrefix = mldData.mldPrefix;
    var mldVolgnr = mldData.mldVolgnr;
    var url = "appl/fin/fin_search.asp?autosearch=1&fin_type=O&opdrnr=" + mldPrefix + mldKey + "/" + mldVolgnr;
    FcltMgr.openDetail(url, (urole == "fe"? L("lcl_menu_fin_fe_overz") : L("lcl_menu_fin_fo_overz")));
  }
}

function toggleOpdrFlag(evt, opdr_key)
{
  FcltMgr.stopPropagation(evt);

  var flagkey = $("#opdrflag"+opdr_key).attr("flagkey");
  var data = { opdr_key: opdr_key
             , flagkey: flagkey
             };
  protectRequest.dataToken(data);
  $.post("../mld/opdr_flag.asp"
        , data
        ,  FcltCallbackAndThen(setOpdrFlag)
        , "json"
        );
}

function setOpdrFlag(data)
{ // Zet de flag en het volgnummer van de flag.
	var opdr_key = data.opdr_key;
	var flagkey  = data.newflag;
	var flagIcon = I("fa-fclt-flag" + (flagkey == 0 ? "0" : ""));

  $("#opdrflag"+opdr_key).attr("flagkey", flagkey)
                         .attr("class", "opdrflag" + flagkey)
                         .html(flagIcon)
                         .parent().attr("title", L("lcl_opdr_flag"+flagkey));
  $("#opdrflag"+opdr_key).next().text(flagkey || 999); // Hidden span voor sorteren
  $("#opdrtable th").removeClass("sorttable_sorted sorttable_sorted_reverse"); // sortable cache't namelijk
}