/* ******************************************************** */
/* ************* Inizializzazione funzioni **************** */
/* ******************************************************** */
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') { window.onload = func; } 
	else {
    window.onload = function() {
      if (oldonload) { oldonload(); }
      func();
    }
  }
}

function RicavaBrowser(QualeBrowser) {
    if (navigator.userAgent.indexOf("MSIE") != (-1)) {
        var Classe = "Msxml2.XMLHTTP";
        if (navigator.appVersion.indexOf("MSIE 5.5") != (-1)); {
            Classe = "Microsoft.XMLHTTP";
        } try {
            OggettoXMLHTTP = new ActiveXObject(Classe);
            OggettoXMLHTTP.onreadystatechange = QualeBrowser;
            return OggettoXMLHTTP;
        } catch(e) { alert("Errore: l'ActiveX non verrà eseguito!"); }
    } else if (navigator.userAgent.indexOf("Mozilla") != (-1)) {
        OggettoXMLHTTP = new XMLHttpRequest();
        OggettoXMLHTTP.onload = QualeBrowser;
        OggettoXMLHTTP.onerror = QualeBrowser;
        return OggettoXMLHTTP;
    } else { alert("L'esempio non funziona con altri browser!"); }
}

/* **************************************************** */
/* FUNZIONE per aprire una finestra di PopUp centrata   */
/* **************************************************** */
function openPopUp(url,w,h,scrl) {
  l = 50;
  t = 50;
  newWin = window.open(url,"nuovafinestra", "width="+w+", height="+h+", left="+l+", top="+t+", scrollbars="+scrl+", resizable=no") 
}

/* **************************************************** */
/* FUNZIONE per aprire una finestra di PopUp            */
/* **************************************************** */
function openPopUpAutoRedim(url,l,t) {
  newWin = window.open(url,"nuovafinestra", "left="+l+", top="+t+", scrollbars=no, resizable=yes") 
}


/* ************************************************* */
/* ****** Elemento a comparsa con un click ********* */
/* ************************************************* */
function scambio(id) {
  if (document.getElementById) {
	if(document.getElementById(id).style.display == 'none') document.getElementById(id).style.display = 'block';
	else document.getElementById(id).style.display = 'none';
  }
}


/* *************************************************** */
/* **** STAMPA E/O NASCONDE UNA PORZIONE DI TESTO **** */
/* **** UTILIZZATO SOPRATTUTTO NELLE FAQ ************* */
/* *************************************************** */
function DivStatus(nom, numero) {
	var divID = nom + numero;
	if ( document.getElementById && document.getElementById(divID)) {
		Pdiv = document.getElementById( divID );
		PcH = true;
	} else if (document.all && document.all[ divID ]) {
		Pdiv = document.all[ divID ];
		PcH = true;
	} else if (document.layers && document.layers[ divID ])	{
		Pdiv = document.layers[ divID ];
		PcH = true;
	} else { PcH = false; }
	if ( PcH ) { Pdiv.className = ( Pdiv.className == 'cachediv' ) ? '' : 'cachediv'; }
}

/*
* Cache tous les divs ayant le même préfixe
*/
function CacheTout( nom ) {	
	var NumDiv = 1;
	if ( document.getElementById ) {
		while ( document.getElementById( nom + NumDiv) ) {
			SetDiv = document.getElementById( nom + NumDiv );
			if ( SetDiv && SetDiv.className != 'cachediv' )	{ DivStatus( nom, NumDiv );	}
			NumDiv++;
		}
	} else if ( document.all ) {
		while ( document.all[ nom + NumDiv ] ) {
			SetDiv = document.all[ nom + NumDiv ];
			if ( SetDiv && SetDiv.className != 'cachediv' )	{ DivStatus( nom, NumDiv );	}
			NumDiv++;
		}
	} else if ( document.layers ) {
		while ( document.layers[ nom + NumDiv ] ) {
			SetDiv = document.layers[ nom + NumDiv ];
			if ( SetDiv && SetDiv.className != 'cachediv' )	{ DivStatus( nom, NumDiv );	}
			NumDiv++;
		}
	}
}

/*
* Montre tous les divs ayant le même préfixe
*/
function MontreTout(nom) {	
	var NumDiv = 1;
	if (document.getElementById) {
		while (document.getElementById(nom + NumDiv)) {
			SetDiv = document.getElementById(nom + NumDiv);
			if (SetDiv && SetDiv.className != '') { DivStatus(nom, NumDiv);	}
			NumDiv++;
		}
	} else if (document.all) {
		while (document.all[nom + NumDiv]) {
			SetDiv = document.all[ nom + NumDiv ];
			if (SetDiv && SetDiv.className != '') {DivStatus(nom, NumDiv); }
			NumDiv++;
		}
	} else if (document.layers) {
		while (document.layers[nom + NumDiv] ) {
			SetDiv = document.layers[nom + NumDiv];
			if (SetDiv && SetDiv.className != '') { DivStatus(nom, NumDiv);	}
			NumDiv++;
		}
	}
}

