// JavaScript Document

//regione selezionata
var currentregione="";

//COMMON
var xmlHttp_Regioni;
var xmlHttp_Citta;
var xmlDoc;
var xmlHttp_Copertura;
var xmlHttp_Sportelli;

//memorizza in array la copertura del servizio
var arrCoperture = new Array();	
//array degli indirizzi per google map
var arrIndirizzi = new Array();

//aggiunge option alla select
function AddOptionToSelect(selectname,optionnode){
		
		try
		  {selectname.add(optionnode,null);}
		catch(ex)
		  {selectname.add(optionnode);}
		  
}
//vuota la select
function ClearOptions(selectname){
	
	selectname.length = 0;	
	try
		//map.clearOverlays();
		{}
	catch(ex)
		{}
	
}
//COMMON

function SetCountry(thiscountry){
	
	currentcountry = thiscountry;
	
}

function SetRegione(thisregione){
	
	currentregione = thisregione;
	
}

//GESTIONE REGIONI
function FilterRegioni(){
	
		xmlHttp_Regioni=GetXmlHttpObject();									
		xmlHttp_Regioni.onreadystatechange=ManageRegioniList;						
		xmlHttp_Regioni.open("GET","ajax/locator_regioni.cfm",true);
		xmlHttp_Regioni.send(null);	
	
}
function ManageRegioniList() {
	
	if(xmlHttp_Regioni.readyState==4){
		
	    //xml	    
		xmlDoc = xmlHttp_Regioni.responseXML.documentElement;			   	
		
		var select_value = "";
	    var select_content = "";
	    var select_regioni = document.getElementById("regioni");			
		//pulisce la select regioni
		ClearOptions(select_regioni);
		
		var select_citta = document.getElementById("citta");			
		//pulisce la select citta
		ClearOptions(select_citta);
		
		//aggiunge un nodo vuoto
		var select_node = document.createElement("option");			
			select_node.text = "";
		AddOptionToSelect(select_regioni,select_node);
		
		//popola la select delle regioni
		for (i = 0; i < xmlDoc.childNodes.length; i++) {
			
			select_value = xmlDoc.childNodes[i].attributes[0].nodeValue;
			select_content = xmlDoc.childNodes[i].childNodes[0].nodeValue;			
			select_node = document.createElement("option");		    
				select_node.value=select_value;			  
		    	select_node.text=select_content;			
			AddOptionToSelect(select_regioni,select_node);
			  
		 }
				
	}
	
}
//GESTIONE REGIONI

//GESTIONE CITTA
function FilterCitta(regioneid){
	
	if(regioneid.length > 0){
		xmlHttp_Citta=GetXmlHttpObject();									
		xmlHttp_Citta.onreadystatechange=ManageCittaList;						
		xmlHttp_Citta.open("GET","ajax/locator_citta.cfm?regioneid=" + regioneid,true);
		xmlHttp_Citta.send(null);
	}
	
}
function ManageCittaList() {
	
	if(xmlHttp_Citta.readyState==4){
		
	    //xml
	    xmlDoc = xmlHttp_Citta.responseXML.documentElement;		
	    var select_value = "";
	    var select_content = "";
	    var select_citta = document.getElementById("citta");
		//pulisce la select citta
		ClearOptions(select_citta);
		
		//aggiunge un nodo vuoto
		var select_node = document.createElement("option");			
			select_node.text = "";
		AddOptionToSelect(select_citta,select_node);
		
		//popola la select delle regioni
		for (i = 0; i < xmlDoc.childNodes.length; i++) {
			
			select_value = xmlDoc.childNodes[i].attributes[0].nodeValue;
			select_content = xmlDoc.childNodes[i].childNodes[0].nodeValue;			
			select_node = document.createElement("option");		    
				select_node.value=select_value;			  
		    	select_node.text=select_content;			
			AddOptionToSelect(select_citta,select_node);
			  
		 }
				
	}
	
}
//GESTIONE CITTA

//GESTIONE PROVINCE
function FilterProvince(cittaid){
	
	if(cittaid.length > 0){
		xmlHttp_Province=GetXmlHttpObject();									
		xmlHttp_Province.onreadystatechange=ManageProvinceList;						
		xmlHttp_Province.open("GET","ajax/locator_province.cfm?cittaid=" + cittaid,true);
		xmlHttp_Province.send(null);
	}
	
}
function ManageProvinceList() {
	
	if(xmlHttp_Province.readyState==4){
		
	    //xml
	    xmlDoc = xmlHttp_Province.responseXML.documentElement;		
	    var select_value = "";
	    var select_content = "";
	    var select_province = document.getElementById("province");
		//pulisce la select province
		ClearOptions(select_province);
		
		//aggiunge un nodo vuoto
		var select_node = document.createElement("option");			
			select_node.text = "";
		AddOptionToSelect(select_province,select_node);
		
		//popola la select delle province
		for (i = 0; i < xmlDoc.childNodes.length; i++) {
						
			select_value = xmlDoc.childNodes[i].attributes[0].nodeValue;
			select_content = xmlDoc.childNodes[i].childNodes[0].nodeValue;						
			
			//copertura
			arrCoperture[i] = xmlDoc.childNodes[i].attributes[1].nodeValue;			
						
			select_node = document.createElement("option");		    
				select_node.value=select_value;			  
		    	select_node.text=select_content;			
			AddOptionToSelect(select_province,select_node);
			  
		 }
				
	}
	
}
//GESTIONE PROVINCE

