/*
     $Revision$
     $Id$

     File:        mld_show_melding.js
*/
function mld_print()
{
   window.open('./mld_opdr_xml.asp?mld_key=' + mld_key);
}

function mld_change(urole)
{
  if (FcltMgr.startEdit(window))
    window.location.href = "mld_edit_melding.asp?urole=" + urole + "&mld_key=" + mld_key + "&embedded=1";
}

// Reload zonder verynew=1. Geen parent.FcltMgr.reload() omdat dan verynew=1 wel meegegeven wordt.
function mld_melding_reload(mld_key)
{
  parent.location.href = "mld_melding.asp?urole=" + urole + "&mld_key=" + mld_key + "&embedded=1";
}
function fn_mld_melding_reload(mld_key)
{
  return function () { mld_melding_reload(mld_key) };
}

function mld_accept(doublecheck)
{
  // Indien verynew (uitgezonderd: setting doublecheck = alleen bij accepteren (2))
  // dan is doublecheck al uitgevoerd en de eventuele pupup met soortgelijke meldingen al geweest.
  if (!verynew || doublecheck == 2)
    $.post("../mld/mld_doublecheck.asp",
           { mld_key: mld_key,
             urole: urole,
             reloadmld: 1 },
           doublecheckCallback,
           "json");
  else
    doAccept( { mld_key: mld_key,
                prefix: mldPrefix
              },
              fn_mld_melding_reload(mld_key)  // callback
             );
}

function mld_continue()
{
  FcltMgr.openModalDetail("../mld/mld_continue.asp?mld_key=" + mld_key + "&urole=" + urole, L("lcl_mld_continuation_mld"));
}

function mld_forward(urole)
{
  if (emptyRequiredFields)
  {
    FcltMgr.alert(L("lcl_shared_validator_missing"));
    // Ga naar de edit mode.
    if (FcltMgr.startEdit(window))
      window.location.href = "mld_edit_melding.asp?urole=" + urole + "&mld_key=" + mld_key + "&embedded=1";
  }
  else
    FcltMgr.openModalDetail("../mld/mld_forward.asp?mld_key=" + mld_key + "&urole=" + urole, L("lcl_mld_forward"), { callback: fn_mld_melding_reload(mld_key) });
}

function mld_fix_sla()
{
  FcltMgr.openModalDetail("../mld/mld_fix_sla.asp?mld_key=" + mld_key + "&urole=" + urole, L("lcl_complain"), { callback: fn_mld_melding_reload(mld_key) });
}

function mld_reject()
{
  if (mld_allow_parentmelding)
    $.post("../mld/mld_doublecheck.asp",
           { mld_key: mld_key,
             urole: urole,
             reloadmld: 1,
             reject: 1 },
           doublecheckCallback,
           "json");
  else
  {
    url = "../mld/mld_reject.asp?urole=" + urole + "&mld_key=" + mld_key
    FcltMgr.openModalDetail(url,
                            L("lcl_mld_complain") + ' ' + mldPrefix + mld_key,
                            {callback: function()
                                {
                                    FcltMgr.closeDetail(window, {close: true});
                                    mld_melding_reload(mld_key);
                                }}
                           );
  }
}

function mld_laatgaan()
{
  // Noot: de status is na invoeren default op 2, dus hoef ik eigenlijk niets te doen.
  FcltMgr.closeDetail(window, {close: true});
}

function mld_doorzet()
{
  var data = { mld_key: mld_key };
  protectRequest.dataToken(data);
  $.post("mld_doorzet.asp",
         data,
         FcltCallbackAndThen(fn_mld_melding_reload(mld_key)),
         "json");
}

function mld_terugzet()
{
  var data = { mld_key: mld_key
             , terug: 1
             };
  protectRequest.dataToken(data);
  $.post("mld_doorzet.asp",
         data,
         FcltCallbackAndThen(fn_mld_melding_reload(mld_key)),
         "json");
}

// Verynew is de frontoffice mode van meldingen
function mld_close(verynew)
{
  var url = "../mld/mld_close_confirm.asp?mld_key=" + mld_key + "&verynew=" + (verynew? 1 : 0);
  FcltMgr.openModalDetail(url,
                          L("lcl_mld_final_close"),
                          {
                            callback: function()
                                {
                                    FcltMgr.closeDetail(window, {close: true});
                                    mld_melding_reload(mld_key);
                                }
                          });
}

function mld_unlink()
{
  FcltMgr.confirm(L("lcl_mld_unlink"), function() {
      var data = {
          mld_key: mld_key,
          parent_key: -1
      }
      protectRequest.dataToken(data);
      $.post("mld_link.asp",
          data,
          FcltCallbackAndThen(fn_mld_melding_reload(mld_key)),
          "json");
  });
}

function preSaveLink()
{ // Ik moet eerst controleren of de melding al is "Afgemeld". Dan moet er eerst een melding gegeven worden.
  var mldnum = $("#mldnum").val();
  var data = { req_info: "mld_status",
               mld_key: mldnum };
  $.getJSON("./get_mld_info_ajax.asp",
            data,
            saveLink);
}

