/*
**Dichiarazione delle variabili globali
*/
var currentContinent = "";
var currentCountry = "";
var currentRegion = "";
var currentArea = "";
var lastZoomView = "";
var showSearchResultPanel = 0;
var pContinent;
var pContinentReturn;
var pCountryReturn;
var pCountryIdReturn;
var pContinentIdReturn;
var pContinentId;
var pRegionIdReturn;
var pLanguageReturn;
var regionId = "";
var lng = "";
var countryId = "";
var continentColoured="";
var countryColored="";
var countryReturnColored="";
var regionReturnColored="";

/*
**
**INIZIO funzioni di caricamento della pagina
**
*/

/*
**Funzione che carica la pagina nel caso in cui NON vi siano parametri di ritorno
*/
function onLoad()
{
	lng = document.getElementById("language").innerHTML;

	if(lng == "null" || lng == null){
		lng = "en";
		}
	showDisplay("worldpanel");
	showDisplay("countrypanel");
	//showDisplay("currentRegionArea");
	getListOfContinents(lng);	
}

/*
**Funzione che carica la pagina nel caso in cui vi siano parametri di ritorno
*/
function onLoadReturn()
{	
	
	lng = pLanguageReturn;
	showDisplay("worldpanel");
	showDisplay("countrypanel");
	//showDisplay("currentRegionArea");
	getListOfContinents(lng);
	
	getLabelOfReturnRegionSelected(pCountryReturn, lng, pRegionIdReturn);
}
/*
**
**FINE funzioni di caricamento della pagina
**
*/

/*
**
**INIZIO funzioni di accesso al db
**
*/


/*
**Funzione che carica la jsp che esegue la query per ottenere i continenti
**Riceve in ingresso la lingua
*/
function getListOfContinents(pLng)
{
	if(pLng == "jp"){
		pLng="en";
	}
	if(pLng == "sv"){
		pLng="en";
		}
	advAJAX.get({url : "getListOfContinents.jsp?lng="+pLng+"",onSuccess : function(obj) {
	
			var p = eval("(" + obj.responseText + ")");			    

			for(l=0;l<p.length;l++)
			{				
				createLink("worldpanellist",p[l].label,"javascript:doHandleListOfCountriesByContinent('"+p[l].label+"','"+p[l].continent_id+"');",p[l].label,"");
			}
			if(pContinentIdReturn != null && pContinentIdReturn != "null" && pContinentIdReturn != ""){
			getContinentReturnLabel(pLanguageReturn, pContinentIdReturn);
			}	
		}				    
	});		
}

/*
**Funzione che carica la jsp che esegue la query per ottenere la label del continente di ritorno
**Riceve in ingresso la lingua e l'id del continente
*/
function getContinentReturnLabel(pLng, continentId)
{
	if(pLng == "jp"){
		pLng="en";
	}
	if(pLng == "sv"){
		pLng="en";
		}
	advAJAX.get({url : "getContinentReturnLabel.jsp?continent_id="+continentId+"&locale="+pLng+"",onSuccess : function(obj) {
			var p = eval("(" + obj.responseText + ")");
			for(l=0;l<p.length;l++)
			{
				pContinentReturn = p[l].label;
								
			}
			document.getElementById(pContinentReturn).className = 'linkRosso';
			//document.getElementById(pContinentReturn).className = 'linkRosso';
			
		doHandleListOfCountriesByContinent(pContinentReturn, pContinentIdReturn);
		if((pCountryReturn == 'Italia')||(pCountryReturn == 'United States')){
			clearPanelsById("regionpanellist");		
			doHandleListOfRegionsByCountry(pCountryReturn, pCountryIdReturn);
			//replacePanelTitle("currentRegionArea",pCountryReturn);
			//clearPanelsById("areapanellist");
			//getListOfRegionsByCountry(pCountryReturn, pLanguageReturn, pCountryIdReturn);
			}
		}			    
	});		
}

/*
**Funzione che carica la jsp che esegue la query per ottenere le nazioni divise in base al
**continente scelto
**Riceve in ingresso l'id del continente
*/
function getListOfCountriesByContinent(continentId)
{
	advAJAX.get({url : "getListOfCountriesByContinent.jsp?continentId="+continentId+"",onSuccess : function(obj) {
	
			var p = eval("(" + obj.responseText + ")");
			
			for(l=0;l<p.length;l++)
			{
				createLink("countrypanellist",p[l].label,"javascript:doHandleListOfRegionsOrCountries('"+p[l].label+"','"+p[l].id+"');",p[l].label,"");
			}
			if((pCountryReturn != "null")&&(countryReturnColored=="")){
			document.getElementById(pCountryReturn).className = 'linkRosso';
			countryReturnColored = pCountryReturn;
			}
		}			    
	});	
}

