//==================================================================
                                           
//------------------------------------------------------------------  DATA E TIMER

//------------------------------- DATA STANDARD
            
//--- IN mSEC
function DataMs(d){
	var D=new Date(d)
	return D.getTime()
		}
		
//--- DATA E ORA	
function Data(d){
	if(isNaN(d))
		var D=new Date(d)
	else	{
		var D=new Date()
        	D.setTime(d)
        		}
	var g=D.getDate()<10? '0'+D.getDate(): D.getDate()
	var m=(D.getMonth()+1)<10? '0'+(D.getMonth()+1): (D.getMonth()+1)
	var a=D.getYear()+''; a=a.substring(2,a.length)
	var data=g+'/'+m+'/'+a
       
	var ora= D.getHours()<10? '0'+D.getHours(): D.getHours() 
	var min= D.getMinutes()<10? '0'+D.getMinutes(): D.getMinutes() 
	var sec=D.getSeconds()<10? '0'+D.getSeconds(): D.getSeconds()

	var ora=ora+':'+min//+':'+sec
	return (data+' '+ora)   
		}
		
//--- SOLO DATA		
function DataCorta(d){
	var data=Data(d).substring(0,Data(d).indexOf(' '))
	return data
		}
		
//------------------------------- TIMER

//---- PERCHE' SIA EFFICACE E DISCRIMINI IL NUMERO DI TENTATIVI E' NECESSARIO CHE LA CONDIZIONE DI VERITA' NELLA FUNZIONE DA RITARDARE 
	// GENERI UN ERRORE FINCHE' NON VIENE SODDISFATTA. ES:
	// if(azione.length<1) cazzullo()	(METODO INESISTENTE PER FARGLI GENERARE UN ERRORE E FAR RITENTARE IL TIMER)

function Timer(sec,tentativi,funzione){		// --- L'ARGOMENTO FUNZIONE DEVE ESSERE PASSATO COME STRINGA! SEC=INT TENTATIVI=INT
	for(var i=0; i<tentativi; i++){
		try{eval(funzione); var esito=true; break}
		catch(error){
			var data1=DataMs(Date()), data2=0
			data1=(data1*1)+(sec*1000)
			while((data2*1)<data1){
				data2=DataMs(Date())
					}
			var esito=false
				}
			}
	return esito
		}


//==================================================================

//------------------------------------------------------------------ FRAMESET

//------------------------------- GENERA IL FRAMESET

function Frameset(index,pag){
	if(top==window)
		pag!=null?location.replace(index+'#'+pag):location.replace(index)
		}  
			
//------------------------------- APRI IL FRAME SU LOCATION.HASH 
//--- RESTITUISCE UN ARRAY DI N.LINKS
function ApriFrame(){
	if(top.location.hash!=''){ 
		var clic=new Array()
		clic=top.location.hash.substring(1).split(',')
		var nClic= clic.length
		for(var i=0; i<nClic; i++)
       			document.links[clic[i]].onclick()
       			}
 	       	}

//------------------------------- ACCENDE IL GRASSETTO SUL MENU DEL FRAME
function Link(id){
	for(var i=0; i<document.links.length; i++)
		document.links[i].style.fontWeight="normal"
	if(id!=null)
		document.getElementById(id).style.fontWeight="bold"
		}
		
//==================================================================

//------------------------------------------------------------------ STRINGHE

//------------------------------- CODIFICA TESTO
//--- TOGLI SPAZI
function TogliSpazi(testo){
	var testo2=new String()
	testo2=testo.replace(/\s+/g,"")
	return testo2
		}           
		
