/*
* XML HTTP Request Class
* 
* Allows the user of multiple XML HTTP requests to happen on the same page
* with the underlying connection processing wrapped up in a nice little class file
* 
* @author: stbarrett
* @date: 2/15/03
* 
*/


function XmlHttp() {
	this.doc = null;
	this.isIE = false;
	this.url = null;
	this.renderedDoc = null;
	this.status = "loading";
}

// Load the url
XmlHttp.prototype.load = function(url) {
	this.url = url;
	//set the var so we can scope the callback
	var _this = this;
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
		this.doc = new XMLHttpRequest();
        this.doc.onreadystatechange = function(){_this.processReqChange()};
        this.doc.open("GET", this.url, true);
        this.doc.send(null);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        this.doc = new ActiveXObject("Microsoft.XMLHTTP");
        if (this.doc) {
            this.doc.onreadystatechange = function(){_this.processReqChange()};
            this.doc.open("GET", url, true);
            this.doc.send();
        }
    }	
}

XmlHttp.prototype.processReqChange = function() {
    // only if doc shows "loaded"
    if (this.doc.readyState == 4) {
        // only if "OK"
        if (this.doc.status == 200) {
			this.renderedDoc = this.doc;
			this.status = "loaded";
         } else {
			this.status = "error";
         }
    }
}

XmlHttp.prototype.getDoc = function() {
	return this.renderedDoc;
}

XmlHttp.prototype.getStatus = function() {
	return this.status;
}

XmlHttp.prototype.getElementText = function(prefix, local, parentElem, index) {
    var result = "";
    if (prefix && isIE) {
        result = parentElem.getElementsByTagName(prefix + ":" + local)[index];
    } else {
        result = parentElem.getElementsByTagName(local)[index];
    }
    if (result) {
        if (result.childNodes.length > 1) {
            return result.childNodes[1].nodeValue;
        } else {
            return result.firstChild.nodeValue;
        }
    } else {
        return null;
    }
}
