<%@language = "javascript" %>
<%
/*
    $Revision$
    $Id$

    File:          bez_smshost.asp
    Description:   shared/Zie pass2mail.asp maar dan PDA-layout
                   Hier sturen we uiteindelijk een inlogtoken
    Parameters:
    Context:       Vanuit bez_list.js

    NOTE:
*/
//ANONYMOUS_Allowed = 1;
%>
<!--#include file="../Shared/common.inc"-->
<!--#include file="../Shared/iface.inc"-->
<!--#include file="../Shared/send_mail.inc"-->
<%

FCLTHeader.Requires({ plugins: ["jQuery"],
                      js: []
                    });

var afspr_key = getQParamInt("afspr_key");
var bezoek_key = getQParamInt("bez_key", -1);

var sql = "SELECT bez_afspraak_naam"
        + "  FROM bez_bezoekers"
        + " WHERE bez_afspraak_key = " + afspr_key
        + "   AND bez_bezoekers_key = " + bezoek_key;
var oRs = Oracle.Execute(sql);
var bezoeker_naam = oRs("bez_afspraak_naam").Value||"";
oRs.Close();
var smstekst = L("lcl_bez_visitor_arrived").format(bezoeker_naam);

var sql = "SELECT a.bez_afspraak_contact_key"
        + "     , (SELECT " + S("prs_pers_string")
              + "    FROM prs_perslid p"
              + "   WHERE p.prs_perslid_key = a.bez_afspraak_contact_key"
              + " ) contact_naam"
        + "     , (SELECT p1.prs_perslid_mobiel"
              + "    FROM prs_perslid p1"
              + "   WHERE p1.prs_perslid_key = a.bez_afspraak_contact_key"
              + " ) contact_mobiel"
        + "     , a.bez_afspraak_host_key"
        + "     , (SELECT " + S("prs_pers_string")
              + "    FROM prs_perslid p"
              + "   WHERE p.prs_perslid_key = a.bez_afspraak_host_key"
              + " ) host_naam"
        + "     , (SELECT p2.prs_perslid_mobiel"
              + "    FROM prs_perslid p2"
              + "   WHERE p2.prs_perslid_key = a.bez_afspraak_host_key"
              + " ) host_mobiel"
       + "  FROM bez_afspraak a"
       + " WHERE a.bez_afspraak_key = " + afspr_key;
var oRs = Oracle.Execute(sql);
var contact = { srtprs: "C"
              , key: oRs("bez_afspraak_contact_key").Value
              , naam: oRs("contact_naam").Value
              , mobiel: oRs("contact_mobiel").Value
              };
var host = { srtprs: "H"
           , key: oRs("bez_afspraak_host_key").Value
           , naam: oRs("host_naam").Value
           , mobiel: oRs("host_mobiel").Value
           };
oRs.Close();


function no_mobile()
{
    Response.Write("<tr><td colspan='2'>"
                 + "<label>" + safe.html(L("lcl_bez_no_mobile")) + "</label>"
                 + "</td></tr>"
                  );
}

function set_check_start()
{
    Response.Write("<tr>"
                 + "<td class='label'><label>" + L("lcl_bez_send_sms_to") + "</label></td>"
                 + "<td id='beznaar'>"
                  );
}

function set_check_end()
{
    Response.Write("</td></tr>");
}

function set_just_one(contactprs)
{
    var chk_label = (contactprs.srtprs == "C" ? L("lcl_vis_contactperson") : L("lcl_vis_visit"))
                  + ": " + contactprs.naam + " (" + contactprs.mobiel + ")";
    Response.Write("<label for='prs_key'>" + safe.html(chk_label) + "</label>"
                 + "<input type='hidden' name='prs_key' id='prs_key' srtprs='" + contactprs.srtprs + "' value='" + contactprs.key + "'>"
                  );
}

function set_checkbox(contactprs)
{
    var chk_label = (contactprs.srtprs == "C" ? L("lcl_vis_contactperson") : L("lcl_vis_visit"))
                  + ": " + contactprs.naam + " (" + contactprs.mobiel + ")";
    Response.Write("<label for='prs_key'>"
                 + "<input type='checkbox' name='prs_key' id='prs_key' srtprs='" + contactprs.srtprs + "' value='" + contactprs.key + "'>"
                 + safe.html(chk_label)
                 + "</label><br>"
                  );
}

%>
<html>
  <head>
<%  FCLTHeader.Generate(); %>

  <script type="text/javascript">

    function sms_submit()
    {
        var prs_key = [];
<%
        if (contact.mobiel && host.mobiel && contact.key != host.key)
        {
%>
            if ( $("[srtprs=C]").is(":checked") )
                prs_key.push( parseInt($("[srtprs=C]").val()) );
            if ( $("[srtprs=H]").is(":checked") )
                prs_key.push( parseInt($("[srtprs=H]").val()) );
<%
        }
        else
        {
%>
            prs_key.push($("#prs_key").val());
<%
        }
%>
        if (prs_key.length)
        {
            $.post($("form[name=u2]")[0].action, $("[name=u2]").serialize());
            FcltMgr.closeDetail( window );
            return true; // disable button
        }
        else
            return false;
    }
  </script>

  </head>

  <body class="fclt-modal" id="mod_token2mail">
<% MODAL_START(); %>
      <form name="u2" method="post" action="../bez/bez_smshost_save.asp">
<%
        var buttons = [];
        MODAL_BLOCK_START("smsinput", "", { "icon": "fa-message-sms" });
            if (!contact.mobiel && !host.mobiel)
            {
                buttons.push( {title: L("lcl_cancel"), icon: "fa-fclt-cancel", action: "FcltMgr.closeDetail( window )" } );
                no_mobile();
            }
            else
            {
                buttons.push( {title: L("lcl_noti_send"), icon: "fa-envelope", action: "sms_submit()" } );
                buttons.push( {title: L("lcl_cancel"), icon: "fa-fclt-cancel", action: "FcltMgr.closeDetail( window )" } );
                RWFIELDTR("smstekst",  "fldsubject required fld", L("lcl_mes_subject_sms"), smstekst);

                set_check_start();
                if (contact.mobiel && host.mobiel)
                {
                    if (contact.key != host.key)
                    {
                        set_checkbox(contact);
                        set_checkbox(host);
                    }
                    else
                        set_just_one(host);
                }
                else
                {
                    if (contact.mobiel)
                        set_just_one(contact);
                    else
                        set_just_one(host);
                }
                set_check_end();
            }
        MODAL_BLOCK_END();
        SIMPLE_BLOCK_START();
        CreateButtons(buttons, { isModal: true });
        SIMPLE_BLOCK_END();
        IFACE.FORM_END();
%>
      </form>
    <% MODAL_END(); %>
  </body>
</html>
<% ASPPAGE_END(); %>
