//FUNZIONE CONVERTI DATA --- INIZIO
// La funzione converte un campo data con i separatori - in / nel formato gg/mm/aaaa
// e restituisce la data gg/mm/aaaa
function form_data_input(valore){
	var valore, giorno, mese, anno, posvirg, anno_2000, risultato;
	
	// Sostituisco il - con la /
	for (i=0; i<valore.length; i++) {
		posvirg = valore.indexOf('-' , 0);
		if (posvirg != -1){
			valore = valore.substring(0 , posvirg)+ '/' + valore.substring(posvirg+1 , valore.length);
		}
	}
	
	// Leggo i caratteri dopo la / per GIORNO
	posvirg = valore.indexOf('/' , 0);
	if (posvirg != -1){
		giorno = valore.substring(0 , posvirg);
		valore = valore.substring(posvirg+1 , valore.length);
		if (giorno.length < 2){
			giorno = '0'+giorno;
		}
	}else{
		giorno = valore;
		valore = '';
	}

	// Leggo i caratteri dopo la / per MESE 
	posvirg = valore.indexOf('/' , 0);
	if (posvirg != -1){
		mese = valore.substring(0 , posvirg);
		valore = valore.substring(posvirg+1 , valore.length);
		if (mese.length < 2){
			mese = '0'+mese;
		}
	}else{
		mese = valore;
		valore = '';
	}

	// Leggo i caratteri dopo la / per ANNO
	posvirg = valore.indexOf('/' , 0);
	if (posvirg != -1){
		anno = valore.substring(0 , posvirg);
	}else{
		anno = valore;
	}
	if (anno != ''){
		anno_2000 = '2000';
		if (anno.length < 4){
			anno = anno_2000.substring(0 , 4-anno.length)+anno;
		}
	}

	risultato = giorno + '/' + mese + '/' + anno;
	return risultato;
}
//FUNZIONE CONVERTI DATA --- FINE

// FUNZIONE PER CONTROLLARE I VALORI AMMESSI NELL'INPUT --- Inizio
//I valori ammessi: spazio . , / -  numerico
function ControlloValori(nome_form,campo){
	var valore = eval('document.'+nome_form+'.'+campo+'.value');
	var lunghezza = eval('document.'+nome_form+'.'+campo+'.value.length');
	var poscar;
	var esito;

	for (x=0; x<lunghezza; x++){
		poscar = valore.indexOf(" ");
		if (poscar > 0){
			valore =  valore.substr(0, poscar) + valore.substr(poscar + 1) ;
			lunghezza = valore.length;
		}
	}
	for (x=0; x<lunghezza; x++){
		poscar = valore.indexOf(".");
		if (poscar > 0){
			valore =  valore.substr(0, poscar) + valore.substr(poscar + 1) ;
			lunghezza = valore.length;
		}
	}
	for (x=0; x<lunghezza; x++){
		poscar = valore.indexOf(",");
		if (poscar > 0){
			valore =  valore.substr(0, poscar) + valore.substr(poscar + 1) ;
			lunghezza = valore.length;
		}
	}
	for (x=0; x<lunghezza; x++){
		poscar = valore.indexOf("/");
		if (poscar > 0){
			valore =  valore.substr(0, poscar) + valore.substr(poscar + 1) ;
			lunghezza = valore.length;
		}
	}
	for (x=0; x<lunghezza; x++){
		poscar = valore.indexOf("-");
		if (poscar > 0){
			valore =  valore.substr(0, poscar) + valore.substr(poscar + 1) ;
			lunghezza = valore.length;
		}
	}

	if (isNaN(valore)){
		esito = false;
	}else{
		esito = true;
	}
	return esito;
}
// FUNZIONE PER CONTROLLARE I VALORI AMMESSI NELL'INPUT --- Fine

// FUNZIONE PER DATE - Inizio
	//La funzione verifica il formato del campo data contenuto nel form
	// Parametri: campo --> nome campo data
	//            form  --> nome form 
	//            lingua  --> alert in lingua 
	//            campo_focus  --> nome del campo su cui fare il focus 