/*
* Inverse les divs: Cache les divs visible et montre le divs cachés :)
*/
function InverseTout(nom)	{	
	var NumDiv = 1;
	if (document.getElementById) {
		while (document.getElementById(nom + NumDiv) ) {
			SetDiv = document.getElementById(nom + NumDiv);
			DivStatus(nom, NumDiv);
			NumDiv++;
		}
	} else if (document.all) {
		while (document.all[nom + NumDiv]) {
			SetDiv = document.all[nom + NumDiv];
			DivStatus(nom, NumDiv);
			NumDiv++;
		}
	} else if (document.layers) {
		while (document.layers[nom + NumDiv]) {
			SetDiv = document.layers[nom + NumDiv];
			DivStatus(nom, NumDiv);
			NumDiv++;
		}
	}
}
/* **** FINE ***************************************** */
/* **** STAMPA E/O NASCONDE UNA PORZIONE DI TESTO **** */
/* **** UTILIZZATO SOPRATTUTTO NELLE FAQ ************* */
/* *************************************************** */


/* ********************************************* */
/* FUNZIONE per controllare il MOTORE DI RICERCA */
/* ********************************************* */
function VerificaQuery(form) {
	if (form.q.value == '') {
    	alert("Inserisci la chiave di ricerca per continuare");
        return false;
	} else if (form.q.value.length <= 3) {
    	alert("La chiave di ricerca deve essere superiore alle tre lettere!");
        return false;
	} else return true; 
}
function GoToSearch(form) {
	// Interpreto le sequenze escape
   	var query = escape(form.q.value);
    if (!VerificaQuery(form)) return false;
    else form.submit();
}

/* ********************************************* */
/* FUNZIONE controllo form login TOOL OWNER      */
/* ********************************************* */
function frmToolOwnerValidate(form) {
    var email 		= form.frmOwn_email.value;
    var pwd 		= form.frmOwn_pwd.value;
	
	if (email == "") {
        alert("ATTENZIONE!!!\n\nIl campo INDIRIZZO E-MAIL è vuoto");
		form.frmOwn_email.focus();
        return false;
    } 
	if (pwd == "") {
        alert("ATTENZIONE!!!\n\nIl campo PASSWORD è vuoto");
		form.frmOwn_pwd.focus();
        return false;
    } 
    return true;
}

/* ********************************************* */
/* FUNZIONE controllo form richiesta password    */
/* ********************************************* */
function frmLoginAskValidate(form) {
    var email 		= form.frmLoginAsk_email.value;
    var key 		= form.key.value;
	
	if (email == "") {
        alert("ATTENZIONE!!!\n\nIl campo INDIRIZZO E-MAIL è vuoto");
		form.frmLoginAsk_email.focus();
        return false;
    } 
	if (key == "") {
        alert("ATTENZIONE!!!\n\nIl campo TRASCRIVI IL CODICE è vuoto");
		form.key.focus();
        return false;
    } 
	if (email != "")
      if (!ControllaMail(email)) {
        alert("ATTENZIONE!!!\n\nL'indirizzo EMAIL non è valido");
		form.frmLoginAsk_email.focus();
        return false;
	} 
    return true;
}

/* **************************************************** */
/* ****** FUNZIONE USERS VALIDATE ********************* */
/* **************************************************** */
function usersValidate(form) {
    var gruppo 		= form.gruppo.value;
    var login 		= form.login.value;
    var password 	= form.password.value;
    var nome 		= form.nome.value;
    var cognome 	= form.cognome.value;
    var email 		= form.email.value;

    if (gruppo == "0") {
        alert("ATTENZIONE!!!\n\nSelezionare un GRUPPO");
		form.gruppo.focus();
        return false;
    } 
	if (login == "") {
        alert("ATTENZIONE!!!\n\nIl campo LOGIN è obbligatorio");
		form.login.focus();
        return false;
    } 
	if (login.length < 4) {
        alert("ATTENZIONE!!!\n\nIl campo LOGIN deve contenere almeno 4 caratteri");
		form.login.focus();
        return false;
    } 
	if (password == "") {
        alert("ATTENZIONE!!!\n\nIl campo PASSWORD è obbligatorio");
		form.password.focus();
        return false;
    } 
	if (password.length < 4) {
        alert("ATTENZIONE!!!\n\nIl campo PASSWORD deve contenere almeno 4 caratteri");
		form.password.focus();
        return false;
    } 
	if (nome == "") {
        alert("ATTENZIONE!!!\n\nIl campo NOME è obbligatorio");
		form.nome.focus();
        return false;
    } 
	if (cognome == "") {
        alert("ATTENZIONE!!!\n\nIl campo COGNOME è obbligatorio");
		form.cognome.focus();
        return false;
    } 
	if (email != "")
      if (!ControllaMail(email)) {
        alert("ATTENZIONE!!!\n\nL'indirizzo EMAIL non è valido");
		form.email.focus();
        return false;
	} 
	if (!form.attivo.checked) {
		var agree = confirm("ATTENZIONE!!!\n\nIl campo \'\'ATTIVO\'\' non è stato flaggato\nQuesto record non verrà visualizzato on-line\n\nVuoi continuare?? (OK: si -- Annulla: no)");
		if (agree) {
			form.attivo.checked = false;
			return true;
		}else{
			form.attivo.checked = false;
			form.attivo.focus();
		    return false;
		}
    }
    return true;
}

