// ******************************************
// * $Id$
// *
// *   sendSMS()
// *
// * sendSMS function is used to send SMS
// *
// * arguments:
// *           p_telnr: Send the sms to this telephone number
// *             p_msg: SMS message to send.
// *
// ******************************************
function sendSMS( p_telnr
                , p_msg
                )
{
    // L("lcl_vis_log_psw") = 'Wachtwoord:'
    var p_msgtxt = String(p_msg).replace(/Wachtwoord: (.*)/ig, 'Wachtwoord: ********'); // Geen wachtwoord in de logging

    if (!S("puo_uid") || !S("puo_pwd"))
    {
        Log2File(1, "SMS bericht: '" + p_msgtxt + "' *niet* verzonden naar " + p_telnr + " want SMS niet geconfigureerd.");
        return false;
    }
    if (p_telnr.length < 10)
    {
        Log2File(1, "SMS bericht: '" + p_msgtxt + "' *niet* verzonden naar " + p_telnr + " want nummer minder dan 10 cijfers.");
        return false;
    }

    //var SXH_PROXY_SET_PROXY = 2; // contante in msxml2
    var SMSsend = false;

    var body   = "";
    Log2File(1, "SMS bericht: '" + p_msgtxt + "' wordt via " + S("puo_smsprotocol") + " verzonden naar: " + p_telnr);

    if (S("puo_forcemobile"))
    {
        Log2File(1, "Mobile number " + p_telnr + " forced to effective " + S("puo_forcemobile"))
        p_telnr = S("puo_forcemobile");
    }
    // Voor de zekerheid alle non-digits eruit
    p_telnr = p_telnr.replace ( /[^0-9]/g, '' );
    switch (S("puo_smsprovider"))
    {
        case 0:
            if (S("puo_smsprotocol") == "HTTP")
            {
                var url = "http://gateway.wireless-services.nl/sendsms";
                body = "UID="  + S("puo_uid")
                     + "&PWD=" + S("puo_pwd")
                     + "&N="   + p_telnr
                     + "&O="   + S("puo_replyphone")
                     + "&M="   + escape(p_msg)   // encodeURI
                     + "&CONCAT=1"
                     + "&INFO=1";
                var xmlHTTP = doHTTP(url, { data: body, headers: { "Content-Type": "application/x-www-form-urlencoded"}});
                if (xmlHTTP.status == 200 && xmlHTTP.responseText.substr(0,3) == '01=')
                {
                    SMSsend = true;
                }
                else
                {   // errors blijven status 200 opeleveren en zijn door doHTTP nog niet gelogged
                    Log2File(0, "SMS xmlHTTP error "
                                          + xmlHTTP.status + "/ " + xmlHTTP.statusText
                                                           + "/" + xmlHTTP.responseText);
                }
            }
            else
            {
                body = "UID=" + S("puo_uid") + "\n"
                     + "PWD=" + S("puo_pwd") + "\n"
                     + "O="   + S("puo_replyphone") + "\n\n"
                              + p_telnr + ","
                              + escape(p_msg) + "\n";
                sendMail( S("puo_fromaddress")
                        , S("puo_smsaccount")
                        , "Bericht per SMS verzonden aan " + p_telnr
                        , body
                        );
                SMSsend = true;
            }
            break;
        case 1:
            var url = "https://licentieserver.aareon.nl/sglicense.dll?sendpasscode";
            var salt = S("puo_pwd");
            if (md5(salt) != "9f4314fd046b7440e5d2c043e90df911")
            {
                Log2File(0, "AAREON SMS bad password");
                return false;
            }
            p_telnr = p_telnr.replace(/^316/, '06');

            var chr255 = String.fromCharCode(255);
            var str = "Method=SMS" + chr255
                    + "To=" + p_telnr + chr255
                    + "Body=" + p_msg + chr255;
            // helaas unsupported + "From=FACILITOR" + chr255;

            var message = str + "CS=" + md5(str + salt) + chr255;
            // Kleine omweg via ADODB.Stream omdat anders data naar utf wordt omgezet in de .send
            var oStream = new ActiveXObject("ADODB.Stream");
            oStream.charset = "Windows-1252";
            oStream.Open();
            oStream.Type = 2; // adTypeText
            oStream.WriteText(message);
            oStream.Position = 0;

            var xmlHTTP = doHTTP(url, { data: oStream, headers: { "Content-Type": "application/x-www-form-urlencoded; charset=Windows-1252"}});
            if (xmlHTTP.status == 200 && xmlHTTP.responseText.indexOf("<PRE>OK") > 0)
            {
                SMSsend = true;
            }
            else
            {   // errors blijven status 200 opeleveren en zijn door doHTTP nog niet gelogged
                Log2File(0, "AAREON SMS xmlHTTP error "
                                      + xmlHTTP.status + "/ " + xmlHTTP.statusText
                                                       + "/" + xmlHTTP.responseText);
            }
            oStream.Close();

            break;
    }

    return SMSsend;
}
