/*
     $Revision$
     $Id$

     File:        mld_list.js
*/
// Resultaat: URL om de data op te halen
function inlineOrdersUrl(row)
{
  var mld_key = row.getAttribute("ROWKEY");
  return "opdr_list.asp?tiny=1&inline=1&urole=" + urole + "&mld_key=" + mld_key;
}

function mld_edit(row)
{
  $(row).addClass('dirty');
  var mld_key = row.getAttribute("ROWKEY");
  url = "appl/mld/mld_melding.asp?urole=" + urole + "&mld_key=" + mld_key;
  FcltMgr.openDetail(url, { reuse: true });
}

function mld_opdr_edit(row)
{
  var mldData = JSON.parse(row.getAttribute("ROWDATA"));
  if (mldData)
  {
    var opdr_key = mldData.opdr_key;
    var url = "appl/mld/mld_opdr.asp?urole=" + urole + "&opdr_key=" + opdr_key;
    FcltMgr.openDetail(url, { reuse: true });
  }
}

function mld_reject(rowArray, isMulti)
{
  var mldKeyString = getKeyString(rowArray);
  var mldData = JSON.parse(rowArray[0].getAttribute("ROWDATA"));

  if (rowArray.length == 1 && mldData && mldData.mldChild) // Niet multi child meldingen kunnen hier al tegengehouden worden.
    return

  // Indien bulk actie dan niet naar dubbele meldingen kijken
  if (rowArray.length == 1 && mldData.mldDoublecheck)
    $.get("../mld/mld_doublecheck.asp"
           + "?mld_key=" + mldKeyString
           + "&multi=" + (isMulti? 1 : 0)
           + "&urole=" + urole
           + "&reject=1",
           doublecheckCallback,
           "json");
  else // Geen doublecheck controle dus direct afhandelen
  {
    doublecheckCallback({ maybeDouble: false,
                          multi: (isMulti? 1 : 0),
                          mld_key: mldKeyString,
                          urole: urole,
                          reject: true
                        });
  }
}

function mld_accept(rowArray, isMulti)
{
  var mldKeyString = getKeyString(rowArray);
  var mldData = JSON.parse(rowArray[0].getAttribute("ROWDATA"));

  if (rowArray.length == 1 && mldData && mldData.mldChild) // Niet multi child meldingen kunnen hier al tegengehouden worden.
    return

  // Indien bulk actie dan niet naar dubbele meldingen kijken
  if (rowArray.length == 1 && mldData.mldDoublecheck)
    $.get("../mld/mld_doublecheck.asp"
          + "?mld_key=" + mldKeyString
          + "&multi=0"
          + "&urole=" + urole
          + "&list=1",
           doublecheckCallback,
           "json");
  else // Geen doublecheck controle dus direct afhandelen
  {
    doublecheckCallback({ maybeDouble: false,
                          multi: (isMulti? 1 : 0),
                          mld_key: mldKeyString,
                          urole: urole,
                          prefix: mldData.mldPrefix,
                          list: "1"
                        });
  }
}

function mld_continue(rowArray, isMulti)
{
  var mldKeyString = getKeyString(rowArray);
  var mldData = JSON.parse(rowArray[0].getAttribute("ROWDATA"));

  if (rowArray.length == 1 && mldData && mldData.mldChild) // Niet multi child meldingen kunnen hier al tegengehouden worden.
    return;

  var mldPrefix = mldData.mldPrefix;
  var mldUrl = "appl/mld/mld_melding.asp?urole=fo&mld_key=" + mldKeyString + "&mld_continue=1";
  FcltMgr.openDetail(mldUrl
                   , L("lcl_mld_continuation_mld") + (rowArray.length == 1
                     ? mldPrefix + rowArray[0].getAttribute("ROWKEY")
                     : "(" + rowArray.length + " " + L("lcl_complains") + ")")
                    );
}

function mld_close(rowArray, isMulti)
{
  var mldKeyString = getKeyString(rowArray);
  var mldUrl = "../mld/mld_close_confirm.asp?mld_key=" + mldKeyString;
  FcltMgr.openModalDetail( mldUrl
                         , L("lcl_mld_final_close")
                         , {
                             callback: function(data)
                                       {
                                         if (data.edit)
                                         { // Ga naar het edit meldingen scherm.
                                           var mldUrl = "appl/mld/mld_edit_melding.asp?urole=bo&mld_key=" +  data.key + "&embedded=1";
                                           FcltMgr.openDetail( mldUrl, L("lcl_mld_complain") + " " + data.mldnr);
                                         }
                                         else
                                           FcltCallbackRefresh({success: true});
                                       }
                           }
                         );
}

function mld_forward(row, fwdType)
{
  $(row).addClass('dirty');
  var mld_key = row.getAttribute("ROWKEY");
  var mldData = JSON.parse(row.getAttribute("ROWDATA"));
  if (mldData && !mldData.mldChild) // Child meldingen kunnen hier al tegengehouden worden.
  {
    var mldPrefix = mldData.mldPrefix;
    var mldUrl = "../mld/mld_forward.asp?mld_key=" + mld_key + "&urole=" + urole + "&fwdType=" + fwdType;
    FcltMgr.openModalDetail( mldUrl
                           , (fwdType == 1 ? L("lcl_mld_forward_disc") : L("lcl_mld_forward_team")) + " " + mldPrefix + mld_key
                           , { callback: FcltCallbackRefresh }
                           );
  }
}
function mld_forward_disc(row) { mld_forward(row, 1); }
function mld_forward_team(row) { mld_forward(row, 2); }

