var zone_selected=new Array();

function cambia_categoria(obj){
	var id = obj.id.substr(11);
	with (window.document) {
		getElementById("divRaggio").style.display='none';
		//3 = CATEGORIA_VACANZE 

		if (id==3){
			getElementById("idContratto1").style.display='none';
			getElementById("divZ").style.display='none';
			
			getElementById("divContratto").style.display='block';

			dati.idContratto[1].checked=true;
			dati.selectPrezzoMinimo.selectedIndex=0;
			dati.selectPrezzoMassimo.selectedIndex=0;
 			xajax_getAjaxFascePrezzoPagIndex(dati.idContratto[1].value);
 			getElementById("divPrezzo").style.display='none';
			getElementById("divCittaDirette").style.display = 'none';
			getElementById("linkDirettiCitta").style.display = 'none';

		}
		else{
			//getElementById("divC").style.display='block';
			//getElementById("divZ").style.display='block';
			getElementById("divContratto").style.display='block';
 			getElementById("idContratto1").style.display='block';
			getElementById("divPrezzo").style.display='block';
			getElementById("divCittaDirette").style.display = 'block';			
			getElementById("linkDirettiCitta").style.display = 'block';
		}
		var tabId = document.getElementById("dati").idCategoria;
		for (var ix=0;ix<=maxCategoria;ix++){
			if (getElementById("idCategoria"+ix)){
				getElementById("idCategoria"+ix).className='';
			}
		}
		getElementById("idCategoria"+id).className='selected';
		tabId.value=id;
		dati.idRegione.selectedIndex=0;
		xajax_getAjaxProvincePagIndex("");
		dati.idTipologia.selectedIndex=0;
	}
}

function enableBack(ricerca){
	var oDiv = document.getElementById("div_back");
	if( document.getElementById("divCittaDirette") != null){
		var oDivCitta = document.getElementById("divCittaDirette");
		var oLinkCitta = document.getElementById("linkDirettiCitta");
		var oShowLinkCitta = document.getElementById("showLinkDirettiCitta");
		}
	var oHref = document.getElementById("href_back");
	if (window.document.dati.idRegione.value){
		
		if(window.document.dati.idProvincia.value){
			if( document.getElementById("divCittaDirette") != null){
				oLinkCitta.style.display = "none";
				oDivCitta.style.display = "none";
				oShowLinkCitta.style.display="none";
			}
			if (window.document.dati.idComune.value)
				if(document.getElementById("divZ").style.display == "block" && document.getElementById("divRaggio").style.display=="none"){
					oHref.onclick = function(){ xajax_getAjaxComuniPagIndex('', window.document.dati.idRegione.value,ricerca); return false; };
					oHref.innerHTML="<img style =\"vertical-align: top;margin-top:1px;\" src=\"/img2/precedente.gif\"/> " + window.document.dati.idRegione.options[window.document.dati.idRegione.selectedIndex].text;
				}
		}else {
			oHref.innerHTML="<img style =\"vertical-align: top;margin-top:1px;\" src=\"/img2/precedente.gif\"/> Italia";
			oHref.onclick = function(){ xajax_getAjaxProvincePagIndex('',ricerca); return false;};
		}
	}else{
		oHref.innerHTML="";
		oShowLinkCitta.style.display = "none";
		if( document.getElementById("divCittaDirette") != null)
		if(document.getElementById("idCategoria1").className == 'selected' || document.getElementById("idCategoria2").className == 'selected'){
			oLinkCitta.style.display = "block";			
			oDivCitta.style.display = "block";
		}
	}
}

function hideShowLinkCitta(){
	if(window.document.dati.idRegione.value){
		var newDiv = document.getElementById("showLinkDirettiCitta");
		var oDiv = document.getElementById("linkDirettiCitta");
		var childDiv = oDiv.childNodes;
		var numShowCitta = 0;
		svuota_cointainer(newDiv);
		
		for (var i=0; i< childDiv.length ; i++){
			if( childDiv[i].id.indexOf("link_" + window.document.dati.idRegione.value) > -1 ){
				if(numShowCitta){
					var oSpan = document.createElement("span");
					oSpan.innerHTML +=", ";
					newDiv.appendChild(oSpan);
				}		
				var oHref = document.createElement("a");
				oHref.setAttribute("href","#");
				oHref.setAttribute("id","show_"+ childDiv[i].id);
				oHref.innerHTML += childDiv[i].innerHTML;
				
				oHref.setAttribute("onclick",childDiv[i].getAttribute("onclick"));
				newDiv.appendChild(oHref);
				numShowCitta++;
			}
		}
		document.getElementById("linkDirettiCitta").style.display = "none";
		
		if(!numShowCitta || (window.document.dati.idProvincia && window.document.dati.idProvincia.value) || document.getElementById("idCategoria3").className == 'selected' ){

			document.getElementById("divCittaDirette").style.display = "none";
			document.getElementById("showLinkDirettiCitta").style.display = "none";
		}else{
			document.getElementById("divCittaDirette").style.display = "block";
			document.getElementById("showLinkDirettiCitta").style.display = "block";
		}
	}
}
		
function changeImage (region) {
	var ShowItem = document.getElementById("div_regioni");
	ShowItem.style.backgroundImage = 'url(/img2/cartine/' + region + '_selected.gif)';
	return true;
}

