// <![CDATA[
var chiave = "";
var chiaveVal = "";
var chiaveTs;
var chiaveLenght = 0;

function sDin() {

	numBrands = 3;
	brandS = "-1";
	continentS = "-1";
	countryS = "-1";
	regionS = "-1";
	
	/*JQ('div#divContinent > div.jqTransformSelectWrapper').remove(0);
	JQ('div#divCountry > div.jqTransformSelectWrapper').remove(0);
	JQ('div#divRegion > div.jqTransformSelectWrapper').remove(0);*/
	
	
	if (s1 == "") {			
		for ( i=1; i<=numBrands; i++) {
			if ($("brandSel"+i).checked)
				brandS = $("brandSel"+i).value;
		}
	} else {
		brandS = s1;
		for ( i=1; i<=numBrands; i++) {
			if ($("brandSel"+i).value == brandS)
				$("brandSel"+i).checked = "true";
		}
	}
	
	var where = (navigator.appName == "Microsoft Internet Explorer") ? -1 : null; 
	
	//CONTINENTS
	if (s2 == "") {		
		continentS = $("continentSel").value;
	} else {
		continentS = s2;
	}
	if (!dependencies[brandS])
		continentS = "-1";
				
	while ($("continentSel").options.length) {
		$("continentSel").remove(0);
	}
	$('divContinent').innerHTML = '<select name="continentSel" id="continentSel" onchange="sDin();"></select>';	//serve per annullare la precedente trasformazione
	
	newElem = document.createElement("option");
	newElem.text = selectContinent;
	newElem.value = "-1";
	if (continentS == "-1")
			newElem.selected="selected";
	$("continentSel").add(newElem,where);
	
	verif = 0;
	for (j in dependencies[brandS]) {
	
		newElem = document.createElement("option");
		newElem.text = continent[j];
		newElem.value = j;		
		if (continentS == j) {
			newElem.selected="selected";
			verif = 1;
		}
		$("continentSel").add(newElem,where);

	}
	if (verif == 0) continentS = "-1";
	
	//COUNTRIES
	if (s3 == "") {
		countryS = $("countrySel").value;
	} else {
		countryS = s3;
	}
	if (continentS != "-1")
		if (!dependencies[brandS][continentS])
			countryS = "-1";
	if (continentS == "-1")
		countryS = "-1";
	
	while ($("countrySel").options.length) {
		$("countrySel").remove(0);
	}		
	$('divCountry').innerHTML = '<select name="countrySel" id="countrySel" onchange="sDin();"></select>'; //serve per annullare la precedente trasformazione
	
	newElem = document.createElement("option");
	newElem.text = selectCountry;
	newElem.value = "-1";
	if (countryS == "-1")
			newElem.selected="selected";
	$("countrySel").add(newElem,where);
	
	if (continentS!="-1") {
		$("divCountry").style.display = "";
		verif = 0;
		for (k in dependencies[brandS][continentS]) {
		
			newElem = document.createElement("option");
			newElem.text = country[k];
			newElem.value = k;			
			if (countryS == k) {
				newElem.selected="selected";
				verif = 1;
			}
			$("countrySel").add(newElem,where);

		}
		if (verif == 0 ) countryS = "-1";
	} else {
		$("divCountry").style.display = "none";
	}
	
	//REGIONS
	if (s4 == "") {		
		regionS = $("regionSel").value;
	} else {
		regionS = s4;
	}
	if (continentS != "-1")
		if (countryS != "-1")
			if (!dependencies[brandS][continentS][countryS])
				regionS = "-1";
	if (continentS == "-1" && countryS == "-1")	
		regionS = "-1";
	if (countryS != "IT")
		regionS = "-1";
	
	while ($("regionSel").options.length) {
		$("regionSel").remove(0);
	}		
	$('divRegion').innerHTML = '<select name="regionSel" id="regionSel" onchange="sDin();"></select>'; //serve per annullare la precedente trasformazione
	
	newElem = document.createElement("option");
	newElem.text = selectRegion;
	newElem.value = "-1";
	if (regionS == "-1")
			newElem.selected="selected";
	$("regionSel").add(newElem,where);
	
	if (continentS=="EU" && countryS=="IT") {
		$("divRegion").style.display = "";
		
		verif = 0;
		for (h in dependencies[brandS][continentS][countryS]) {
		
			newElem = document.createElement("option");
			newElem.text = region[h];
			newElem.value = h;
			
			if (regionS == h) {
				newElem.selected="selected";
				verif = 1;
			}
			$("regionSel").add(newElem,where);

		}
		if (verif == 0) regionS = "-1";
	} else {
		$("divRegion").style.display = "none";
	}
	
	//ZIPCODE CITY inputtext
	if (countryS!="-1")
		$("divZipcode").style.display = "";
	else
		$("divZipcode").style.display = "none";
	
	//Ripristino stato iniziale JQTransform per reinizializzare la form	
	$('distribuzione-form').className="";
	$('continentSel').className="";
	$('countrySel').className="";
	$('regionSel').className="";
	
	//Abilito/disabilito il pulsante di ricerca
	if (brandS != "-1" && continentS != "-1" && countryS != "-1") {
		$("cerca").disabled = false;
	} else {
		$("cerca").disabled = true;
	}
	
	//Converto i %20 nel campo di testo della city in spazi
	citta = $("insertCity").value;
	$("insertCity").value = citta.replace(/%20/gi," ");	
	
	//Effettuo la trasformazione del form
	JQ('form').jqTransform();	
	
	s1=""; s2=""; s3=""; s4="";	
	
	JQ('div#divContinent > div.jqTransformSelectWrapper').keydown(
		function(event){
			sel = 'div#divContinent';
			dropSelect(sel, event);
		});
	JQ('div#divCountry > div.jqTransformSelectWrapper').keydown(
		function(event){
			sel = 'div#divCountry';
			dropSelect(sel, event);
		});
	JQ('div#divRegion > div.jqTransformSelectWrapper').keydown(
		function(event){
			sel = 'div#divRegion';
			dropSelect(sel, event);
		});
	
}		

