var isIE = navigator.appName.indexOf("Microsoft") != -1;
var DDLFatte=0;
var DDLDaFare=0;
var UrlComune='';
var DDLProv=null;
var DDLComu=null;
var UrlProvincia='';
var UrlComune='';
var LabTotComu=null
//
var PausaDopoOgniNuovaOption=3;

//Percorso relativo o assoluto + nome Xml
function InitDDL(DDLName,PercorsoXml)
{
	loadXMLDoc(DDLName,PercorsoXml)
}

function InitDDLProvComu(DDLNameProv,DDLNameComu,PercorsoXmlProv,PercorsoXmlComu)
{
	DDLProv=document.getElementsByName(DDLNameProv)[0];
	DDLComu=document.getElementsByName(DDLNameComu)[0];
	if(DDLProv!=null&&DDLComu!=null)
	{
		DDLDaFare++
		this.UrlProvincia=PercorsoXmlProv;
		this.UrlComune=PercorsoXmlComu;
		loadXMLDoc('',PercorsoXmlProv)
	}
}

function loadXMLDoc(DDLName,url)
{
	var XMLRequest=false;
	if(window.XMLHttpRequest&&!(window.ActiveXObject)) // branch for native XMLHttpRequest object
	{
		try{XMLRequest=new XMLHttpRequest();}
		catch(e){XMLRequest=false;}
	}
	else if(window.ActiveXObject)// branch for IE/Windows ActiveX version
	{
		try{XMLRequest=new ActiveXObject("Msxml2.XMLHTTP");}
		catch(e)
		{
			try{XMLRequest=new ActiveXObject("Microsoft.XMLHTTP")}
			catch(e){XMLRequest=false}
		}
	}
	if(XMLRequest)
	{
		var DDLObj=document.getElementsByName(DDLName)[0]
		XMLRequest.onreadystatechange=function()
		{
			if (XMLRequest.readyState == 4)// only if req shows "loaded"
			{
				if (XMLRequest.status == 200)// only if "OK"
				{
					url=url.toLowerCase()
					if(url.indexOf("province.xml")>-1)//Caso speciale Province Comuni
					{
						LoadProvComu(XMLRequest,'LabTotComu')
					}
					else if(url.indexOf("comuni.xml")>-1)//Se carico tutti i comuni
					{
						LoadComuni(XMLRequest,'LabTotComu')
					}
					else
					{
						LoadDDLGenerico(XMLRequest,DDLObj)
					}
				}
				else
				{
					alert("There was a problem retrieving the XML data:\n"+XMLRequest.statusText)
				}
			}
		}
		XMLRequest.open("GET", url, true);
		XMLRequest.send(null);
	}
	else alert('Errore')
}



//Serve per interromperli se cambi la selezione della provincia
var PuntatoreCaricamentiComuni
var VetAllComuniValue=new Array()
var VetAllComuniText=new Array()
function LoadTuttiComuni(XMLHTTPRequest,DDLObj)
{
	VetAllComuniValue=new Array()
	VetAllComuniText=new Array()
	DDLProv.disabled=true
	DDLComu.disabled=true
	//Chiamata asincrona caricamneto
	var Records=GetChilds(XMLHTTPRequest.responseXML.documentElement)//responseText
	var TotRecords=Records.length;
	
	//Vettorializzo tutti i comuni
	for(i=0;i<TotRecords;i++)
	{
		VetAllComuniValue[i]=Records[i].getAttribute("id");
		VetAllComuniText[i]=Records[i].getAttribute("desc");
	}
	
	var MyDDL=new DDLVarie(DDLComu,VetAllComuniValue,VetAllComuniText,new Option('Scegliere una voce','0'))
	MyDDL.onreadystatechange=function()
	{
		if(MyDDL.readyState==2)
		{
			DDLComu.value=DDLObj.getAttribute("title")//Elemento selezionato
			DDLComu.value=SelectedID
			DDLComu.disabled=false
			if(LabTotComu!=null)LabTotComu.innerHTML=TotRecords
		}
	}
}
//Parte delle Province comuni
var VetProv=new Array()
var VetComuni=new Array()