function Check_dmg(campo,form,lingua,campo_focus) {
	var valore,giorno,barra1,mese,barra2,anno,str_mesi,str_giorni,pos_mese,max_giorno_mese;
	str_mesi   = '01*02*03*04*05*06*07*08*09*10*11*12*';
	str_giorni = '31*29*31*30*31*30*31*31*30*31*30*31*';
	valore = eval("document."+form+"."+campo+".value");
	// gg/mm/aaaa
	// 0123456789
	// il secondo parametro di substring indica la posizione +1 dell'ultimo carattere da selezionare
	giorno = valore.substring(0,2);
	barra1 = valore.substring(2,3);
	mese = valore.substring(3,5);
	barra2 = valore.substring(5,6);
	anno = valore.substring(6,10);
	
	switch(lingua){
		case 'it':
			data_arrivo_valori_alert = 'Data '+valore+' non valida. Formato gg/mm/aaaa';
		break;
	
		case 'en':
			data_arrivo_valori_alert = 'Date '+valore+' is wrong. Format gg/mm/aaaa';
		break;
	
		case 'es':
			data_arrivo_valori_alert = 'Fecha '+valore+' es incorrecto. Formato gg/mm/aaaa';
		break;
	}

	if (isNaN(giorno)){
		alert (data_arrivo_valori_alert);
		eval("document."+form+"."+campo_focus+".focus()");
		return false;
	}else if(giorno <1 || giorno >31){
		alert (data_arrivo_valori_alert);
		eval("document."+form+"."+campo_focus+".focus()");
		return false;
	}
		
	if (barra1 != '/'){
		alert (data_arrivo_valori_alert);
		eval("document."+form+"."+campo_focus+".focus()");
		return false;
	}
		
	if (isNaN(mese)){
		alert (data_arrivo_valori_alert);
		eval("document."+form+"."+campo_focus+".focus()");
		return false;
	}else if(mese <1 || mese >12){
		alert (data_arrivo_valori_alert);
		eval("document."+form+"."+campo_focus+".focus()");
		return false;
	}else{
		pos_mese = str_mesi.indexOf(mese+'*');
		max_giorno_mese = str_giorni.substring(pos_mese,pos_mese + 2);
		if (giorno > max_giorno_mese){
		alert (data_arrivo_valori_alert);
		  eval("document."+form+"."+campo_focus+".focus()");
		  return false;
		}
	}

	if (barra2 != '/'){
		alert (data_arrivo_valori_alert);
		eval("document."+form+"."+campo_focus+".focus()");
		return false;
	}
		
	if (isNaN(anno)){
		alert (data_arrivo_valori_alert);
		eval("document."+form+"."+campo_focus+".focus()");
		return false;
	}else if(anno < 1900){
		alert (data_arrivo_valori_alert);
		eval("document."+form+"."+campo_focus+".focus()");
		return false;
	}else if(valore.length != 10){
		alert (data_arrivo_valori_alert);
		eval("document."+form+"."+campo_focus+".focus()");
		return false;
	}

	// Gestione bisestile
	if (mese === '02'){
		if (eBisestile(anno)){
			if (giorno > 29){
				alert (data_arrivo_valori_alert);
				eval("document."+form+"."+campo_focus+".focus()");
				return false;
			}
		}else if (giorno > 28){
			alert (data_arrivo_valori_alert);
			eval("document."+form+"."+campo_focus+".focus()");
			return false;
		}
	}
	return true;
}	
	
	
/*
L'anno bisestile cade normalmente ogni quattro anni, 
Il problema di questo 'strano' 29 febbraio nasce dal calendario gregoriano, introdotto nel 1582, 
che fissa un anno bisestile ogni quattro, ma prevede anche che la regola non si applichi agli anni divisibili per 100,
esclusi quelli divisibili per 400. Non sono quindi stati bisestili il 1700, il 1800 e il 1900, mentre lo è stato il 1600 e lo è il 2000.
La fonte di potenziali problemi sta nel fatto che non tutti i programmatori possono aver conosciuto nel dettaglio 
la clausola del 'bisesto se divisibile per 400' e che quindi abbiano considerato il 2000 'divisibile per 100' e quindi con un febbraio da 28 giorni.
*/
function eBisestile(anno){ 
	if(anno%4 == 0 && (anno%100!=0 || anno%400==0)){
	//	alert (anno + ' bisestile');
		return true; 
	}
	else {
	//	alert (anno + ' NON bisestile');
		return false; 
	}
} 

function durata(arrivo, partenza) {
    var gionims, giorni;
	giornims=arrivo.getTime() - partenza.getTime();
	giorni=Math.floor(giornims / (1000 * 60 * 60 * 24));
	return giorni;
}
// FUNZIONE PER DATE - Fine


// FUNZIONE PER ATTIVARE E DISATTIVARE IL BOTTONE INVIA --- INIZIO
var checkobj;
function accetta(el){
	checkobj=el;
	if (document.all||document.getElementById){
		for (i=0;i<checkobj.form.length;i++){
			var tempobj=checkobj.form.elements[i];
			if(tempobj.type.toLowerCase()=="submit"){
				tempobj.disabled=!checkobj.checked;
			}
		}
	}
}
function disabilita(el){
	if (!document.all&&!document.getElementById){
		if (window.checkobj&&checkobj.checked){
			return true;
		}
		else{
			alert("Per favore autorizza il trattamento dei dati personali");
			return false;
		}
	}
}
// FUNZIONE PER ATTIVARE E DISATTIVARE IL BOTTONE INVIA --- FINE

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);

function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' deve contenere un indirizzo e-mail valido.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+' deve contenere un numero.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' deve contenere un numero tra '+min+' e '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' è richiesto.\n'; }
  } if (errors) alert('Errore:\n'+errors);
  document.MM_returnValue = (errors == '');
}


