﻿/* --- AjaxAgent --- */

function AjaxAgent() {

    // internal code
    
    this.getXmlHttpObject = function(handler) { 
        var objXMLHttp=null
        if (window.XMLHttpRequest)
        {
            objXMLHttp=new XMLHttpRequest()
        }
        else if (window.ActiveXObject)
        {
            objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
        }
        return objXMLHttp
    }
    this.onStateChanged = function(xmlHttp) { 
        if (xmlHttp.readyState==4 && xmlHttp.status==200) {
            // successful request
            xmlHttp.responseHandler(xmlHttp.responseXML, xmlHttp.responseText, xmlHttp.dataObj);
        } else if(xmlHttp.readyState==4 && xmlHttp.status!=200) {
            // error occured in request
            alert("AjaxAgent request failed. HTTP Status code: " + xmlHttp.status + ", Requested URL:" + xmlHttp.requestedUrl);
            xmlHttp.responseHandler(null);
        }
    }

    // interface functions
    
    this.request = function(url, responseHandler, dataObj) {
        var xmlHttp = this.getXmlHttpObject();
        xmlHttp.responseHandler = responseHandler;
        xmlHttp.requestedUrl = url;
        xmlHttp.dataObj = dataObj;
        var thisObj = this;
        xmlHttp.onreadystatechange = function() { thisObj.onStateChanged(xmlHttp) };
        xmlHttp.open("GET", url, true)
        xmlHttp.send(null);
    }
}