/* **************************************************** */
/* FUNZIONE per aprire una finestra di PopUp centrata   */
/* **************************************************** */
function openPopUp(url,w,h,scrl) {
  l = (screen.width-w)/2;
  t = (screen.height-h)/2;
  newWin = window.open(url,"nuovafinestra", "width="+w+", height="+h+", left="+l+", top="+t+", scrollbars="+scrl+",resizable=no,toolbar=no,location=no,status=no,menubar=no") 
  }

/* ************************************************* */
/* ** POPUP CHE SI AUTODIMENSIONA SU UNA IMMAGINE ** */
/* ************************************************* */
function PrevFoto(img){
  foto1= new Image()
  foto1.src=(img)
  Controlla(img)
}
function Controlla(img){
  if((foto1.width!=0)&&(foto1.height!=0)){
    viewFoto(img);
  }
  else{
    funzione="Controlla('"+img+"')";
    intervallo=setTimeout(funzione,20);
  }
}
function viewFoto(img){
  largh=foto1.width;
  altez=foto1.height;
  posY=(screen.width-foto1.width)/2;
  posX=(screen.height-foto1.height)/2;
  stringa="width="+largh+",height="+altez+",top="+posX+",left="+posY;
  finestra=window.open(img,"",stringa);
}


/* ************************************************* */
/* ****** Elemento a comparsa con un click ********* */
/* ************************************************* */
function scambio(idNew,records){
	if (document.getElementById){
		if(document.getElementById(idNew).style.display == 'none') {
			for(i=0;i<records;i++) {
				document.getElementById(i).style.display = 'none';
			}
			document.getElementById(idNew).style.display = 'block';
		} else {
			document.getElementById(idNew).style.display = 'none';
		}
	}
}


/* **************************************************** */
/* ****** 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 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 ANAGRUPPI VALIDATE ***************** */
/* **************************************************** */
function menuValidate(form) {
    var nome = form.nome.value;

    if (nome == "") {
        alert("ATTENZIONE!!!\n\nIl campo NOME è 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 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 anaGruppiValidate(form) {
    var nome = form.nome.value;

    if (nome == "") {
        alert("ATTENZIONE!!!\n\nIl campo NOME è obbligatorio");
		form.nome.focus();
        return false;
    }
    return true;
}


/* **************************************************** */
/* ****** FUNZIONE NOSTRI LAVORI VALIDATE ************* */
/* **************************************************** */
function nostriLavoriValidate(form) {
    var nome    = form.nome.value;
	var sezione = form.sezione.options[form.sezione.selectedIndex].value;

    if (sezione == 0) {
        alert("ATTENZIONE!!!\n\nScegliere un menù di visualizzazione");
		form.sezione.focus();
        return false;
    }
    if (nome == "") {
        alert("ATTENZIONE!!!\n\nIl campo NOME è obbligatorio");
		form.nome.focus();
        return false;
    }
    return true;
}

/* **************************************************** */
/* ****** FUNZIONE NEWS VALIDATE ********************** */
/* **************************************************** */
function newsValidate(form) {
    var titolo = form.titolo.value;
    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 CLIENTI VALIDATE ******************* */
/* **************************************************** */
function clientiValidate(form,tipo) {
    var nome = form.nome.value;

	/*if (tipo == "NORM" || tipo == "SIST") {
	  	var sezione = form.sezione.options[form.sezione.selectedIndex].value;
	    if (sezione == 0) {
	        alert("ATTENZIONE!!!\n\nScegliere un menù di visualizzazione");
			form.sezione.focus();
	        return false;
	    }
	}
	
var bIsChecked = false,
chks = document.forms["myform"].elements["mese[]"];

for (var i=0;i<chks.length;i++)
if ( chks[i].checked ){
bIsChecked = true;
break;
}

if ( !bIsChecked )
alert("Nessun checkbox selezionato");
	*/
	if (tipo == "COMP") {
	    if ((!form.compat_h.checked) && (!form.compat_s.checked)) {
	        alert("ATTENZIONE!!!\n\nScegliere almeno un tipo di compatibilità");
			form.compat_h.focus();
	        return false;
	    }
	}
	if (nome == "") {
		alert("ATTENZIONE!!!\n\nIl campo NOME è 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 DOPPIATORI VALIDATE **************** */
/* **************************************************** */
function doppiatoriValidate(form) {
    var nome = form.nome.value;
	var audio = form.audio.options[form.audio.selectedIndex].value;

   /* if (audio == 0) {
        alert("Scegliere un file audio per questo Doppiatore");
		form.audio.focus();
        return false;
    } else */
	
	if (nome == "") {
        alert("ATTENZIONE!!!\n\nIl campo NOME DEL DOPPIATORE è 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 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;
}

function ControllaMail(addr) {
   Filtro = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/;
   if (Filtro.test(addr))
      return true;
   else
      return false;
}


/* **************************************************** */
/* ****** FUNZIONE MOLTI-A-MOLTI ********************** */
/* **************************************************** */
  function sposta(direzione,campo,primo,secondo){
	j=0;
	form=document.modulo;
	if(direzione=='-->'){
	  partenza=form.elements(primo);
	  arrivo=form.elements(secondo);
	}
	else{
	  partenza=form.elements(secondo);
	  arrivo=form.elements(primo);
	}

	non_sel=new Array();
	sel=new Array();

	//per ogni opzione di partenza controlla:
	for(i=0;i<partenza.options.length;i++){
		//se è selezionata, mettila nell'array sel
		if(partenza.options[i].selected){
		  partenza.options[i].selected=false;
		  sel.push(partenza.options[i]);
		}
		//altrimenti non è selezionata, quindi mettila nell'array non_sel
		else{
		  non_sel.push(partenza.options[i]);
		}
	}

	//per ogni opzione di arrivo
	for(i=0;i<arrivo.options.length;i++){
	  //aggiungi all'array sel tutti i valori di arrivo
	  sel.push(arrivo.options[i]);
	}

	function sorting(a,b){
	  if(a.text<b.text) return -1;
	  if(a.text>b.text) return 1;
	  else return 0;
	}

	sel.sort(sorting);
	non_sel.sort(sorting);

	//azzera le due select
	partenza.options.length=0;
	arrivo.options.length=0;

	//Per ogni valore in non_sel
	for(i=0;i<non_sel.length;i++){
	  //scrivilo in partenza
	  partenza.options[i]=non_sel[i];
	}

	//Per ogni valore in sel
	for(i=0;i<sel.length;i++){
	  //scrivilo in arrivo
	  arrivo.options[i]=sel[i];
	}

    var valori="";
    for(var k=0;k<form.elements[secondo].length;k++){
      if(k==(form.elements[secondo].length-1)){
        var valori=valori+form.elements[secondo][k].value;
      }else{
        var valori=valori+form.elements[secondo][k].value +",";
      }
    }
    form.elements(campo).value=valori;
  }
  
  
/* **************************************************** */
/* ****** 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://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,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);
	}
}


/* **************************************************** */
/* ****** FUNZIONE bubble tooltips ******************** */
/* **************************************************** */
function enableTooltips(id){
	var links,i,h;
	if(!document.getElementById || !document.getElementsByTagName) return;
	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");
	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 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";
}
