/*

1.1
---
2007-06-20 Aggiunta StrDateTime()
2007-06-20 Aggiunto msg['input_error']

*/

msg = new Array();
msg['obbligatorio'] = "Campo Obbligatorio: ";
msg['input_error'] = "Immissione Errata: ";
msg['confirm_save'] = "Confermi SALVATAGGIO del record?";
msg['confirm_cancel'] = "ANNULLARE le modifiche?";
msg['confirm_del'] = "Confermi la CANCELLAZIONE DEFINITIVA del Record?";
msg['confirm_del_all'] = "Confermi la CANCELLAZIONE DEFINITIVA di TUTTI I 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 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 ;
}
// Converte una stringa data in un oggetto data
function StrDateTime( str_date_time )
{
	str_date_time = str_date_time || '';
	var partOfDateTime = str_date_time.split(" ");
	var partOfDate = partOfDateTime[0].split("/");
	var partOfTime = partOfDateTime[1].split(":");	
	var d = partOfDate[0];
	var m = partOfDate[1] - 1;
	var y = partOfDate[2];
	var hh = partOfTime[0];
	var mm = partOfTime[1];
	var ss = partOfTime[2];	
	var objDate = new Date(y,m,d,hh,mm,ss);
	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 (!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="";
	}
}



