Messaggio_Risposta = "";
Decimali = 2;
// ******************************************************************************
//  Funzione di controllo validità della estensione di un file selezionato in un campo file
// ******************************************************************************
function ValidaEstensioneFile(str) {
	var campo = str.value;
	var estensione_file="";
	var posizione_ultimo_punto=0;
	var lunghezza_stringa=0;

	lunghezza_stringa=campo.length;
	posizione_ultimo_punto=campo.lastIndexOf(".");
	estensione_file=campo.substring(posizione_ultimo_punto+1,lunghezza_stringa);
	estensione_file=estensione_file.toLowerCase();
		
	if ((estensione_file!="gif")&&(estensione_file!="jpg")&&(estensione_file!="jpeg")&&(estensione_file!="txt")){
		Messaggio_Risposta = "Tipo di file non valido.\r\nI tipi accettati sono quelli con estensione .gif .jpg .jpeg .txt";
		return false;
	}
}
// ******************************************************************************
//  Funzione di controllo validità del nome di un file selezionato in un campo file
// ******************************************************************************
function ValidaNomeFile(str) {
	var campo = str.value;
	var nome_file="";
	var posizione_ultimo_slash=0;
	var lunghezza_stringa=0;

	lunghezza_stringa=campo.length;
	posizione_ultimo_slash=campo.lastIndexOf("\\");
	nome_file=campo.substring(posizione_ultimo_slash+1,lunghezza_stringa);
		
    if (campo.match(/[òèéùàìùç@'"£$%&]/)){ 
		Messaggio_Risposta = "Nome del file non valido."
		return false;
	}
}
// ******************************************************************************
//  Funzione di controllo validità del nome di un file inserito in un campo testo
// ******************************************************************************
function ValidaNome(str) {
	var campo = str.value;	
    if (campo.match(/[òèéùàìùç@'"£$%&]/))
		{ 
		Messaggio_Risposta = "Nome non valido."
		return false;
		};
	};
// ******************************************************************************
//  Funzione di controllo validità dei campi data 
// ******************************************************************************
function ValidaData(campo){
//	var dt=document.frmSample.txtDate
	var dt=campo
	if (isDate(dt.value)==false){
		dt.focus()
		return false
	}
    return true
 }
// ******************************************************************************
function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Controllo che il carattere sia numerico
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // Tutti i caratteri sono numerici
    return true;
}
// ******************************************************************************
function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Verifica i caratteri uno ad uno
    // Se il carattere è ok lo appende alla stringa di ritorno
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}
// ******************************************************************************
function daysInFebruary (year){
	// Controllo su 29 febbraio 
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
// ******************************************************************************
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}
// ******************************************************************************
function isDate(dtStr){
	var dtCh= "/";
	var MinAnno=1890;
	var MaxAnno=2050;
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		Messaggio_Risposta = "Il formato della data è il seguente : gg/mm/aaaa";
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		Messaggio_Risposta = "Digitare un mese valido";
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		Messaggio_Risposta = "Digitare un giorno valido";
		return false
	}
	if (strYear.length != 4 || year==0 || year<MinAnno || year>MaxAnno){
		Messaggio_Risposta = "Digitare una anno compreso fra "+MinAnno+" e "+MaxAnno;
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		Messaggio_Risposta = "Digitare una data valida";
		return false
	}
	return true
}
// ******************************************************************************
//  Funzione di controllo validità dei campi numerici 
// ******************************************************************************
function ValidaNumero(str,chkdec) {
	var decimali = 2;
	var campo = str.value;	
	var Virgola = ",";
    if (chkdec == 1) { var patternvalido = "^[0-9\,]+$"; };
    if (chkdec == 0) { var patternvalido = "^[0-9]+$"; };
    var reg = new RegExp(patternvalido);
    if (campo.match(reg) == null ){
		if (chkdec == 0) { Messaggio_Risposta = "Digitare un valore numerico intero." };
		if (chkdec == 1) { Messaggio_Risposta = "Digitare un valore numerico." };
		return false
	}
	if (chkdec == 1) {	
		var pos1=campo.indexOf(Virgola)
		pos1++
		if (pos1 > 0) {
			// controlla il numero dei decimali
			var lunghezza = campo.length
			var diff = lunghezza - pos1
			if (diff > decimali) {
					Messaggio_Risposta = "Il numero massimo dei decimali è "+decimali
					return false
			}	
		}
	}	
}
// ******************************************************************************
//  Funzione di controllo validità dei campi numerici #2
// ******************************************************************************
function ValidaNumero2(str,chkdec) {
	var decimali = 2;
	var campo = str.value;	
	var Virgola = ",";
	var segno = "-";
	if (chkdec == 1) { var patternvalido = "^[0-9\,\-]+$"; };
	if (chkdec == 0) { var patternvalido = "^[0-9]+$"; };
	var reg = new RegExp(patternvalido);
	if (campo.match(reg) == null ){
		if (chkdec == 0) { 
			Messaggio_Risposta = "Digitare un valore numerico intero." 
			}
		if (chkdec == 1) {
			Messaggio_Risposta = "Digitare un valore numerico." 
		}
		return false
	}
		
	if (chkdec == 1) {	
		var pos1=campo.indexOf(Virgola)
		pos1++
		if (pos1 > 0) {
			// controlla il numero dei decimali
			var lunghezza = campo.length
			var diff = lunghezza - pos1
			if (diff > decimali) {
					Messaggio_Risposta = "Il numero massimo dei decimali è "+decimali;
					return false
			}
		}
	}
	
	if (chkdec == 1) {	
		var pos2=campo.indexOf(segno)
		pos2++
		if (pos2 > 1) {
					Messaggio_Risposta = "Digitare un valore numerico.";
					return false
		}
	}
}
				
// ******************************************************************************
//  Funzione di controllo validità della Partita IVA
// ******************************************************************************
function ValidaPIva(str)
{
	pi = str.value
	if (pi == "00000000000") {
		Messaggio_Risposta = "Digitare una partita IVA valida"
		return false
		};	
	var lunghezza = pi.length
	if (lunghezza != 11) {
		Messaggio_Risposta = "La lunghezza della partita IVA è di 11 cifre"
		return false
		};	
	validi = "0123456789";
	for( i = 0; i < 11; i++ ) {
		if( validi.indexOf( pi.charAt(i) ) == -1 ) {
			Messaggio_Risposta = "La partita IVA deve essere composta esclusivamente di caratteri numerici" 
			return false
		};
	}
	s = 0;
	for( i = 0; i <= 9; i += 2 )
		s += pi.charCodeAt(i) - '0'.charCodeAt(0);
	for( i = 1; i <= 9; i += 2 ) {
		c = 2*( pi.charCodeAt(i) - '0'.charCodeAt(0) );
		if( c > 9 )  c = c - 9;
		s += c;
	}
	if( ( 10 - s%10 )%10 != pi.charCodeAt(10) - '0'.charCodeAt(0) ) {
		Messaggio_Risposta = "La partita IVA non è valida" 
		return false
		};
};
// ******************************************************************************
//  Funzione di controllo validità e-mail
// ******************************************************************************
function ValidaEmail(str) {
	// Espressione regolare dell'email
	email_reg_exp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/;
	if (!email_reg_exp.test(str.value)) {
	   Messaggio_Risposta = "Inserire un indirizzo email corretto. Es.: nome@dominio.it";
	   return false;
	}
}
// ******************************************************************************
//  Funzione di controllo validità nome sito
// ******************************************************************************
function ValidaSito(str) {
	sito_reg_exp = /^(www)+\.(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/;
	if (!sito_reg_exp.test(str.value)) {
	   Messaggio_Risposta = "Inserire un nome sito corretto. Es.: www.dominio.it";
	   return false;
	}
}
// ******************************************************************************
//  Funzione di controllo validità pagina web - http://
// ******************************************************************************
function Validahttp(str) {
if ((str.value == "") || (str.value == "undefined") || (str.value.indexOf("http://") == (-1))) {
   Messaggio_Risposta = "Il campo Homepage non può essere privo di http://";
   return false;
   }
}
// ******************************************************************************
//  Funzione di controllo validità del Codice Fiscale
// ******************************************************************************
function ValidaCodiceFiscale(str)
{
	var validi, i, s, set1, set2, setpari, setdisp;
	cf = str.value
	cf = cf.toUpperCase();
	if( cf.length != 16 ) {
		Messaggio_Risposta = "La lunghezza del codice fiscale è di 16 caratteri"
		return false
		}
	validi = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
	for( i = 0; i < 16; i++ ){
		if( validi.indexOf( cf.charAt(i) ) == -1 ) {
			Messaggio_Risposta = "Il codice fiscale contiene un carattere non valido `" +
				cf.charAt(i) +
				"'.\nI caratteri validi sono le lettere e le cifre.\n";
			return false
			};
	};
	set1 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	set2 = "ABCDEFGHIJABCDEFGHIJKLMNOPQRSTUVWXYZ";
	setpari = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	setdisp = "BAKPLCQDREVOSFTGUHMINJWZYX";
	s = 0;
	for( i = 1; i <= 13; i += 2 )
		s += setpari.indexOf( set2.charAt( set1.indexOf( cf.charAt(i) )));
	for( i = 0; i <= 14; i += 2 )
		s += setdisp.indexOf( set2.charAt( set1.indexOf( cf.charAt(i) )));
	if( s%26 != cf.charCodeAt(15)-'A'.charCodeAt(0) ) {
		Messaggio_Risposta = "Il codice fiscale non è corretto"
		return false
		}
}
// ******************************************************************************
//  Funzione di controllo validità dell'orario
// ******************************************************************************
function ValidaOrario(str)
{
	// Controlla l'orario nel formato HH:MM:SS AM/PM 
	// La forma AM/PM è opzionale
	var timeStr = str.value
	var timePat = /^(\d{1,2}):(\d{2})(:(\d{2}))?(\s?(AM|am|PM|pm))?$/;
	var matchArray = timeStr.match(timePat);
	if (matchArray == null) {
		Messaggio_Risposta = "L'orario non è in un formato valido";
		return false;
	}
	hour = matchArray[1];
	minute = matchArray[2];
	second = matchArray[4];
	if (second=="") { second = 0; }
	if (hour < 0  || hour > 23) {
		Messaggio_Risposta = "L'ora deve essere compresa fra 0 e 23";
		return false;
	}
	if (minute<0 || minute > 59) {
		Messaggio_Risposta = "I minuti devono essere compresi fra 0 e 59";
		return false;
	}
	if (second != null && (second < 0 || second > 59)) {
		Messaggio_Risposta = "I secondi devono essere compresi fra 0 e 59";
		return false;
	}
}
