/*
** Objects and prototypes handling AJAX metodology version 1.30 by Mariusz Lewandowski <tekno@vgr.pl> and Paweł Lewandowski <electro@vgr.pl>
*/

if(!window.XMLHttpRequest && window.ActiveXObject) {
	if(axoXHRName!==undefined) {
		throw('XMLHttpRequest initialization error: global variable axoXHRName already exists');
	}
	try {
		var axoXHRName = 'Msxml2.XMLHTTP';
		new ActiveXObject(axoXHRName);
	} catch(e) {
		try {
			axoXHRName = 'Microsoft.XMLHTTP';
			new ActiveXObject(axoXHRName);
		} catch(e) {
			axoXHRName = false;
			throw "Your browser does not support XMLHttpRequest() or Microsoft.XMLHTTP";
		}
	}
	if(axoXHRName) {
		var XMLHttpRequest = function() {
			this.axo = new ActiveXObject(XMLHttpRequest.axoXHRName);
			var onrsc = function() {
				var xhr = arguments.callee.xhr;
				xhr.readyState = xhr.axo.readyState;
				if(xhr.readyState==4) {
					xhr.responseText = xhr.axo.responseText;
					xhr.responseXML = xhr.axo.responseXML;
					xhr.status = xhr.axo.status;
					xhr.statusText = xhr.axo.statusText;
				}
				if(xhr.onreadystatechange) {
					xhr.onreadystatechange();
				}
			}
			onrsc.xhr = this;
			this.axo.onreadystatechange = onrsc;
		}
		XMLHttpRequest.axoXHRName = axoXHRName;
		delete axoXHRName;
		XMLHttpRequest.prototype.responseXML = null;
		XMLHttpRequest.prototype.responseText = '';
		XMLHttpRequest.prototype.status = 0;
		XMLHttpRequest.prototype.statusText = '';
		XMLHttpRequest.prototype.abort = function() {
			this.axo.abort();
		}
		XMLHttpRequest.prototype.getAllResponseHeaders = function() {
			return this.axo.getAllResponseHeaders();
		}
		XMLHttpRequest.prototype.getResponseHeader = function(headerlabel) {
			return this.axo.getResponseHeader(headerlabel);
		}
		XMLHttpRequest.prototype.open = function(method,url,asyncflag,username,password) {
			this.responseXML = null;
			this.responseText = '';
			this.status = 0;
			this.statusText = '';
			this.axo.open(method,url,asyncflag,username,password);
		}
		XMLHttpRequest.prototype.send = function(content) {
			this.axo.send(content);
		}
		XMLHttpRequest.prototype.setRequestHeader = function(label,value) {
			this.axo.setRequestHeader(label,value);
		}
		XMLHttpRequest.prototype.readyState = 0;
		XMLHttpRequest.prototype.onreadystatechange = null;
	}
}

if(window.XMLHttpRequest) {
	var AjaxLoader = function() {}
	AjaxLoader.prototype.responseXML = null;
	AjaxLoader.prototype.responseText = '';
	AjaxLoader.prototype.status = 0;
	AjaxLoader.prototype.statusText = '';
	AjaxLoader.prototype.statefulLoad = function(url, xhlorsc, content) {
		xhr = new XMLHttpRequest();
		xhr.open(content ? 'post' : 'get', url, Boolean(xhlorsc));
		if(content) {
			xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		}
		if(xhlorsc) {
			var xhrorsc = function() {
				var xhl = arguments.callee.xhl;
				var xhr = arguments.callee.xhr;
				xhl.readyState = xhr.readyState;
				if(xhl.readyState==4) {
					if(xhr.status != 200) {
						throw "Invalid http response code: " + xhr.status;
					}
					delete arguments.callee.xhl;
					delete arguments.callee.xhr;
					xhl.responseText = xhr.responseText;
					xhl.responseXML = xhr.responseXML;
					xhl.status = xhr.status;
					xhl.statusText = xhr.statusText;
				}
				if(xhlorsc instanceof Function) {
					xhlorsc(xhl.readyState);
				}
			}
			xhrorsc.xhl = this;
			xhrorsc.xhr = xhr;
			xhr.onreadystatechange = xhrorsc;
		}
		xhr.send(content);
		if(!xhlorsc) {
			this.responseXML = xhr.responseXML;
			this.responseText = xhr.responseText;
			this.status = xhr.status;
			this.statusText = xhr.statusText;
		}
	}
	AjaxLoader.prototype.load = function(url, onload, content) {
		return this.statefulLoad(
			url,
			onload instanceof Function ?
			function(readyState) {
				if(readyState==4) {
					onload();
				}
			} :
			false,
			content
		);
	}
}

