var zone_selected=new Array();


function getScrollPosition(name){
	scrolly = (document.all)?document.documentElement.scrollTop:window.pageYOffset;
	scrollx = (document.all)?document.documentElement.scrollLeft:window.pageXOffset;
	createCookie(name+'_yPos',scrolly,0,0,5);
	createCookie(name+'_yPos',scrolly,0,0,5);
}

function resetCookies(){
 createCookie('ricerca_immobili_yPos',0,0,0,5);
 createCookie('ricerca_agenzie_yPos',0,0,0,5);
 createCookie('nuove_costruzioni_yPos',0,0,0,5);
 
}

function setScrollPosition(name){
    var yPos = readCookie(name+'_yPos');
    var xPos = readCookie(name+'_xPos');
   
    if (yPos || xPos){
	window.scrollTo(xPos,yPos);
	setTimeout("resetCookies()",1000);
    }
	
}

function setHomeSection(cat){
	createCookie('home_section',cat);
}

function getHomeSection(){
	var sec = readCookie('home_section');
	var obj = document.getElementById("idCategoria"+sec);
	if (obj){
		cambia_categoria(obj);
		xajax_getAjaxTipologiaPagIndex(sec);
	}
		
}

function cambia_categoria_nc(obj){
	var id=obj.id;
	var menus = document.getElementsByClassName("nc_tab");
	for (var i=0; i<menus.length; i++){
		menus[i].setAttribute('class',"nc_tab"); 
	}
	var menu = document.getElementById(obj.id);
	menu.setAttribute('class','nc_tab selected');
	
	
	
	var subid;
	subid = "sub_"+obj.id;
	var submenus = document.getElementsByClassName("sub_idCategoria");
	for (var i=0; i<submenus.length; i++){
		submenus[i].style.visibility='hidden'; 
	}
	var submenu = document.getElementById(subid);
	submenu.style.visibility='visible';
	
}