//HIGHLIGHT DELLO SPORTELLO
function SetRowHighLight(rowindex){
	
	var tablesportelli = document.getElementById("tablesportelli");
	for (j=0; j < tablesportelli.rows.length; j++){
			
		if(j == rowindex){				
			tablesportelli.rows[j].style.backgroundColor = "#CCCCCC";				
		}else{
			tablesportelli.rows[j].style.backgroundColor = "#FFFFFF";	
		}
		
	}	
	
}
//HIGHLIGHT DELLO SPORTELLO

//PULIZIA DELLA TABELLA
function CleanTableSportelli(){
	
	var tablesportelli = document.getElementById("tablesportelli");
	var maxrows = tablesportelli.rows.length; 
	for (j=0; j <maxrows; j++){
			
		tablesportelli.deleteRow(0);
		
	}		
	
}
//PULIZIA DELLA TABELLA

//ELENCO SPORTELLI DELLA PROVINCIA
function FilterSportelli(cittaid){
	
	if(cittaid.length > 0){
		xmlHttp_Sportelli=GetXmlHttpObject();									
		xmlHttp_Sportelli.onreadystatechange=ManageSportelliList;						
		xmlHttp_Sportelli.open("GET","ajax/locator_sportelli.cfm?cittaid=" + cittaid,true);
		xmlHttp_Sportelli.send(null);
	}
	
}