/**
 * Sets/unsets the pointer in browse mode
 *
 * @param   object   the table row
 * @param   object   the color to use for this row
 *
 * @return  boolean  whether pointer is set or not
 */

function setPointer(theRow, thePointerColor)
{
    if (typeof(theRow.style) == 'undefined' || typeof(theRow.cells) == 'undefined') {
        return false;
    }

    var row_cells_cnt           = theRow.cells.length;
    for (var c = 0; c < row_cells_cnt; c++) {
        theRow.cells[c].bgColor = thePointerColor;
    }

    return true;
} // end of the 'setPointer()' function

//FUNZIONE PER SOSTITUIRE NEI CAMPI DI INPUT L'APPICE " CON L'APPICE ' --- INIZIO
function controlla_appice(nome_campo,nome_form){
	var campo, nome_form, valore;

	campo = eval("document."+nome_form+"."+nome_campo+".name");
	valore = eval("document."+nome_form+"."+nome_campo+".value");

	// Sostituisco l'appice doppio con l'appice singolo
	for (i=0; i<valore.length; i++) {
		posvirg = valore.indexOf('"' , 0);
		if (posvirg != -1){
			valore = valore.substring(0 , posvirg)+ "'" + valore.substring(posvirg+1 , valore.length );
		}
	}

	return valore;
}

/***********************************************
* Switch Content script II- © Dynamic Drive (www.dynamicdrive.com)
* This notice must stay intact for legal use. Last updated April 2nd, 2005.
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/

var enablepersist="on" //Enable saving state of content structure using session cookies? (on/off)
var memoryduration="7" //persistence in # of days

var contractsymbol='../img/minus.gif' //Path to image to represent contract state.
var expandsymbol='../img/plus.gif' //Path to image to represent expand state.

/////No need to edit beyond here //////////////////////////

function getElementbyClass(rootobj, classname){
var temparray=new Array()
var inc=0
var rootlength=rootobj.length
for (i=0; i<rootlength; i++){
if (rootobj[i].className==classname)
temparray[inc++]=rootobj[i]
}
return temparray
}

function sweeptoggle(ec){
var inc=0
while (ccollect[inc]){
ccollect[inc].style.display=(ec=="contract")? "none" : ""
inc++
}
revivestatus()
}


function expandcontent(curobj, cid){
if (ccollect.length>0){
document.getElementById(cid).style.display=(document.getElementById(cid).style.display!="none")? "none" : ""
curobj.src=(document.getElementById(cid).style.display=="none")? expandsymbol : contractsymbol
}
}

function revivecontent(){
selectedItem=getselectedItem()
selectedComponents=selectedItem.split("|")
for (i=0; i<selectedComponents.length-1; i++)
document.getElementById(selectedComponents[i]).style.display="none"
}

function revivestatus(){
var inc=0
while (statecollect[inc]){
if (ccollect[inc].style.display=="none")
statecollect[inc].src=expandsymbol
else
statecollect[inc].src=contractsymbol
inc++
}
}

function get_cookie(Name) { 
var search = Name + "="
var returnvalue = "";
if (document.cookie.length > 0) {
offset = document.cookie.indexOf(search)
if (offset != -1) { 
offset += search.length
end = document.cookie.indexOf(";", offset);
if (end == -1) end = document.cookie.length;
returnvalue=unescape(document.cookie.substring(offset, end))
}
}
return returnvalue;
}

function getselectedItem(){
if (get_cookie(window.location.pathname) != ""){
selectedItem=get_cookie(window.location.pathname)
return selectedItem
}
else
return ""
}

function saveswitchstate(){
var inc=0, selectedItem=""
while (ccollect[inc]){
if (ccollect[inc].style.display=="none")
selectedItem+=ccollect[inc].id+"|"
inc++
}
if (get_cookie(window.location.pathname)!=selectedItem){ //only update cookie if current states differ from cookie's
var expireDate = new Date()
expireDate.setDate(expireDate.getDate()+parseInt(memoryduration))
document.cookie = window.location.pathname+"="+selectedItem+";path=/;expires=" + expireDate.toGMTString()
}
}

function do_onload(){
uniqueidn=window.location.pathname+"firsttimeload"
var alltags=document.all? document.all : document.getElementsByTagName("*")
ccollect=getElementbyClass(alltags, "switchcontent")
statecollect=getElementbyClass(alltags, "showstate")
if (enablepersist=="on" && get_cookie(window.location.pathname)!="" && ccollect.length>0)
revivecontent()
if (ccollect.length>0 && statecollect.length>0)
revivestatus()
}

if (window.addEventListener)
window.addEventListener("load", do_onload, false)
else if (window.attachEvent)
window.attachEvent("onload", do_onload)
else if (document.getElementById)
window.onload=do_onload

if (enablepersist=="on" && document.getElementById)
window.onunload=saveswitchstate