function filterSearch (url, lang) {

	var indirizzo = "/portal/page/portal/UI/webpages/groupsite/" + url; 
	var ind = "";
	
	if (brandS != "-1" && continentS != "-1" && countryS != "-1") {
	
		ind = "brand:" + brandS;		
		ind = ind + ";continent:" + continentS;		
		ind = ind + ";country:" + countryS;		
		if (regionS != "-1")
			ind = ind + ";region:" + regionS;
		if ($("insertCity").value != "")
			ind = ind + ";city:" + $("insertCity").value;
		
		indirizzo = indirizzo + "?p=" + ind + "&lang=" + lang;
		indirizzo = indirizzo.replace(" ", "%20");
		//alert (indirizzo);
		location.href = indirizzo;
		//$("distribuzione-form").action = indirizzo;
		
	}
}

function dropSelect(sel, e) {
	chiave1=e.keyCode ? e.keyCode : e.which ? e.which : e.charCode;
	chiaveVal1 = String.fromCharCode(chiave1);
	if (chiaveLenght == 0)
		chiaveTsOld = new Date();
	else
		chiaveTsOld= chiaveTs;
	chiaveTs = new Date();
	intervallo = chiaveTs.getTime() - chiaveTsOld.getTime();
	if (intervallo > 500) {
		chiaveLenght = 0;
		chiaveVal = "";
	}
	chiaveLenght++;
	chiaveVal = chiaveVal + String.fromCharCode(chiave1);
		
	var op = JQ(sel + ' > div.jqTransformSelectWrapper > ul > li:last > a').attr('index');
	var j=1;
	for (var i=1; i<=op; i++) {
		var op2 = JQ(sel + ' > div.jqTransformSelectWrapper > ul > li:eq('+i+') > a');
		if ( op2.text().substring(0,chiaveLenght).toUpperCase() == chiaveVal) {		
			i=op+1;
			op2.focus();
		}								
	}						
}

// ]]>				