function cambia_categoria(obj){
	var subid = "sub_"+obj.id;
	if (subid){
		var submenus = document.getElementsByClassName("sub_idCategoria");
		for (var i=0; i<submenus.length; i++){
			submenus[i].style.visibility='hidden'; 
		}
		var submenu = document.getElementById(subid);
		submenu.style.visibility='visible';
		
		
		var id = obj.id.substr(11);
		with (window.document) {
			$('divLocalita').style.display='none';

			// 5 = ESTERO
			if (id == 5){
				$('CartinaGrande').style.display = 'none'
				if ($('CartinaGrandeEstero'))
					if ($('CartinaGrandeEstero').style.display == 'none')
						$('CartinaGrandeEstero').style.display = 'block';
				if ($('idCategoriaEstero'))
					$('idCategoriaEstero').value = 5;

				OnPageload();
				
				if ($('international'))
					$('international').style.display='none';

				$('divN').style.display='block';

				$('divCittaDirette').style.display = 'none';
				$('linkDirettiCitta').style.display = 'none';
				$('showLinkDirettiCitta').style.display = 'none';

				$('divEsteroCittaDirette').style.display = 'block';
				$('linkEsteroDirettiCitta').style.display = 'block';
				$('showLinkEsteroDirettiCitta').style.display = 'block';


				xajax_getAjaxNazionePagIndex("");
				  
//				$('box_ricerca_home').setStyle({height : '337px'}) ;
				hideRicercaAvanzata('ricercaAvanzata','textRicercaA');
	
				if (!$('selectBox').hasClassName('resizeEstero')){
					$('selectBox').addClassName('resizeEstero');

					if (!window.XMLHttpRequest) {
						$('selectBox').style.marginRight = '-38px';
					}
					$('selectBox').setStyle({width : '325px'});
				}

				if ($('div_back').style.display)
					$('div_back').setStyle({display : 'none'}) ;

				if ( $('MimMax'))
 					$('MimMax').setStyle({width:'240px'});


				if ($('bottoneInvia'))
					$('bottoneInvia').setStyle({width : '311px'});
				
				if ($('idTipologia').hasClassName('selFormGeo')){
					$('idTipologia').removeClassName('selFormGeo');
					$('idTipologia').addClassName('selFormGeoEstero');
				}

 				if ($('prezzoMinimo'))
					$('prezzoMinimo').style.width = '98px';

 				if ($('prezzoMassimo'))
					$('prezzoMassimo').style.width = '98px';

				//xajax_getAjaxRegioniPagIndex('window.document.dati.idNazione.value')

				$('divR').style.display='none';
			}else {
				if ($('CartinaGrandeEstero'))
					$('CartinaGrandeEstero').style.display = 'none'
				if ($('CartinaGrande').style.display == 'none')
					$('CartinaGrande').style.display = 'block'
				if ($('idCategoriaEstero'))
					$('idCategoriaEstero').value = "";

			
				if ($('international'))
					$('international').style.display='block';
				$('divN').style.display='none';
				$('divR').style.display='block';
				$('showLinkEsteroDirettiCitta').style.display = 'none';
				$('divEsteroCittaDirette').style.display = 'none';
				$('linkEsteroDirettiCitta').style.display = 'none';

//				$('box_ricerca_home').setStyle({height : '337px'}) ;

				if ($('selectBox').hasClassName('resizeEstero')){
					$('selectBox').removeClassName('resizeEstero');

					if (!window.XMLHttpRequest) {
						$('selectBox').style.marginRight = '0px';
					}

					$('selectBox').setStyle({width : '380px'});
				}

				if ($('div_back').style.display)
					$('div_back').setStyle({display : 'block'}) ;

				if ($('MimMax'))
					$('MimMax').setStyle({width:'294px'});

				if ($('bottoneInvia'))
					$('bottoneInvia').setStyle({width:'372px'});

				if ($('idTipologia').hasClassName('selFormGeoEstero')){
					$('idTipologia').removeClassName('selFormGeoEstero');
					$('idTipologia').addClassName('selFormGeo');
				}

				if ($('prezzoMinimo'))
					$('prezzoMinimo').style.width = '125px';

				if ($('prezzoMassimo'))
					$('prezzoMassimo').style.width = '125px';


				xajax_getAjaxRegioniPagIndex("IT");
				xajax_getAjaxProvincePagIndex("");

			}
			//3 = CATEGORIA_VACANZE , 4 = CATEGORIA_STANZE
			if (id==3 || id==4){
//				$('box_ricerca_home').setStyle({height : '337px'}) ;
				hideRicercaAvanzata('ricercaAvanzata','textRicercaA');

				$('idContratto1').style.display='none';
				$('divZ').style.display='none';
				$('divContratto').style.display='none';
				$('divC').style.display='none';
				
				dati.idContratto[1].checked=true;
//				dati.selectPrezzoMinimo.selectedIndex=0;
//				dati.selectPrezzoMassimo.selectedIndex=0;
				xajax_getAjaxFascePrezzoPagIndex(dati.idContratto[1].value);
				$('divPrezzo').style.display='none';
				$('divCittaDirette').style.display = 'none';
				$('linkDirettiCitta').style.display = 'none';
				$('divEsteroCittaDirette').style.display = 'none';
				$('linkEsteroDirettiCitta').style.display = 'none';
			}
			else{
				$('divC').style.display='block';
				$('divZ').style.display='block';
				$('divContratto').style.display='block';
				$('idContratto1').style.display='block';
				$('divPrezzo').style.display='block';

				if (id != 5){
					showRicercaAvanzata('ricercaAvanzata','textRicercaA','ricercaAvanzata','divPrezzo','box_ricerca_home');
					if ($('ricercaAvanzata').style.display=='none'){
						$('divCittaDirette').style.display = 'block';	
						$('linkDirettiCitta').style.display = 'block';
					}else{
						$('divCittaDirette').style.display = 'none';	
						$('linkDirettiCitta').style.display = 'none';
					 }
					
/*					getElementById("divEsteroCittaDirette").style.display = 'block';
					getElementById("linkEsteroDirettiCitta").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("");
	
			if(dati.idNazione)
				dati.idNazione.selectedIndex=0;
			if (dati.idRegione) 
				dati.idRegione.selectedIndex=0;
			if (dati.idProvincia) 
				dati.idProvincia.selectedIndex=0;
			if (dati.idTipologia)
				dati.idTipologia.selectedIndex=0;
		}
	}
}

function resettaEstero(valore){	
	
		if ((valore == 0)&&(window.document.dati.idNazione.value!='')) {

			window.document.getElementById("divEsteroCittaDirette").style.display = 'none';
			window.document.getElementById("linkEsteroDirettiCitta").style.display = 'none';
	
	
		}else{
			window.document.getElementById("divEsteroCittaDirette").style.display = 'none';
			if (!window.document.dati.idNazione.value){
			/*	window.document.getElementById("linkEsteroDirettiCitta").style.display = 'block';
				window.document.getElementById("showLinkEsteroDirettiCitta").style.display = 'block';
*/
			}
			
		}	
	
}


function enableBack(ricerca){
	if (typeof Italia == "undefined"){
		Italia = "Italia";
	}
	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 && window.document.dati.idRegione.value){
		if(window.document.dati.idProvincia && 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 && window.document.dati.idComune.value){
				if(document.getElementById("divZ").style.display == "block" && document.getElementById("divLocalita").style.display=="none"){
					if(!ricerca) {
						oHref.onclick = function(){ xajax_getAjaxComuniPagIndex('', window.document.dati.idRegione.value); return false; };
					}else{
						oHref.onclick = function(){ xajax_getAjaxComuniPagIndex('', window.document.dati.idRegione.value,ricerca); return false; };
					}
					oHref.innerHTML="<img style =\"vertical-align: top;margin-top:1px;\" src=\"/casa/it/img/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=\"/casa/it/img/precedente.gif\"/> "+Italia;
				if(!ricerca) {
					oHref.onclick = function(){ xajax_getAjaxProvincePagIndex(''); return false;};
				}else{
					oHref.onclick = function(){ xajax_getAjaxProvincePagIndex('','IT', ricerca); return false;};
				}
			}
		}else {
			oHref.innerHTML="<img style =\"vertical-align: top;margin-top:1px;\" src=\"/casa/it/img/precedente.gif\"/> "+Italia;
			if(!ricerca) {
					oHref.onclick = function(){ xajax_getAjaxProvincePagIndex(''); return false;};
			}else{
					oHref.onclick = function(){ xajax_getAjaxProvincePagIndex('','IT', ricerca); return false;};
			}
		}
	}
	else{
		oHref.innerHTML="";
		if( document.getElementById("divCittaDirette") != null) {
			oShowLinkCitta.style.display = "none";
			oLinkCitta.style.display = "block";			
			oDivCitta.style.display = "block";
		}
	}
}

function enableBack_NC(ricerca){
	
	var oDiv = document.getElementById("div_back");
	oLinkCitta = document.getElementById("linkallecitta_NC");
	var oHref = document.getElementById("href_back");
	oHref.innerHTML="<img style =\"vertical-align: top;margin-top:1px;\" src=\"/casa/it/img/precedente.gif\"/> Italia";
	oHref.onclick = function(){ xajax_getAjaxProvincePagIndex('','IT',ricerca);oLinkCitta.style.display = "block";return false;};

}




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 && 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.className="linkHome";
				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){
			document.getElementById("divCittaDirette").style.display = "none";
			document.getElementById("showLinkDirettiCitta").style.display = "none";
		}else{
			if (document.getElementById('ricercaAvanzata').style.display=='none'){
				document.getElementById("divCittaDirette").style.display = "block";
				document.getElementById("showLinkDirettiCitta").style.display = "block";
			}
		}
	}
}


