// ******************************************
// * $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 xmlHTTP = new ActiveXObject("Msxml2.ServerXMLHTTP.6.0");


    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");
    }

    if (S("puo_smsprotocol") == "HTTP")
    {
        if (S("puo_useproxy"))
        {
            var proxy_ip = S("puo_proxyserveripaddress");
            Log2File(1, "Opening proxy " + proxy_ip);
            xmlHTTP.setProxy(SXH_PROXY_SET_PROXY, proxy_ip);
        }
        xmlHTTP.open("POST", "http://gateway.wireless-services.nl/sendsms", false);
        xmlHTTP.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        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";
        xmlHTTP.send(body);
        if (xmlHTTP.status == 200 && xmlHTTP.responseText.substr(0,3) == '01=')
        {
            SMSsend = true;
        }
        else
        {
            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;
    }

    return SMSsend;
}
