ajax = Class.create();
ajax.prototype = {
	initialize: function(url, options){
		this.transport = this.getTransport();
		this.postBody = options.postBody || '';
		this.method = options.method || 'post';
		this.onComplete = options.onComplete || null;
		this.onError = $(options.onError) || null;
		this.errorText = options.errorText || null;
		this.displayText = options.displayText || null;
		this.request(url);
	},

	request: function(url){
		try{
			showAjax(this.displayText);
			this.transport.open(this.method, url, true);
			this.transport.onreadystatechange = this.onStateChange.bind(this);
			if (this.method == 'post') {
				this.transport.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
				if (this.transport.overrideMimeType) this.transport.setRequestHeader('Connection', 'close');
			}
			this.transport.send(this.postBody);
		}catch(f){
			alert('Error occured: ' + f);
			if (this.onError)
				setTimeout(function(){this.onError.innerHTML = this.errorText;}.bind(this), 1);
			hideAjax();
		}
	},

	onStateChange: function(){
		if ((this.transport.readyState == 4) || (this.transport.readyState == 0)) {
			if ((this.transport.status == 200) || (this.transport.status == 0)){
				if (this.onComplete){ 
					setTimeout(function(){this.onComplete(this.transport);}.bind(this), 1);
					hideAjax();
				}
				this.transport.onreadystatechange = function(){};
			}else{
				if (this.onError){
					alert(this.transport.status);
					setTimeout(function(){this.onError.innerHTML = this.errorText;}.bind(this), 1);
					hideAjax();
				}
				this.transport.onreadystatechange = function(){};
			}
		}
	},

	getTransport: function() {
		if (window.ActiveXObject) return new ActiveXObject('Microsoft.XMLHTTP');
		else if (window.ActiveXObject) return new ActiveXObject('Msxml2.XMLHTTP');
		else if (window.XMLHttpRequest) return new XMLHttpRequest();
		else return false;
	}
};