function mld_forward_empty(row)
{
  $(row).addClass('dirty');
  var mld_key = row.getAttribute("ROWKEY");
  var mldData = JSON.parse(row.getAttribute("ROWDATA"));
  if (mldData && !mldData.mldChild) // Child meldingen kunnen hier al tegengehouden worden.
  {
    FcltMgr.alert(L("lcl_shared_validator_missing"));
    // Ga naar het edit meldingen scherm.
    var mldPrefix = mldData.mldPrefix;
    var mldUrl = "appl/mld/mld_edit_melding.asp?urole=" + urole + "&mld_key=" + mld_key + "&embedded=1";
    FcltMgr.openDetail( mldUrl,
                        L("lcl_mld_forward") + " " + mldPrefix + mld_key
                      );
  }
}

function mld_doorzet(rowArray, isMulti)
{
  var mldKeyString = getKeyString(rowArray);
  FcltMgr.confirm(L("lcl_mld_doorzetten_1") + mldKeyString + L("lcl_mld_doorzetten_2"), { autoconfirm: isMulti }, function() {
      var data = {
          mld_key: mldKeyString
      };
      protectRequest.dataToken(data);
      $.post("../mld/mld_doorzet.asp",
          data,
          FcltCallbackRefresh,
          "json");
  });
}

function mld_group(rowArray, isMulti)
{
  var mldKeyString = getKeyString(rowArray);
  var mldData = JSON.parse(rowArray[0].getAttribute("ROWDATA"));

  var mldPrefix = mldData.mldPrefix;
  var mldUrl = "../mld/mld_group.asp?urole=" + urole + "&mld_key=" + mldKeyString
  FcltMgr.openModalDetail( mldUrl
                         , L("lcl_mld_groeperen") + (rowArray.length == 1 ? mldPrefix + rowArray[0].getAttribute("ROWKEY")
                                                                                : "(" + rowArray.length + " " + L("lcl_complains") + ")")
                         );
}

function mld_terugzet(rowArray, isMulti)
{
  var mldKeyString = getKeyString(rowArray);
  FcltMgr.confirm(L("lcl_mld_terugzetten_1") + mldKeyString + L("lcl_mld_terugzetten_2"), { autoconfirm: isMulti }, function() {
      var data = {
          mld_key: mldKeyString,
          terug: 1
      };
      protectRequest.dataToken(data);
      $.post("../mld/mld_doorzet.asp",
          data,
          FcltCallbackRefresh,
          "json");
  });
}

function mld_verwerk(rowArray, isMulti)
{
  var mldKeyString = getKeyString(rowArray);
  FcltMgr.confirm(L("lcl_mld_verwerken").format(mldKeyString), { autoconfirm: isMulti }, function() {
      var data = {
          mld_key: mldKeyString
      };
      protectRequest.dataToken(data);
      $.post("../mld/mld_verwerken.asp",
          data,
          FcltCallbackRefresh,
          "json");
  });
}

function mld_herplan(rowArray, isMulti)
{
    var mldKeyString = getKeyString(rowArray);
    var mldUrl = "../mld/mld_reschedule.asp?urole=" + urole + "&mld_key=" + mldKeyString;
    FcltMgr.openModalDetail( mldUrl
                           , L("lcl_reschedule")
                             , { callback: function(data)
                                       {
                                           FcltCallbackRefresh({success: true});
                                       }
                               }
                           );
}

function mld_kenmerk(rowArray, isMulti)
{
    var mldKeyString = getKeyString(rowArray);
    var confirmstr = ( rowArray.length == 1
                     ? L("lcl_shared_doaction1").format(L("lcl_properties"))
                     : L("lcl_shared_doaction").format(L("lcl_properties"),rowArray.length)
                     );
    FcltMgr.confirm( confirmstr
                   , { autoconfirm: isMulti }
                   , function()
                        {
                            var mldUrl = "../mld/mld_edit_kenmerk_multi.asp?urole=" + urole + "&mld_key=" + mldKeyString;
                            FcltMgr.openModalDetail( mldUrl
                                                   , L("lcl_multiedit_kenmerk")
                                                   , { callback: function(data)
                                                          {
                                                            FcltCallbackRefresh({success: true});
                                                          }
                                                     }
                                                   );
                        }
                   );
}

function toggleMldFlag(evt, mld_key)
{
	FcltMgr.stopPropagation(evt);

  var flagkey = $('#mldflagimg'+mld_key).attr('flagkey');
  var data = { mld_key: mld_key
             , flagkey: flagkey
             };
  protectRequest.dataToken(data);
  $.post("../mld/mld_flag.asp"
        , data
        ,  FcltCallbackAndThen(setMldFlag)
        , "json"
        );
}

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

  $('#mldflagimg'+mld_key).attr('flagkey', flagkey)
                          .attr('class', "mldflag"+flagkey+" mlds")
                          .html(flagIcon)
                          .parent().attr('title', L("lcl_mld_flag" + flagkey));
  $('#mldflagimg'+mld_key).next().text(flagkey||999); // Hidden span voor sorteren
  $('#complainstable th').removeClass('sorttable_sorted sorttable_sorted_reverse'); // sortable cache't namelijk
}

