<%@ LANGUAGE = JavaScript %>
<% /*
     $Revision$
     $Id$

    File:               msg_edit_message.asp (formerly message.asp)
    Description:        Muteerscherm voor berichten
    Parameters:         message_key: optioneel==>impliceert een reply op een persoonlijk portalbericht!
                        -
    Context:
    Note:               Aanmaken van een Facilitor bericht
                        Alleen portal-berichten kunnen achteraf gewijzigd worden!
                        Dat gebeurt dan in msg_portal_edit.asp

*/ %>
<!-- #include file="../Shared/common.inc"-->
<!-- #include file="../Shared/persoonselector.inc" -->
<!-- #include file="../Shared/plaatsselector.inc" -->
<!-- #include file="../Shared/selector.inc" -->
<!-- #include file="../Shared/iface.inc"-->
<!-- #include file="../Shared/calendar.inc"-->
<%
FCLTHeader.Requires({plugins:["suggest"]});

var urole = getQParamSafe("urole");
var backo = urole=="bo";                     // voor nieuwe berichten
var frontend = (urole == "fe" || !backo );   // voor replies

var message_key    = getQParamInt("message_key", -1);
var message_reply  = message_key>0;
var lBestaandBericht = message_key != -1;
var vandate = new Date();
var totdate = new Date();

var subject;
var ilink;
if( lBestaandBericht ) {  // Dit moet/zal altijd een reply zijn!

  sql =   " SELECT m.prs_perslid_key"
        + ", m.msg_message_oms"
        + ", m.msg_message_aanmaak"
        + ", m.msg_message_replykey"
        + ", m.msg_message_onderwerp"
        + ", m.msg_message_prioriteit"
        + ", m.msg_message_url"
        + ", pf.prs_perslid_naam_full"
        + " FROM   msg_message m, prs_v_perslid_fullnames_all pf"
        + " WHERE  msg_message_key = " + message_key
        + " AND m.prs_perslid_key = pf.prs_perslid_key";
  oRsMes = Oracle.Execute(sql);
  if (!oRsMes.eof ) {
      key_sender   = oRsMes("prs_perslid_key").value;
      mess_dsc     = oRsMes("msg_message_oms").value;
      action_datum = oRsMes("msg_message_aanmaak").value;
      ref_mess_key = oRsMes("msg_message_replykey").value;
      subject      = oRsMes("msg_message_onderwerp").value;
      priority_key = oRsMes("msg_message_prioriteit").value;
      ilink        = oRsMes("msg_message_url").value;
      sender_name  = oRsMes("prs_perslid_naam_full").value;
  } else {
    Response.write("internal error or message deleted");
    Response.End;
  }
  receiver_key = user_key;  // that's me of course
  ref_mess_dsc = "";
  if (ref_mess_key != null)
    shared.internal_error("Reply op reply niet mogelijk");

  // prio: number to text
  if (priority_key == 1)
    priority = L("lcl_mes_high");
  else if (priority_key == 2) priority = L("lcl_mes_normaal");
  else priority = L("lcl_mes_low");

  oRsMes.Close();

  // Goed bepalen: welke rechten veris ik eigenlijk?
  // - BOF/USE -write  om aan te maken
  // - USE read om te lezen  (of is niks voldoende?)
  // - USE write om te replyen

} else { // New message
    var key_sender   = '';  // user_key
    var mess_dsc     = '';
    var action_datum = '';

    var authparams = user.checkAutorisation("WEB_MSGBOF");
}


%>
<html>
<head>
<%
   FCLTHeader.Generate();
%>
<script type="text/javascript">

function ta_message_onfocus() {
  if ($("#ta_message").val() == L("lcl_mes_empty")) {
    $("#ta_message").val("");
  }
}

