/*
    $Id$
 */

// Doe een SOAP-API-request naar FACILITOR
// Forceer altijd JSON antwoord
function apiFACILITOR(API, req)
{
    var xmlhttp = new ActiveXObject("WinHTTP.WinHTTPRequest.5.1");
    SXH_PROXY_SET_PROXY = 2;
    if (config.proxy)
        xmlhttp.setProxy(SXH_PROXY_SET_PROXY, config.proxy);

    var xmlUrl = config.fclturl + "&API=" + API + "&APIKEY=" + config.APIKEY + "&json=1&pretty=1&tm=" + (new Date).getTime();
    xmlhttp.open(req?"POST":"GET", xmlUrl, false);

    var body = null;
    if (req)
    {
        xmlhttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8")
        var body = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
                 + "<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'"
                 + "               xmlns:xsd='http://www.w3.org/2001/XMLSchema'"
                 + "               xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>"
                 + "  <soap:Body>"
                 + req
                 + "  </soap:Body>"
                 + "</soap:Envelope>";
    }
    xmlhttp.send(body);

    if (xmlhttp.status == 200)
    {
        try
        {
            var txt = xmlhttp.responseText || "{}";
            var result_data = eval("(" + txt + ")");
            return result_data;
        }
        catch (e)
        {
            // Waarschijnlijk http-error terug gekregen
            __Log("URL: " + xmlUrl + "\nBody:\n" + msg);
            __Log("eval failed: " + e.description);
            __Log(txt);
            WScript.Quit();
        }
    }
    else
    {
        __Log("URL: " + xmlUrl + "\nBody:\n" + msg);
        __Log("Status: "+xmlhttp.status);
        __Log(xmlhttp.responseText);
        WScript.Quit();
    }
}
