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.save = function(file,azione,form,funzione){
	if(this.loaded && dom.loaded){
		funzione = (typeof funzione=='undefined')?'ajax.parse':funzione;
		if(typeof form.azione=='undefined'){ i = createElement('input','type','hidden','name','azione'); form.appendChild(i); }
		if(typeof form.iframe=='undefined'){ i = createElement('input','type','hidden','name','iframe'); form.appendChild(i); }
		form.azione.value = azione;
		for(i=0;i<form.elements.length;i++){
			if(form.elements[i].type == 'file' && form.elements[i].value!=""){
				this.upload = true;
				break;
			}
		}
		if(this.upload==true){
			this.preCall();
			disableTiny();
			form.method = this.metodo;
			form.target = this.IFrame.name;
			form.iframe.value=funzione;
			form.action = this.path+file;
			for (i=0;i<form.elements.length;i++){
				if(form.elements[i].type!='file'){
					form.elements[i].value = escape(form.elements[i].value);
				}
			}
			form.submit();
		} else {
			form.iframe.value="";
			this.query(file,this.loadForm(form),funzione);
		}
	} else {
		if(in_array(this.queue,"ajax.save('"+file+"','"+(azione?azione:'')+"'"+(typeof funzione!="undefined" && funzione!=''?",'"+funzione+"'":"")+")")==-1){
			this.queue[this.queue.length] = "ajax.save('"+file+"','"+(azione?azione:'')+"'"+(typeof funzione!="undefined" && funzione!=''?",'"+funzione+"'":"")+")";
		}
		if(this._queueTimerId==''){
			this._queueTimerId = setTimeout('ajax.checkQueue();',this.timer);
		}
	}
	return false;
}

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;
			case "get":
				document.location.replace(msg['target']);
			break;
			default:
			break;
		}
		if(typeof msg['post_function']!=="undefined" && msg['post_function']!=""){
			eval(msg['post_function']);
		}
	}
	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 = '';
		}
	}
}

ajax.loadForm = function(form){
	str = "";
	for(i=0;i<form.elements.length;i++){
		with(form.elements[i]){
			if(	((type=="text" || type=="hidden" || type=="password")) || (type=="select-one" && value!="+") || ((type=="radio" || type=="checkbox") && checked==true) || (type=="textarea" && value!="") ){
				str += "&"+name+"="+encodeURIComponent(value);
			}
		}
	}
	return str;
}

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