function ManageSportelliList(){
	
	if(xmlHttp_Sportelli.readyState==4){
		
	    //xml
	    xmlDoc = xmlHttp_Sportelli.responseXML.documentElement;	
		
		var select_value = "";
	    var select_content = "";
	    var select_sportelli = document.getElementById("sportelli");
		//pulisce la select sportelli
		ClearOptions(select_sportelli);
		
		//aggiunge un nodo vuoto
		var select_node = document.createElement("option");			
			select_node.text = "";
		AddOptionToSelect(select_sportelli,select_node);
		
		var Node;
		
		var thistable = document.getElementById("tablesportelli")		
		var riga;
		var colonna;
		
		//popola la select degli sportelli
		for (i = 0; i < xmlDoc.childNodes.length; i++) {
			
			Node = xmlDoc.childNodes;			
			//dettaglio degli sportelli
			
			//comune	
			select_value = i;
			select_content = Node[i].childNodes[1].childNodes[0].nodeValue;			
			//alert(select_content);
			select_node = document.createElement("option")
			select_node.text=select_content
			AddOptionToSelect(select_sportelli,select_node);			
			
			//indirizzi per googlemap
			//arrIndirizzi[i] = Node[i].childNodes[2].childNodes[0].nodeValue + " " + Node[i].childNodes[3].childNodes[0].nodeValue + "," + Node[i].childNodes[1].childNodes[0].nodeValue;
			arrIndirizzi[i] = Node[i].childNodes[30].childNodes[0].nodeValue + " " + Node[i].childNodes[31].childNodes[0].nodeValue + "," + Node[i].childNodes[29].childNodes[0].nodeValue;
			
			//aggiunge il marker alla mappa
			ShowMarkerOnMap(i,arrIndirizzi[i],9);
			
			//aggiunge tutti i dettagli alla tabella
			riga = thistable.insertRow(thistable.rows.length);
			
			//comune + indirizzo + civico + cap + citta + note + telefono + fax + cellulare
			colonna = riga.insertCell(0);
			colonna.vAlign = "top";
			
			colonna.innerHTML = "<b>" + Node[i].childNodes[0].childNodes[0].nodeValue.toUpperCase() + "</b><br/><br/>" + Node[i].childNodes[2].childNodes[0].nodeValue + " " + Node[i].childNodes[3].childNodes[0].nodeValue + "," + Node[i].childNodes[4].childNodes[0].nodeValue + " " + Node[i].childNodes[1].childNodes[0].nodeValue;
			
			if(Node[i].childNodes[28].childNodes[0].nodeValue.length > 0){
				colonna.innerHTML = colonna.innerHTML  + " (" + Node[i].childNodes[28].childNodes[0].nodeValue + ")" ;
			}
			colonna.innerHTML = colonna.innerHTML + "<br/><br/>";
			colonna.innerHTML = colonna.innerHTML + "Tel." + Node[i].childNodes[5].childNodes[0].nodeValue + "<br/>Fax " + Node[i].childNodes[6].childNodes[0].nodeValue; 			
			colonna.innerHTML = colonna.innerHTML + "<br/>Cell." + Node[i].childNodes[7].childNodes[0].nodeValue; 
			
			//orari
			colonna = riga.insertCell(1);
			colonna.vAlign = "top";
			
			colonna.innerHTML = "<b>Luned&igrave;</b><br/>" + Node[i].childNodes[18].childNodes[0].nodeValue;
			if(Node[i].childNodes[19].childNodes[0].nodeValue.length > 0){
			  colonna.innerHTML = colonna.innerHTML + " / " + Node[i].childNodes[19].childNodes[0].nodeValue; 
			}
				
			colonna.innerHTML = colonna.innerHTML + "<br/><b>Marted&igrave;</b><br/>" + Node[i].childNodes[20].childNodes[0].nodeValue; 
			colonna.className="orario";
			if(Node[i].childNodes[21].childNodes[0].nodeValue.length > 0){
				colonna.innerHTML = colonna.innerHTML + " / " + Node[i].childNodes[21].childNodes[0].nodeValue; 
			}
				
			colonna.innerHTML = colonna.innerHTML + "<br/><b>Mercoled&igrave;</b><br/>" + Node[i].childNodes[22].childNodes[0].nodeValue; 
			if(Node[i].childNodes[23].childNodes[0].nodeValue.length > 0){
				colonna.innerHTML = colonna.innerHTML +" / " + Node[i].childNodes[23].childNodes[0].nodeValue;
			}
				
			colonna.innerHTML = colonna.innerHTML + "<br/><b>Gioved&igrave;</b><br/>" + Node[i].childNodes[24].childNodes[0].nodeValue; 
			if(Node[i].childNodes[25].childNodes[0].nodeValue.length > 0){
				colonna.innerHTML = colonna.innerHTML + " / " + Node[i].childNodes[25].childNodes[0].nodeValue; 
			}
				
			colonna.innerHTML = colonna.innerHTML + "<br/><b>Venerd&igrave;</b><br/>" + Node[i].childNodes[26].childNodes[0].nodeValue; 
			if(Node[i].childNodes[27].childNodes[0].nodeValue.length > 0){
				colonna.innerHTML = colonna.innerHTML + " / " + Node[i].childNodes[27].childNodes[0].nodeValue;
			}
				
			//note dei giorni della settimana
			colonna = riga.insertCell(2);
			colonna.vAlign = "bottom";
			colonna.className = "noe";
			
				//lunedi
				if(Node[i].childNodes[13].childNodes[0].nodeValue.length > 0){
					colonna.innerHTML = "* " + Node[i].childNodes[13].childNodes[0].nodeValue;	
				}
				//martedi
				if(Node[i].childNodes[14].childNodes[0].nodeValue.length > 0){
					colonna.innerHTML = "* " + Node[i].childNodes[14].childNodes[0].nodeValue;	
				}
				//mercoledi
				if(Node[i].childNodes[15].childNodes[0].nodeValue.length > 0){
					colonna.innerHTML = "* " + Node[i].childNodes[15].childNodes[0].nodeValue;	
				}
				//giovedi
				if(Node[i].childNodes[16].childNodes[0].nodeValue.length > 0){
					colonna.innerHTML = "* " + Node[i].childNodes[16].childNodes[0].nodeValue;	
				}				
				//venerdi
				if(Node[i].childNodes[17].childNodes[0].nodeValue.length > 0){
					colonna.innerHTML = "* " + Node[i].childNodes[17].childNodes[0].nodeValue;	
				}
			
		 }
		 
	}
	
}

//ELENCO SPORTELLI DELLA PROVINCIA

//COPERTURA
function getCopertura(cittaid){
	
	if(cittaid.length > 0){
		xmlHttp_Copertura=GetXmlHttpObject();									
		xmlHttp_Copertura.onreadystatechange=ManageCopertura;						
		xmlHttp_Copertura.open("GET","ajax/locator_copertura.cfm?cittaid=" + cittaid,true);
		xmlHttp_Copertura.send(null);
	}
	
}

function ManageCopertura(){
	
	if(xmlHttp_Copertura.readyState==4){
		
	    //xml
	    xmlDoc = xmlHttp_Copertura.responseXML.documentElement;	
		if(xmlDoc.childNodes.length > 0){
		   usermessage = "L'offerta &egrave; disponibile nella zona indicata";
		}else{
		   usermessage = "L'offerta non &egrave; disponibile nella zona indicata.<br/> L'offerta E.ON di energia elettrica &egrave; invece disponibile";
		}
		document.getElementById("mess_copertura").innerHTML = usermessage;
		
	}
	
}
//COPERTURA


//COPERTURA
//function getCopertura(arrindex){
	
	//var usermessage = "";
	//if(arrCoperture[arrindex] == 1){
		//usermessage = "Il comune selezionato &egrave; coperto dai nostri servizi";
	//}
	//else{
		//usermessage = "Il comune selezionato non &egrave; coperto dai nostri servizi";
	//}
	//document.getElementById("mess_copertura").innerHTML = usermessage;
		
//}
//COPERTURA
