//XMLDataHandler provides a means to get site data through JavaScript
var XMLDataHandler_XMLDataHandler = null; //global

function XMLDataHandler(url,async,func) {
    this._url = url;
    this._async = async;
    this._function = func;
    this._req = null; //an XMLHttpRequest object
    this._initialized = false; //gets set to true when its request is finished
    this._req = this.getRequestObj(); //set up the XMLHttpRequest
} 

//used to add anonymous functions as methods
var _XMLDataHandler_inst = XMLDataHandler;

_XMLDataHandler_inst.prototype.getRequestObj = function() {
    var req = false;
    // branch for native XMLHttpRequest object
    if(window.XMLHttpRequest) {
    	try {
            req = new XMLHttpRequest();
        } catch(e) {
            req = false;
        }
        // branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
       	try {
            req = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
            try {
                req = new ActiveXObject("Microsoft.XMLHTTP");
            } catch(e) {
                req = false;
            }
        }
    }
    return(req);
}

_XMLDataHandler_inst.prototype.submit = function(varHT) {
    if (this._req == null) {
        return(null);
    }

    var url = this._url;
    if (varHT != null) {
        var n;
        var sep = '?';
        for (n in varHT) {
            url += sep + n + '=' + escape(varHT[n]);
            sep = '&';
        }
    }
    
    //if a function is supplied, it is the responsibility of the caller to set this._initialized
    if (this._function != null) this._req.onreadystatechange = this._function;
    else this._req.onreadystatechange = this.finishReq;

    this._req.open("GET",url,this._async);
    this._req.send('');
    return(this._req); //could wait forever if _async is false
}

_XMLDataHandler_inst.prototype.post = function(varHT) {
    if (this._req == null) return(null);

    var url = this._url;
    var data = '';
    if (varHT != null) {
        var n;
        var sep = '';
        for (n in varHT) {
            data += sep + n + '=' + escape(varHT[n]);
            sep = '&';
        }
    }
    
    //if a function is supplied, it is the responsibility of the caller to set this._initialized
    if (this._function != null) this._req.onreadystatechange = this._function;
    else this._req.onreadystatechange = this.finishReq;

    this._req.open("POST",url,this._async);
    this._req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    this._req.send(data);

    return(this._req); //could wait forever if _async is false
}

_XMLDataHandler_inst.prototype.finishReq = function() {
    if (this._req == null) return;
                                   
    if (this._req.readyState == 4) this._initialized = true;
}

_XMLDataHandler_inst.prototype.isInitialized = function() {
    if (!this._initialized) {
        if (this._req.readyState == 4) this._initialized = true;
    }                                             
    return(this._initialized);
}

_XMLDataHandler_inst.prototype.getReq = function() {
    return(this._req);
}

_XMLDataHandler_inst.prototype.getXMLDataHandler = function() {
    return(XMLDataHandler_XMLDataHandler);
}

_XMLDataHandler_inst.prototype.setXMLDataHandler = function(XMLDataHandler) {
    XMLDataHandler_XMLDataHandler = XMLDataHandler;
}