/* **************************************************** */
/* ****** FUNZIONE FILES VALIDATE ********************* */
/* **************************************************** */
function filesValidate(form) {
	var tipo   = form.tipo.options[form.tipo.selectedIndex].value;
	var nome   = form.nome.options[form.nome.selectedIndex].value;
    var titolo = form.titolo.value;
    
    if (tipo == "") {
        alert("ATTENZIONE!!!\n\nScegliere un TIPO DI FILE dal I° menù a tendina");
		form.tipo.focus();
        return false;
    }
   /* if (tipo != "" && nome == "") {
        alert("ATTENZIONE!!!\n\nSelezionare un FILE dal II° menù a tendina");
		form.nome.focus();
        return false;
    }*/
    if (titolo == "") {
        alert("ATTENZIONE!!!\n\nIl campo TITOLO è obbligatorio");
		form.titolo.focus();
        return false;
    }
	if (!form.attivo.checked) {
		var agree = confirm("ATTENZIONE!!!\n\nIl campo \'\'ATTIVO\'\' non è stato flaggato\nQuesto record non verrà visualizzato on-line\n\nVuoi continuare?? (OK: si -- Annulla: no)");
		if (agree) {
			form.attivo.checked = false;
			return true;
		}else{
			form.attivo.checked = false;
			form.attivo.focus();
		    return false;
		}
    }
    return true;
}


/* **************************************************** */
/* ****** FUNZIONE ANAGRUPPI VALIDATE ***************** */
/* **************************************************** */
function menuValidate(form) {
    var nome = form.nome.value;

    if (nome == "") {
        alert("ATTENZIONE!!!\n\nIl campo NOME MENU è obbligatorio");
		form.nome.focus();
        return false;
    }
	if (!form.attivo.checked) {
		var agree = confirm("ATTENZIONE!!!\n\nIl campo \'\'ATTIVO\'\' non è stato flaggato\nQuesto record non verrà visualizzato on-line\n\nVuoi continuare?? (OK: si -- Annulla: no)");
		if (agree) {
			form.attivo.checked = false;
			return true;
		}else{
			form.attivo.checked = false;
			form.attivo.focus();
		    return false;
		}
    }
    return true;
}

/* ********************************************************* */
/* ****** FUNZIONE ANAGRAFICA GRUPPI LINKS VALIDATE ******** */
/* ********************************************************* */
function anagraficaGruppiLinksValidate(form) {
    var nome = form.nome.value;
    if (nome == "") {
        alert("ATTENZIONE!!!\n\nIl campo NOME GRUPPO è obbligatorio");
		form.nome.focus();
        return false;
    }
	if (!form.attivo.checked) {
		var agree = confirm("ATTENZIONE!!!\n\nIl campo \'\'ATTIVO\'\' non è stato flaggato\nQuesto record non verrà visualizzato on-line\n\nVuoi continuare?? (OK: si -- Annulla: no)");
		if (agree) {
			form.attivo.checked = false;
			return true;
		}else{
			form.attivo.checked = false;
			form.attivo.focus();
		    return false;
		}
	}
    return true;
}

//* **************************************************** */
/* ****** GESTIONE ALBUMS FOTOGRAFICI ****************** */
/* **************************************************** */
function galleryAlbumsValidate(formCMS_edit) {
  if (formCMS_edit.nome.value.length == 0 || formCMS_edit.nome.value.length > 255) {
    alert("ATTENZIONE!!!\n\nControllare il campo \"Nome album\"\n(lunghezza 0 oppure lunghezza max (255) superata)");
    formCMS_edit.nome.focus();
    return false;
  }
  if (!formCMS_edit.attivo.checked) {
	var agree = confirm("ATTTENZIONE!!!\n\nIl campo \'\'Visualizza\'\' non è stato flaggato\nQuesto record non verrà visualizzato on-line\n\nVuoi continuare?? (OK: si -- Annulla: no)");
	if (agree) {
	  formCMS_edit.attivo.checked = false;
	  return true;
	}else{
	  formCMS_edit.attivo.checked = false;
      formCMS_edit.attivo.focus();
	  return false;
	}
  }
  return true;
}

//* **************************************************** */
/* ****** GESTIONE GALLERIE DI IMMAGINI **************** */
/* **************************************************** */
function galleryImgsValidate(formCMS_edit) {
  if (formCMS_edit.imgid.value == "0") {
    alert("ATTENZIONE!!!\n\nScegliere almeno un\'immagine dall\'elenco");
    formCMS_edit.imgid.focus();
    return false;
  }
  if (formCMS_edit.didascaliaIt.value.length > 255) {
    alert("ATTENZIONE!!!\n\nControllare il campo \"Didascalia\" in Italiano \n(lunghezza max (90) superata)");
    formCMS_edit.didascaliaIt.focus();
    return false;
  }
  if (formCMS_edit.didascaliaEn.value.length > 255) {
    alert("ATTENZIONE!!!\n\nControllare il campo \"Didascalia\" in Inglese \n(lunghezza max (90) superata)");
    formCMS_edit.didascaliaEn.focus();
    return false;
  }
  if (formCMS_edit.didascaliaFr.value.length > 255) {
    alert("ATTENZIONE!!!\n\nControllare il campo \"Didascalia\" in Francese \n(lunghezza max (90) superata)");
    formCMS_edit.didascaliaFr.focus();
    return false;
  }
  if (formCMS_edit.didascaliaDe.value.length > 255) {
    alert("ATTENZIONE!!!\n\nControllare il campo \"Didascalia\" in Tedesco \n(lunghezza max (90) superata)");
    formCMS_edit.didascaliaDe.focus();
    return false;
  }
  if (formCMS_edit.didascaliaEs.value.length > 255) {
    alert("ATTENZIONE!!!\n\nControllare il campo \"Didascalia\" in Spagnolo \n(lunghezza max (90) superata)");
    formCMS_edit.didascaliaEs.focus();
    return false;
  }
  return true;
}

