

msg = new Array();
msg['obbligatorio'] = "Campo Obbligatorio: ";
msg['confirm_save'] = "Confermi SALVATAGGIO del record?";
msg['confirm_cancel'] = "ANNULLARE le modifiche?";
msg['confirm_del'] = "Confermi la CANCELLAZIONE DEFINITIVA del Record?";
msg['view_all_result'] = "Vedi tutti i risultati";
msg['format_err_valuta'] = 'Formato non corretto, usare: 9999999,99';
msg['format_err_cf'] = 'Codice Fiscale errato';
msg['format_err_pi'] = 'Partita IVA errata';
msg['format_err_email'] = 'Email errata';




// Funzione che filtra i tasti premuti negli input
// va impostata sull'evento onKeyDown e richiamata con return ForceKey(event,"filtro")
// spazio		(32)
// tab			(9)
// invio		(13)
// canc			(46)
// shift		(16)
// caps-lock	(20)
// up/down/left/right (37|38|39|40)
// ,			(188)
// -			(109|189)
// '			(219|222)
// /			(46|111)
// 0-9			(48-57)
// A-Z			(65-90)
// a-z			(97-122)
function ForceKey(e,TypeFilter,Alert)
{	// se alert è true invio messaggi di errore
	Alert = Alert || false;
	var charCode = (navigator.appName == "Netscape") ? e.which : e.keyCode;
	var Filter = TypeFilter.toUpperCase();
	// variabile che verifica se ci sono errori
	var keyOut = false ;
	switch (Filter)
	{	// o 96!105 o 97|122
		case '':
			return true;
		case 'NUMERI':
			if ( !( charCode<=33 || (charCode>=48 && charCode<=57) || (charCode>=96 && charCode<=105) ) || charCode==32 ) {
				if (Alert) alert ("Attenzione puoi inserire solo i caratteri\n1234567890");
				return false;
			} else {
				return true;
			}
		case 'VALUTA':
			if ( !( charCode<=47 || (charCode>=48 && charCode<=57) || (charCode>=96 && charCode<=105) || charCode==188 ) || charCode==32) {
				if (Alert) alert ("Attenzione puoi inserire solo i caratteri\n1234567890 e la , per i decimali");
				return false;
			} else {
				return true;
			}
		case 'TEL':
			if ( !( charCode<=47 || charCode==109 || charCode==189 || (charCode>=48 && charCode<=57) || (charCode>=96 && charCode<=105) ) || charCode==32) {
				if (Alert) alert ("Attenzione il formato di un n.ro di telefonico deve essere\n####-#######");
				return false;
			} else {
				return true;
			}
		case 'TESTO':
			if ( !( charCode<=47 || (charCode>=65 && charCode<=90) || (charCode>=97 && charCode<=122) || (charCode>=96 && charCode<=105) ) ) {
				if (Alert) alert ("Attenzione puoi inserire solo testo");
				return false;
			} else {
				return true;
			}
		case 'TESTO_NUMERI':
			if ( !( charCode<=47 || (charCode>=48 && charCode<=57) || (charCode>=65 && charCode<=90) || (charCode>=97 && charCode<=122) || (charCode>=96 && charCode<=105) ) ) {
				if (Alert) alert ("Attenzione puoi inserire solo caratteri alfanumerici");
				return false;
			} else {
				return true;
			}
		case 'DATA': 
			if ( !( charCode<=47 || (charCode>=48 && charCode<=57) || (charCode>=96 && charCode<=105) || charCode==111 ) || charCode==32) {
				if (Alert) alert ("Attenzione il formato della data deve essere\ngg/mm/aaaa");
				return false;
			} else {
				return true;
			}
		case 'TEST':
			alert(charCode);
			break;
		default:
			alert ("ForceKey: FiltroKey '"+Filter+"' non riconosciuto");
			break;
	}
	// pressione di tasto non consentito
	if (keyOut) {
		return false;
	} else {
		return true;
	}
}
// Da impostare all'oblur, trasforma il contenuto della text nello standard x la valuta
function format_valuta( oText )
{
	oText.value = valuta(oText.value);
	if (!IsEuroImport(oText.value))
	{
		alert(msg['format_err_valuta']);
		oText.focus();
	}
}
// Trasforma il testo nel formato standard valuta	
function valuta(price) 
{
   string = "" + price ;
   if (string=="") {
		return '0,00' ;		
   } else {
		number = string.length - string.indexOf(',');
		if (string.indexOf(',') == -1)
		  return string + ',00';
		if (number == 1)
		  return string + '00';
		if (number == 2)
		  return string + '0';
		if (number > 3)
		  return string.substring(0,string.length-number+3);
		return string;
	}

}