function checkInput() {
  // We voorkomen:  @@todo
  // - leeg bericht (subject en/of body)
  // - bericht zonder extra bevestiging naar iedereen

  // Een SMS verzendt alleen het onderwerp. Als er alleen een SMS wordt verstuurd mag het berichtebveld leeg blijven
  if ($("#cb_sms").is(":checked") && !($("#cb_mail").is(":checked") || $("#cb_fmessage").is(":checked") || $("#cb_portal").is(":checked")))
    $("#ta_message").val("_");

  if (!validateForm("u2"))
    return false;

  var doSendMessage = true;
  var selectGroupKeys = '';

  var txt = $("#ta_message").val()
  if (txt == L("lcl_mes_empty")) {
    alert(L("lcl_shared_validator_missing"));
    return;
  }

  if (($("#perslid_key").val() == -1) &&
      ($("#districtkey").val() == -1) &&
      ($("#locatiekey").val() == -1) &&
      !$("#msggroup").val() &&
      ($("#cb_email:checked").val() ||  $("#cb_fmessage:checked").val())
     )
  {
    { // Wilt U naar alle Facilitor gebruikers sturen?
      if (!window.confirm(L("lcl_mes_all")))
          return false;
    }
  }

  document.u2.submit();
  return true;
}

function checkNrChar() {
  if (document.getElementById("ta_message").value.length > 4000) {
    alert(L("lcl_mes_to_many_char") + ' (' + document.getElementById("ta_message").value.length + '). ' + L("lcl_mes_less_500"));
    document.getElementById("ta_message").focus();
    document.getElementById("ta_message").select();
  }
}

jQuery(document).ready(function()
                     {
                       $('textarea').resize(function () { FcltMgr.resized(window) });
                       $('textarea').autogrow();
                     });

</script>
</head>

<body id="editbody">
    <%
        buttons = [{ icon: "opslaan.png", title: L("lcl_mes_send"), action: 'checkInput()' },
                   { icon: "undo.png",    title: L("lcl_cancel"),   action:"FcltMgr.closeDetail(window, { cancel: true } );" } ];

        IFRAMER_HEADER(L("lcl_mes_message"), buttons);
    %>
<form name="u2" action="msg_message_save.asp?message_reply=<%=message_reply?1:0%>&message_key=<%=message_key%>" method="post">
<%
  if (!lBestaandBericht)
  {
      BLOCK_START("msgPrs", L("lcl_msg_medewerkergroep"));

        FCLTpersoonselector(
                        "perslid_key",
                        "sgPerson",
                      { perslidKey: -1,
                        label: L("lcl_name"),
                        fieldNameKey: "person",
                        fieldId: "fldmelder",
                        filtercode: "MSG"
                      });
      sql =   " SELECT fac_groep_key, fac_groep_omschrijving, fac_groep_upper"
            + " FROM   fac_groep"
            + " ORDER BY 3";

        FCLTselector("msggroup", sql,
                        { label: L("lcl_mes_group"),
                          multi: true
                        }
                      );

      BLOCK_END();

      BLOCK_START("msgAlg", L("lcl_msg_plaats"));
         FCLTplaatsselector  (authparams.ALGreadlevel, { startlevel: 1, eindlevel:3});
      BLOCK_END();
  }


  BLOCK_START("msgMessage", L("lcl_mes_message"));

  // Een tabel met 4 kolommen: label-veld-label-veld

   if (lBestaandBericht) {
%>
<tr>
    <td class="label"><label><%=L("lcl_from")%>:</label></td>
    <td><input value="<%=safe.htmlattr(sender_name)%>" type="text" id="receiver" readonly></td>
    <td class="label"><label><%=L("lcl_mes_send_date_time")%>:</label></td>
    <td><input class="flddatetime" value="<%=action_datum%>" type="text" id="time" readonly></td>
</tr><tr>
    <td class="label"><label><%=L("lcl_mes_subject")%>:</label></td>
    <td>
      <input class="fldmsgsubj" value="<%=safe.htmlattr(subject)%>" type="text" id="subject0" name="subject0" <%=lBestaandBericht?" readonly ": ""%>>
    </td>
    <td class="label"><label><%=L("lcl_mes_priority")%>:</label></td>
    <td><input class="fldmsgprio" value="<%=priority%>" type="text" id="priority0" name="priority0" readonly></td>
  </tr>

<%
  RWTEXTAREATR("ta_message0",
               "fldmsgmessage",
               L("lcl_mes_message_received"),
               mess_dsc,
               {html: (lBestaandBericht? " readonly": ""),
                tdhtml_ta: "colspan='4'"});
%>
      <input type=hidden name="received_message" value="<%=mess_dsc%>">
<%
   }

    if (ilink != null) {
%>
  <tr>
    <td class="label"><label><%=L("lcl_mes_link")%>:</label></td>
    <td colspan="3"><a class="fldmsgurl" href="<%=safe.htmlattr(ilink)%>" target="_blank"><%=ilink%></a></td>
  </tr>
<% }