function hideShowLinkEsteroCitta(){

        if (window.document.dati.idNazione.value || window.document.dati.idProvincia ) {
		
		var newDiv = document.getElementById("showLinkEsteroDirettiCitta");
		var oDiv = document.getElementById("linkEsteroDirettiCitta");
		
		var childDiv = oDiv.childNodes;
		var numShowCitta = 0;
		svuota_cointainer(newDiv);

		for (var i=0; i< childDiv.length ; i++){
			if(childDiv[i].id && childDiv[i].id.indexOf("link_" + window.document.dati.idNazione.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("linkEsteroDirettiCitta").style.display = "none";

		if(!numShowCitta || (window.document.dati.idRegione && window.document.dati.idRegione.value) || document.getElementById("idCategoria3").className == 'selected' || document.getElementById("idCategoria4").className == 'selected' ){
			document.getElementById("divEsteroCittaDirette").style.display = "none";
			document.getElementById("showLinkEsteroDirettiCitta").style.display = "none";
		}
	}
	else
	{
		document.getElementById("divEsteroCittaDirette").style.display = "block";
		document.getElementById("linkEsteroDirettiCitta").style.display = "block";

	}
}

function changeImageNaz(nazione) {
	var ShowItem = document.getElementById("div_nazioni");
	ShowItem.style.backgroundImage = 'url(/pic/cartine/' + nazione + '_selected.gif)';
	return true;
}

function hideImageNaz(nazione) {
	var ShowItem = document.getElementById("div_nazioni");
	ShowItem.style.backgroundImage = 'url(/pic/transparent.gif)';
	return true;
}
		
function changeImage(region) {
	var ShowItem = document.getElementById("div_regioni");
	ShowItem.style.backgroundImage = 'url(/pic/cartine/' + region + '_selected.gif)';
	return true;
}

function hideImage(region) {
	var ShowItem = document.getElementById("div_regioni");
	ShowItem.style.backgroundImage = 'url(/pic/transparent.gif)';
	return true;
}

function changeImageProv(provincia) {
	var oDiv = document.getElementById("div_provincie");
	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(/pic/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(/pic/transparent.gif)';	
}

function selectLocalita(citta, zone) {
	var new_selected=new Array();
	var flag=0;

	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];
				j++;				
			}else {
				flag=1;
			} 
		}
		if (!flag){ 
			new_selected[zone_selected.length] = zone;
		}
		zone_selected=new_selected;
	}else { 
			zone_selected[0] = zone;
	}
	return true;
}


