/*
     $Revision$
     $Id$

     File:          fin_list.js
*/

function finEdit(row)
{
  $(row).addClass('dirty');
  var fin_key = row.getAttribute("ROWKEY");
  var url = "appl/fin/fin_factuur.asp?fin_key=" + fin_key
          + "&mode=viewUpdate";
  FcltMgr.openDetail(url, { reuse: true });
}

function finOrder(row)
{
  var finData = JSON.parse(row.getAttribute("ROWDATA"));
  if (finData)
  {
    if (finData.mld_opdr_key)
    {
      url = "appl/mld/mld_opdr.asp?opdr_key=" + finData.mld_opdr_key + "&readonly=1" + "&close_window=1&urole=bo&embedded=1";
    }
    else if (finData.cnt_contract_key)
    {
      url = "appl/cnt/cnt_contract.asp?cnt_key=" + finData.cnt_contract_key + "&mode=ReadOnly" + "&close_window=1&urole=bo&embedded=1";
    }
    else if (finData.bes_bestelopdr_key)
    {
      url = "appl/bes/bes_opdr.asp?ordernr=" + finData.bes_bestelopdr_key + "&reado=1" + "&close_window=1&urole=bo&embedded=1";
    }
    FcltMgr.openDetail(url, { reuse: true });
  }
}

function finApprove(rowArray, isMulti)
{
  FcltMgr.confirm(L("lcl_fin_approve_select"), { autoconfirm: isMulti }, function() {
      var finKeyString = getKeyString(rowArray);
      var data = {
          fin_key: finKeyString
      };
      protectRequest.dataToken(data);
      $.post("fin_approve.asp?a=1",
          data,
          FcltCallbackRefresh,
          "json");
  });
}

function finGoedkeur(rowArray, isMulti)
{
  FcltMgr.confirm(L("lcl_fin_goedkeur_select"), { autoconfirm: isMulti }, function() {
      var finKeyString = getKeyString(rowArray);
      var data = {
          fin_key: finKeyString
      };
      protectRequest.dataToken(data);
      $.post("fin_goedkeur.asp?a=1",
          data,
          FcltCallbackRefresh,
          "json");
  });
}

function finUnapprove(rowArray, isMulti)
{
  FcltMgr.confirm(L("lcl_fin_unapprove_select"), { autoconfirm: isMulti }, function() {
      var finKeyString = getKeyString(rowArray);
      var data = {
          fin_key: finKeyString
      };
      protectRequest.dataToken(data);
      $.post("fin_unapprove.asp",
          data,
          FcltCallbackRefresh,
          "json");
  });
}

function finRejectApprove(rowArray, isMulti)
{
  var finKeyString = getKeyString(rowArray);
  url = "../fin/fin_reject.asp?mode=F&fin_key=" + finKeyString;
  FcltMgr.openModalDetail(url,
                          L("lcl_fin_reject_invoices"),
                          {callback: FcltCallbackRefresh}
                         );
}

function finRejectGoedkeur(rowArray, isMulti)
{
  var finKeyString = getKeyString(rowArray);
  url = "../fin/fin_reject.asp?mode=G&fin_key=" + finKeyString;
  FcltMgr.openModalDetail(url,
                          L("lcl_fin_reject_invoices"),
                          {callback: FcltCallbackRefresh}
                         );
}

function finUnreject(rowArray, isMulti)
{
  FcltMgr.confirm(L("lcl_fin_unreject_select"), { autoconfirm: isMulti }, function() {
      var finKeyString = getKeyString(rowArray);
      var data = {
          fin_key: finKeyString
      };
      protectRequest.dataToken(data);
      $.post("fin_unreject.asp",
          data,
          FcltCallbackRefresh,
          "json");
  });
}

function finDelete(rowArray, isMulti)
{
  FcltMgr.confirm(L("lcl_shared_row_delete_confirm"), { autoconfirm: isMulti }, function() {
      var finKeyString = getKeyString(rowArray);
      var data = {
          fin_key: finKeyString
      };
      protectRequest.dataToken(data);
      $.post("fin_delete.asp",
          data,
          FcltCallbackRefresh,
          "json");
  });
}

function toggleFlag(evt, fin_key)
{
  FcltMgr.stopPropagation(evt);

  var flagkey = $("#finflag"+fin_key).attr("flagkey");
  var data = { fin_key: fin_key
             , flagkey: flagkey
             };
  protectRequest.dataToken(data);
  $.post("../fin/fin_flag.asp"
        , data
        ,  FcltCallbackAndThen(setFlag)
        , "json"
        );
}

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

  $("#finflag"+fin_key).attr("flagkey", flagkey)
                       .attr('class', "finflag" + flagkey)
                       .html(flagIcon)
                       .parent().attr("title", L("lcl_fin_flag"+flagkey));
  $("#finflag"+fin_key).next().text(flagkey||999); // Hidden span voor sorteren
  $("#fintable th").removeClass("sorttable_sorted sorttable_sorted_reverse"); // sortable cache't namelijk
  $("#lbl-finflag"+fin_key)?.text(L("lcl_fin_flag" + flagkey));
}