//Carica Tutti i Comuni
function LoadComuni(XMLHTTPRequest,LabTotComu)
{
	//alert('Load Comuni di provincia')
	DDLProv.disabled=true
	DDLComu.disabled=true
	var LabTotComu=document.getElementById(LabTotComu)
	var Records=GetChilds(XMLHTTPRequest.responseXML.documentElement)//responseText
	var TotRecords=Records.length;
	if(LabTotComu)LabTotComu.innerHTML=TotRecords
	var VetValue=new Array()
	var VetText=new Array()
	for(i=0;i<TotRecords;i++)
	{
		var value=Records[i].getAttribute("ID")
		var text=Records[i].getAttribute("Desc")
		VetComuni[i]=new Array()
		VetComuni[i][0]=value
		VetComuni[i][1]=text
		VetValue[i]=value
		VetText[i]=text
	}
	var MyDDL=new DDLVarie(DDLComu,VetValue,VetText,new Option("Scegliere un comune..","0"))
	MyDDL.onreadystatechange=function()
	{
		if(MyDDL.readyState==2)
		{
			DDLComu.value=DDLComu.getAttribute("title")//Elemento selezionato
			DDLComu.disabled=false
			DDLProv.disabled=false
		}
	}
	MyDDL.Popola();
}

function LoadProvComu(XMLHTTPRequest,ID_LabTotComu)
{
	//Creo Vettore Province Comuni
	var Records=GetChilds(XMLHTTPRequest.responseXML.documentElement)//responseText
	
	var TotRecords=Records.length;
	var TotAllComuni=0
	
	var VetValue=new Array()
	var VetText=new Array()
	for(i=0;i<TotRecords;i++)
	{
		var Provincia=new Array()
		Provincia[0]=Records[i].getAttribute("ID")
		Provincia[1]=Records[i].getAttribute("Desc")
		VetValue[i]=Provincia[0]
		VetText[i]=Provincia[1]
		Provincia[2]=new Array()
		var VetComuni=GetChilds(Records[i])
		var TotComuni=VetComuni.length
		for(co=0;co<TotComuni;co++)
		{
			Provincia[2][co]=new Array()
			Provincia[2][co][0]=VetComuni[co].getAttribute("ID")
			Provincia[2][co][1]=VetComuni[co].getAttribute("Desc")
			TotAllComuni++
		}
		VetProv[i]=Provincia
	}
	
	if(LabTotComu!=null)
	{
		this.LabTotComu.innerHTML=TotAllComuni
	}


	//DDLComu.options[0]=new Option('nessun elemento selezionato','0')
	var MyDDL=new DDLVarie(DDLProv,VetValue,VetText,new Option("0 seleziona tutte le province..","0"))//new Option("0 da scegliere..","-1")
	MyDDL.onreadystatechange=function()
	{
		if(MyDDL.readyState==2)
		{
			//Carico i comuni
			Rip_Prov_Comu();//Riposiziono i valori selezionati al postback precedente
		}
	}
	MyDDL.Popola();
}

//Riposiziona il comune e la provincia
function Rip_Prov_Comu()
{
	//alert("Riposiziono provincia Comune")
	var IDProv=DDLProv.getAttribute('title')
	var IDComu=DDLComu.getAttribute('title')
	if(IDProv==''||IDProv=='-1')IDProv='0'//Prima del PostBack
	DDLProv.value=IDProv
	
	//Aggiungo tutti i comuni della provincia nel DDLComu
	PopolaComuni()
	if(IDComu=='')IDComu='0'
	DDLComu.value=IDComu
}
function FoundIdxProvincia(ID)
{
	var TotElem=VetProv.length
	for(i=0;i<TotElem;i++)
	{
		if(VetProv[i][0]==ID)return i
	}
	return 0
}
//Funzione per i comuni principale
function PopolaComuni()
{
	SvuotaDDL(DDLComu)
	var ContatoreComuni=0
	
	//AddOption(DDLComu,"0","0 Seleziona tutti i comuni..")
	var SelectedValue=parseInt(DDLProv.value);
	if(SelectedValue>0)//Solo i comuni della provincia
	{
		CaricaComuniDellaProvincia()
	}
	else if(SelectedValue==0)//Tutti i comuni
	{
		CaricaTuttiIComuni()
	}
	else//Provincia -1
	{
		DDLComu.options[0]=new Option('Nessuna provincia selezionata','0')
		if(LabTotComu!=null){LabTotComu.innerHTML='0'}
	}
}
function CaricaTuttiIComuni()
{
	loadXMLDoc('DDLComu',this.UrlComune)//UrlComune=creato nella pagina ricerca è anti cache (file versionato)
}
function CaricaComuniDellaProvincia()
{
	//Indice Option della DDL Provincia
	var SelectedValue=DDLProv.value
	var ProvElemIdx=FoundIdxProvincia(SelectedValue)
	var vetComuni=VetProv[ProvElemIdx][2]
	var TotComuni=vetComuni.length
	if(TotComuni==0){
		DDLComu.options[0]=new Option('Nessun comune presente','0')
		if(LabTotComu!=null)LabTotComu.innerHTML="0";
		DDLComu.disabled=false
		DDLProv.disabled=false
	}
	else
	{
		var VetValue=new Array()
		var VetText=new Array()
		for(i=0;i<TotComuni;i++)
		{
			VetValue[i]=vetComuni[i][0]//ID
			VetText[i]=vetComuni[i][1]//Desc
			//ContatoreComuni++
		}
		var MyDDL=new DDLVarie(DDLComu,VetValue,VetText,new Option("Scegliere il comune","0"))
		MyDDL.onreadystatechange=function()
		{
			if(MyDDL.readyState==2)
			{
				DDLComu.disabled=false
				DDLProv.disabled=false
				DDLComu.value=DDLComu.title
				if(DDLComu.value!=DDLComu.title)
				{
					DDLComu.value="0"
				}
				if(LabTotComu!=null)LabTotComu.innerHTML=TotComuni;
			}
		}
		MyDDL.Popola();
	}
}