// Converte una stringa data in un oggetto data
function StrDate( str_date )
{
	str_date = str_date || '';
	var partOfDate = str_date.split("/");
	var d = partOfDate[0];
	var m = partOfDate[1] - 1;
	var y = partOfDate[2];
	var objDate = new Date(y, m, d);
	return objDate ;
}
// Visualizzazione standard della data partendo da un oggetto data
function ViewDate( obj_date )
{
	var d = obj_date.getDate().toString();
	d = (d.length==1) ? "0"+d : d;
	var m = (obj_date.getMonth()+1).toString();
	m = (m.length==1) ? "0"+m : m;
	var y = obj_date.getFullYear().toString();
	var str_date = d +'/'+ m +'/'+ y;
	return str_date;
}






// Verifica che l'importo sia nello standard 99999,99
function IsEuroImport(importo)
{
	if (importo==0) return true;
	var first_comma_sign = importo.indexOf(",");
	var last_comma_sign = importo.lastIndexOf(",");
	if (first_comma_sign != last_comma_sign || first_comma_sign <= 0) return false;
	var lmax = first_comma_sign + 3;
	if (first_comma_sign == last_comma_sign && importo.length == lmax) {
		if (navigator.appName == "Netscape") {
			var num=importo.replace(/\,/g,"").replace(/\./g,"");
			if(num.search(/[^0-9]/) != -1) return false;
		}
		return true;
	}
	else return false;
}
// stringa data in formato GG/MM/YYYY
function IsDate(str_date)
{
	var partOfDate = str_date.split("/");
	var d = partOfDate[0];
	var m = partOfDate[1] - 1;
	var y = partOfDate[2];
	var objDate = new Date(y, m, d);
	if (d != objDate.getDate() || m != objDate.getMonth() || y != objDate.getFullYear()) return false;
	else {
		if (objDate.getFullYear() > 1900 && objDate.getFullYear() < 2079) return true;
		else return false;
	}
}






// Da impostare all'oblur, verifica lo standard codice fiscale
function format_cf( oText )
{
	oText.value = oText.value.toUpperCase();
	if (!IsCodiceFiscale(oText.value))
	{
		alert(msg['format_err_cf']);
		oText.focus();
	}
}
function IsCodiceFiscale(cf)
{
	cf = cf.toUpperCase() || '' ;
	var validi, i, s, set1, set2, setpari, setdisp;
	if( cf=='') return true;
	// La lunghezza del codice fiscale non è corretta: il codice fiscale dovrebbe essere lungo esattamente 16 caratteri
	if( cf.length!=16 ) return false;
	validi = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
	for( i = 0; i < 16; i++ ){
		if( validi.indexOf( cf.charAt(i) ) == -1 )
			// Trovato Carattere non vaido
			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) )));
	// Il codice fiscale non è corretto il codice di controllo non corrisponde	
	if( s%26 != cf.charCodeAt(15)-'A'.charCodeAt(0) ) return false
	return true;
}







// Da impostare all'oblur, verifica lo standard codice fiscale
function format_piva( oText )
{
	if (!IsPartitaIVA(oText.value))
	{
		alert(msg['format_err_pi']);
		oText.focus();
	}
}
function IsPartitaIVA(pi)
{	
	pi = pi || '';
	if( pi=='' ) return true;
	// La lunghezza della partita IVA non è corretta: la partita IVA dovrebbe essere lunga esattamente 11 caratteri
	if( pi.length != 11 ) return false;
	validi = "0123456789";
	for( i = 0; i < 11; i++ ){
		if( validi.indexOf( pi.charAt(i) ) == -1 )
			// La partita IVA contiene un carattere non valido I caratteri validi sono le cifre
			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;
	}
	// La partita IVA non è valida il codice di controllo non corrisponde
	if( ( 10 - s%10 )%10 != pi.charCodeAt(10) - '0'.charCodeAt(0) ) return false;
	return true;
}












