/////////////////////////////////
// AJAX /////////////////////////
/////////////////////////////////

var RS_UNINITIALIZED = 0;       // The object has been created, but has not been initialized (the open method has not been
var RS_LOADING = 1;             // The object has been created but the send method has not been called.
var RS_LOADED = 2;              // The send method has been called and the status and headers are available, but th
var RS_INTERACTIVE = 3;         // Some data has been received. Get the partial results using responseBody and responseText
var RS_COMPLETED = 4;           // All the data has been received, and is available.

function HttpRequest(url,param,method,output_type) {
        var needProcessReq = false;  // I want to wait for a reply before carrying on

        if (window.XMLHttpRequest) {
                try {
                        req = new XMLHttpRequest();
                } catch (e) {
                        req = false;
                }

                if (req) {
                        req.onreadystatechange = ProcessReqChange;
                        if (method == "POST") {
                                req.open(method, url, needProcessReq);
                                req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
                                req.send(param);
                        } else {
                                req.open(method, url + "?" + param, needProcessReq);
                                req.send(null);
                        }
                }
        } else if (window.ActiveXObject) {
                isIE = true;
                try {
                        req = new ActiveXObject("Msxml2.XMLHTTP");
                } catch (e) {
                        try {
                                req = new ActiveXObject("Microsoft.XMLHTTP");
                        } catch (e) {
                                alert("Your browser does not support Ajax (2) " + e);
                                req = false;
                        }
                        alert("Your browser does not support Ajax (1) " + e);
                }

                if (req)
                {
                        if(needProcessReq == true) { req.onreadystatechange = ProcessReqChange; }
                        if (method == "POST") {
                                req.open(method, url, needProcessReq);
                                req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                                req.send(param);
                        } else {
                                req.open(method, url + "?" + param, needProcessReq);
                                req.send(null);
                        }
                }
        }

        if (output_type.toUpperCase() == "XML") {
                return req.responseXML;
        } else if (output_type.toUpperCase() == "JSON") {
                return eval("(" + req.responseText + ")");
        } else {
                return req.responseText;
        }
}

function ProcessReqChange() {
        if (req.readyState == RS_COMPLETED) {
                if (req.status == 200) {
                        //alert('SUCCES!!');
                } else {
                        alert("Une erreur est survenue lors de l'execution:\\n" + req.status + " " + req.statusText);
                }
        }
}
