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 startList() {
	if ( document.all && document.getElementById ) {
		navRoot = document.getElementById("menu");
		for ( i = 0; i < navRoot.childNodes.length; i++ ) {
			node = navRoot.childNodes[i];
			if ( node.nodeName == "LI" ) {
				node.onmouseover = function() {
					this.className += " over";
				}
				node.onmouseout = function() {
					this.className = this.className.replace( " over", "" );
				}
			}
		}
	}
}
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 spedisci( email ) {
	location = "mailto:" + email.replace( "[presso]", "@" );
}
function changeSchoolTypeCaption( ID ) {
	document.getElementById( "schoolTypeCaption" ).innerHTML = schoolTypes[ID];
}
function changeActivityTypeCaption( ID ) {
	document.getElementById( "activityTypeCaption" ).innerHTML = activityTypes[ID];
}
function dettaglioFoto( img, width, height ) {
	var win = window.open( "/dettaglio-foto.html?img=" + img, "dettaglioFoto", "width=" + ( width + 20 ) + ",height=" + ( height + 40 ) );
	win.focus();
}
function checkSearch( form ) {
	if ( form.searchString.value == "" ) {
		alert( "Inserire un termine di ricerca" );
		return false;
	}
}
function validateEmail( email ) {
	var emailReg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/;
	var emailReg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,6}|[0-9]{1,3})(\]?)$/;
	return ! emailReg1.test( email ) && emailReg2.test( email );
}
function convalidaInfo() {
	form = document.getElementById( "info" );
	campiObbligatori = {	"email" : "Email",
							"firstName" : "Nome",
							"lastName" : "Cognome",
							"description" : "Richiesta" };
	errore = "";
	for ( i in campiObbligatori ) {
		switch ( i ) {
			case "email":
				if ( ! validateEmail( form[i].value ) ) {
					errore += "Inserire un indirizzo e-mail valido\n"
				}
			break;
			default:
				if ( form[i].value == "" ) {
					errore += "Compilare correttamente il campo " + campiObbligatori[i] + "\n";
				}
			break;
		}
	}
	if ( errore ) {
		alert( errore );
		return false;
	}
}
function abilitaSubmit( abilita ) {
	form = document.getElementById( "info" );
	form.submitButton.disabled = ( abilita == 0 );
}
addEvent( window, 'load', externalLinks );
addEvent( window, 'load', startList );