/*
	XMLUtilityClass
	Last modified 2007-03-30
*/

var XML_READYSTATE_COMPLETE = 4;
var XML_STATUS_OK = 200;

function XmlUtilityClass() {
	this.xmlRequest = null;
	this.readyStateChangeFunction = null;
	this.requestError = false;
	
	// begin attempting to load an XML document
	this.getXml = function(url, readyStateChangeFunction, sendText, method, sync, contentType) {
		if (!sendText) sendText = "";
		if (!method) method = "GET";
		if (method=="GET") {
			// force IE not to cache GET requests
			url += (url.indexOf("?")>=0 ? "&" : "?") + "donotcache="+(new Date()).getTime();
		}
		this.requestError = false;
		if (!this.xmlRequest) this.xmlRequest = this.getRequestObject();
		if (this.xmlRequest) {
			this.xmlRequest.open(method, url, !sync);
			this.xmlRequest.onreadystatechange = readyStateChangeFunction;
			//if (method=="POST") this.xmlRequest.setRequestHeader("Man", "POST "+url+" HTTP/1.1");
			if (contentType) this.xmlRequest.setRequestHeader("Content-Type", contentType)
			this.xmlRequest.send(sendText);
		}
	}
	
	// begin posting XML to the server
	this.postXml = function(url, readyStateChangeFunction, sendText, method, sync, contentType) {
		if (!contentType) contentType = "text/xml";
		this.getXml(url, readyStateChangeFunction, sendText, "POST", sync, contentType);
	}
	
	// abort a request
	this.abort = function() {
		if (this.xmlRequest) this.xmlRequest.abort();
	}
	
	this.getDoc = function() {
		if (this.xmlRequest) return(this.xmlRequest.responseXML);
		else return(null);
	}
	
	// return a XML HttpRequest object
	this.getRequestObject = function() {
		var request = null;
		if (window.XMLHttpRequest) {
			try {
				request = new XMLHttpRequest();
			} catch(e) {
				request = null;
			}
		} else if (window.ActiveXObject) {
			try {
				request = new ActiveXObject("Msxml2.XMLHTTP");
			} catch(e) {
				try {
					request = new ActiveXObject("Microsoft.XMLHTTP");
				} catch(e) {
					request = null;
				}
			}
		}
		return(request);
	}

	// status methods
	this.isRequestComplete = function() {
		if (this.xmlRequest && this.xmlRequest.readyState==XML_READYSTATE_COMPLETE) {
			if (this.xmlRequest.status == XML_STATUS_OK) {
				return(true);
			} else {
				this.requestError = true;
				return(false);
			}
		} else return(false);
	}
	this.isRequestError = function() {
		return(this.requestError);
	}
	this.getStatus = function() {
		return(this.xmlRequest ? this.xmlRequest.statusText : null);
	}

	return(this);
}

// create default instance of class
var XmlUtility = new XmlUtilityClass();