/*
**Funzione che carica la jsp che esegue la query per ottenere le regioni divise in base alla
**nazione scelta
**Riceve in ingresso l'id e la label della nazione e la lingua
*/
function getListOfRegionsByCountry(country, pLng, countryId)
{
	if(pLng == "jp"){
		pLng="en";
	}
	if(pLng == "sv"){
		pLng="en";
		}
	advAJAX.get({url : "getListOfRegionsByCountry.jsp?country="+country+"&lng="+pLng+"",onSuccess : function(obj) {

			var p = eval("(" + obj.responseText + ")");			    

			for(l=0;l<p.length;l++)
			{
				//alert(p[l].label);
				createLink("regionpanellist",p[l].label,"javascript:doHandleListOfAreasByCountryAndRegion('"+ country+"','"+countryId+"','"+p[l].area_id+"');",p[l].label,"");						
			}
			if(regionReturnColored != ""){
					document.getElementById(regionReturnColored).className = 'linkRosso';
					regionReturnColored = "";
					}
			//document.getElementById(country).className = 'linkRosso';				
		}			    
	})	
}


function getLabelOfReturnRegionSelected(country, pLng, paramRegionIdReturn)
{
	if(pLng == "jp"){
		pLng="en";
	}
	if(pLng == "sv"){
		pLng="en";
	}
	/*alert(country)
	alert(pLng)
	alert(paramRegionIdReturn)*/
	advAJAX.get({url : "getLabelOfReturnRegionSelected.jsp?country="+country+"&lng="+pLng+"&areaId="+paramRegionIdReturn+"",onSuccess : function(obj) {

			var p = eval("(" + obj.responseText + ")");			    

			for(l=0;l<p.length;l++)
			{
				regionReturnColored= p[l].label;		
			}				
		}			    
	})	
}
/*
**
**FINE funzioni di accesso al db
**
*/



/*
**
**INIZIO funzioni di gestione dei pannelli in base alle query sul db
**
*/

/*
**Funzione che visualizza la lista dei continenti
**Riceve in ingresso la label e l'id del continente
*/
function doHandleListOfCountriesByContinent(continent, continentId)
{
	pContinentId = continentId;
	//replacePanelTitle("currentRegion",continent);
	clearPanelsById("countrypanellist");
	clearPanelsById("regionpanellist");
	getListOfCountriesByContinent(continentId);
	showDisplay("countrypanel");
	/*alert("continentColoured: "+continentColoured);
	alert("continent: "+continent);*/
	if((continentColoured != continent) && (continentColoured != "")){
		document.getElementById(continentColoured).className = 'linkDefault';
		if((countryReturnColored=="")&&(document.getElementById(countryReturnColored) != null)){
			alert('linea 241')
			document.getElementById(countryReturnColored).className = 'linkDefault';
			}
		}
	continentColoured = continent;
	//alert("document.getElementById(continent): "+continent);
	if(document.getElementById(continent) != null){		
		document.getElementById(continent).className = 'linkRosso';
	}
	/*if(document.getElementById(continent) == null && pContinentIdReturn != null){	
		getContinentReturnLabel(pLanguageReturn, pContinentIdReturn);	
		
		alert('ci sono')
	}*/

}

/*
**Funzione che controlla se la nazione scelta è divisa in regioni
**Riceve in ingresso la label e l'id della nazione
*/
function doHandleListOfRegionsOrCountries(country, countryId)
{
	if(country=="Italia" || country=="United States")
	{
		if((countryReturnColored!="")&&(document.getElementById(countryReturnColored) != null)){
			document.getElementById(countryReturnColored).className = 'linkDefault';
		}
		if(country != ""){
			document.getElementById(country).className = 'linkRosso';
			countryColored=country;
		}
		clearPanelsById("regionpanellist");		
		doHandleListOfRegionsByCountry(country, countryId);
		
	}
	else
	{
		clearPanelsById("regionpanellist");
		doHandleListOfAreasByCountry(country, countryId);
	}
}

/*
**Funzione che cambia il titolo del pannello e richiama la funzione per ottenere la lista delle regioni
**Riceve in ingresso la label e l'id della nazione 
*/
function doHandleListOfRegionsByCountry(country, countryId)
{		
	//replacePanelTitle("currentRegionArea",country);
	if(country == 'Italia'){
		countryId ="185";
		}	
	if(country == 'United States'){
		countryId ="146";
	}
	getListOfRegionsByCountry(country , lng, countryId);
}