// Da impostare all'oblur, verifica lo standard email
function format_email( oText )
{
	oText.value = oText.value.toLowerCase();
	if (oText.value=="") return true;
	if (!IsEmail(oText.value))
	{
		alert(msg['format_err_email']);
		oText.focus();
	}
}
function IsEmail(e_mail)
{
	function check_invalid_char(e_mail) {
		if( ( e_mail.search(/[^a-z,A-Z,0-9,\x22,\x23,\x24,\x25,\x26,\x27,\x2A,\x2D,\x2E,\x3C,\x3E,\x40,\x5F,\x7E]/) ) != -1 ) return false;
		else return true;
	}

	function check_sign(e_mail) {
		var first_at_sign = e_mail.indexOf("@");
		var last_at_sign = e_mail.lastIndexOf("@");

		if ( last_at_sign == -1 ) return false;

		var last_dot_sign = e_mail.lastIndexOf(".");
		if ( (first_at_sign == last_at_sign ) && ( first_at_sign > 0 ) && ( last_at_sign < (e_mail.length - 3) ) && ( last_dot_sign > (first_at_sign + 1) ) && ( last_dot_sign < (e_mail.length - 1) ) ) return true;
		else return false;
	}

	if (check_invalid_char(e_mail) && check_sign(e_mail) ) return true;
	else return false;
}




// Visualizza una text quando la select viene valorizzata
function display_text_on_select_value( oSelect, text_id )
{
	oText = oID(text_id);
	if (oSelect.value)
	{
		oText.style.display = "inline";
		oText.focus();
	}
	else
	{
		oText.style.display = "none";
		oText.value="";
	}
}