function hideImage (region) {

	var ShowItem = document.getElementById("div_regioni");
	if (ShowItem)
		ShowItem.style.backgroundImage = 'url(/img2/cartine/transparent.gif)';
	return true;
}

function changeImageProv (provincia) {
	var oDiv = document.getElementById("div_provincie");
	if (oDiv)
		oDiv.style.backgroundImage = 'url(/img2/cartine/' + window.document.dati.idRegione.value + '/select_' + provincia + '.gif)';		
}

function hideImageProv (provincia) {
	var oDiv = document.getElementById("div_provincie");
	oDiv.style.backgroundImage = 'url(/img2/cartine/transparent.gif)';	
}

function changeImageZone(zona) {
	var oDiv = document.getElementById("div_zone");
	if(zona > 4999 && zona < 5200) {
		oDiv.style.backgroundImage = 'url(/img2/cartine/' + window.document.dati.idRegione.value + '/MB/map_zona_' + zona + '.gif)';
	} else {
		oDiv.style.backgroundImage = 'url(/img2/cartine/' + window.document.dati.idRegione.value + '/' + window.document.dati.idProvincia.value +'/map_zona_' + zona + '.gif)';
	}
}


function hideImageZone() {
	var oDiv = document.getElementById("div_zone");
	oDiv.style.backgroundImage = 'url(/img2/cartine/transparent.gif)';	
}

function selectZona(citta, zone) {
	url_map = "/img2/cartine/" + window.document.dati.idRegione.value + "/" + citta + "/map_zona_";
	var oCointainer = document.getElementById("zone_container");
	var browser = navigator.userAgent.toLowerCase();
	if ( browser && browser.indexOf("msie")!=-1){
		oCointainer.style.position ="relative";
		oCointainer.style.left = "-130px";
	}
	
	var new_selected=new Array();
	var flag=0;
	svuota_cointainer(oCointainer);
	if (zone_selected.length != 0 ){
		for (i=0,j=0; i< zone_selected.length ;i++){
			if (zone_selected[i] != zone){
				new_selected[j] = zone_selected[i];
				createDivMap(oCointainer, new_selected[j], url_map); 
				j++;				
			} else { flag=1; } 
		}
		if (!flag){ 
			new_selected[zone_selected.length] = zone;
			createDivMap(oCointainer, zone, url_map);
		}
		zone_selected=new_selected;
	}else { 
			zone_selected[0] = zone;
			createDivMap(oCointainer, zone, url_map);
	}
	return true;
}

function selectProvincia(provincia) {
	var oContainer = document.getElementById("provincie_container");
	var oDivChild = document.createElement("div");
	var oImg = document.createElement("img");
	var browser = navigator.userAgent.toLowerCase();
	if ( browser && browser.indexOf("msie")!=-1){
		oContainer.style.position ="relative";
		oContainer.style.left = "-130px";
	}

	svuota_cointainer(oContainer);
	
	oDivChild.style.position="absolute";
	oDivChild.id = provincia;
	oImg.src ="/img2/cartine/" + window.document.dati.idRegione.value + "/select_" + provincia + ".gif";
	oImg.useMap="#Map";
	oImg.border="0";
	oDivChild.appendChild(oImg);
	oContainer.appendChild(oDivChild);
	
	var oArea = document.getElementById("_map_" + provincia);
	oArea.removeAttribute("shape");
}

function createDivMap (oCointainer, id_zona, url_map){
	var oDiv = document.createElement("div");
    var oImg = document.createElement("img");
	
	oDiv.id = "map_zona_" + id_zona;		
	oDiv.style.position = "absolute";	
	oImg.src = url_map + id_zona + ".gif";
	oImg.useMap = "#Map";
	oImg.border="0";
	oDiv.appendChild(oImg);

	oCointainer.appendChild(oDiv);
}

function svuota_cointainer(oCointainer){
	while (oCointainer.hasChildNodes())
		oCointainer.removeChild(oCointainer.firstChild);	
}	
	
function enableTooltip(id_zona){
	var oDiv = document.getElementById("tooltip_div");
	var oArea=document.getElementById(id_zona);
	
	titolo=oArea.getAttribute("alt");
	if (titolo != "" && oDiv){
		oArea.removeAttribute("title");
		oDiv.innerHTML = titolo;
	}
}
	
function disableTooltip(){
	var oDiv = document.getElementById("tooltip_div");
	if(oDiv && oDiv.hasChildNodes())
		oDiv.removeChild(oDiv.firstChild);
}

function preloadImageNazione(){
	if (!document.getElementById("regioni"))
		return (null);

	var oMapRegioni = document.getElementById("regioni");

	//var childDiv = oMapRegioni.childNodes;
	var aoAreaReagioni = oMapRegioni.getElementsByTagName('area');

	var imageLoad = new Image();
	for (var i = 0; i < aoAreaReagioni.length; i++){
		if (!aoAreaReagioni[i])
			continue;

		var sPathImg = "/img2/cartine/" + aoAreaReagioni[i].id.substring(5) + "_selected.gif";
		imageLoad.src = sPathImg;
	}

}

function preloadImageProvincie(regione, image){
		var imageLoad = new Image();
		imageLoad.src = "/img2/cartine/" + regione + "/select_prov_" + image + ".gif";
}

function preloadImageZone(comune, image){
		var imageLoad = new Image();
		imageLoad.src = "/img2/cartine/" + window.document.dati.idRegione.value + "/" + comune + "/map_zona_" + image + ".gif";
}