function saveLink(json, textStatus)
{
  if (json)
  {
    var autoConf = (json.mld_status > 1 && json.mld_status < 5) || json.mld_status == 7;
    var label = "placeholder";

    // Bij Afgewezen(1), Afgemeld(5) of  Administratief voltooid(6) krijg je een melding.
    if (json.mld_status == 1)
        label = L("lcl_mld_will_be_rejected");
    else if (json.mld_status == 5 || json.mld_status == 6)
        label = L("lcl_mld_will_be_closed");
    // else autoConf = true en label = "placeholder"

    FcltMgr.confirm(label, { autoconfirm: autoConf }, function() {
        $("#showlink").hide();
        var mldnum = $("#mldnum").val();
        if (!mldnum)
            return;

        var data = {
            mld_key: mld_key,
            parent_key: mldnum
        };
        protectRequest.dataToken(data);
        $.post("mld_link.asp",
            data,
            FcltCallbackAndThen(fn_mld_melding_reload(mld_key)),
            "json");
    });
  }
  else
    FcltMgr.alert(L("lcl_linkedparent_notfound"));
}

function createLink()
{
  if (!evt) var evt = window.event; // PNBR#24239 evt is hier de globale var uit iface.js/iface.button.click
                                    // Lelijk maar werkend

  if (mld_allow_parentmelding)
  {
    $("#showlink").show();
    $("#showlink").css("left", evt.clientX - $("#showlink").width() - 50);
  }
}

function mld_orders(doublecheck)
{
  // Maakt -direct na invoer melding- een nieuwe order aan in huidige tab (let op: autoswitch naar bo-rechten?)
  // Moeten we nu ook eerst wel even accepteren!
  if (!verynew || doublecheck == 2)
  {
    $.post("../mld/mld_doublecheck.asp",
           { mld_key: mld_key,
             urole: urole,
             reloadmld: 2 }, // Voeg aan een nieuwe melding ook gelijk een opdracht toe.
           doublecheckCallback,
           "json");
  }
  else
  {
    var data = { mld_key: mld_key };
    protectRequest.dataToken(data);
    $.post("mld_accept_save.asp",
           data,
           FcltCallbackAndThen(function ()
           {
             FcltMgr.setTitle(L("lcl_mld_new_order"));
             FcltMgr.closeDetail(window, { redirect: "../mld/mld_opdr.asp?urole=bo&mld_key=" + mld_key });
           }),
           "json");
  }
}

function mld_bestel()
{
  // Moeten we nu ook eerst wel even accepteren!
  var data = { mld_key: mld_key };
  protectRequest.dataToken(data);
  $.post("mld_accept_save.asp",
         data,
         FcltCallbackAndThen(function ()
         {
           FcltMgr.setTitle(L("lcl_mld_new_bestelling"));
           FcltMgr.closeDetail(window, { redirect: "../bes/bes_bestelling.asp?urole=fo&mld_key=" + mld_key });
         }),
         "json");
}

function mld_copy()
{
   FcltMgr.openDetail("appl/mld/mld_melding.asp?urole=" + urole + "&mld_key=" + mld_key + "&mld_copy=1",  L("lcl_mld_copy") + " " + L("lcl_shared_complain").toLowerCase());
}

function mld_afrond()
{
  FcltMgr.confirm(L("lcl_mld_ask_afrond").format(mldPrefix + mld_key), function() {
      var data = {
          mld_key: mld_key
      };
      protectRequest.dataToken(data);
      $.post("mld_afronden.asp",
          data,
          FcltCallbackAndThen(fn_mld_melding_reload(mld_key)),
          "json");
  });
}

function mld_add2faq()
{
   FcltMgr.openModalDetail("../mld/mld_2faq.asp?mld_key=" + mld_key + "&urole=" + urole,
                                            //+ "&faq_question=" + escape(data.question)
                                            //+ "&faq_answer=" + escape(data.answer)
                                            //+ "&faq_perslid_key=" + escape(data.author),
                           L("lcl_mld_addtofaq"));
}

function mld_show_kennisbank(data)
{
  if (data.fixed_faq.length > 0)
  {
      var $tbl = $("#mldFaq").find("table");
      $tbl.empty();
      for (var i = 0; i < data.fixed_faq.length; i++)
      {
          var safequestion = $("<span>").text(data.fixed_faq[i].question).html().replace(/\n/g, "<br>");
          var $newrow = $("<tr class='trlabel flexlabel flexcollapsable flexcollapsed'><td class='fixedfaqquestion lhint'>" + safequestion + "</td></tr>")
                            .click(function () {
                                                 $(this).next().toggle();
                                                 $(this).toggleClass('flexcollapsed');
                                                 FcltMgr.resized(window);
                                               });
          $tbl.append($newrow)
          var safeanswer = $("<span>").text(data.fixed_faq[i].answer).html().replace(/\n/g, "<br>");
          if (data.fixed_faq[i].hurl)
            safeanswer += "<br><a href={0} target='_new'>{1}</a>".format(data.fixed_faq[i].hurl, data.fixed_faq[i].hlnk);
          $tbl.append("<tr style='display:none'><td class='fixedfaqanswer'>" + safeanswer + "</td></tr>");
      }
      $("#mldFaq").show();
  }
  else
      $("#mldFaq").hide();
}