//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;
}

function basename_js(pathname){
// Funzione che permette di ottenere il nome del file estraendolo da un path name
	if (pathname != ""){
		posbar = pathname.lastIndexOf("\\");
		if (posbar == -1){
			posbar = pathname.lastIndexOf("/");
		}		
	
		if (posbar == -1){
			return pathname;
		}
		else {
			return pathname.substring(posbar+1);
		}
	}
	return pathname;
}

// 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'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 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

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