function LoadDDLGenerico(XMLHTTPRequest,DDLObj)
{
	DDLObj.disabled=true
	
	var Records=GetChilds(XMLHTTPRequest.responseXML.documentElement)//responseText
	var TotRecords=Records.length;
	var VetValue=new Array()
	var VetText=new Array()
	for(i=0;i<TotRecords;i++)
	{
		VetValue[i]=Records[i].getAttribute("id")//ID
		VetText[i]=Records[i].getAttribute("desc")//Desc
	}
	var MyDDL=new DDLVarie(DDLObj,VetValue,VetText,new Option("Scegliere una voce..",""))
	MyDDL.onreadystatechange=function()
	{
		if(MyDDL.readyState==2)
		{
			DDLObj.disabled=false
			DDLObj.value=DDLObj.title
		}
	}
	MyDDL.Popola();
}

function ChangeProv()
{
	PopolaComuni()
	DDLComu.value='0'
}
function AddOption(DDLObj,value,text)
{
	var UltimoElem=DDLObj.length
	var op=new Option(text,value)
	DDLObj.options[UltimoElem]=op
}

//Class per popolare le DDL in maniera sincrona
DDLVarie=function(DDLObj,VetValue,VetText,OptionOnFinish)
{
	var self=this
	this.DDLObj=DDLObj;
	this.VetValue=new Array();this.VetValue=VetValue;
	this.VetText=new Array();this.VetText=VetText;
	this.TotElem;
	this.TotaliInseriti;
	this.readyState=0
	this.OptionOnFinish=OptionOnFinish;
	this.TimerIDPuntini;
	this.Puntini="";
	this.Popola = function()
	{
		this.readyState=1;
		this.onreadystatechange();
		this.DDLObj.disabled=true;
		this.DDLObj.options[0]=new Option("Attendere prego","0");
		this.DDLObj.value="0";
		this.TimerIDPuntini=setInterval(function()
		{
			var str="Attendere prego"+self.Puntini
			self.Puntini=self.Puntini+"."
			if(str.length>17)self.Puntini="";
			self.DDLObj.options[0].text=str;
		}
		,500)
		
		
		this.TotaliInseriti=0;
		this.TotElem=this.VetValue.length;
		var Intervallo=0;
		this.Indice=0;
		
		//alert(DDLObj.value)
		for(i=0;i<this.TotElem;i++)
		{
			window.setTimeout(function(){self.AddOption3()},parseInt(Intervallo))
			Intervallo=Intervallo+PausaDopoOgniNuovaOption
		}
	}
	this.Indice;//Elemento attualmente processato
	this.AddOption3 = function()
	{
		this.TotaliInseriti++
		var value=this.VetValue[this.Indice]
		var text=this.VetText[this.Indice]
		var op=document.createElement("option")
		op.innerHTML=text
		op.setAttribute("value",value)
		this.DDLObj.appendChild(op)
		this.Indice++
		if(this.TotaliInseriti==this.TotElem)
		{
			window.clearInterval(this.TimerIDPuntini)
			this.DDLObj.options[0]=this.OptionOnFinish;
			
			this.readyState=2
			this.onreadystatechange()
		}
	}
	this.onreadystatechange = function()
	{
		
	}
}
