// ################################################################
// ######   COOKIES ###############################################
// ################################################################

// SETTA la data di espirazione A 31 GIORNI

var exp = new Date();                      
exp.setTime(exp.getTime() + (1000 * 60 * 60 * 24 * 31));

var path = "/";

// SETTA IL COOKIE

function setCookieFont(value, expires, percorso) {
	document.cookie = "sFont=" + escape(value) + "; expires=" + expires.toGMTString() + "; path=" + escape(percorso);
}

function setCookieSize(value, expires, percorso) {
	document.cookie = "sSize=" + escape(value)+ "; expires=" + expires.toGMTString() + "; path=" + escape(percorso);
}

function setCookieAllinea(value, expires, percorso) {
	document.cookie = "sAllinea=" + escape(value)+ "; expires=" + expires.toGMTString() + "; path=" + escape(percorso);
}

//  SALVA I VALORI DEGLI ELEMENTI DEL FORM
function setValueFont(face) {
	document.getElementById('content').style.fontFamily = face;
	setCookieFont(face, exp, path);
}

function setValueSize(size) {
	document.getElementById('content').style.fontSize = size;
	setCookieSize(size, exp, path);
}

function setValueAllinea(allignement) {
	document.getElementById('content').style.textAlign = allignement;  
	setCookieAllinea(allignement, exp, path);
}


// #############################################################################################
// ########   FUNZIONI DI INIZIALIZZAZIONE #####################################################
// #############################################################################################


// inizializza le proprietà di style del content che consente a Mozilla di espandere bene il menù principale


function getCookie(NameOfCookie)
{

// Prima controlliamo se c'è un cookie memorizzato.
	if (document.cookie.length > 0){ 

// Poi controlliamo se il nome del cookie è conservato nell'oggetto
// "document.cookie"della pagina 
			begin = document.cookie.indexOf(NameOfCookie+"="); 
			
			if (begin != -1){ 
// Il nostro cookie era presente. 
// Il valore memorizzato nel cookie viene rimandato dalla funzione.
			begin += NameOfCookie.length+1; 
			end = document.cookie.indexOf(";", begin);
			if (end == -1) end = document.cookie.length;
			return unescape(document.cookie.substring(begin, end)); } 
			}
return null; 
// Il nostro cookie non era presente. 
// Il valore "null" viene rimandato dalla funzione.
}


function init(){
		
	/*if (getCookie('sAllinea') != '') {
		document.getElementById('content').style.textAlign = getCookie('sAllinea'); 
	}*/
	if (getCookie('sFont') != '') {
		document.getElementById('content').style.fontFamily = getCookie('sFont'); 
	}
	if (getCookie('sSize') != '') {
		document.getElementById('content').style.fontSize = getCookie('sSize'); 	
	}	
	
	if (document.all) initIE();

	//if (document.getElementById('content').offsetHeight < 450)
	//	document.getElementById('content').style.height =  document.getElementById('leftcol').offsetHeight + 'px';
	
	var lH = (document.getElementById('leftcol') ? document.getElementById('leftcol').offsetHeight : 0);
	var rH = (document.getElementById('rightcol') ? document.getElementById('rightcol').offsetHeight : 0);
	var cH = (lH > rH ? lH : rH);
	if (document.getElementById('content').offsetHeight < cH)
		document.getElementById('content').style.height =  cH + 'px';
}
	
function initIE(){
	var aMenuLi = document.getElementById('menu').getElementsByTagName('li')
	var aSelect = document.getElementById('content').getElementsByTagName('select')
	
	for (var i = 0; i<aMenuLi.length; i++){
		if (aMenuLi[i].className == 'menuitem'){
			aMenuLi[i].onmouseover = function(){
				menuIEOff();
				this.firstChild.nextSibling.nextSibling.style.display='block';
				for (var i = 0; i<aSelect.length; i++){
					if (this.firstChild.nextSibling.nextSibling.offsetHeight - 100 > aSelect[i].offsetTop) aSelect[i].style.visibility='hidden';
				}
				this.firstChild.nextSibling.nextSibling.onmouseleave = function(){ 
					this.style.display='none'; 
					for (var i = 0; i<aSelect.length; i++){
						aSelect[i].style.visibility='visible';
					}
				}

			};
		}
	}

}

function menuIEOff(){
	var aMenuLi = document.getElementById('menu').getElementsByTagName('li')
	for (var i = 0; i<aMenuLi.length; i++){
		if (aMenuLi[i].className == 'menuitem'){
			aMenuLi[i].firstChild.nextSibling.nextSibling.style.display='none';
		};
	}
}

onload = init;



	


	
	 
// #############################################################################################
// ########   FORMS #####################################################################
// #############################################################################################
	
function Form1_Validator(theForm){
var dominio;
var aEmail;
  if (theForm.mittente.value == "") {
    alert("Inserisci il tuo indirizzo e-mail");     
	theForm.mittente.focus();
	}else{		
  	   if (theForm.mittente.value.indexOf("@") == -1) {
		alert("Inserire un indirizzo e-mail valido");
    	theForm.mittente.focus();
		}else{
		aEmail = theForm.mittente.value.split("@");
		dominio = aEmail[1];
		if (dominio.indexOf(".") == -1){
			alert("Inserire un indirizzo e-mail valido");
	    	theForm.mittente.focus();
			}else{		
		 if (theForm.oggetto.value == ""){
		   alert("Inserisci l'oggetto del messaggio");
		   theForm.oggetto.focus();
		 }else{		 
			 if (theForm.messaggio.value == ""){
			   alert("Inserisci il corpo del messaggio");
			   theForm.messaggio.focus();
			 }else{
			 	theForm.submit();
			 }
		}
	}
}
}
}
   
 


/* inizializza i form */

