
function clsAjax() {
	this.instance		= null;
	this.callback		= null;
	this.async		= false;

	this.method		= "GET";
	this.url		= null;
	this.headers		= new Object();
	this.postFields		= new Object();
	this.dataToSend		= null;

	this.inProcess		= false;

	this.responseText	= "";
	this.responseXML	= null;

	this._constructor = function() {
		this.instance	= null;
		if(window.XMLHttpRequest) {
			this.instance = new XMLHttpRequest();
		} else if(window.ActiveXObject) {
			try {
				this.instance = new ActiveXObject("MSXML2.XMLHTTP");
			} catch(ex) {
				try {
					this.instance = new ActiveXObject("Microsoft.XMLHTTP");
				} catch(ex2) {
					throw("No ActiveX object to provide XML over HTTP.");
				}
			}
		} else {
			throw("Couldn't create an XMLHttpRequest instance");
		}
	}

	this.fetchData = function() {
		var ajax	= this.instance;
		var dataToSend	= (this.method == "POST") ? this.getPOSTString() : this.dataToSend;

		// Check if a URL has been passed along.
		if(!this.url) {
			throw("An URL is needed");
		}
		ajax.open(this.method, this.url, this.async);

		// Set the callback.
		ajax.onreadystatechange = (this.async) ? function() { this.responseHandler(this) } : function() {};

		// Avoid IE from having the response cached by adding a random string at the end.
		var d = new Date();
		this.url += ((this.url.indexOf('?') == -1) ? "?" : "&") + 'ajaxtime=' + d.getTime();

		for(var header in this.headers) {
			ajax.setRequestHeader(header, this.headers[header]);
		}

		ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		ajax.setRequestHeader('Content-Length', dataToSend.length);
		ajax.send(dataToSend);

		if(!this.async) {
			this.responseHandler(this);
		} else {
			this.inProcess = true;
		}

		return 0;
	}

	this.responseHandler = function(oInstance) {
		var ajax = oInstance.instance;

		if(ajax.readyState == 4) {
			oInstance.inProcess = false;
			if(ajax.status == 200) {
				oInstance.responseText	= ajax.responseText;
				oInstance.responseXML	= ajax.responseXML;
			}

			if(oInstance.loadEvent) {
				oInstance.loadEvent(ajax.status, oInstance);
			}
		}
	}

	this.addHeader = function(header, value) {
		this.headers[header] = value;
	}

	this.addPostField = function(field, value) {
		this.postFields[field] = value;
	}

	this.getPOSTString = function() {
		var result = "";

		for(var post in this.postFields) {
			result += escape(post).replace('+', '%252b') + '=' + escape(this.postFields[post]).replace('+', '%252b') + '&';
		}

		result = result.substring(0, result.length - 1);
		return result;
	}

	this._constructor();
}

