function crea_http_req() {
	var req = false;
	if (typeof XMLHttpRequest != "undefined")
		req = new XMLHttpRequest();
	if (!req && typeof ActiveXObject != "undefined") {
		try {
			req=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e1) {
			try {
				req=new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e2) {
				try {
					req=new ActiveXObject("Msxml2.XMLHTTP.4.0");
				} catch (e3) {
					req=null;
				}
			}
		}
	}
	if(!req && window.createRequest)
	req = window.createRequest();
	if (!req) alert("Il browser non supporta AJAX");
	return req;
}

// l'oggetto per comunicare con il server
var	http_req = crea_http_req();
var tiposerver=document.location.href;
tiposerver=tiposerver.split(":");
tiposerver=tiposerver[0];

function attendi(cosa) {
	if (cosa=="blocca") {
		document.body.style.cursor='wait';
	} else {
		document.body.style.cursor='default';
	}
}

function aggiorna (cosa,val,dove) {
	if (dove != 'ricerca') {
			var dati_post = "tab="+cosa+"&rif="+val+'&aaa=aaa';
	} else {
		var dati_post = "tab="+cosa+"&rif="+val+'&aaa=bbb';
	}
	new Ajax.Updater(cosa, '/ajax/aggiorna.php', {
				method: 'post',	 
				parameters: dati_post,
				onCreate: disabilita_select(),
				onComplete: function() { 
					$(cosa).selectedIndex=0; 
					abilita_select();
				}
			});
}

function disabilita_select() {
	a=document.getElementsByTagName("select");
	num=a.length;
	//alert(num);
	for(i=0; i<num; i++) {
		a[i].disabled=true;	
	}

}

function abilita_select() {
	a=document.getElementsByTagName("select");
	num=a.length;
	//alert(num);
	for(i=0; i<num; i++) {
		a[i].disabled=false;	
	}

}
		
function ctlaggut(cosa) {
	if (http_req.readyState == 4) {
		var esito = http_req.responseText;
		//alert(esito);
		esito=esito.split("|");
		switch (cosa) {
			case('citta'):
				for (i=0;i<document.utente.citta.options.length;i++) {
					document.utente.citta.options[i]= new Option ('','');
				}
				for (i=1;i<esito.length+1;i++) {
					k=i-1;
					a=esito[k].split("=");
					document.utente.oggetti.options[i] = new Option (urldecode(urldecode(urldecode(a[0]))),a[1]);
				}
			break;
			case('tipologie'):
				for (i=0;i<document.utente.tipologia.options.length;i++) {
					document.utente.tipologia.options[i]= new Option ('','');
				}
				for (i=1;i<esito.length+1;i++) {
					k=i-1;
					a=esito[k].split("=");
					document.utente.tipologia.options[i] = new Option (urldecode(urldecode(urldecode(a[0]))),a[1]);
				}
			break;
		}
		attendi('sblocca');
	} else {
		setTimeout("ctlaggut('"+cosa+"')",0100);
		//http_req.onreadystatechange = ctlagg(cosa,dove);
	}
}
	
function ctlaggogg(cosa) {
	if (http_req.readyState == 4) {
		var esito = http_req.responseText;
		//alert(esito);
		esito=esito.split("|");
		switch (cosa) {
			case('citta'):
				for (i=0;i<document.oggetti.citta.options.length;i++) {
					document.oggetti.citta.options[i]= new Option ('','');
				}
				for (i=1;i<esito.length+1;i++) {
					k=i-1;
					a=esito[k].split("=");
					document.oggetti.citta.options[i] = new Option (urldecode(urldecode(urldecode(a[0]))),a[1]);
				}
			break;
			case('tipologie'):
				for (i=0;i<document.oggetti.tipologia.options.length;i++) {
					document.oggetti.tipologia.options[i]= new Option ('','');
				}
				for (i=1;i<esito.length+1;i++) {
					k=i-1;
					a=esito[k].split("=");
					document.oggetti.tipologia.options[i] = new Option (urldecode(a[0]),a[1]);
				}
			break;
		}
		attendi('sblocca');
	} else {
		setTimeout("ctlaggogg('"+cosa+"')",0100);
		//http_req.onreadystatechange = ctlagg(cosa,dove);
	}
}
	
	
function ctlaggric(cosa) {
	if (http_req.readyState == 4) {
		var esito = http_req.responseText;
		//alert(esito);
		esito=esito.split("|");
		switch (cosa) {
			case('citta'):
				for (i=0;i<document.ricerca.citta.options.length;i++) {
					document.ricerca.citta.options[i]= new Option ('','');
				}
				for (i=1;i<esito.length+1;i++) {
					k=i-1;
					a=esito[k].split("=");
					document.ricerca.citta.options[i] = new Option (urldecode(urldecode(urldecode(a[0]))),a[1]);
				}
			break;
			case('tipologie'):
				for (i=0;i<document.ricerca.tipologia.options.length;i++) {
					document.ricerca.tipologia.options[i]= new Option ('','');
				}
				for (i=1;i<esito.length+1;i++) {
					k=i-1;
					a=esito[k].split("=");
					document.ricerca.tipologia.options[i] = new Option (urldecode(urldecode(urldecode(a[0]))),a[1]);
				}
			break;
		}
		attendi('sblocca');
	} else {
		setTimeout("ctlaggric('"+cosa+"')",0100);
		//http_req.onreadystatechange = ctlagg(cosa,dove);
	}
}


