function externalLinks() { 
	if ( ! document.getElementsByTagName ) {
		return;
	}
	var anchors = document.getElementsByTagName( "a" );
	for ( var i = 0; i < anchors.length; i++ ) {
		var anchor = anchors[i];
		if ( anchor.getAttribute( "href" ) && anchor.getAttribute( "rel" ) == "external" ) {
			anchor.target = "_blank";
		}
	} 
}
function registrati() { 
	win = window.open( "registrati.html", "Registrati", "width=500,height=500,top=50,left=100,scrollbars=1");
	win.focus();
}
function richiesta() { 
	win = window.open( "richiesta.html" , "Richiesta", "width=500,height=500,top=50,left=100,scrollbars=1");
	win.focus();
}
function credits () { 
	win = window.open( "credits.html" , "Credits", "width=300,height=300,top=50,left=100,scrollbars=0");
	win.focus();
}	
function validateEmail( email ) {
	var emailReg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/; // not valid
	var emailReg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,6}|[0-9]{1,3})(\]?)$/; // valid
	if ( ! ( ! emailReg1.test( email ) && emailReg2.test( email ) ) ) {
		return false;
	} else {
		return true;
	}
}
function registratiSubmit() {
	opener.name = "registrati"
	document.forms[0].target = opener.name;
	self.close();
}
function convalidaRegistrazione( formObj ) {
	if ( ! validateEmail( formObj.email.value ) ) {
		alert( "L'inidirizzo email inserito non è corretto" );
		return false;
	}
}
function spedisci( email ) {
	location = "mailto:" + email.replace( "[presso]", "@" );
}
var menuids = ["treemenu1"];
function buildsubmenus_horizontal() {
	for ( var i = 0; i< menuids.length; i++ ){
		if ( document.getElementById( menuids[i] ) && document.getElementById( menuids[i] ).getElementsByTagName( "ul" ) ) {
			var ultags = document.getElementById(menuids[i] ).getElementsByTagName( "ul" );
			for (var t=0; t<ultags.length; t++){
				if (ultags[t].parentNode.parentNode.id == menuids[i] ){ //if this is a first level submenu
					ultags[t].style.top=ultags[t].parentNode.offsetHeight + "px" //dynamically position first level submenus to be height of main menu item
					ultags[t].parentNode.getElementsByTagName("a")[0].className = "mainfoldericon"
				} else { //else if this is a sub level menu (ul)
					ultags[t].style.left=ultags[t-1].getElementsByTagName("a")[0].offsetWidth+"px" //position menu to the right of menu item that activated it
					ultags[t].parentNode.getElementsByTagName("a")[0].className = "subfoldericon"
				}
				ultags[t].parentNode.onmouseover=function(){
					this.getElementsByTagName("ul")[0].style.visibility = "visible";
				}
				ultags[t].parentNode.onmouseout=function(){
					this.getElementsByTagName("ul")[0].style.visibility = "hidden";
				}
			}
		}
	}
}
function ajaxConnect( strURL, method, formID ) {
	if ( method == undefined ) {
		method = "get";
	}
	var xmlHttpReq = false;
	if ( window.XMLHttpRequest ) {
		xmlHttpReq = new XMLHttpRequest();
	} else if ( window.ActiveXObject ) {
		xmlHttpReq = new ActiveXObject( "Microsoft.XMLHTTP" );
	} else {
		alert( "Il browser in uso non supporta la tecnologia Ajax" );
	}
	if ( method.toLowerCase() == "get" && formID != undefined ) {
	    strURL += ( ( strURL.indexOf( "?" ) == - 1 ) ? "?" : "&" ) + getquerystring( formID );
	}
	xmlHttpReq.open( method, strURL );
	xmlHttpReq.setRequestHeader( "Content-Type", "application/x-www-form-urlEncoded" );
	if ( formID != undefined && method.toLowerCase() == "post" ) {
	    xmlHttpReq.send( getquerystring( formID ) ); 
	} else {
	    xmlHttpReq.send( null ); 
	}
	xmlHttpReq.onreadystatechange = function() {
		if ( xmlHttpReq.readyState == 4 ) {
			eval( xmlHttpReq.responseText );
		}
	};
}
function scegliComune( campoRicerca, identificatoreZona, nomeCampoDestinazione ) {
	if ( campoRicerca.value.length < 3 ) {
		document.getElementById( "suggestMunicipality" ).display = "";
		campoRicerca.form[nomeCampoDestinazione].value = 0;
	} else {
		ajaxConnect( "/js/scegli-comune.php?testoRicerca=" + campoRicerca.value + "&identificatoreZona=" + identificatoreZona + "&nomeCampoDestinazione=" + nomeCampoDestinazione );
	}
}
function selezionaComune( identificatore, nome, nomeCampo ) {
	form = document.forms[0];
	form[nomeCampo].value = identificatore;
	form.searchMunicipality.value = nome;
	document.getElementById( "suggestMunicipality" ).style.display = "";
}
function agency( searchAgency ) {
	form = document.getElementById( "createAccountForm" );
	if ( searchAgency.length < 3 ) {
		document.getElementById( "suggestAgency" ).display = "";
		form["account[entityID]"].value = 0;
	} else {
		ajaxConnect( "/js/agency.php?searchAgency=" + searchAgency );
	}
}
function chooseAgency( agencyID, shortDescription ) {
	form = document.getElementById( "createAccountForm" );
	form["account[entityID]"].value = agencyID;
	form.searchAgency.value = shortDescription;
	document.getElementById( "suggestAgency" ).style.display = "";
}
function zonaMappa( identificatore ) {
	ajaxConnect( "/js/zona-mappa.php?identificatore=" + identificatore );
}
function verificaZonaComune( identificatoreZona, identificatoreComune ) {
	ajaxConnect( "/js/verifica-zona-comune.php?identificatoreZona=" + identificatoreZona + "&identificatoreComune=" + identificatoreComune );
}
function checkAccount() {
}
function immobile( entityID, mode ) {
	var immobileWin = window.open( "immobile.html", "immobile", "width=600,height=600,scrollbars=1" );
	immobileWin.focus();
	document.immobileForm.entityID.value = entityID;
	document.immobileForm.mode.value = mode;
	document.immobileForm.submit();
}
function initGallery( entityID, enviroment ) {
	photoID = 1;
	gallery( entityID, photoID, enviroment );
}
function gallery( entityID, photoID, ambiente ) {
	ajaxConnect( "/js/photogallery.php?entityID=" + entityID + "&photoID=" + photoID + "&ambiente=" + ambiente );
}
function galleryDetail( entityID, photoID ) {
	var gallery = window.open( "gallery-detail.html?entityID=" + entityID + "&photoID=" + photoID, "galleryDetail", "width=560,height=650" );
	gallery.focus();
}
function loadDetail( entityID, photoID ) {
	ajaxConnect( "/js/photodetail.php?entityID=" + entityID + "&photoID=" + photoID );
}
function account() {
	var accountWin = window.open( "account.html", "account", "width=600,height=600" );
	accountWin.focus();
}
function appuntamento( entityID ) {
	var appuntamentoWin = window.open( "appuntamento.html?entityID=" + entityID, "appuntamento", "width=600,height=600" );
	appuntamentoWin.focus();
}
function checkImmobile( form ) {
	if ( form["entities[shortDescription]"].value == "" ) {
		alert( "Inserire il nome dell'immobile" );
		return false;
	}
	form["immovables[price]"].value = form["immovables[price]"].value.replace( '.', '' );
	form["immovables[price]"].value = form["immovables[price]"].value.replace( ',', '.' );
}
function modifyRichTextArea( fieldNameLocal, formNameLocal, divNameLocal ) {
	fieldName = fieldNameLocal;
	formName = formNameLocal;
	divName = divNameLocal;
	window.open( "textarea.html", "textarea", "width=760,height=520" );
}
function addOnloadFunction( functionName ) {
	if ( window.addEventListener ) {
		window.addEventListener( "load", functionName, false );
	}
	if ( window.attachEvent ) {
		window.attachEvent( "onload", functionName );
	}
}
addOnloadFunction( externalLinks );
addOnloadFunction( buildsubmenus_horizontal );
function chiudi() {
	if ( confirm( "Attenzione: la chiusura della finestra comporterà la perdita di tutte le modifiche non salvate. Continuare?" ) ) {
		self.close();
	}
}
function selectValue( oggettoSelect ) {
	return oggettoSelect.options[oggettoSelect.selectedIndex].value;
}
function deleteAllOptions( selectObject ) {
	for ( var i = selectObject.options.length - 1; i >= 0; i-- ) {
		selectObject.remove( i );
	}
}
function addOption( selectObject, newOptionText, newOptionValue ) {
	optionObject = document.createElement( "OPTION" );
	optionObject.text = newOptionText;
	optionObject.value = newOptionValue;
	selectObject.options.add( optionObject );
}
function popolaSelectPrezzo( identificatoreTipoContratto, nomeCampo ) {
	deleteAllOptions( document.getElementById( nomeCampo ) );
	ajaxConnect( "/js/popola-select-prezzo.php?identificatoreTipoContratto=" + identificatoreTipoContratto + "&nomeCampo=" + nomeCampo );
}