/* **************************************************** */
/* ****** FUNZIONE CONTACTS VALIDATE ****************** */
/* **************************************************** */
function contactsValidate(form) {
    var nome = form.nome.value;
    var mailto = form.mailto.value;

    if (nome == "") {
        alert("ATTENZIONE!!!\n\nIl campo NOME CONTATTO è obbligatorio");
		form.nome.focus();
        return false;
    }
    if (mailto != "") {
      if (!ControllaMail(mailto)) {
        alert("ATTENZIONE!!!\n\nL'indirizzo EMAIL non è valido");
		form.mailto.focus();
        return false;
      }
	}
	if (!form.attivo.checked) {
		var agree = confirm("ATTENZIONE!!!\n\nIl campo \'\'ATTIVO\'\' non è stato flaggato\nQuesto record non verrà visualizzato on-line\n\nVuoi continuare?? (OK: si -- Annulla: no)");
		if (agree) {
			form.attivo.checked = false;
			return true;
		}else{
			form.attivo.checked = false;
			form.attivo.focus();
		    return false;
		}
	}
    return true;
}

// da testare ma dovrebbe essere ok - 18 marzo 2008
/* **************************************************** */
/* ****** FUNZIONE che Verifica la validità della mail  */
/* **************************************************** */
function ControllaMail(mail){	
	var espressione = /^[_a-z0-9+-]+(\.[_a-z0-9+-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)+$/;	
	if (espressione.test(mail)) return true;
	else return false;
}

/* **************************************************** */
/* ****** FUNZIONE che Verifica la validità dell'URL ** */
/* **************************************************** */
function ControllaURL(dominio){	
	var RE = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
	//var RE = /^http:\/\/(www\.)?[a-zA-Z0-9-]{3,}\.([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\.[a-zA-Z]{2,}(\/)?$/;
	//var RE = /^http:\/\/(www\.)?[a-zA-Z0-9-]{3,}\.[a-zA-Z]{2,}(\/)?$/;
	if (RE.test(dominio)) return true;
	else return false;
}

/* **************************************************** */
/* ****** FUNZIONE CORREGGI DATA ********************** */
/* **************************************************** */
function nd() {
  var d = document.getElementById('day'  ).value;
  var m = document.getElementById('month').value - 1;
  var y = document.getElementById('year' ).value;
  var dt = new Date(y, m, d);
  // this two fix wrong dates such as 30-feb
  document.getElementById('day').selectedIndex = dt.getDate() - 1;
  document.getElementById('month').selectedIndex = dt.getMonth();
}

/* ****************************************************** */
/* FUNZIONE per togliere cornice ai filmati flash in IE   */
/* ****************************************************** */
function loadswf(swf,w,h){
	document.write("<object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\"");
	document.write("codebase=\"http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0\"");
	document.write("width=\""+w+"\" height=\""+h+"\">");
	document.write("<param name='movie' value='"+swf+"' />\n");
	document.write("<param name='quality' value='high' /><param name='wmode' value='transparent' />\n");
	document.write("<embed src=\""+swf+"\" quality=\"high\" wmode=\"transparent\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" type=\"application/x-shockwave-flash\" width=\""+w+"\" height=\""+h+"\"></embed>");
	document.write("</object>\n");
}

/* ************************************************************* */
/* 2 FUNZIONI per aprire il DIV di aiuto per l'editor di testo   */
/* ************************************************************* */
function overlay(curobj,subobj,w,h){
	if (document.getElementById){
		var subobj=document.getElementById(subobj);
		subobj.style.left=(screen.width-w)/2+"px";
		//distanza fissa e non calcolata
		//subobj.style.top=(screen.height-h)/2+"px"; 
		subobj.style.top=h+"px";
		subobj.style.zIndex=1;
		subobj.style.display="block";
		return false;
	}
	else
	  return true;
}
function overlayclose(subobj){ document.getElementById(subobj).style.display="none" }

/* ************************************************* */
/* ****** COUNTDOWN CARATTERI SULLE TEXTAREA ******* */
/* ************************************************* */
function countChars(d) {
	if (navigator.appName.indexOf("Netscape")>=0 &&
	    parseInt(navigator.appVersion) >=4) { var nn4 = true }
		document.formCMS_edit.testo.onkeydown = countChars;
	if (nn4) document.captureEvents(Event.KEYDOWN);

	var oldString = "";

	if (document.formCMS_edit.testo.value.length> 255) {
        var agree = confirm("Hai raggiunto il numero massimo di caratteri disponibili (255)\n\n• \'\'OK\'\' per svuotare l\'area di testo\n• \'\'Annulla\'\' per visualizzare solo i primi 255 caratteri");
        if (agree) {
			document.formCMS_edit.rimasti.value = 255;
		    document.formCMS_edit.testo.value = "";
			return true;
		}else{
			document.formCMS_edit.rimasti.value = 0;
		    document.formCMS_edit.testo.value = document.formCMS_edit.testo.value.substring(0,255);
			return false;
		}
	} else {
	  document.formCMS_edit.rimasti.value = 255 - document.formCMS_edit.testo.value.length;
	  oldString = document.formCMS_edit.testo.value.substring(0,255);
	}
}
/* #### INIZIO #### */
/* COUNTDOWN CARATTERI SULLA TEXTAREA DELLA PAGINA galleryAlbums_edit.asp */
/* TESTI NELLE 5 POTENZIALI LINGUE */
function countCharsIt(d) { //ITALIANO
	if (navigator.appName.indexOf("Netscape")>=0 &&
	    parseInt(navigator.appVersion) >=4) { var nn4 = true }
		document.formCMS_edit.testoIt.onkeydown = countCharsIt;
	if (nn4) document.captureEvents(Event.KEYDOWN);

	var oldString = "";

	if (document.formCMS_edit.testoIt.value.length> 255) {
        var agree = confirm("Hai raggiunto il numero massimo di caratteri disponibili (255)\n\n• \'\'OK\'\' per svuotare l\'area di testo in Italiano\n• \'\'Annulla\'\' per visualizzare solo i primi 255 caratteri");
        if (agree) {
			document.formCMS_edit.rimastiIt.value = 255;
		    document.formCMS_edit.testoIt.value = "";
			return true;
		}else{
			document.formCMS_edit.rimastiIt.value = 0;
		    document.formCMS_edit.testoIt.value = document.formCMS_edit.testoIt.value.substring(0,255);
			return false;
		}
	} else {
	  document.formCMS_edit.rimastiIt.value = 255 - document.formCMS_edit.testoIt.value.length;
	  oldString = document.formCMS_edit.testoIt.value.substring(0,255);
	}
}

function countCharsEn(d) { // INGLESE
	if (navigator.appName.indexOf("Netscape")>=0 &&
	    parseInt(navigator.appVersion) >=4) { var nn4 = true }
		document.formCMS_edit.testoEn.onkeydown = countCharsEn;
	if (nn4) document.captureEvents(Event.KEYDOWN);

	var oldString = "";

	if (document.formCMS_edit.testoEn.value.length> 255) {
        var agree = confirm("Hai raggiunto il numero massimo di caratteri disponibili (255)\n\n• \'\'OK\'\' per svuotare l\'area di testo in Inglese\n• \'\'Annulla\'\' per visualizzare solo i primi 255 caratteri");
        if (agree) {
			document.formCMS_edit.rimastiEn.value = 255;
		    document.formCMS_edit.testoEn.value = "";
			return true;
		}else{
			document.formCMS_edit.rimastiEn.value = 0;
		    document.formCMS_edit.testoEn.value = document.formCMS_edit.testoEn.value.substring(0,255);
			return false;
		}
	} else {
	  document.formCMS_edit.rimastiEn.value = 255 - document.formCMS_edit.testoEn.value.length;
	  oldString = document.formCMS_edit.testoEn.value.substring(0,255);
	}
}

function countCharsFr(d) { // FRANCESE
	if (navigator.appName.indexOf("Netscape")>=0 &&
	    parseInt(navigator.appVersion) >=4) { var nn4 = true }
		document.formCMS_edit.testoFr.onkeydown = countCharsFr;
	if (nn4) document.captureEvents(Event.KEYDOWN);

	var oldString = "";

	if (document.formCMS_edit.testoFr.value.length> 255) {
        var agree = confirm("Hai raggiunto il numero massimo di caratteri disponibili (255)\n\n• \'\'OK\'\' per svuotare l\'area di testo in Francese\n• \'\'Annulla\'\' per visualizzare solo i primi 255 caratteri");
        if (agree) {
			document.formCMS_edit.rimastiFr.value = 255;
		    document.formCMS_edit.testoFr.value = "";
			return true;
		}else{
			document.formCMS_edit.rimastiFr.value = 0;
		    document.formCMS_edit.testoFr.value = document.formCMS_edit.testoFr.value.substring(0,255);
			return false;
		}
	} else {
	  document.formCMS_edit.rimastiFr.value = 255 - document.formCMS_edit.testoFr.value.length;
	  oldString = document.formCMS_edit.testoFr.value.substring(0,255);
	}
}

function countCharsDe(d) { // TEDESCO
	if (navigator.appName.indexOf("Netscape")>=0 &&
	    parseInt(navigator.appVersion) >=4) { var nn4 = true }
		document.formCMS_edit.testoDe.onkeydown = countCharsDe;
	if (nn4) document.captureEvents(Event.KEYDOWN);

	var oldString = "";

	if (document.formCMS_edit.testoDe.value.length> 255) {
        var agree = confirm("Hai raggiunto il numero massimo di caratteri disponibili (255)\n\n• \'\'OK\'\' per svuotare l\'area di testo in Tedesco\n• \'\'Annulla\'\' per visualizzare solo i primi 255 caratteri");
        if (agree) {
			document.formCMS_edit.rimastiDe.value = 255;
		    document.formCMS_edit.testoDe.value = "";
			return true;
		}else{
			document.formCMS_edit.rimastiDe.value = 0;
		    document.formCMS_edit.testoDe.value = document.formCMS_edit.testoIt.value.substring(0,255);
			return false;
		}
	} else {
	  document.formCMS_edit.rimastiDe.value = 255 - document.formCMS_edit.testoDe.value.length;
	  oldString = document.formCMS_edit.testoDe.value.substring(0,255);
	}
}

function countCharsEs(d) { // SPAGNOLO
	if (navigator.appName.indexOf("Netscape")>=0 &&
	    parseInt(navigator.appVersion) >=4) { var nn4 = true }
		document.formCMS_edit.testoEs.onkeydown = countCharsEs;
	if (nn4) document.captureEvents(Event.KEYDOWN);

	var oldString = "";

	if (document.formCMS_edit.testoEs.value.length> 255) {
        var agree = confirm("Hai raggiunto il numero massimo di caratteri disponibili (255)\n\n• \'\'OK\'\' per svuotare l\'area di testo in Spagnolo\n• \'\'Annulla\'\' per visualizzare solo i primi 255 caratteri");
        if (agree) {
			document.formCMS_edit.rimastiEs.value = 255;
		    document.formCMS_edit.testoEs.value = "";
			return true;
		}else{
			document.formCMS_edit.rimastiEs.value = 0;
		    document.formCMS_edit.testoEs.value = document.formCMS_edit.testoEs.value.substring(0,255);
			return false;
		}
	} else {
	  document.formCMS_edit.rimastiEs.value = 255 - document.formCMS_edit.testoEs.value.length;
	  oldString = document.formCMS_edit.testoEs.value.substring(0,255);
	}
}
/* #### FINE #### */

/* **************************************************** */
/* ****** FUNZIONE bubble tooltips ******************** */
/* **************************************************** */
function enableTooltips(id){
	var links,i,h;
	if(!document.getElementById || !document.getElementsByTagName) return;
	AddCss();
	h=document.createElement("span");
	h.id="btc";
	h.setAttribute("id","btc");
	h.style.position="absolute";
	document.getElementsByTagName("body")[0].appendChild(h);
	if(id==null) links=document.getElementsByTagName("a");
	else links=document.getElementById(id).getElementsByTagName("a");
	for(i=0;i<links.length;i++){ Prepare(links[i]); }
}

function Prepare(el){
	var tooltip,t,b,s,l;
	t=el.getAttribute("title");
	if(t==null || t.length==0) t="link:";
	el.removeAttribute("title");
	tooltip=CreateEl("span","tooltip");
	s=CreateEl("span","top");
	s.appendChild(document.createTextNode(t));
	tooltip.appendChild(s);
	b=CreateEl("b","bottom");
	l=el.getAttribute("href");
	//commentato xkè (non sò il motivo) bloccava l'Ajax nelle select multiple
	//if(l.length>30) l=l.substr(0,27)+"...";
	//commentato per non far comparire il link in fondo al tooltip
	//b.appendChild(document.createTextNode(l)); 
	tooltip.appendChild(b);
	setOpacity(tooltip);
	el.tooltip=tooltip;
	el.onmouseover=showTooltip;
	el.onmouseout=hideTooltip;
	el.onmousemove=Locate;
}

function showTooltip(e){
	document.getElementById("btc").appendChild(this.tooltip);
	Locate(e);
}

function hideTooltip(e){
	var d=document.getElementById("btc");
	if(d.childNodes.length>0) d.removeChild(d.firstChild);
}

function setOpacity(el){
	el.style.filter="alpha(opacity:95)";
	el.style.KHTMLOpacity="0.95";
	el.style.MozOpacity="0.95";
	el.style.opacity="0.95";
}

function CreateEl(t,c){
	var x=document.createElement(t);
	x.className=c;
	x.style.display="block";
	return(x);
}

function AddCss(){
	var l=CreateEl("link");
	l.setAttribute("type","text/css");
	l.setAttribute("rel","stylesheet");
	l.setAttribute("href","bt.css");
	l.setAttribute("media","screen");
	document.getElementsByTagName("head")[0].appendChild(l);
}

function Locate(e){
	var posx=0,posy=0;
	if(e==null) e=window.event;
	if(e.pageX || e.pageY){ posx=e.pageX; posy=e.pageY; }
	else if(e.clientX || e.clientY){
	    if(document.documentElement.scrollTop){
	        posx=e.clientX+document.documentElement.scrollLeft;
	        posy=e.clientY+document.documentElement.scrollTop;
	        }
	    else{
	        posx=e.clientX+document.body.scrollLeft;
	        posy=e.clientY+document.body.scrollTop;
	        }
	    }
	document.getElementById("btc").style.top=(posy+10)+"px";
	document.getElementById("btc").style.left=(posx-20)+"px";
}

/* **************************************************** */
/* ****** FUNZIONE DATA ******************************* */
/* **************************************************** */
function data() {
	thisDate = new Date()
		giorno = thisDate.getDay();
		oggi = thisDate.getDate();
		monthNum = thisDate.getMonth();
		year = thisDate.getYear();
	if (year <=100)
		year=1900+Year;
	
	if (monthNum == 0) NomeMese = "Gennaio ";
		else if (monthNum == 1) NomeMese = "Febbraio ";
		else if (monthNum == 2) NomeMese = "Marzo ";
		else if (monthNum == 3) NomeMese = "Aprile ";
		else if (monthNum == 4) NomeMese = "Maggio ";
		else if (monthNum == 5) NomeMese = "Giugno ";
		else if (monthNum == 6) NomeMese = "Luglio ";
		else if (monthNum == 7) NomeMese = "Agosto ";
		else if (monthNum == 8) NomeMese = "Settembre ";
		else if (monthNum == 9) NomeMese = "Ottobre ";
		else if (monthNum == 10) NomeMese = "Novembre ";
		else if (monthNum == 11) NomeMese = "Dicembre ";
	
	if (giorno == 0) NomeGiorno = "Domenica";
		else if (giorno == 1) NomeGiorno = "Lunedì";
		else if (giorno == 2) NomeGiorno = "Martedì";
		else if (giorno == 3) NomeGiorno = "Mercoledì";
		else if (giorno == 4) NomeGiorno = "Giovedì";
		else if (giorno == 5) NomeGiorno = "Venerdì";
		else if (giorno == 6) NomeGiorno = "Sabato";
	document.write(NomeGiorno+' '+oggi+' '+NomeMese+ ' '+year);
}

/* **************************************************** */
/* ************** FUNZIONE MOLTI-A-MOLTI ************** */
/* **************************************************** */
var fromBoxArray = new Array();
var toBoxArray = new Array();
var selectBoxIndex = 0;
var arrayOfItemsToSelect = new Array();

function moveSingleElement() {
	var selectBoxIndex = this.parentNode.parentNode.id.replace(/[^\d]/g,'');
	var tmpFromBox;
	var tmpToBox;
	if(this.tagName.toLowerCase()=='select'){			
		tmpFromBox = this;
		if(tmpFromBox==fromBoxArray[selectBoxIndex])tmpToBox = toBoxArray[selectBoxIndex]; else tmpToBox = fromBoxArray[selectBoxIndex];
	}else{
		if(this.value.indexOf('>')>=0){
			tmpFromBox = fromBoxArray[selectBoxIndex];
			tmpToBox = toBoxArray[selectBoxIndex];			
		}else{
			tmpFromBox = toBoxArray[selectBoxIndex];
			tmpToBox = fromBoxArray[selectBoxIndex];	
		}
	}
	
	for(var no=0;no<tmpFromBox.options.length;no++){
		if(tmpFromBox.options[no].selected){
			tmpFromBox.options[no].selected = false;
			tmpToBox.options[tmpToBox.options.length] = new Option(tmpFromBox.options[no].text,tmpFromBox.options[no].value);
			
			for(var no2=no;no2<(tmpFromBox.options.length-1);no2++){
				tmpFromBox.options[no2].value = tmpFromBox.options[no2+1].value;
				tmpFromBox.options[no2].text = tmpFromBox.options[no2+1].text;
				tmpFromBox.options[no2].selected = tmpFromBox.options[no2+1].selected;
			}
			no = no -1;
			tmpFromBox.options.length = tmpFromBox.options.length-1;
		}			
	}
	
	var tmpTextArray = new Array();
	for(var no=0;no<tmpFromBox.options.length;no++){
		tmpTextArray.push(tmpFromBox.options[no].text + '___' + tmpFromBox.options[no].value);			
	}
	tmpTextArray.sort();
	var tmpTextArray2 = new Array();
	for(var no=0;no<tmpToBox.options.length;no++){
		tmpTextArray2.push(tmpToBox.options[no].text + '___' + tmpToBox.options[no].value);			
	}		
	tmpTextArray2.sort();
	
	for(var no=0;no<tmpTextArray.length;no++){
		var items = tmpTextArray[no].split('___');
		tmpFromBox.options[no] = new Option(items[0],items[1]);
	}		
	
	for(var no=0;no<tmpTextArray2.length;no++){
		var items = tmpTextArray2[no].split('___');
		tmpToBox.options[no] = new Option(items[0],items[1]);			
	}
}

function sortAllElement(boxRef) {
	var tmpTextArray2 = new Array();
	for(var no=0;no<boxRef.options.length;no++){
		tmpTextArray2.push(boxRef.options[no].text + '___' + boxRef.options[no].value);			
	}		
	tmpTextArray2.sort();		
	for(var no=0;no<tmpTextArray2.length;no++){
		var items = tmpTextArray2[no].split('___');
		boxRef.options[no] = new Option(items[0],items[1]);			
	}		
}

function moveAllElements() {
	var selectBoxIndex = this.parentNode.parentNode.id.replace(/[^\d]/g,'');
	var tmpFromBox;
	var tmpToBox;		
	if(this.value.indexOf('>')>=0){
		tmpFromBox = fromBoxArray[selectBoxIndex];
		tmpToBox = toBoxArray[selectBoxIndex];			
	}else{
		tmpFromBox = toBoxArray[selectBoxIndex];
		tmpToBox = fromBoxArray[selectBoxIndex];	
	}
	
	for(var no=0;no<tmpFromBox.options.length;no++){
		tmpToBox.options[tmpToBox.options.length] = new Option(tmpFromBox.options[no].text,tmpFromBox.options[no].value);			
	}	
	tmpFromBox.options.length=0;
	sortAllElement(tmpToBox);
}

/* This function highlights options in the "to-boxes". 
   It is needed if the values should be remembered after submit. 
   Call this function onsubmit for your form */
function multipleSelectOnSubmit() {
	for(var no=0;no<arrayOfItemsToSelect.length;no++){
		var obj = arrayOfItemsToSelect[no];
		for(var no2=0;no2<obj.options.length;no2++){
			obj.options[no2].selected = true;
		}
	}
}

function createMovableOptions(fromBox,toBox,totalWidth,totalHeight,labelLeft,labelRight) {		
	fromObj = document.getElementById(fromBox);
	toObj = document.getElementById(toBox);
	
	arrayOfItemsToSelect[arrayOfItemsToSelect.length] = toObj;
	
	fromObj.ondblclick = moveSingleElement;
	toObj.ondblclick = moveSingleElement;
	
	fromBoxArray.push(fromObj);
	toBoxArray.push(toObj);
	
	var parentEl = fromObj.parentNode;
	
	var parentDiv = document.createElement('DIV');
	parentDiv.className='multipleSelectBoxControl';
	parentDiv.id = 'selectBoxGroup' + selectBoxIndex;
	parentDiv.style.width = totalWidth + 'px';
	parentDiv.style.height = totalHeight + 'px';
	parentDiv.style.textAlign = 'center';
	parentEl.insertBefore(parentDiv,fromObj);
	
	var subDiv = document.createElement('DIV');
	subDiv.style.width = (Math.floor(totalWidth/2) - 15) + 'px';
	fromObj.style.width = (Math.floor(totalWidth/2) - 15) + 'px';
	
	var label = document.createElement('SPAN');
	label.innerHTML = labelLeft;
	subDiv.appendChild(label);
	
	subDiv.appendChild(fromObj);
	subDiv.className = 'multipleSelectBoxDiv';
	parentDiv.appendChild(subDiv);
	
	var buttonDiv = document.createElement('DIV');
	buttonDiv.style.verticalAlign = 'middle';
	buttonDiv.style.paddingTop = (totalHeight/2) - 50 + 'px';
	buttonDiv.style.width = '30px';
	buttonDiv.style.textAlign = 'center';
	parentDiv.appendChild(buttonDiv);
	
	var buttonRight = document.createElement('INPUT');
	buttonRight.type='button';
	buttonRight.value = '>';
	buttonRight.style.textAlign = 'center';
	buttonDiv.appendChild(buttonRight);	
	buttonRight.onclick = moveSingleElement;	
	
	var buttonAllRight = document.createElement('INPUT');
	buttonAllRight.type='button';
	buttonAllRight.value = '>>';
	buttonAllRight.style.textAlign = 'center';
	buttonAllRight.onclick = moveAllElements;
	buttonDiv.appendChild(buttonAllRight);		
	
	var buttonLeft = document.createElement('INPUT');
	buttonLeft.style.marginTop='10px';
	buttonLeft.type='button';
	buttonLeft.value = '<';
	buttonLeft.style.textAlign = 'center';
	buttonLeft.onclick = moveSingleElement;
	buttonDiv.appendChild(buttonLeft);		
	
	var buttonAllLeft = document.createElement('INPUT');
	buttonAllLeft.type='button';
	buttonAllLeft.value = '<<';
	buttonAllLeft.style.textAlign = 'center';
	buttonAllLeft.onclick = moveAllElements;
	buttonDiv.appendChild(buttonAllLeft);
	
	var subDiv = document.createElement('DIV');
	subDiv.style.width = (Math.floor(totalWidth/2) - 15) + 'px';
	toObj.style.width = (Math.floor(totalWidth/2) - 15) + 'px';
	
	var label = document.createElement('SPAN');
	label.innerHTML = labelRight;
	subDiv.appendChild(label);
			
	subDiv.appendChild(toObj);
	parentDiv.appendChild(subDiv);		
	
	toObj.style.height = (totalHeight - label.offsetHeight) + 'px';
	fromObj.style.height = (totalHeight - label.offsetHeight) + 'px';
	
	selectBoxIndex++;
}

/* **************************************************** */
/* jQuery Scroll to Top Control script
/* **************************************************** */
var scrolltotop={
	setting: {startline:100, scrollduration:1000, fadeduration:[500, 100]}, //startline=number of pixels from top of doc scrollbar is at before showing control
	controlHTML: '<img class="scrollToTop" src="images/_icons/ico_top.png" style="width:60px; height:22px" />', //HTML for control, which is auto wrapped in DIV w/ ID="topcontrol"
	controlattrs: {offsetx:15, offsety:15}, //offset of control relative to right/ bottom of window corner
	anchorkeyword: '#top', //Enter href value of HTML anchors on the page that should also act as "Scroll Up" links
	
	state: {isvisible:false, shouldvisible:false},

	scrollup:function(){
		if (!this.cssfixedsupport)
			this.$control.css({opacity:0})
			this.$body.animate({scrollTop: 0}, this.setting.scrollduration);
	},

	keepfixed:function(){
		var $window=jQuery(window)
		var controlx=$window.scrollLeft() + $window.width() - this.$control.width() - this.controlattrs.offsetx
		var controly=$window.scrollTop() + $window.height() - this.$control.height() - this.controlattrs.offsety
		this.$control.css({left:controlx+'px', top:controly+'px'})
	},

	togglecontrol:function(){
		var scrolltop=jQuery(window).scrollTop()
		if (!this.cssfixedsupport)
			this.keepfixed()
			this.state.shouldvisible=(scrolltop>=this.setting.startline)? true : false
		if (this.state.shouldvisible && !this.state.isvisible){
			this.$control.stop().animate({opacity:1}, this.setting.fadeduration[0])
			this.state.isvisible=true
		}
		else if (this.state.shouldvisible==false && this.state.isvisible){
			this.$control.stop().animate({opacity:0}, this.setting.fadeduration[1])
			this.state.isvisible=false
		}
	},
	
	init:function(){
		jQuery(document).ready(function($){
			var mainobj=scrolltotop
			var iebrws=document.all
			mainobj.cssfixedsupport=!iebrws || iebrws && document.compatMode=="CSS1Compat" && window.XMLHttpRequest //not IE or IE7+ browsers in standards mode
			mainobj.$body=$('html,body')
			mainobj.$control=$('<div id="topcontrol">'+mainobj.controlHTML+'</div>')
				.css({position:mainobj.cssfixedsupport? 'fixed' : 'absolute', bottom:mainobj.controlattrs.offsety, right:mainobj.controlattrs.offsetx, opacity:0, cursor:'pointer'})
				.attr({title:'top of the page'})
				.click(function(){mainobj.scrollup(); return false})
				.appendTo('body')
			if (document.all && !window.XMLHttpRequest && mainobj.$control.text()!='') //loose check for IE6 and below, plus whether control contains any text
				mainobj.$control.css({width:mainobj.$control.width()}) //IE6- seems to require an explicit width on a DIV containing text
				mainobj.togglecontrol()
			$('a[href="' + mainobj.anchorkeyword +'"]').click(function(){
				mainobj.scrollup()
				return false
			})
			$(window).bind('scroll resize', function(e){
				mainobj.togglecontrol()
			})
		})
	}
}
