/*
 ajax request with polled callback.
 usage eg Ajax.request({uri:poll.xml", callback:this._loadedItem, scope:this, post:firstname=mark,surname=thomas});
*/
var Ajax = {
	bActive	: 0,
	queue 	: [],
	request : function(oRequest){
		if(arguments.length == 1) this.queue[this.queue.length] = oRequest;
		if(this.bActive) return;
		var oXML = oMTLib.xmlHTTP();
		oXML.open((oRequest.post)?"POST":"GET", oRequest.uri, true);
		if(oRequest.post){
			oXML.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=utf-8');
		}else{
			oXML.setRequestHeader('Content-Type','text/html;');
		};
		oXML.onreadystatechange = function(){
			Ajax._callback(oXML);
		};
		oRequest.post ? oXML.send(oRequest.post) : oXML.send(null);
		this.bActive = 1;
		delete oXML;
	},
	_callback : function(oXML){
		if(oXML.readyState == 4){
			this.queue.reverse();
			var oRequest = this.queue[this.queue.length-1];
			this.queue.length --;
			this.queue.reverse();
			if(oRequest.scope){
				oRequest.callback.apply(oRequest.scope, [oXML]);
			}else{
				oRequest.callback(oXML);
			};
			this.bActive = 0;
			if(this.queue.length){
				this.request(this.queue[0], 1);
			};
		};
	}
};

oMTLib.xmlHTTP = function(){
	if(window.XMLHttpRequest){
		return new XMLHttpRequest();
	};
	if(window.ActiveXObject){
		if(this.__MSXML()){
			return new ActiveXObject(this.sMSXMLtype + ".XMLHTTP");
		};
	};
	return false;
};
oMTLib.xmlDocument = function(){
	try{
		if(document.implementation && document.implementation.createDocument){
			var oDoc = document.implementation.createDocument("", "", null);
			return oDoc;
		};
		if(window.ActiveXObject){
			if(this.__MSXML()){
				return new ActiveXObject(this.sMSXMLtype + ".DOMDocument");
			}
		};
	}catch(e){};
	return false;
};
oMTLib.__MSXML = function(){
	if(!this.sMSXMLHTTPtype){
		this.aMSXML = new Array("MSXML2", "Microsoft", "MSXML", "MSXML3");
		var oTest;
		for(var i=0;i<this.aMSXML.length; i++){
			try{
				oTest = new ActiveXObject(this.aMSXML[i] + ".DOMDocument");
				oTest = new ActiveXObject(this.aMSXML[i] + ".XMLHTTP");
				this.sMSXMLtype = this.aMSXML[i];
				return this.sMSXMLtype;
			}catch(e){};
		};
	};
	return this.sMSXMLtype;
};
/****************************************************************************************
DOM additions
****************************************************************************************/

if(window.DOMParser && window.XMLSerializer && window.Node && Node.prototype && Node.prototype.__defineGetter__){
	Document.prototype.loadXML = function(s){
		var oDoc = (new DOMParser()).parseFromString(s, "text/xml");
		while(this.hasChildNodes())
	    	this.removeChild(this.lastChild);
		for(var i = 0; i < oDoc.childNodes.length; i++) {
			this.appendChild(this.importNode(oDoc.childNodes[i], true));
		};
	};
	Document.prototype.__defineGetter__("xml", function(){
		return (new XMLSerializer()).serializeToString(this);
	});
};