// ajaxXMLDOC class

function ajaxXMLDoc() {
	this.debugid = 'ajaxoutput';
	this.debug = _ajaxdebug;
	this.clearDebug = _ajaxclearDebug;

	this._ajaxinit = _ajaxinit;
	this._ajaxprocessStateChange = _ajaxprocessStateChange;
	
	this.postURL = _ajaxPostURL;
	this.getURL = _ajaxGetURL;
		
	this.isIE = false;
	this.ajaxHTTP = null;
	
	this.processing = false;
	
	this._ajaxinit();
}

function _ajaxinit() {
	try {
		if (window.XMLHttpRequest) {
			this.debug('Using window.XMLHttpRequest');
			this.ajaxHTTP = new XMLHttpRequest();
		} else if (window.ActiveXObject) {
			this.isIE = true;

            try {
                this.ajaxHTTP = new ActiveXObject("Msxml2.XMLHTTP");
				this.debug('Using ActiveXObject Msxml2.XMLHTTP');
            } catch (e) {
                try {
                    this.ajaxHTTP = new ActiveXObject("Microsoft.XMLHTTP");
	    			this.debug('Using ActiveXObject Microsoft.XMLHTTP');
            	} catch (e) {}
    				this.debug('Error getting activeX Version !!');
	        }
		}
	} catch (e) {
		var msg = (typeof e == "string") ? e : ((e.message) ? e.message : "Unknown Error");
		this.debug("ERROR AJAX loadXMLDoc:" + msg);
	}	
}

function _ajaxPostURL( strUrl, strPost ) {
	this.processing = true;
	this.clearDebug();
	this._ajaxinit();
	try {
		if (this.ajaxHTTP) {
			var tthis = this;
			this.ajaxHTTP.onreadystatechange = function() { tthis._ajaxprocessStateChange(); }
			this.ajaxHTTP.open("POST", strUrl, true);
			this.ajaxHTTP.send(strPost);
		} else {
			this.debug("Failed to create AJAX xml request object");
		}
	} catch (e) {
		var msg = (typeof e == "string") ? e : ((e.message) ? e.message : "Unknown Error");
		this.debug("ERROR AJAX loadXMLDoc:" + msg);
	}
}

function _ajaxGetURL( strUrl ) {
	this.processing = true;
	this.clearDebug();
	this._ajaxinit();
	try {
		if (this.ajaxHTTP) {
			var tthis = this;
			this.ajaxHTTP.onreadystatechange = function() { tthis._ajaxprocessStateChange(); }
			this.ajaxHTTP.open("GET", strUrl, true);
			this.ajaxHTTP.send("");
		} else {
			this.debug("Failed to create AJAX xml request object");
		}
	} catch (e) {
		var msg = (typeof e == "string") ? e : ((e.message) ? e.message : "Unknown Error");
		this.debug("ERROR AJAX loadXMLDoc:" + msg);
	}
}


function _ajaxprocessStateChange() {
	try	{
		this.debug("AJAX state:" + this.ajaxHTTP.readyState);

		if (this.ajaxHTTP.readyState == 4) {
			if (this.ajaxHTTP.status == 200) {
				this.processing = false;
				
				this.debug("responseText.length : " + this.ajaxHTTP.responseText.length);
				if (this.ajaxHTTP.responseXML) {
					this.debug(this.ajaxHTTP.responseText);
					if (this.processXML) {
						this.processXML(this.ajaxHTTP.responseXML);
					}
				} else {
					if (this.ajaxHTTP.responseText) {
						this.debug(this.ajaxHTTP.responseText);
					}
					this.debug("ERROR AJAX XML: empty xml response");
					if (this.processXML) {
						this.processXML(null);
					}
				}
			 } else {
				 this.processing = false;
				if (this.ajaxHTTP.responseText) {
					this.debug(this.ajaxHTTP.responseText);
				}
				this.debug("ERROR AJAX XML: " + this.ajaxHTTP.status + ":" + this.ajaxHTTP.statusText);
				if (this.processXML) {
					this.processXML(null);
				}
			 }
		}
	} catch (e) {
		var msg = (typeof e == "string") ? e : ((e.message) ? e.message : "Unknown Error");
		this.debug("ERROR AJAX processStateChange:" + msg);
	}
}

function _ajaxdebug( strMsg ) {
	var objOutput = document.getElementById(this.debugid);

	if (objOutput != null && typeof(objOutput.value) == 'string') {
		if (strMsg) {
			objOutput.value = strMsg + "\n" + objOutput.value;
		} else {
			objOutput.value = "undefined\n" + objOutput.value;
		}
	}
}

function _ajaxclearDebug() {
	var objOutput = document.getElementById(this.debugid);

	if (objOutput && typeof(objOutput.value) == 'string') {
		objOutput.value = "";
	}
}