//==================================================================

//------------------------------------------------------------------  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
		}
		
//--- CONVERTI CAPORIGA			
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
		}
		
//--- 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 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
					}
				} 
		if(ogg[i].name.toUpperCase()=='PREZZO')
			ogg[i].value=''+ogg[i].value.replace(/ /g,"")         	
		if(ogg[i].name.toUpperCase()=='EMAIL'){
			ogg[i].value=''+ogg[i].value.toLowerCase()
			var emailExists=true
					}			
			}
	var verifica=i<ogg.length-1?false:true  
	if(!verifica)alert('Compilazione incompleta! ('+ ogg[i].name+')')
	else	ControllaCaratteriForm(ogg)

	if(verifica&&emailExists){
		try{
		stringaCriptata=document.applets[0].DammiDato(document.forms[0].email.value)+''	//CODIFICA EMAIL
			}
		catch(error){
			return verifica
				}
		for(var i=0; i<1000000; i++){}
			
		if(stringaCriptata.length<1)
			return verifica
		else	{
			document.forms[0].email.value=stringaCriptata
				}
			}
	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
		}	


//==================================================================

//------------------------------------------------------------------ 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)
		}

//------------------------------- CREDITS		
function Credits(){
	l=document.all?layer:document.getElementById('layer')
	l.style.visibility='visible'
	document.onclick=Chiudi  
	try{
		parent.frames[1].document.onclick=Chiudi
		parent.frames[1].frames[0].document.onclick=Chiudi 
			}
	catch(error){}
		}  
function Chiudi(){l.style.visibility='hidden'}      


//------------------------------- 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)
		}


//------------------------------- CONTROLLA DIMENSIONI IMG

function LimitaImg(maxW, maxH){
	for(var i=0; i<document.images.length; i++){
		var w=document.images[i].width
		var h=document.images[i].height
		if(w>maxW){
			h=maxW*(h/w); w=maxW 
				}
		else if(h>maxH){
			w=maxH*(w/h); h=maxH
				}
		document.images[i].height=h; document.images[i].width=w; 
			}
		}

//------------------------------- CERCA IMG SU ERRORE
//--- IMPOSTARE L'IMG CON ESTENSIONE GIF. LA FUNZIONE E' CHIAMATA DA onerror(this,"percorso defaultImg").
//--- SU ERRORE SOSTITUISCE .GIF CON .JPG; SU ULTERIORE ERRORE ASSEGNA UN'IMG DI COMODO (defaultImg)

function CercaImg(file, defaultImg){
	var stringaFile=file.src
	if(stringaFile.slice(stringaFile.lastIndexOf('.'))!=".jpg")
		file.src=stringaFile.substring(0,stringaFile.lastIndexOf('.'))+".jpg"
	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	
		}