function evidenzia (id) {
	var dati_post="id="+id;	
	if (http_req.readyState!=1) {
		http_req.open('POST', ''+tiposerver+'://'+window.location.hostname+'/ajax/evidenzia.php', false);
	}
	http_req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	http_req.setRequestHeader("Content-length", dati_post.length);
	http_req.setRequestHeader("Connection", "close");
	http_req.send(dati_post);
	var esito = http_req.responseText;
	if (esito!="si") {
		alert('Impossibile evidenziare\n\rErrore:\n\r'+esito);
	}
}

function cancellaoggetto (id) {
	var dati_post="id="+id;	
	if (http_req.readyState!=1) {
		http_req.open('POST', ''+tiposerver+'://'+window.location.hostname+'/ajax/cancellaoggetto.php', false);
	}
	http_req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	http_req.setRequestHeader("Content-length", dati_post.length);
	http_req.setRequestHeader("Connection", "close");
	http_req.send(dati_post);
	var esito = http_req.responseText;
	if (esito!="si") {
		alert('Impossibile evidenziare\n\rErrore:\n\r'+esito);
	}
}


function cancellaevidenziati() {
	var dati_post="";	
	if (http_req.readyState!=1) {
		http_req.open('POST', ''+tiposerver+'://'+window.location.hostname+'/ajax/cancella.php', false);
	}
	http_req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	http_req.setRequestHeader("Content-length", dati_post.length);
	http_req.setRequestHeader("Connection", "close");
	http_req.send(dati_post);
	var esito = http_req.responseText;
	if (esito!="si") {
		alert('Impossibile evidenziare\n\rErrore:\n\r'+esito);
	}
}
function urldecode (string)
{
   // Replace + with ' '
   // Replace %xx with equivalent character
   // Put [ERROR] in output if %xx is invalid.
   var HEXCHARS = "0123456789ABCDEFabcdef"; 
   var encoded = string;
   var plaintext = "";
   var i = 0;
   while (i < encoded.length) {
       var ch = encoded.charAt(i);
	   if (ch == "+") {
	       plaintext += " ";
		   i++;
	   } else if (ch == "%") {
			if (i < (encoded.length-2) 
					&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 
					&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
				plaintext += unescape( encoded.substr(i,3) );
				i += 3;
			} else {
				alert( 'Bad escape combination near ...' + encoded.substr(i) );
				plaintext += "%[ERROR]";
				i++;
			}
		} else {
		   plaintext += ch;
		   i++;
		}
	} // while
  return plaintext;
};

function cambia_ultime(num) {
	//alert(num);	
	if(num=='1') {
		Effect.SwitchOff('set2');
		setTimeout("Effect.Appear('set1', { duration: 1.0 })", 1000);
	}
	if(num=='2') {
		//alert(num);
		Effect.SwitchOff('set1');
		setTimeout("Effect.Appear('set2', { duration: 1.0 })", 1000);
	}
	
}

function ricarica_vendi(campo, param, val) {
	
	par="campo="+campo+"&param="+param+"&val="+val;
	new Ajax.Updater(campo, '/ajax/aggiorna_vendi.php', {
			method: 'get',
			parameters: par,
			onCreate: disabilita_select(),
			onComplete: function() {
				$(campo).selectedIndex=0; 
				abilita_select();
			}
		});
	
}

function ctl_vendita() {

	a=document.vendi;
	errori=0;
	
	if(a.provincia.value=="" || a.categoria.value=="") {
		alert("La provincia e la categoria sono obbligatori");	
	} else {
		if(a.vendi_prezzo.value!="" && isNaN(a.vendi_prezzo.value)) {
			alert("Nel campo prezzo inserire solo valori numerici");
			errori++;
		}
		
		myreg=new RegExp("^([a-zA-Z0-9\._-])+@([a-zA-Z0-9])+\.([a-zA-Z0-9])+$");
		if(a.vendi_email.value != "") {
			res=myreg.test(a.vendi_email.value);
			if(!res) {
				errori++;
				alert("Il campo email non è corretto");
			}
		}
		
		if(a.vendi_email.value=="" && a.vendi_telefono.value=="") {
			if(!confirm("Non hai lasciato recapiti: telefono o email.\n\rDesideri ugualmente inviare la tua proposta ?")) {
				errori++;	
			}
		}
		if(errori==0) {
			a.submit();	
			alert("Il tuo messaggio è stato inviato al nostro indirizzo e-mail. Verrai ricontattato al più presto ");			
		}
	}

}