//--- SOSTITUISCI SPAZI
function SostituisciSpazi(testo){
	var testo2=new String()
	testo2=testo.replace(/\s+/g,"_")    
	testo2=testo2.replace(/'/g,"_")   
	testo2=testo2.replace(/´/g,"_")
	testo2=testo2.replace(/,/g,"_")
	testo2=testo2.replace(/\//g,"_")
	return testo2
			}    
			
//--- RIMETTI SPAZI
function RimettiSpazi(testo){
	var testo2=new String()
	testo2=testo.replace(/_/g," ") 
	return testo2
			}
			
//--- CONVERTI CAPORIGA DA PLAIN-TEXT A HTML			
function ConvertiCaporiga(testo){
	testo=testo.replace(/\n/g,'<br>')			
	return testo
		}		
					
//--- DISATTIVA HTML
function DisattivaHTML(testo){
	testo=testo.replace(/</g,'&lt;')
	testo=testo.replace(/>/g,'&gt;')
	return testo
		}

//--- CONVERTI CAPORIGA DA HTML A PLAIN-TEXT
function ConvertiCaporiga2(testo){
	testo=testo.replace(/<br>/g,  '\n');  testo=testo.replace(/<BR>/g,  '\n'); 
	return testo
			}
					
//--- RIMETTI HTML
function RimettiHTML(testo){
	testo=testo.replace(/&lt;br&gt;/g,'<br>')
	return testo
			}
			
//--- CONVERTI IN MAIUSCOLO
function Maiuscolo(testo){
	testo=testo.toUpperCase(); 
	return testo
			}
			
//--- CONVERTI IN MINUSCOLO
function Minuscolo(testo){
	testo=testo.toLowerCase(); 
	return testo
			}
			
//------------------------------- CODIFICA INPUT
function CodificaInput(testo){                       
	testo=new String(testo)
	testo=testo.replace(/'/g,"´")
	testo=escape(testo)
	return testo
		}
		
//------------------------------- DECODIFICA OUTPUT

function DecodificaOutputHTML(testo){
	testo=unescape(testo)
	testo=ConvertiCaporiga(testo)	
	testo=DisattivaHTML(testo)
	return testo
		}				

//------------------------------- CODIFICA PREZZO   

//--- PREZZO ITALIANO
function PrezzoItaliano(prezzo){      
	prezzo=prezzo+'';   
	if(prezzo.lastIndexOf('.')!=-1){
		if(prezzo.lastIndexOf('.')==(prezzo.length-2))
			prezzo=prezzo+'0'
				}
        else 
	 	prezzo=prezzo+'.00'
	return prezzo.replace('.',',')
		}  
		  
//--- PREZZO VERO
function PrezzoVero(prezzo){
	prezzo=prezzo.replace(/,/,'.')+''	
       	return (prezzo*1)
      		}
		  
//--- PREZZO INGLESE
function PrezzoInglese(prezzo){
	prezzo=prezzo+'';   
	if(prezzo.indexOf(',',prezzo.length-2)>-1)
		prezzo=prezzo+'0'; 
	else if(prezzo.indexOf(',',prezzo.length-3)<0)
		prezzo=prezzo+',00';
		
	prezzo=prezzo.replace(/,/,'.')+''	
       	return prezzo
      		}

//------------------------------- CODIFICA QUERYSTRING

//--- MIO URLENCODE
function UrlEncode(testo){
	testo=escape(testo)
	testo=testo.replace(/\+/g,'plusplus')
	return testo
		}

//--- MIO HTMLENCODE
function HtmlEncode(testo){
	testo=unescape(testo)
	testo=testo.replace(/&amp;/g,String.fromCharCode(38))
	testo=testo.replace(/plusplus/g,String.fromCharCode(43))
	return testo
		}					

//==================================================================

//------------------------------------------------------------------ ARRAY		
//------------------------------- ORDINA ARRAY DI ARRAY NUMERICI
//--- L'ARGOMENTO "ORDINE" E' OPZIONALE. VALORE PREVISTO="crescente". SE OMESSO L'ARRAY E' DECRESCENTE PER DEFAULT
function Ordina(arrayA,elemento,ordine){
	var arrayB=new Array(arrayA[0])
	for(var i=1; i<arrayA.length; i++){
		arrayB[i]=arrayA[i]
		if(arrayB[i][elemento]>arrayA[0][elemento]){
			arrayB[0]=arrayB[i]
			arrayB[i]=arrayA[0]
			arrayA[0]=arrayA[i]
			arrayA[i]=arrayB[i]
			i=1
				}
		if(arrayB[i][elemento]>arrayA[i-1][elemento]){
			arrayB[i-1]=arrayB[i]
			arrayB[i]=arrayA[i-1]
			arrayA[i-1]=arrayA[i]
			arrayA[i]=arrayB[i]
			i=1
				}
			}
	return ordine=='crescente'? arrayB.reverse(): arrayB
		}	
		
//------------------------------- SOSTITUISCI FUNZIONE SPLICE

function Splice(arrayA,elemento){
	var arrayB=new Array()
	for(var i=0,n=0; i<arrayA.length;i++)
		if(i!=elemento){
			arrayB[n]=arrayA[i]
			n++
				}
	return arrayB
		}		
		                                                                                                                		
//==================================================================

//------------------------------------------------------------------ FORM

//------------------------------- SELECT BOX

function NumeriSelect(selectBox,min,max){
	var n=0
	for(var i=min; i<max; i++){
		var opt=new Option(i,i,false,false)
		selectBox.options[n++]=opt
			}
		}

//------------------------------- CONTROLLO
//--- SE QUALCHE PIATTAFORMA NON SUPPORTA L' APPLET DI CODIFICA DELLA EMAIL, IO SCAVALLO IL BUG METTENDO UN TRY - CATCH
	// SULLA FUNZIONE CODIFICA(). LA MAIL NON VIENE CODIFICATA E VIENE PASSATA ALLA PAGINA ASP SUCCESSIVA DOVE LA FUNZIONE
	// "AGGIUNGIRECORD()" PREVEDE CHE SE TROVA IL CARATTERE @ NON PROCEDE ALLA DECODIFICA DELLA EMAIL.

function ControllaForm(ogg){
	var emailExists=false
	for(var i=0; i<ogg.length; i++){
		if(ogg[i].value==''&&ogg[i].title!="facoltativo")break
		if(ogg[i].title!="facoltativo"){
			if(ogg[i].name.toUpperCase()=='EMAIL' &&(ogg[i].value.indexOf('@')<0 || ogg[i].value.indexOf('.')<0 || ogg[i].value.length<8)){
				alert("Email non corretta.")
				return false
					}
			var msg="Il campo "+ogg[i].name+" deve contenere solo cifre e la virgola per separare i centesimi!"
			if((Maiuscolo(ogg[i].name).indexOf('PREZZO')>-1 || ogg[i].name.toUpperCase()=='IVA' || ogg[i].name.toUpperCase()=='SPESE') && ogg[i].value!=0) {       
				if(ogg[i].value.slice(ogg[i].value.indexOf(',')).length!=3 && ogg[i].name.toUpperCase()!='IVA'){alert(msg); return false} 
				var prezzo=new Array(); prezzo=ogg[i].value.split('')
				for(var n=0; n<prezzo.length; n++)	{
					if((prezzo[n].charCodeAt<48 && prezzo[n].charCodeAt()!=44) || prezzo[n].charCodeAt()>57 ){     
						alert(msg); return false
						}
					}					
				}
			}	
		if(ogg[i].name.toUpperCase()=='EMAIL')
			ogg[i].value=''+ogg[i].value.toLowerCase()
			}
	var verifica=i<ogg.length-1?false:true  
	if(!verifica)alert('Compilazione incompleta! ('+ ogg[i].name+')')
	else	ControllaCaratteriForm(ogg)       
	return verifica			
		}	
		
function ControllaCaratteriForm(ogg){
	for(var i=0; i<ogg.length; i++){
		if(ogg[i].type.indexOf('select')<0)
			ogg[i].value=CodificaInput(ogg[i].value)
			}
		}
		
function RipristinaCaratteriForm(ogg){
	for(var i=0; i<ogg.length; i++){
		if(ogg[i].type.indexOf('select')<0)
			ogg[i].value=DecodificaOutput(ogg[i].value)
			}
		}
		
function ControllaMaiuscoloForm(form){	
	for(var n=0; n<form.length; n++)
		form[n].value=form[n].name=='email'?form[n].value.toLowerCase():form[n].value.toUpperCase()
		}
		
//------------------------------- CRIPTA		
function Codifica(ogg){
	stringaCriptata=document.applets[0].DammiDato(ogg)+''
	return stringaCriptata
		}	

//------------------------------- CONTROLLO NOME FILE

//--- I NOMI DEI FILE SONO GESTITI COME VARIABILI PERTANTO VA LIMITATO L'ACCESSO AI CARATTERI VALIDI

function ControllaNomeFile(nome){  
	for(var i=0; i<nome.length; i++){
		if(nome.indexOf('&')>-1 || nome.indexOf('.')>-1 || (nome.charCodeAt(i)<48&&nome.charCodeAt(i)!=46) || (nome.charCodeAt(i)>57&&nome.charCodeAt(i)<64) || (nome.charCodeAt(i)>90&&nome.charCodeAt(i)<95) || (nome.charCodeAt(i)>123)){
			 alert("Il nome del file contiene caratteri speciali! Consultare la guida."); 
			 return false 
			 	}  
			 }         
	return true
		}
		
//==================================================================

//------------------------------------------------------------------ LAYOUT

//------------------------------- POPUP    
function Apri(URL,nome,w,h,sta,scr,rsz){
	var l=(screen.availWidth-w)/2
	var t=(screen.availHeight-h)/2
	if(nome=='')nome='pop'
	popUp=open(URL,nome,'left='+l+',top='+t+',width='+w+',height='+h+',menubar=0,toolbar=0,status='+sta+',scrollbars='+scr+',resizable='+rsz)
		}                                                     

//------------------------------- AIUTO    
function Aiuto(ancora) {
	Apri("/pannello/aiuto.htm#"+ancora,"",640,480,1,1,1)
		}
		
//------------------------------- CREDITS		
function Credits(){
	document.getElementById('credits').getElementsByTagName('div')[0].style.display='block'
	document.onclick=function(){document.getElementById('credits').getElementsByTagName('div')[0].style.display='none'}
	try{
		parent.frames[1].document.onclick=Chiudi
		parent.frames[1].frames[0].document.onclick=Chiudi 
			}
	catch(error){}
		}  

//------------------------------- TESTO BLINK
n=0	
function TestoBlink(){
	var ogg=document.getElementsByTagName('span')
	n++
	if(n==ogg.length){ogg[n-1].style.color='008822'; n=0; ogg[n].style.color='00cc33'}
	else {ogg[n].style.color='00cc33'; ogg[n-1].style.color='008822'}
	window.setTimeout('TestoBlink()',100)
		}

//------------------------------- LIMITA DIMENSIONI IMG      
var bloccoReload=false
function FormattaImg(img, wMax, hMax){          
	var w=img.width, h=img.height       
	//--- CONTROLLO SE L'IMMAGINE SFORA RISPETTO AI PARAMETRI	
        if(w< wMax && h<hMax) return
	//--- DIMENSIONO IN BASE ALLA LARGHEZZA
	var w0=wMax, h0=w0*(h/w)
	//--- SE L'ALTEZZA SFORA, DIMENSIONO IN BASE ALLA LUNGHEZZA
	if(h0>hMax){h0=hMax; w0=h0*(w/h) }
	img.width=w0; img.height=h0      
	/*if((''+location).indexOf('#')<0 && bloccoReload==false){    
		setTimeout('location.hash="#";location.reload()',200)
			}                                      */
 		}                                     

//------------------------------- RESTITUISCE LE ICONE CORRISPONDENTI AI FILE CARICATI
function IconaFile(percorso){     
	var ext=percorso.slice((percorso.lastIndexOf('.')-percorso.length+1))	  
  	var percorsoIcone="/pannello/img/icons/"  
	switch(Minuscolo(ext)){           
		case Minuscolo(percorso):  return percorsoIcone+"folder.gif"; break 
   		case 'gif': return percorso.slice(0,-3)+ext; break  
		case 'jpg': return percorso.slice(0,-3)+ext; break  
		case 'pdf': return percorsoIcone+"pdf.gif"; break    
		case 'zip': return percorsoIcone+"zip.gif"; break
       		case 'doc': return percorsoIcone+"wordpad.gif"; break
		case 'rtf': return percorsoIcone+"wordpad.gif"; break    
		case 'txt': return percorsoIcone+"wordpad.gif"; break    
		case 'htm': return percorsoIcone+"ie.gif"; break        
		case 'html': return percorsoIcone+"ie.gif"; break
       		case 'mp3': return percorsoIcone+"mp3.gif"; break
       		default: return percorsoIcone+"file.gif"; break    
			}
		}		      		
		
//------------------------------- CERCA IMG SU ERRORE
//--- IMPOSTARE L'IMG CON ESTENSIONE GIF. LA FUNZIONE E' CHIAMATA DA onerror(this,"percorso defaultImg").
//--- SU ERRORE SOSTITUISCE .JPG CON .GIF; SU ULTERIORE ERRORE ASSEGNA UN'IMG DI COMODO (defaultImg)

function CercaImg(file, defaultImg){
	var stringaFile=file.src
	if(stringaFile.slice(stringaFile.lastIndexOf('.'))!=".gif")
		file.src=stringaFile.substring(0,stringaFile.lastIndexOf('.'))+".gif"
	else	file.src=defaultImg
		}
		
//------------------------------- FADE IMG

//--- CREARE OGGETTO: fade= new FadeImg(idImg1, idImg2, numImg, cartella, estensione, tempo)
//--- CHIAMARLO: fade.Preload()

//--- COSTRUTTORE
function FadeImg(idImg1, idImg2, numImg, cartella, estensione, tempo){
	this.idImg1= idImg1
	this.idImg2= idImg2
	this.numImg= numImg-1
	this.cartella= cartella
	this.estensione= estensione
	this.tempo= tempo/100	 //--- IN DECIMI/SEC
	this.Preload=Preload
	this.opacity1=100
	this.opacity2=0
	this.coeff=Math.round(100/(this.tempo/2))
		}

function Preload(){
	var im=new Array()
	for(var i=0; i<fade.numImg; i++){
		im[i]=new Image(); 
		im[i].src=fade.cartella+'/'+i+'.'+fade.estensione
			}
	Fade()
		}
var n=0
function Fade(){
	n++
	with(fade){
		if(n==Math.floor(tempo/2)){
			opacity1=0; opacity2=100
			Ruota()
			document.getElementById(idImg1).src=im
				}
		if(n==(tempo+1)){
			opacity1=100; opacity2=0
			Ruota()
			document.getElementById(idImg2).src=im
			n=0
				}	
		if(n<Math.floor(tempo/2)){
			opacity1=opacity1-coeff; opacity2=opacity2+coeff
				}
		if(n>Math.floor(tempo/2)){
			opacity1=opacity1+coeff; opacity2=opacity2-coeff		
			}
	document.getElementById(fade.idImg1).style.opacity = (opacity1 / 100); document.getElementById(fade.idImg2).style.opacity = (opacity2 / 100) 	//--- W3c
    	document.getElementById(fade.idImg1).style.MozOpacity = (opacity1 / 100); document.getElementById(fade.idImg2).style.MozOpacity = (opacity2 / 100) //--- Mozilla/NN
    	document.getElementById(fade.idImg1).style.KhtmlOpacity = (opacity1 / 100); document.getElementById(fade.idImg2).style.KhtmlOpacity = (opacity2 / 100) //--- Safari
    	document.getElementById(fade.idImg1).style.filter = "alpha(opacity="+opacity1+")"; document.getElementById(fade.idImg2).style.filter= "alpha(opacity="+opacity2+")" //--- IE
	
	setTimeout('Fade()',100)
			}
		}
		
 var i=2, im=null
 function Ruota(){
	if(i>fade.numImg-1) i=-1
	i++
	im=fade.cartella+'/'+i+'.'+fade.estensione
		} 
 								
//------------------------------- TENDINE
var sel='document.links[1]', sel2='document.links[1]'
function InizializzaTendine(){
	for(var i=1; i<document.links.length; i++){
		document.links[i].onclick=function(){
			if(this.target!=''&&this.target!='_blank')parent.frames[this.target].location.replace(this.href)
			if(this.className=='menu1') Tendina(this)
			else if(this.className=='menu2') Tendina2(this)
				}
			}
		}	
function Tendina(t){
	sel=eval(sel); sel2=eval(sel2)
	sel.style.background=sfondoLink; sel.style.color=colorLink
	sel2.style.background=sfondoLink; sel2.style.color=colorLink
	sel.onmouseover=function(){this.style.color=colorSel; this.style.background=sfondoSel}	
	sel.onmouseout=function(){this.style.color=colorLink; this.style.background=sfondoLink}		
	sel=t
	
	for(var i=1; i<document.getElementsByTagName('table').length; i++)
		document.getElementsByTagName('table')[i].style.visibility='hidden'
			
	sel.style.background=sfondoSel; sel.style.color=colorSel
	sel.onmouseover=null
	sel.onmouseout=null	
	var link=sel.href+''
	if(link.indexOf('#')>-1) document.getElementById(sel.innerHTML).style.visibility='visible'
		}
function Tendina2(t){
	sel2=eval(sel2)
	sel2.style.background=sfondoLink; sel2.style.color=colorLink
	sel2.onmouseover=function(){this.style.color=colorSel; this.style.background=sfondoSel}	
	sel2.onmouseout=function(){this.style.color=colorLink; this.style.background=sfondoLink}	
	sel2=t
	sel2.style.background=sfondoSel; sel2.style.color=colorSel
	sel2.onmouseover=null
	sel2.onmouseout=null	
		}