/*
**Funzione che crea il link alla nuova pagina partendo dalla regione
**Alla nuova jsp viene passata la lingua, l'id del continente, l'id e la label della nazione, l'id della regione
**Riceve in ingresso la label e l'id della nazione e l'id della regione
*/
function doHandleListOfAreasByCountryAndRegion(country, countryId, pRegionId)
{
	regionId = pRegionId;
	location.href = "/dealerlocator/gm_countrySelected.jsp?lng="+lng+"&continentId="+pContinentId+"&countryId="+countryId+"&regionId="+regionId+"&country="+country+"";
	regionId = "";
}

/*
**Funzione che crea il link alla nuova pagina partendo dalla nazione
**Alla nuova jsp viene passata la lingua, l'id del continente, l'id e la label della nazione, l'id della regione
**Riceve in ingresso la nazione e l'id della nazione
*/
function doHandleListOfAreasByCountry(country, countryId)
{
	//document.getElementById(country).className = 'linkRosso';
	location.href = "/dealerlocator/gm_countrySelected.jsp?lng="+lng+"&continentId="+pContinentId+"&countryId="+countryId+"&regionId="+regionId+"&country="+country+"";
}
/*
**
**FINE funzioni di gestione dei pannelli in base alle query sul db
**
*/



/*
**
**INIZIO funzioni di gestione dei pannelli
**
*/

/*
**Funzione che visualizza un pannello
**Riceve in ingresso l'id del div
*/
function showDisplay(id)
{
	document.getElementById(id).style.display = "block";
}

/*
**Funzione che nasconde un pannello
**Riceve in ingresso l'id del div
*/
function hideDisplay(id)
{
	document.getElementById(id).style.display = "none";
}

/*
**Funzione che pulisce un pannello
**Riceve in ingresso l'id del div
*/
function clearPanelsById(idPanel){
	var element = document.getElementById(idPanel);
	//alert("clearPanel[" + idPanel + "]:" + element);
	element.innerHTML = "";
	/*while (element.firstChild) {
		element.removeChild(element.firstChild);
	}*/
}

/*
**Funzione che cambia il titolo di un pannello
**Riceve in ingresso l'id del div e il testo da visualizzare
*/
function replacePanelTitle(id,txt)
{
	var o=document.getElementById(id);
	var newNode=document.createTextNode(txt);
	o.replaceChild(newNode,o.childNodes[0]); 	
}
/*
**
**FINE funzioni di gestione dei pannelli
**
*/


/*
**Funzione che crea i link degli elementi dei pannelli
*/

function createLink(id,str,lnk,linkId,title)
{
	var o=document.getElementById(id);
	var textNode=document.createTextNode(str);
	var link=document.createElement("a");
	var br=document.createElement("br");
	link.setAttribute("title",title);
	link.setAttribute("href",lnk);
	link.setAttribute("id",linkId);
	link.className="linkDefault";
	link.appendChild(textNode);
	o.appendChild(link).appendChild(br);
}

/*
**Funzione che salva i parametri ricevuti dalla pagina precedente
*/
function parameterToSaveReturn(){
	
	 pContinentIdReturn = document.getElementById("returnContinentIdDiv").innerHTML;
	 pCountryReturn = document.getElementById("returnCountryDiv").innerHTML;
	 pCountryIdReturn = document.getElementById("returnCountryIdDiv").innerHTML;
	 pLanguageReturn = document.getElementById("returnLanguageDiv").innerHTML;
	 pRegionIdReturn = document.getElementById("returnRegionIdDiv").innerHTML;
	 
	 if((pContinentIdReturn == '17')||(pContinentIdReturn == '28')||(pContinentIdReturn == '96')||(pContinentIdReturn == '97')||(pContinentIdReturn == '20')||(pContinentIdReturn == '99')||(pContinentIdReturn == '100')||(pContinentIdReturn == '101')){	 
	 	onLoadReturn();
	 }
	 if((pContinentIdReturn != '17')&&(pContinentIdReturn != '28')&&(pContinentIdReturn != '96')&&(pContinentIdReturn != '97')&&(pContinentIdReturn != '20')&&(pContinentIdReturn != '99')&&(pContinentIdReturn != '100')&&(pContinentIdReturn != '101')){
	 	onLoad();
	 }
	 
}