var ajax = {
	loaded		: false,
	xml			: false,
	IFrameObj	: false,
	metodo		: "post",
	uri			: "",
	pureAjax	: true,
	upload		: false,
	path		: '',
	_preCall		: '',
	_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(){
	// ajax for IE
	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; }

	// pseudo ajax based on iframe for browsers that doesn't support ajax
	// or for file uploads
	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 is for IE5 Mac, because it will only
		// allow access to the document object
		// of the IFrame if we access it through
		// the document.frames array
		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;
	}
	eval(this.preCall());
	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.save = function(file,azione,funzione){
	if(this.loaded && dom.loaded){
		funzione = (typeof funzione=='undefined')?'ajax.parse':funzione;
		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.action = this.path+file+"?funct="+funzione;
			form.submit();
		} else {
			this.query(file,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);
		}
	}
}

ajax.doUpload = function(file,azione){
	if(this.loaded && dom.loaded){
		this.preCall();
		form.action = file+"?funct=ajax.parse";
		form.azione.value = azione;
		form.target = 'salvataggio';
		form.submit();
		/*funzione = (typeof funzione=='undefined')?'ajax.parse':funzione;
		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;
			}
		}
		disableTiny();
		form.method = this.metodo;
		form.target = this.IFrame.name;
		form.action = this.path+file+"?funct="+funzione;
		form.submit();*/
	} 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);
		}
	}
}

ajax.load = function(file,azione,funzione){
	if(this.loaded && dom.loaded && form){
		//if(azione.indexOf('&sezione')==-1){ azione += "&sezione="+form.sezione.value; }
		//if(azione.indexOf('&area')==-1){ azione += "&area="+form.area.value; }
		this.query(file,"azione="+azione);
	} else {
		if(in_array(this.queue,"ajax.load('"+file+"','"+(azione?azione:'')+"'"+(typeof funzione!="undefined" && funzione!=''?",'"+funzione+"'":"")+")")==-1){
			this.queue[this.queue.length] = "ajax.load('"+file+"','"+(azione?azione:'')+"'"+(typeof funzione!="undefined" && funzione!=''?",'"+funzione+"'":"")+")";
		}
		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+";");
			//xml = undefined;
		} else {
			if(this.xml.readyState==4){
				switch(this.xml.status){
					case 404:
						alert("Pagina non trovata! Errore 404.");
					break;
					default:
						alert("Errore "+this.xml.status);
					break;
				}
				this.upload = false;
				this.postCall();
			}
			return;
		}
	}
	if(typeof msg=="object"){
		if(typeof msg['id']!="undefined"){
			form.id.value = msg['id'];
		}

		if(typeof msg['pre_function']!="undefined" && msg['pre_function']!=""){
			eval(msg['pre_function']);
			if(msg['pre_function']=="doLogout();"){ return; }
		}
		form.azione.value='';
		switch(msg['azione']){
			case "addpost":
				setStyle(getElement('postthank'),'display','block');
				setStyle(getElement('addpost'),'display','none');
				getElement('commenta').disabled=false;
				getElement('addpostlink').innerHTML = '+';
				form.reset();
			break;
			case "writemsg":
				getElement(msg['target']).innerHTML = msg['msg'];
			break;
			case "error":
				alert(msg['msg']);
			break;
			default:
			break;
		}
		if(typeof msg['error']=="string" && msg['error']!=""){
			alert(msg['error']);
		}
		if(typeof msg['post_function']!=="undefined" && msg['post_function']!=""){
			eval(msg['post_function']);
		}
		msg = "";
	}
	this.upload = false;
	this.postCall();
}

ajax.preCall = function(){
	this.call = true;
	eval(this._preCall);
	this._preCall = '';
	//disableTiny();
}

ajax.postCall = function(){
	//if(this.xml.readyState==4 && this.xml.status=="200"){
		this.call = false;
		if(this.queue.length==0){
			eval(this._postCall);
			this._postCall = '';
		}
	//}
	this.checkQueue();
}

ajax.appendQueue = function(call){

}

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!=''){
			eval(this.postQueue);
			this.postQueue = '';
		}
	}
}

var form;
//dom.functions[dom.functions.length] = "form = document.forms['commentform'];";
//dom.functions[dom.functions.length] = "ajax.init()";
form = document.forms['commentform'];
ajax.init();

function loadForm(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!="") ){
				if(type=="hidden" && getElement(name+'___Frame')!=null){ //fck element
					str += "&"+name+"="+fetchFCK(name);
				} else {
					str += "&"+name+"="+encodeURIComponent(escape(value));
				}
			}
		}
	}
	return str;
}
