var ajax = {
	loaded		: false,
	xml			: false,
	IFrameObj	: false,
	metodo		: "post",
	uri			: "",
	pureAjax	: true,
	upload		: false,
	path		: '',
	_preCall	: '',
	_preParse	: '',
	_postCall	: '',
	queue		: new Array(),
	call		: false,
	timer 		: 0,
	postQueue	: '',
	timer		: 500,
	_queueTimerId: ''
};

ajax._ieFix = function(){
	if(typeof ActiveXObject!='undefined'){
		var msxmlhttp = new Array(
					'Msxml2.XMLHTTP.5.0',
					'Msxml2.XMLHTTP.4.0',
					'Msxml2.XMLHTTP.3.0',
					'Msxml2.XMLHTTP',
					'Microsoft.XMLHTTP'
					);
	
		for (idx in msxmlhttp) {
			try {
				this.xml = new ActiveXObject(msxmlhttp[idx]);
				break;
			} catch (e) { }
		}
	}
}

ajax.init = function(){
	if(typeof XMLHttpRequest != "undefined"){
		this.xml = new XMLHttpRequest();
	} else {
		if(typeof ActiveXObject!='undefined'){
			var msxmlhttp = new Array(
					'Msxml2.XMLHTTP.5.0',
					'Msxml2.XMLHTTP.4.0',
					'Msxml2.XMLHTTP.3.0',
					'Msxml2.XMLHTTP',
					'Microsoft.XMLHTTP'
					);
			for (idx in msxmlhttp) {
				try {
					this.xml = new ActiveXObject(msxmlhttp[idx]);
					break;
				} catch (e) { }
			}
		}
	}
	if(!this.xml){ this.pureAjax = false; }
	var tempIFrame = createElement('IFRAME','id','ajaxFrame','name','ajaxFrame');
	setStyle(tempIFrame,'border','0px');
	setStyle(tempIFrame,'width','0px');
	setStyle(tempIFrame,'height','0px');
	setStyle(tempIFrame,'display','none');
	setStyle(tempIFrame,'visibility','hidden');

	this.IFrame = document.body.appendChild(tempIFrame);
	if (document.frames) {
		this.IFrame = document.frames['ajaxFrame'];
	}
	this.loaded = true;
}

ajax.query = function(file,uri,funzione){
	if(this.call){
		if(in_array(this.queue,'ajax.query("'+file+'","'+uri+'"'+(typeof funzione!="undefined"?',"'+funzione+'"':"")+');')==-1){
			this.queue[this.queue.length] = 'ajax.query("'+file+'","'+uri+'"'+(typeof funzione!="undefined"?',"'+funzione+'"':"")+');';
		}
		if(this._queueTimerId == ''){
			this._queueTimerId = setTimeout('ajax.checkQueue()',this.timer);
		}
		return;
	}
	if(typeof funzione=='undefined'){ funzione='ajax.parse();'; } else if(funzione=='nulla'){ funzione = 'function(){}'; } else { funzione = (funzione.indexOf('();')!=-1)?funzione:funzione+"();" }
	if(this.pureAjax){
		this._ieFix();
		this.xml.onreadystatechange = function(){ eval(funzione); }
		if(this.metodo=="post"){
			this.xml.open("POST", this.path+file, true);
			this.xml.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			this.xml.send(uri);
		} else {
			this.xml.open("GET", this.path+file+"?"+uri, true);
			this.xml.send(null);
		}
	} else {
		form.submit();
	}
}

ajax.load = function(file,azione,funzione){
	if(this.loaded && dom.loaded){
		this.query(file,"azione="+azione);
	} else {
		if(this._queueTimerId == ''){
			this._queueTimerId = setTimeout('ajax.checkQueue()',this.timer);
		}
	}
}

ajax.parse = function(text){
	if(typeof text == "object"){
		msg = text;
	} else {
		if(this.xml.readyState==4 && this.xml.status=="200"){
			msg = this.xml.responseText;
			eval("msg = "+msg+";");
		}
	}
	if(typeof msg=="object"){
		switch(msg['azione']){
			case "load": //write HTML in a specific target identified by msg['target']
				if(msg['target'] != undefined && getElement(msg['target'])){
					getElement(msg['target']).innerHTML = msg['html'];
				}
			break;
			default:
			break;
		}
		msg = "";
	}
}

ajax.checkQueue = function(){
	this._queueTimerId = '';
	if(this.queue.length>0){
		if(this.call==false){
			call = this.queue.shift();
			call = call.replace(/ajax./,'this.');
			eval(call);
			if(this.queue.length>0){
				this._queueTimerId = setTimeout('ajax.checkQueue()',this.timer);
			}
		} else {
			this._queueTimerId = setTimeout('ajax.checkQueue()',this.timer);
		}
	} else {
		if(this.postQueue!=''){
			r = (this.postQueue.split(';'));
			for(i=0;i<r.length;i++){
				eval(r[i]);
			}
			this.postQueue = '';
		}
	}
}

dom.functions[dom.functions.length] = "ajax.init();";
