
// Basic XmlHttp functions
// Just call xmlhttpCall with the following parameters:
//     method: 'get', 'post' or 'head'
//     callback_function: function to call upon response from the server.
//                        If an error occured this function will be called with false as the
//                        first parameter and the error code as a second parameter.
//                        Otherwise the only parameter will be the response text from the server.
//     url: ...
//     arguments[3](optional): a urlencoded string of parameters key1=val1&key2=val2 ...



function xmlhttpCreateObject(onreadystate_function) {

    var xmlhttp = false;

    /* IE's infamous Conditional Compilation */
    /*@cc_on
    @if (@_jscript_version >= 5)
    try {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try {
           xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (E) {
           xmlhttp = false;
        }
    }
    @end @*/

    /* Mozilla/Safari/GoodBrowsers(TM) */
    if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
        try {
            xmlhttp = new XMLHttpRequest();
            xmlhttp.overrideMimeType('text/xml');
        } catch (e) {
        }
    }

    if (xmlhttp) {
        xmlhttp.onreadystatechange = function () {
            if (xmlhttp.readyState == 4) {
                try {
                    if (xmlhttp.status && xmlhttp.status == 200) {
                        var r = xmlhttp.responseText;
                        onreadystate_function(r);
                    } else {
                        onreadystate_function(false, xmlhttp.status);
                    }
                } catch(e) {
                }
            }
        }
    }

    return xmlhttp;
}



function xmlhttpCall(method, callback_function, url) {

    var r = false;
    var xmlhttp = xmlhttpCreateObject(callback_function);
    var send_content = arguments[3] || '';
    var send_raw_post_data = arguments[4] || false;

    if (xmlhttp) {
        method = method.toUpperCase();
        switch (method) {
            case 'POST':
                xmlhttp.open('POST', url, true);
                if (!send_raw_post_data) {
                    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                }
                xmlhttp.setRequestHeader('Content-Length', send_content.length);
                xmlhttp.send(send_content);
                r = true;
                break;

            case 'GET':
                xmlhttp.open('GET', url + (arguments[3] ? arguments[3] + '?' : ''), true);
                xmlhttp.send(null);
                r = true;
                break;

            case 'HEAD':
                xmlhttp.open('HEAD', url, true);
                xmlhttp.send(null);
                var r = true;
                break;

            default:
                break;
        }
    }

    return r;
}