function trim(stringa){
    while (stringa.substring(0,1) == ' '){
        stringa = stringa.substring(1, stringa.length);
    }
    while (stringa.substring(stringa.length-1, stringa.length) == ' '){
        stringa = stringa.substring(0,stringa.length-1);
    }
    return stringa;
}

function replaceChars(str)
{
	str = str.replace(/&agrave;/, "à");
	str = str.replace(/&egrave;/, "è");
	str = str.replace(/&igrave;/, "ì");
	str = str.replace(/&ograve;/, "ò");
	str = str.replace(/&ùgrave;/, "ù");
	str = str.replace(/&eacute;/, "é");
	return str;
}

function restoreChars(str)
{
	str = str.replace(/à/, "&agrave;");
	str = str.replace(/è/, "&egrave;");
	str = str.replace(/ì/, "&igrave;");
	str = str.replace(/ò/, "&ograve;");
	str = str.replace(/ù/, "&ùgrave;");
	str = str.replace(/é/, "&eacute;");
	return str;
}

function GetXmlHttpObject()
{
	var xmlHttp;
	try
	{
		xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	return xmlHttp;
}

var xmlHttp;
var IEVer = window.navigator.appVersion;
IEVer = IEVer.substr( IEVer.indexOf('MSIE') + 5, 3 );

function adviceCity(city, prov, select)
{
	city = city != null ? trim(restoreChars(city)) : null;
	prov = prov != null ? prov : null;
	select = select != null ? select : null;

	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request");
	}


	if (document.getElementById('province').value)
	{
		prov = document.getElementById('province').value;
	}
	else
	{
		prov = null;
	}

	var url="get_city.php?"

	url=url+"city="+city;

	if (prov)
	{
		url=url+"&prov="+prov;
	}

	if ((city==null||city=="") && (prov==null||prov==""))
	{
	/*	document.getElementById("city_advice").style.display = "none";
		document.getElementById("city").value = '';*/
	}
	else
	{
		 if ((city==null||city=="") && (prov!=null||prov!=""))
		{
			document.getElementById("city").value = '';
		}

		//elaborazione della risposta attraverso una funzione js
		xmlHttp.onreadystatechange=function()
		{
			document.getElementById("city_advice").style.display = "block";

			if(IEVer <= 6.0 && select != null) {
				for (i=0; i<select.length; i++)
					document.all[select[i]].style.visibility= "hidden";
			}
			//controllo lo stato della risposta 0 (non inizializzato), 1 (sta caricando), 2 (già caricato), 3 (interattivo), 4 (completato)
			if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
			{
				var res = false;
				var cont;
				if (xmlHttp.responseText) res = eval('(' + xmlHttp.responseText + ')')

				if (!res)
				{
					cont = '<span class="ins"><span class="alert"><strong>ATTENZIONE</strong></span><br />La citt&agrave; inserita non &egrave; stata trovata</span>';
				}
				else
				{
					var	nome;
					var provincia;
					var found = false;
					var cont = "";
					var max_result = res.city.length;

					for(i=0; i<max_result; i++)
					{
						nome = res.city[i].nome;
						provincia = res.city[i].provincia;
						if (max_result == 1 )
						{
							setCity(provincia, nome);
						}
						else
						{
							cont += '<span class="res_advice" onmouseover="this.style.background=\'#F2F2F2\'" onmouseout="this.style.background=\'#EAEAEA\'" onclick="setCity(\''+provincia+'\',\''+  nome.replace(/'/,"_")+'\')">'+nome+'</span>';
						}
						if(city && city.toLowerCase() === nome.toLowerCase())
						{
							found = true;
						}
					}
				}
				document.getElementById("city_advice").innerHTML = cont;
			}
			else
			{
				document.getElementById("city_advice").innerHTML = '<img src="/img/loadinfo.gif" alt="caricamento in corso" width="24" height="24"/> Caricamento in corso';
			}
		}
		//alert(city.length);
		if ((prov != null) && (city != null && city.length >= 2))
		{
			xmlHttp.open("GET",url,true) //invio la richiesta / true la funzione JavaScript resterà in esecuzione anche prima dell'arrivo della risposta
			xmlHttp.send(null) // dati da inviare se di tipo POST. querystring: nome1=valore1&nome2=valore2&ecc=ecc
		}
	}
}

function setCity(key, name)
{
	document.getElementById("city").value = replaceChars(name.replace("_", "'"));
	document.getElementById("province").value = key;
	document.getElementById("city_advice").style.display = 'none';

	if(IEVer <= 6.0 ) {
		selectList = document.getElementsByTagName("select");
		for (var i=0; i < selectList.length; i++){
			selectList[i].style.visibility= 'visible';
		}
	}
}

function no_province()
{
	document.getElementById('province').value = '';
}

function resetta()
{
	document.getElementById("city").value = '';
	/*document.getElementById("province").value = '';*/
	document.getElementById("city_advice").style.display = "none";
}