%>
  <tr>
    <td class="label"><label><%=L("lcl_mes_send_option")%>:</label></td>
    <td colspan="3">
    <div id="msgoptions">
    <%
        // ik moet zelf een e-mail adres hebben om mail te mogen sturen..
    if ( user.prs_perslid_email() != "" ) {
%>
        <input type=checkbox id=cb_email name="cb_email" value="1">
    <%  } else { %>
        <input type=checkbox id=cb_email name="cb_email" disabled>
    <%  } %>
        <label for=cb_email><%=L("lcl_mes_email")%></label>

        <input type="checkbox" id="cb_sms" name="cb_sms" value="1">
        <label for="cb_sms"><%=L("lcl_mes_sms")%></label>

        <input type=checkbox id=cb_fmessage name="cb_fmessage" value="1" checked>
        <label for=cb_fmessage><%=L("lcl_mes_fmessage")%></label>
<%      if (!lBestaandBericht) {
%>
        <input type=checkbox id=cb_portal name="cb_portal" value="1">
        <label for=cb_portal><%=L("lcl_msg_portalbericht")%></label>
<%
    }
   %>
  </div>
</td>
</tr>
  <tr>
    <td class="label"><label><%=L("lcl_mes_subject_sms")%>:</label></td>
    <td><input class="required fldmsgsubj" type="text" id="subject" name="subject" value="<%=(subject?"Re:":"")+ safe.htmlattr(subject)%>"></td>
    <td class="label"><label><%=L("lcl_mes_priority")%>:</label></td>
    <td>
      <select  class="fldmsgprio" name='priority'>
        <option value='1'><%=L("lcl_mes_high")%></option>
        <option value='2' selected><%=L("lcl_mes_normaal")%></option>
        <option value='3'><%=L("lcl_mes_low")%></option>
      </select>
   </td>
  </tr>

<%
  RWTEXTAREATR("ta_message",
               "fldmsgmessage required",
               L("lcl_mes_message"),
               L("lcl_mes_empty"),
               {html: "onfocus='ta_message_onfocus();' onblur='checkNrChar()'",
                tdhtml_ta: "colspan='4'"});

  if (!lBestaandBericht)
  {
   %>
      <tr>
        <td class="label"><label><%=L("lcl_mes_link")%>:</label></td>
        <td>
          <input class="fldmsgurl" type="text" id="ilink" name="ilink"  value="">
        </td>
      </tr>
      <tr>
        <td class="xlabel" colspan="2"><label><%=L("lcl_msg_portalbericht")%>:</label></td>
      </tr>
   <%
      FCLTcalendar( "datefrom",
                    { label : L("lcl_period_from"),
                      datum : vandate,
                      volgnr: 1
                    });
      FCLTcalendar( "dateto",
                    { label : L("lcl_period_to"),
                      datum  : totdate,
                      volgnr: 2
                    });
   }
   BLOCK_END();
IFACE.FORM_END();
%>
</form>
</body>
</html>