function initForms(){
		aInput = document.getElementsByTagName("input")
		aTextAreas = document.getElementsByTagName("textarea")
		aButtons = [];
		aText = [];
		
		for (var i=0; i<aInput.length; i++){
			switch(aInput[i].type){
				case 'text':
					aText[aText.length] = aInput[i];
					break;
				case 'button':
					aButtons[aButtons.length] = aInput[i];
					break;
			}
		}
		
		for (var i=0; i<aButtons.length; i++){
			aButtons[i].onmouseover = function(){
				this.className = 'buttonhover'
			}
			aButtons[i].onmouseout = function(){
				this.className = 'button'
			}
		}
		
		for (var i=0; i<aText.length; i++){
			aText[i].onfocus = function(){
				this.className = 'textfocus'
			}
			aText[i].onblur = function(){
				this.className = 'text'
			}
		}
	
		for (var i=0; i<aTextAreas.length; i++){
			aTextAreas[i].onfocus = function(){
				this.className = 'textfocus'
			}
			aTextAreas[i].onblur = function(){
				this.className = 'text'
			}
		}		
	}
	
	
function reginitForms(){
		aInput = document.getElementsByTagName("input")
		aTextAreas = document.getElementsByTagName("textarea")
		aButtons = [];
		aText = [];
		
		for (var i=0; i<aInput.length; i++){
			switch(aInput[i].type){
				case 'text':
					aText[aText.length] = aInput[i];
					break;
				case 'button':
					aButtons[aButtons.length] = aInput[i];
					break;
			}
		}
		
		for (var i=0; i<aButtons.length; i++){
			aButtons[i].onmouseover = function(){
				this.className = 'buttonhover'
			}
			aButtons[i].onmouseout = function(){
				this.className = 'button'
			}
		}
		
		for (var i=0; i<aText.length; i++){
			aText[i].onfocus = function(){
				this.className = 'regfocus'
			}
			aText[i].onblur = function(){
				this.className = 'regtxt'
			}
		}
	
		for (var i=0; i<aTextAreas.length; i++){
			aTextAreas[i].onfocus = function(){
				this.className = 'regfocus'
			}
			aTextAreas[i].onblur = function(){
				this.className = 'regtxt'
			}
		}		
	}
	
function verifyForm(formname){
	aInput = document.getElementsByTagName("input")
	aTextAreas = document.getElementsByTagName("textarea")
	aLabels = document.getElementsByTagName("label")
	aSelect = document.getElementsByTagName("select")
	// loop through all the elements on the form
	for (var i=0; i<aInput.length; i++){
		if ((aInput[i].getAttribute('required')=='yes' && aInput[i].value=='') || (aInput[i].getAttribute('textmail')=='yes' && (aInput[i].value.indexOf('@') < 0 || aInput[i].value.indexOf('.') < 0))){
			if (aInput[i].getAttribute('textmail')=='yes' && aInput[i].value.length > 1 && (aInput[i].value.indexOf('@') < 0 || aInput[i].value.indexOf('.') < 0)){
				alert('Inserire correttamente il proprio indirizzo di posta elettronica');	
			}else{	
				alert('Il campo' + ' "' + document.getElementById(aInput[i].id + 'label').innerHTML.replace("*", "") + '" ' + 'è obbligatorio');	
			}
			aInput[i].focus();
			return false;
		}
	}
	
	for (var i=0; i<aTextAreas.length; i++){
		if (aTextAreas[i].getAttribute('required')=='yes' && aTextAreas[i].value==''){
			alert('Il campo' + ' "' + document.getElementById(aTextAreas[i].id + 'label').innerHTML + '" ' + 'è obbligatorio');	
			aTextAreas[i].focus();
			return false;
		}
	}
	
	for (var i=0; i<aSelect.length; i++){
		if (aSelect[i].getAttribute('required')=='yes' && aSelect[i].value==''){
			alert('Il campo' + ' "' + document.getElementById(aSelect[i].id + 'label').innerHTML + '" ' + 'è obbligatorio');	
			aSelect[i].focus();
			return false;
		}
	} 
	
	/*
	if (document.forms[formname].pw1.value != document.forms[formname].pw2.value) {
				alert("La password immessa non corrisponde")
				myForm.pw1.focus()
				myForm.pw1.select()
				return false
			}
	*/
	document.forms[formname].submit();
}

function fullScreen(theURL) {

window.open(theURL, '', 'fullscreen=yes, scrollbars=yes, width=' + screen.width + ', height='+ screen.height);
}


/* FIX PER L'IE6 SELECTION BUG */
if (document.all && window.createPopup && document.compatMode && document.compatMode=="CSS1Compat")
{
  document.onreadystatechange = onresize = function fixIE6AbsPos()
  {
    if (!document.body) return;
    if (document.body.style.margin != "0px") document.body.style.margin = 0;
    onresize = null;
    document.body.style.height = 0;
    setTimeout(function(){ document.body.style.height = document.documentElement.scrollHeight+'px'; }, 1);
    /*setTimeout(function(){ onresize = fixIE6AbsPos; }, 100);*/
  }
}

/******** FUNZIONI PER "SEGNALA IL SITO AD UN AMICO" **********/
function checkEmailAddress(field) {
	var goodEmail = field.value.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,2}))$)\b/gi);
	if (goodEmail)
		return true;
	else {
		alert('Inserire un indirizzo e-mail valido.')
		field.focus();
		field.select();
		return false;
	}
}
function mailThisUrl(){
	u = "http://www.gesuiti.it";
	m = "Ti segnalo questo sito: www.gesuiti.it - sito dei gesuiti in italia ";
	if (checkEmailAddress(document.eMailer.address))
		document.location.href = "mailto:"+document.eMailer.address.value+"?subject="+m+"&body="+document.title+" "+u;
}
