function addEvent( obj, ev, fn ){
	if ( obj.addEventListener ) {
		obj.addEventListener( ev, fn, false );
	} else if ( obj.attachEvent ) {
		obj.attachEvent('on'+ev, fn);
	} else {
		if ( typeof( obj['on' + ev] ) == 'function' ){
			var f = obj['on' + ev];
			obj['on' + ev] = function(){if(f)f();fn()}
		}
		else obj['on' + ev] = fn;
	}
}
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 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'indirizzo email inserito non è corretto" );
		return false;
	}
}
function ajaxConnect( strURL, method, formID ) {
	if ( method == undefined ) {
		method = "get";
	}
	var xmlHttpReq = false;
    var self = this;
    if ( window.XMLHttpRequest ) {
        self.xmlHttpReq = new XMLHttpRequest();
    } else if ( window.ActiveXObject ) {
        self.xmlHttpReq = new ActiveXObject( "Microsoft.XMLHTTP" );
    } else {
		alert( "Il browser in uso non supporta la tecnologia Ajax" );
	}
	self.xmlHttpReq.open( method, strURL );
	self.xmlHttpReq.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded" );
	self.xmlHttpReq.onreadystatechange = function() {
		if ( self.xmlHttpReq.readyState == 4 ) {
			eval( self.xmlHttpReq.responseText );
		}
	}
	if ( formID != undefined ) {
	    self.xmlHttpReq.send( getquerystring( formID ) ); 
	} else {
	    self.xmlHttpReq.send( null ); 
	}
	return self.xmlHttpReq;
}
function region( regionID ) {
	src = "/js/region.php?regionID=" + regionID;
	ajaxConnect( src );
}
function municipality( searchMunicipality, municipalityID ) {
	if ( searchMunicipality.length < 3 ) {
		document.getElementById( "suggestMunicipality" ).display = "";
		document.forms[0].municipalityID.value = 0;
	} else {
		src = "/js/municipality.php?searchMunicipality=" + searchMunicipality;
		ajaxConnect( src );
	}
}
function chooseMunicipality( municipalityID, shortDescription ) {
	form = document.forms[0];
	form.municipalityID.value = municipalityID;
	form.searchMunicipality.value = shortDescription;
	document.getElementById( "suggestMunicipality" ).style.display = "";
}
function checkAccount( form ) {
	return true;
}
function gallery( entityID ) {
	var win = window.open( "galleria-det.html?entityID=" + entityID, "GalleriaFotografica", "width=820,height=740,scrollbars=1" );
	win.focus();
}
function account() {
	var accountWin = window.open( "account.html", "account", "width=600,height=600,scrollbars=1" );
	accountWin.focus();
}
function spedisci( email ) {
	location = "mailto:" + email.replace( "[presso]", "@" );
}
addEvent( window, 'load', externalLinks );