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";
		}
	} 
}
addEvent( window, "load", externalLinks );
aVantaggi = new Array(	"diritti",
						"informazione",
						"consulenza",
						"assistenza" );
function scegliVantaggio( vantaggio ) {
	for ( i = 0; i < aVantaggi.length; i++ ) {
		testo = document.getElementById( aVantaggi[i] );
		pul = document.getElementById( aVantaggi[i] + "Pul" );
		if ( aVantaggi[i] == vantaggio ) {
			testo.style.display = "block";
			pul.className = "evidenziato";
			
		} else {
			testo.style.display = 'none';
			pul.className = "normale";
		}
		selectedVantaggio = vantaggio;
	}
	pul = document.getElementById( vantaggio + "Pul" );
	pul.className = "evidenziato";
}
function changeClass( status, pulsID ) {
	if ( status == "over"  || selectedVantaggio == pulsID ) {
		return "evidenziato";
	} else {
		return "normale";
	}
}
function changeAllClasses( status ) {
	for ( i = 0; i < aVantaggi.length; i++ ) {
		pul = document.getElementById( aVantaggi[i] + "Pul" );
		if ( status == "over" ) {
			pul.className = "evidenziato";
		} else {
			pul.className = "normale";
		}
	}
}
function resetClass( pulsID ) {
	pul = document.getElementById( pulsID + "Pul" );
	if ( pulsID != selectedVantaggio ) {
		pul.className = "normale";
		pul = document.getElementById( selectedVantaggio + "Pul" );
	}
	pul.className = "evidenziato";
}
function vantaggi( printableVersion ) {
	if ( arguments.length == 1 && printableVersion ) {
		window.open('vantaggi_print.html','vantaggi','width=500,height=380,top=150,left=250,scrollbars=1');
	} else {
		window.open('vantaggi.html','vantaggiPrint','width=500,height=380,top=150,left=250');
	}
}
function prevFoto( img ) {
	document.viewDetail.selectedImage.value = img;
	foto = new Image();
	foto.src = img;
	controlla( img );
}
function controlla( img ){
	if ( ( foto.width != 0 ) && ( foto.height != 0 ) ){
		viewFoto();
	} else{
		intervallo = setTimeout( "controlla('" + img + "')", 100 );
	}
}
function viewFoto() {
	largh = foto.width + 20;
	altez = foto.height + 35;
	stringa = "width=" + largh + ", height=" + altez;
	finestra = window.open( "gallery_det.html", "", stringa );
}
function checkSearchContact() {
	if (	document.searchContact.service.value == ''
			&& document.searchContact.mandamento.value == ''
			&& document.searchContact.referer.value == '' ) {
		alert( "Compilare almento uno dei campi di ricerca" );
		return false;
	} else {
		if ( 	document.searchContact.service.value != ''
				&& document.searchContact.mandamento.value != ''
				&& ! document.searchContact.service.disabled
				&& ! document.searchContact.mandamento.disabled ) {
			alert( "Compilare solo uno fra i primi due campi" );
			return false;
		} else {
			return true;
		}
	}
}
function disableField( fieldName, disableFieldName ) {
	if ( document.searchContact[fieldName].value == '' ) {
		document.searchContact[disableFieldName].disabled = false;
	} else {
		document.searchContact[disableFieldName].disabled = true;
	}
}
function checkField() {
	if ( document.searchContact.service.value != '' ) {
		document.searchContact.mandamento.disabled = true;
	} else if ( document.searchContact.mandamento.value != '' ) {
		document.searchContact.service.disabled = true;
	}
}
function presentazione() {
	window.open( "http://www.omniacom.it/presentazione.html", "presentazione", "width=760,height=540" );
}
function goDetail( selectedEntity, pageName ) {
	if ( pageName != '' ) {
		document.goDetail.selectedEntity.value = selectedEntity;
		document.goDetail.action = pageName + ".html";
		document.goDetail.submit();
	}
}
function checkRegistrati() {
	if ( document.registrati.referer.value == "" || document.registrati.email.value == "" ) {
		alert( "Compilare correttamente i campi obbligatori" );
		return false;
	}
	extra3 = "";
	document.registrati.extra3[0].value = 1;
	document.registrati.extra3[1].value = 0;
	for ( i = 0; i < document.registrati.extra3.length; i++ ) {
		if ( document.registrati.extra3[i].checked ) {
			extra3 = document.registrati.extra3[i].value;
			break;
		}
	}
	if (	document.registrati.extra2.checked == true
			&& extra3 != 1 ) {
		alert( "Per ricevere materiale informativo via e-mail è necessario \n acconsentire al trattamento dei dati personali" );
		return false;
	} else {
		for ( i = 0; i < document.registrati.extra3.length; i++ ) {
			if ( ! document.registrati.extra3[i].checked ) {
				document.registrati.extra3[i].disabled = false;
			}
		}
	}
}
function rotateBox( boxName, boxNumber, boxVisible ) {
	exit = false;
	i = 1;
	while ( exit == false ) {
		if ( box = document.getElementById( boxName + i ) ) {
			box.style.display = "none";
			i++;
		} else {
			exit = true;
		}
	}
	if ( boxNumber + boxVisible > i ) {
		boxNumber = 1;
	}
	for ( i = boxNumber; i < boxNumber + boxVisible; i++ ) {
		if ( box = document.getElementById( boxName + i ) ) {
			box.style.display = "block";
		}
	}
	setTimeout( "rotateBox('" + boxName + "'," + ( boxNumber + 1 ) + "," + boxVisible + ")", 10000 );
}
function setOpacity( boxName, opacity ) {
	if ( box = document.getElementById( boxName ) ) {
		box.style.opacity = opacity;
	}
}
function fadeOut( boxName, opacity ) {
	setOpacity( boxName, opacity / 100 );
	setTimeout( "fadeOut( '" + boxName + "', " + ( opacity - 1 ) + " )", 10 );
}
function fadeIn( boxName, opacity ) {
	setOpacity( boxName, opacity / 100 );
	setTimeout( "fadeIn( '" + boxName + "', " + ( opacity - 1 ) + " )", 10 );
}
function oggiAscomPopUp() {
	window.open( "oggi_ascom.html", "oggiAscom", "width=250,height=280" );
}
function cambiaStatoCartella( cartellaID ) {
	a = document.getElementById( cartellaID + "A" );
	ul = document.getElementById( cartellaID + "Ul" );
	if ( a.className == 'cartellaClosed' ) {
		a.className = 'cartellaOpen';
		ul.style.display = 'block';
	} else {
		a.className = 'cartellaClosed';
		ul.style.display = '';
	}
}
function mostraAg() {
	document.getElementById( "agenda" ).style.display = "block";
	document.getElementById( "opinione" ).style.display = "none";
}
function mostraOp() {
	document.getElementById( "opinione" ).style.display = "block"; 
	document.getElementById( "agenda" ).style.display = "none"; 
}
function agendaDetail( selectedEntity ) {
	win = window.open( "agenda.html?selectedEntity=" + selectedEntity, "agenda", "width=400,height=400,resizabled=1,scrollbars=1" );
	win.focus();
	return false;
}
function convalidaIscrizione( formObj ) {
	campiObbligatori = {	"trainingID" : "Corso prescelto",
							"firstName" : "Nome",
							"lastName" : "Cognome",
							"taxCode" : "Codice fiscale",
							"address" : "Indirizzo",
							"zipCode" : "Cap",
							"municipality" : "Comune",
							"phone" : "Telefono/Cellulare",
							"partner" : "Socio Ascom",
							"rgs" : "Ragione soc. della ditta a cui inviare fattura",
							"invoicePhone" : "Dati fatturazione: telefono",
							"email" : "Dati fatturazione: email",
							"consenso1" : "Consenso al trattamento dei dati personali",
							"consenso2" : "Consenso al trattamento dei dati personali",
							"consenso3" : "Consenso al trattamento dei dati personali" };
	error = "";
	for ( field in campiObbligatori  ) {
		switch ( field ) {
			case "email":
				if ( ! validateEmail( formObj[field].value ) ) {
					error += "Inserire un indirizzo e-mail valido\n";
				}
			break;
			case "phone":
				if ( formObj.phone.value == "" && formObj.mobile.value == "" ) {
					error += "Inserire un recapito telefonico (fisso o mobile) valido\n";
				}
			break;
			case "consenso1":
			case "consenso2":
			case "consenso3":
				if ( ! formObj[field].checked ) {
					error += "Il consenso al trattamento dei dati personali è obbligatorio\n";
				}
			break;
			default:
				if ( formObj[field].value == "" ) {
					error += "Compilare correttamente il campo " + campiObbligatori[i] + "\n";
				}
			break;
		}
	}
	if ( error != "" ) {
		alert( error );
		return false;
	}
}
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 selezionaCorso( identificatore ) {
	if ( identificatore != undefined ) {
		ajaxConnect( "/js/seleziona-corso.php?identificatore=" + identificatore );
	}
}
function spedisci( email ) {
	location = "mailto:" + email.replace( "[presso]", "@" );
}
function mostraRicerca() {
	obj = document.getElementById( "corpoRicerca" );
	if ( obj.style.display == "none" ) {
		obj.style.display = "block";
	} else {
		obj.style.display = "none";
	}
}
function validateEmail( email ) {
	var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
	return emailPattern.test( email ); 
}
function chiudiPopup( popupID ) {
	if ( document.getElementById( popupID ) ) {
		document.getElementById( popupID ).style.display = "none";
		document.getElementById( popupID ).innerHTML = "";
	}
}
function testoCompleto( id ) {
	var voci = new Array( "informazioni", "vantaggi", "richiedila", "filmato" );
	for ( var i = 0; i < voci.length; i++ ) {
		if ( voci[i] != id && document.getElementById( voci[i] ) ) {
			var el = document.getElementById( voci[i] );
			if ( el.style.display == "block" ) {
				comprimi( voci[i], el.offsetHeight );
			}
		}
	}
	espandiComprimi( id );
}
function espandiComprimi( id ) {
	if ( document.getElementById( id ) ) {
		var el = document.getElementById( id );
		if ( el.style.display == "block" ) {
			comprimi( id, parseInt( el.style.height.replace( "px", "" ) ) );
		} else {
			el.style.display = "block";
			if ( ! el.height ) {
				el.height = el.offsetHeight;
			}
			espandi( id, 0 );
		}
	}
}
function espandi( id, i ) {
	var el = document.getElementById( id );
	i = ( i + 10 < el.height ) ? i + 10 : el.height;
	el.style.height = i + "px";
	if ( i < el.height ) {
		setTimeout( "espandi( '" + id + "', " + i + ")", 1 );
	}
}
function comprimi( id, i ) {
	var el = document.getElementById( id );
	i = ( i - 10 > 0 ) ? i - 10 : 0;
	el.style.height = i + "px";
	if ( i > 0 ) {
		setTimeout( "comprimi( '" + id + "', " + i + " )", 1 );
	} else {
		el.style.display = "none";
		el.style.height = el.height + "px";
	}
}
function newsTicker() {
	$(function() {
		var ticker = function() {
			setTimeout(function() {
				$('#news li:first-child').animate( {marginTop: '-110px'}, 800, function() {
				$(this).detach().appendTo('ul#news').removeAttr('style');
					});
					ticker();
				}, 5000);
		};
		ticker();
	});
}
function eventiTicker() {
	$(function() {
		var ticker = function() {
			setTimeout(function() {
				$('#eventi li:first-child').animate( {marginTop: '-100px'}, 800, function() {
				$(this).detach().appendTo('ul#eventi').removeAttr('style');
					});
					ticker();
				}, 9000);
		};
		ticker();
	});
}
function agendaTicker() {
	$(function() {
		var ticker = function() {
			setTimeout(function() {
				$('#agenda li:first-child').animate( {marginTop: '-100px'}, 800, function() {
				$(this).detach().appendTo('ul#agenda').removeAttr('style');
					});
					ticker();
				}, 6500);
		};
		ticker();
	});
}