function selectZona(citta, zone) {
	url_map = "/img2/cartine/" + window.document.dati.idRegione.value + "/" + citta + "/map_zona_";
	var oContainer = document.getElementById("zone_container");
	var browser = getBrowser();
	if ( browser && browser[0].toLowerCase().indexOf("explorer")!=-1 && parseInt(browser[1]) < 8){
	//if ( browser && browser.indexOf("msie")!=-1){
		oContainer.style.position ="relative";
		oContainer.style.left = "-150px";
		oContainer.style.top = "-1px";
	}
	
	var new_selected=new Array();
	var flag=0;
	svuota_cointainer(oContainer);
	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(oContainer, new_selected[j], url_map); 
				j++;				
			} else { flag=1; } 
		}
		if (!flag){ 
			new_selected[zone_selected.length] = zone;
			createDivMap(oContainer, zone, url_map);
		}
		zone_selected=new_selected;
	}else { 
			zone_selected[0] = zone;
			createDivMap(oContainer, 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 = getBrowser();
	if ( browser && browser[0].toLowerCase().indexOf("explorer")!=-1 && parseInt(browser[1]) < 8){
	//if ( browser && browser.indexOf("msie")!=-1){
		oContainer.style.position ="relative";
		oContainer.style.left = "-150px";
		oContainer.style.top = "-1px";
	}

	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);
	if (oArea)
		oArea.removeAttribute("shape");
}
function selectNazione(nazione) {
	var oContainer = document.getElementById("nazioni_container");
	var oDivChild = document.createElement("div");
	var oImg = document.createElement("img");
	var browser = getBrowser();
	if ( browser && browser[0].toLowerCase().indexOf("explorer")!=-1 && parseInt(browser[1]) < 8){
	//if ( browser && browser.indexOf("msie")!=-1){
		oContainer.style.position ="relative";
		oContainer.style.left = "-150px";
		oContainer.style.top = "-1px";
	}

	svuota_cointainer(oContainer);
	
	oDivChild.style.position="absolute";
	oDivChild.id = nazione;
	oImg.src ="/img2/cartine/" + window.document.dati.idNazione.value + "_selected.gif";
	oImg.useMap="#nazioni";
	oImg.border="0";
	oDivChild.appendChild(oImg);
	oContainer.appendChild(oDivChild);
	
	//var oArea = document.getElementById("_map_" + nazione);
	//oArea.removeAttribute("shape");
}

function createDivMap(oContainer, 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);

	oContainer.appendChild(oDiv);
}

function svuota_cointainer(oContainer){
	if(oContainer) {
		while (oContainer.hasChildNodes())
			oContainer.removeChild(oContainer.firstChild);
	}
	
}

function svuotaDivContainer(idDiv)
{
	oDivParent = window.document.getElementById(idDiv);
	if (oDivParent)
		svuota_cointainer(oDivParent);
}
	
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 preloadImageEstero(){
	var oMapNazioni = document.getElementById("nazioni");
	if (oMapNazioni){
		var childDiv = oMapNazioni.childNodes;
		for (var i=0; i< (childDiv.length - 1) ; i++){
			var imageLoad = new Image();
			if(childDiv[i].id)
				imageLoad.src = "/img2/cartine/" + childDiv[i].id.substring(5) + "_selected.gif";

		}
	}
}

function preloadImageNazione(){
	if (!document.getElementById("regioni"))
		return (null);

	var oMapRegioni = document.getElementById("regioni");
	var aRegioni = oMapRegioni.getElementsByTagName('area');
	var imageLoad = new Image();
	for (var i = 0; i < aRegioni.length; i++){
		if (!aRegioni[i])
			continue;
		var sPathImg = "/img2/cartine/" + aRegioni[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";
}

function abilitaCitta() {
	var oDivNewCitta = window.document.getElementById("otherLinkDirettiCitta");
	var oDivCitta = window.document.getElementById("linkDirettiCitta");
	if (oDivNewCitta && oDivCitta){
		oDivAltre = window.document.getElementById("linkAltreCitta");
		if(oDivAltre)
			oDivAltre.style.display="none";
		oDivCitta.innerHTML += oDivNewCitta.innerHTML;
	}
}

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}

