/*
     $Revision$
     $Id$
*/

function opdrEdit(row)
{
  var opdr_key = row.getAttribute("ROWKEY");
  url = "appl/bes/bes_opdr.asp?ordernr=" + opdr_key + "&urole=" + urole;
  FcltMgr.openDetail(url, { reuse: true });
}

function opdrReject(rowArray, isMulti)
{
  function opdrRejectCallback(json, modalParams)
  {
    FcltCallbackRefresh(json, modalParams);
  }

  var opdrKeyString = getKeyString(rowArray);
  url = "../BES/opdr_reject.asp?opdr_key=" + opdrKeyString + "&urole=" + urole;
  FcltMgr.openModalDetail(url, L("lcl_bes_order_reject"), {callback: opdrRejectCallback});
}

function opdrAccept(rowArray, isMulti)
{
  var opdrData = JSON.parse(rowArray[0].getAttribute("ROWDATA"));
  if (isMulti || opdrData)
  {
    FcltMgr.confirm(L("lcl_bes_confirm_accept").format(opdrData.opdrId), { autoconfirm: isMulti }, function() {
        var opdrKeyString = getKeyString(rowArray);
        var data = { urole: urole
                   , opdr_key: opdrKeyString
                   };
        protectRequest.dataToken(data);
        $.post("opdr_accept.asp", data, FcltCallbackRefresh, "json");
    });
  }
}

function opdrSent(rowArray, isMulti)
{
  var opdrData = JSON.parse(rowArray[0].getAttribute("ROWDATA"));
  if (isMulti || opdrData)
  {
    FcltMgr.confirm(L("lcl_bes_confirm_sent").format(opdrData.opdrId), { autoconfirm: isMulti }, function() {
        var opdrKeyString = getKeyString(rowArray);
        var data = { urole: urole
                   , opdr_key: opdrKeyString
                   };
        protectRequest.dataToken(data);
        $.post("opdr_sent.asp", data, FcltCallbackRefresh, "json");
    });
  }
}

function opdrDeliver(rowArray, isMulti)
{
  var opdrData = JSON.parse(rowArray[0].getAttribute("ROWDATA"));

  if (isMulti || opdrData)
  {
		var opdrKeyString = getKeyString(rowArray);
	  var opdrData = JSON.parse(rowArray[0].getAttribute("ROWDATA"));
	  if (opdrData)
	  {
	    $(rowArray[0]).addClass('dirty');
	    if (isMulti)
	    {
		    url = "opdr_delivery_save.asp?opdr_key=" + opdrKeyString + "&urole=" + urole + "&is_multi=1";
		    var data = {  };
            protectRequest.dataToken(data);
            $.post(url, data, FcltCallbackRefresh, "json");
	    }
	    else
	    {
		    url = "opdr_delivery.asp?opdr_key=" + opdrKeyString + "&urole=" + urole + "&is_multi=" + (isMulti || 0);
  	    FcltMgr.openModalDetail(url, L("lcl_bes_delivery"), {xwidth: 800, callback: FcltMgr.refresh });
	    }
	  }
  }
}

function toggleBesopdrFlag(evt, besopdr_key)
{
  FcltMgr.stopPropagation(evt);

  var flagkey = $('#besopdrflagimg'+besopdr_key).attr('flagkey');
  var data = { besopdr_key: besopdr_key
             , flagkey    : flagkey
             };
  protectRequest.dataToken(data);
  $.post("../bes/bes_flag.asp"
        , data
        ,  FcltCallbackAndThen(setBesopdrFlag)
        , "json"
        );
}

function setBesopdrFlag(data)
{ // Zet de flag en het volgnummer van de flag.
  var bes_key = data.besopdr_key;
  var flagkey = data.newflag;
  var flagIcon = I("fa-fclt-flag" + (flagkey == 0 ? "0" : ""));
  
  $('#besopdrflagimg'+bes_key).attr('flagkey', flagkey)
                              .attr('class', "besopdrflag"+flagkey)
                              .html(flagIcon)
                              .parent().attr('title', L("lcl_bestelopdr_flag" + flagkey));
  $('#besopdrflagimg'+bes_key).next().text(flagkey||999); // Hidden span voor sorteren
  $('#opdrtable th').removeClass('sorttable_sorted sorttable_sorted_reverse'); // sortable cache't namelijk
}