/*


// verfica che il singolo valore immesso sia un numero oppure una virgola (per l'EURO)
// onkeyPress="return IsEuro(event)
function IsEuro(e)
{
	if (!checkEuro(e)) {
		alert("Sono consentiti solo valori numerici e la virgola come separatore di decimali.");
		return false;
	}
	else return true;
}
// onkeyPress="return IsNumero(event)"
function IsNumero(e)
{
	if (!checkInt(e)) {
		alert("Sono ammessi solo valori numerici.");
		return false;
	} else return true;
}




// verfica che l'importo in Euro immesso sia nel formato standard 9999,99
function IsEuroImport(importo)
{
	if (importo==0) return true;
	var first_comma_sign = importo.indexOf(",");
	var last_comma_sign = importo.lastIndexOf(",");
	if (first_comma_sign != last_comma_sign || first_comma_sign <= 0) return false;
	var lmax = first_comma_sign + 3;
	if (first_comma_sign == last_comma_sign && importo.length == lmax) {
		if (navigator.appName == "Netscape") {
			var num=importo.replace(/\,/g,"").replace(/\./g,"");
			if(num.search(/[^0-9]/) != -1) return false;
		}
		return true;
	}
	else return false;
}

// verfica che l'importo in Valuta immesso sia nel formato standard 9999,999
function IsValutaImport(importo)
{
	if (importo==0) return true;
	var first_comma_sign = importo.indexOf(",");
	var last_comma_sign = importo.lastIndexOf(",");
	if (first_comma_sign != last_comma_sign || first_comma_sign <= 0) return false;
	var lmax = first_comma_sign + 4;
	if (first_comma_sign == last_comma_sign && importo.length == lmax) {
		if (navigator.appName == "Netscape") {
			var num=importo.replace(/\,/g,"").replace(/\./g,"");
			if(num.search(/[^0-9]/) != -1) return false;
		}
		return true;
	}
	else return false;
}


// verifica che il numero immesso abbia i decimali indicati dal parametro
function IsDecimal(value,NumFloat)
{
	if (value==0) return true;
	var charCode = (navigator.appName == "Netscape") ? e.which : e.keyCode;
	var first_comma_sign = value.indexOf(",");
	var last_comma_sign = value.lastIndexOf(",");
	if (first_comma_sign != last_comma_sign || first_comma_sign <= 0) return false;
	var lmax = first_comma_sign + NumFloat + 1;
	if (first_comma_sign == last_comma_sign && value.length == lmax) {
		if (navigator.appName == "Netscape") {
			var num=value.replace(/\,/g,"").replace(/\./g,"");
			if(num.search(/[^0-9]/) != -1) return false;
		}
		return true;
	}
	else return false;
}


// true = tutto bene: non ci sono caratteri di controllo Host
function CheckHostCode(stringa)
{
	var charCode = 0;
	var IsOK = false;
	var i = 0;
	if (stringa.length == 0 ) return true;
	while (1) {
		charCode = stringa.charCodeAt(i);
		IsOK =  ((charCode == 10 || charCode == 13) || ((charCode != 47) && (charCode != 92) && (charCode != 58) && (charCode != 39) && (charCode != 176) && (charCode >= 32 && charCode <= 127)));
		if (! IsOK || ++i >=  stringa.length) return IsOK;
	}
}


// verfica che il singolo valore immesso sia un numero oppure una virgola (per l'EURO)
function checkEuro(e)
{
	var charCode = (navigator.appName == "Netscape") ? e.which : e.keyCode;
	//status = charCode // see ASCII character value!
	if (charCode > 31 && (charCode < 48 || charCode > 57) && charCode != 44) return false;
	else return true;
}

// verfica che il singolo valore immesso sia un numero
function checkInt(e)
{
	var charCode = (navigator.appName == "Netscape") ? e.which : e.keyCode;
	//status = charCode // see ASCII character value!
	if (charCode > 31 && (charCode < 48 || charCode > 57)) return false;
	else return true;
}

// verfica che il singolo valore immesso sia una lettera o invio
function checkLetter(e)
{
	var charCode = (navigator.appName == "Netscape") ? e.which : e.keyCode;
	//status = charCode // see ASCII character value!
	if ( (charCode >= 65 && charCode <=90) || (charCode >= 97 && charCode <=122) || charCode == 13 ) return true;
	else return false;
}

// verfica che il singolo valore immesso sia un numero o una lettera 
function checkIntandLetter(e)
{
	var charCode = (navigator.appName == "Netscape") ? e.which : e.keyCode;
	//status = charCode // see ASCII character value!
	if ((charCode >= 65 && charCode <=90) || (charCode >= 48 && charCode <=57) || (charCode >= 97 && charCode <=122)) return true;
	else return false;
}

// verfica che la stinga immessa sia composta di lettere
function IsAlfa(stringa)
{
	var charcode ;
	for (var i = 0; i < stringa.length; i++) {
		charcode = stringa.charCodeAt(i);
		if ( !( (charcode >= 65 && charcode <=90) || (charcode >= 97 && charcode <=122) ) ) return false;
	}
	return true;
}

// verifica che la stringa immessa sia un numero
function isNumber(inputStr)
{ 
	for (var i = 0; i < inputStr.length; i++) {
		var oneChar = inputStr.charAt(i);
		if (oneChar < "0" || oneChar > "9") return false;
	}
	return true;
}

// verifica che la stringa immessa sia alfanumerica
function IsAlfaNum(stringa)
{
	var check_comma = stringa.indexOf(",");
	var check_dot = stringa.indexOf(".");
	if (check_comma!=-1 || check_dot!=-1) return false;
	if(stringa.search(/[^A-Z,0-9]/) != -1) return false;
	else return true;
}

// verifica la validità di una stringa email
function IsEmail(e_mail)
{
	function check_invalid_char(e_mail) {
		if( ( e_mail.search(/[^a-z,A-Z,0-9,\x22,\x23,\x24,\x25,\x26,\x27,\x2A,\x2D,\x2E,\x3C,\x3E,\x40,\x5F,\x7E]/) ) != -1 ) return false;
		else return true;
	}

	function check_sign(e_mail) {
		var first_at_sign = e_mail.indexOf("@");
		var last_at_sign = e_mail.lastIndexOf("@");

		if ( last_at_sign == -1 ) return false;

		var last_dot_sign = e_mail.lastIndexOf(".");
		if ( (first_at_sign == last_at_sign ) && ( first_at_sign > 0 ) && ( last_at_sign < (e_mail.length - 3) ) && ( last_dot_sign > (first_at_sign + 1) ) && ( last_dot_sign < (e_mail.length - 1) ) ) return true;
		else return false;
	}

	if (check_invalid_char(e_mail) && check_sign(e_mail) ) return true;
	else return false;
}

// verifica che strDate1 sia inferiore di strDate2
function OrderDate(strDate1,strDate2)
{
	var partOfDate = strDate1.split("/");
	var objDate1 = new Date(partOfDate[2], partOfDate[1] - 1, partOfDate[0]);
	var partOfDate = strDate2.split("/");
	var objDate2 = new Date(partOfDate[2], partOfDate[1] - 1, partOfDate[0]);

	if (objDate1 <= objDate2) return true;
	else return false;
}

// conversione al carattere maiuscolo
function upperMe(field)
{
	if (field.value != field.value.toUpperCase()) field.value = field.value.toUpperCase();
}

// elimina gli spazi a destra e a sinistra
function trim(strString)
{
	var retStr = strString;
	while (retStr.substring(0,1)==" ")
		retStr = retStr.substring(1,retStr.length);
	while (retStr.substring(retStr.length-1,retStr.length)==" ")
		retStr = retStr.substring(0,retStr.length-1);
	return retStr;
}

// Lire to Euro
function Lit2Euro(ImportoLit)
{
	var fltPart = eval(ImportoLit / 1936.27);
	if (navigator.appName == "Netscape") {
		if (ImportoLit < 1936.27) fltPart = "0" + fltPart;
	}
	var intPart = parseInt(fltPart,10);
	var fractPart = Math.round((fltPart - intPart) * 100);
	if(fractPart<10) fractPart = "0" + fractPart;
	if(fractPart==100) {
		fractPart = 0;
		intPart += 1;
	}
	var strPart = intPart + "." + fractPart;
	return new Number (strPart);
}

// Euro To Lire
function Euro2Lit(ImportoEur)
{
	var ImportoLit = parseInt (1936.27 * parseFloat(ImportoEur));
	return new Number (ImportoLit);
}

// Formattazione Euro
function EuroFormat(ImportoEuro)
{
	if (navigator.appName == "Netscape") {
		if (ImportoEuro<1) var intPart=parseInt(0);
		else var intPart=parseInt(ImportoEuro,10);
	}
	else var intPart = parseInt(ImportoEuro,10);
	var fractPart = Math.round((ImportoEuro - intPart) * 100);
	if(fractPart<10) fractPart = "0" + fractPart;
	if(fractPart==100) {
		fractPart = 0;
		intPart += 1;
	}
	var intPartStr = "";
	var intPartTmp = new String(intPart);
	var j = 0;
	for(i=intPartTmp.length;i>0;i--) {
		intPartStr = intPartTmp.substring(i-1,i) + intPartStr;
		if(j==2 && i > 1) {
			intPartStr = "." + intPartStr;
			j = 0;
		}
		else j++;
	}
	return intPartStr + "," + fractPart;
}

// Formattazione Valuta
function ValutaFormat(ImportoEuro)
{
	if (navigator.appName == "Netscape") {
		if (ImportoEuro<1) var intPart=parseInt(0);
		else var intPart=parseInt(ImportoEuro,10);
	}
	else var intPart = parseInt(ImportoEuro,10);
	var fractPart = Math.round((ImportoEuro - intPart) * 1000);
		
	if(fractPart<10) fractPart = "00" + fractPart;
	else if	(fractPart<100) fractPart = "0" + fractPart;
	
	if(fractPart==100) {
		fractPart = 0;
		intPart += 1;
	}
	var intPartStr = "";
	var intPartTmp = new String(intPart);
	var j = 0;
	for(i=intPartTmp.length;i>0;i--) {
		intPartStr = intPartTmp.substring(i-1,i) + intPartStr;
		if(j==2 && i > 1) {
			intPartStr = "." + intPartStr;
			j = 0;
		}
		else j++;
	}
	return intPartStr + "," + fractPart;
}

// Formattazione Lire
function LitFormat(ImportoLit)
{
	var intPart = parseInt(ImportoLit,10);
	var intPartStr = "";
	var intPartTmp = new String(intPart);
	var j = 0;
	for(i=intPartTmp.length;i>0;i--) {
		intPartStr = intPartTmp.substring(i-1,i) + intPartStr;
		if(j==2 && i > 1) {
			intPartStr = "." + intPartStr;
			j = 0;
		}
		else j++;
	}
	return intPartStr;
}

function getYear(d)
{
	var yyyy = d.getYear();
	if(yyyy < 100) yyyy = "19" + yyyy;
	if(yyyy >= 100 && yyyy < 1000) yyyy = 1900 + yyyy;
	return yyyy;
}
function getMonth(d)
{
	var mm = d.getMonth() + 1;
	if(mm < 10) mm = "0" + mm;
	return mm;
}
function getDay(d)
{
	var dd = d.getDate();
	if(dd < 10) dd = "0" + dd;
	return dd;
}


// verfica che il singolo valore immesso sia Enter
function checkEnter(e)
{
	var charCode = (navigator.appName == "Netscape") ? e.which : e.keyCode;
	// status = charCode // see ASCII character value!
	if (charCode != 13) return false;
	else return true;
}







function UserAllowedChars(stringValue)
{
	for (var i = 0; i < stringValue.length; i++) {
		var c = stringValue.charCodeAt(i);
		if (c!=32 && c!=34 && c!=38 && c!=39 && (c<44 || c>57) && (c<65 || c>90) && (c<97 || c>122))
			return false;
	}
	return true;
}
//Verifica dei primi due caratteri digitati della targa digitati dall'utente
function IsTarga(targa)
{
	if (!IsAlfa(targa.substring(0,2))) {
		alert("Attenzione. Il numero di targa inserito non è corretto");
		return false;
	}
	if (targa.length != 7 && targa.length != 8) {
		alert("Attenzione. Il numero di targa inserito non è corretto");
		return false;
	}
	return true;
}

/*
Ritorna true se la stringa passata è composta solo dai seguenti caratteri:
spazio		(32)
"			(34)
&			(38)
'			(39)
,			(44)
-			(45)
.			(46)
/			(47)
0-9			(48-57)
A-Z			(65-90)
a-z			(97-122)
*/
