Root.Classes.XMLHTTP = function(a) { this.init(a); }

Root.Classes.XMLHTTP.prototype = {

	init: function(a) {
		this.url = a.url;
		this.queue = [];
		this.queuePtr = 0;
		this.separator = a.separator || '\n-=-=-\n';
		this.isBusy = false;
		this.async = true;
		this.request = {};
		this.createHandler(); // avoid firefox bug
	},

	exec: function(a) {
		var url = this.url, i;
		for (i in a) if (i!="_POST") url += i+'='+escape(a[i])+'&';
		this.add({url:url, post:a._POST});
	},

	createHandler: function() {
		this.handler = false;
		/*@cc_on @*/
		/*@if (@_jscript_version >= 5) try { this.handler = new ActiveXObject("Msxml2.XMLHTTP"); } 
			 catch (e) { try { this.handler = new ActiveXObject("Microsoft.XMLHTTP"); } 
			 catch (E) { this.handler = false;	} }
		@end @*/
		if (!this.handler && typeof XMLHttpRequest!='undefined') this.handler = new XMLHttpRequest();
	},
	
	syncSend: function(a) {
		var url = this.url, i;
		for (i in a) if (i!="_POST") url += i+'='+escape(a[i])+'&';
		this.request.url = url; this.request.post = a._POST;

		var method = (typeof(this.request.post)!="undefined" && this.request.post!="") ? "POST" : "GET";

		this.createHandler(); // avoid firefox bug

		this.isBusy = true;

		this.handler.open(method, this.request.url, false);

		if (method=="POST") {
			this.handler.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		}

		try { 
			this.handler.send(this.request.post); 
		} catch(e) { 
			return false; 
		}
		return this.handler.responseText;
	},

	send: function() {
		var method = (typeof(this.request.post)!="undefined" && this.request.post!="") ? "POST" : "GET";

//		this.isBusy = true;

		this.createHandler(); // avoid firefox bug

		this.handler.open(method, this.request.url, true);

		if (method=="POST") {
			this.handler.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		}

		this.handler.onreadystatechange = this.onReadyStateChange;

		try { 
			this.handler.send(this.request.post); 
		} catch(e) { 
			return false; 
		}
		return true;
	},

	onReadyStateChange: function() {
		var self = Root.XMLHTTP;

		if (self.handler.readyState==4) {
			delete self.queue[self.queuePtr];
			self.queuePtr++;
			self.isBusy = false;
			self.processResponse();
			self.processQueue();
		}
	},

	add: function(a) {
		this.queue[this.queue.length] = a;
		if (!this.isBusy) this.processQueue();
	},

	processQueue: function() {
		if (this.queuePtr>=this.queue.length) return;

		this.request = this.queue[this.queuePtr];
		this.result = this.send();
	},

	processResponse: function() {
		var i;
		var result = this.handler.responseText;
		var sections = result.split(this.separator);
		for (var s in sections) {
			var section = sections[s].toString().replace(/(^\s+|\s+$)/,'');

			var pos = section.indexOf('\n');
			var param = section.substring(0,pos).split('/');
			for (i=0; i<param.length; i++) param[i] = param[i].replace(/\s/,'');
			var text = section.substring(pos+1,section.length);
			switch (param[0]) {
				case "id":
/*
					if (this.queue[param[1]]) {
						this.queuePtr--;
						delete this.queue[param[1]];
					} else {
						alert("Problem (id): task # "+param[1]+" doesn't exist in the queue.");
					}
*/
					break;

				case "js":
//alert("JS:\n\n"+text);
					try { eval(text); } 
					catch(e) { alert('Problem (js): '+e.description+'\n\n'+text); }
					break;

				case "html":
					try { 
						if (param.length==3) {
							top.frames[param[1]].document.getElementById(param[2]).innerHTML = text;
						} else {
//alert("HTML: "+param[1]+"\n\n"+text);
							document.getElementById(param[1]).innerHTML = text;
						}
					}
					catch(e) { alert('Problem (html): '+e.description+'\n\n'+section); }
					break;

				default:
//alert(param[0]);
					try { eval(sections[s]); } 
					catch(e) { alert('Problem (default): '+e.description+'\n\n'+section); }
			}
		}
	}

}
