//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(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'fr':
 		data_arrivo_valori_alert = 'Date '+valore+' est mal. Format gg/mm/aaaa';

	break;
	case'de':
 		data_arrivo_valori_alert = 'Datum '+valore+' ist unrecht. Format 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 ApriImmagine(imagePath,alt,posLeft,posTop) {
	newWindow = window.open("","newWindow","left="+posLeft+",top="+posTop);
	newWindow.document.open();
	newWindow.document.writeln('<html><title>'+alt+'</title>');
	newWindow.document.writeln('<head>');
	newWindow.document.writeln('<script language="javascript"> ');
	newWindow.document.writeln('function centra() {');
	newWindow.document.writeln('var Xpos;');
	newWindow.document.writeln('var Ypos;');
	newWindow.document.writeln("window.resizeTo(document.images['foto'].width,document.images['foto'].height + 28);"); 
	newWindow.document.writeln('Xpos=(screen.width-document.images["foto"].width)/2;');
	newWindow.document.writeln('Ypos=(screen.height-document.images["foto"].height)/2;');
	newWindow.document.writeln('window.moveTo(Xpos,Ypos);');
	newWindow.document.writeln('}');
	newWindow.document.writeln('</script>');
	newWindow.document.writeln('</head>');
	newWindow.document.writeln('<body bgcolor="#FFFFFF" leftmargin="0" topmargin="0" marginheight="0" marginwidth="0">'); 
	newWindow.document.writeln('<img src=' + imagePath + ' name=foto alt=' + alt +' onLoad="javascript:centra();">'); 
	newWindow.document.writeln('</body></html>');
	newWindow.document.close();
	newWindow.focus();
}


//Converti mese in valore numerico
function Converti_mese(nome){
	var numero="";
	switch (nome){
		case "GEN":
			numero = "01";
		break;
		case "FEB":
			numero = "02";
		break;
		case "MAR":
			numero = "03";
		break;
		case "APR":
			numero = "04";
		break;
		case "MAG":
			numero = "05";
		break;
		case "GIU":
			numero = "06";
		break;
		case "LUG":
			numero = "07";
		break;
		case "AGO":
			numero = "08";
		break;
		case "SET":
			numero = "09";
		break;
		case "OTT":
			numero = "10";
		break;
		case "NOV":
			numero = "11";
		break;
		case "DIC":
			numero = "12";
		break;
	}
	return numero;	
}
