//==========================================================================================================
//=== TRIM
//==========================================================================================================
//---  LeftTrim: elimina tutti gli spazi a sinistra della stringa ------------------------------------------
function LeftTrim(theString)
{
	var RE;
	RE = /^\s+/gi; // tutti gli spazi, a partire dall'inizio della stringa
	return (theString.replace(RE, ""));
}
//---  RightTrim: elimina tutti gli spazi a destra della stringa -------------------------------------------
function RightTrim(theString)
{	var RE;

	RE = /\s+$/gi; // tutti gli spazi, a partire dalla fine della stringa
	return (theString.replace(RE, ""));
}
//---  Trim: elimina tutti gli spazi a sinistra e a destra della stringa -----------------------------------
function Trim(theString)
{
	theString = LeftTrim(theString);
	theString = RightTrim(theString);

	return (theString);
}

//==========================================================================================================
//=== Le funzioni di controllo hanno parametri di ingresso dati di tipo stringa; restituiscono:
//=== 	 true: se il controllo ha avuto esito positivo
//===    false: se il controllo ha avuto esito negativo
//==========================================================================================================
//---  IsStringUndefined: stringa non valorizzata ----------------------------------------------------------
function IsStringUndefined(theString)
{
	//alert("IsStringUndefined")
	return false;
}
//---  IsStringBlank: stringa contenente solo spazi --------------------------------------------------------
function IsStringBlank(theString)
{
	//alert("IsStringBlank")
	//... Blank ............................................
	if ((Trim(theString)) == "") return true;
	// ... Else ............................................
	return false;
}
//---  IsStringEmpty: stringa "vuota" ----------------------------------------------------------------------
function IsStringEmpty(theString)
{
	//alert("IsStringEmpty")
	//... Undefined ........................................
	if (IsStringUndefined(theString)) return true;
	//... Blank ............................................
	if (IsStringBlank(theString)) return true;
	// ... Else ............................................
	return false;
}

//==========================================================================================================
//---  IsNumberInteger: numero intero ----------------------------------------------------------------------
function IsNumberInteger(theNumber)
{
	var RE;
	var matchArray;

	theNumber += ""; // cast a stringa
	RE = /-?\d+/; // sequenza di un numero qualsiasi di cifre, eventualmente precedute da "-"
	matchArray = theNumber.match(RE);
	//--- Se il numero ?diverso dalla sequenza estratta
	if ((matchArray == null) || (theNumber != matchArray[0])) return false;
	//--- Else
	return true;
}

//==========================================================================================================
//---  ValidatorNumerico(object): input solo determinati caratteri -----------------------------------------
function ValidatorNumber(object) {
 // Se window.event.returnValue ?non definito oppure ?false non si applica il validator
 // per non sovrascrivere l'azione di un eventuale validator scatenato prima di quello attuale
 if (window.event.returnValue == "undefined" || window.event.returnValue == false) return;
 
 // Il tasto Enter in caso di textbox multiline deve essere sempre consentito
 if (window.event.keyCode == 13) return;
 
 var values = "0123456789";
 if (values.indexOf(String.fromCharCode(window.event.keyCode)) == -1)
  window.event.returnValue = false;
}

//--------------------------------------------------------------------------------------------------
//--- function ValidatorLetterale(object)
function ValidatorLetterale(object) {
	// Se window.event.returnValue ?non definito oppure ?false non si applica il validator
	// per non sovrascrivere l'azione di un eventuale validator scatenato prima di quello attuale
 	if (window.event.returnValue == "undefined" || window.event.returnValue == false) return;
 
 	// Il tasto Enter in caso di textbox multiline deve essere sempre consentito
 	if (window.event.keyCode == 13) return;
	
	var values = "abcdefghijklmnopqrstuvwyxzABCDEFGHIJKLMNOPQRSTUVWYXZ";
	if (values.indexOf(String.fromCharCode(window.event.keyCode)) == -1)
		window.event.returnValue = false;
}

//--------------------------------------------------------------------------------------------------
//--- function ValidatorLetterale(object)
function ValidatorLetteraleNumeric(object) {
	// Se window.event.returnValue ?non definito oppure ?false non si applica il validator
	// per non sovrascrivere l'azione di un eventuale validator scatenato prima di quello attuale
 	if (window.event.returnValue == "undefined" || window.event.returnValue == false) return;
 
 	// Il tasto Enter in caso di textbox multiline deve essere sempre consentito
 	if (window.event.keyCode == 13) return;
	
	var values = "abcdefghijklmnopqrstuvwyxzABCDEFGHIJKLMNOPQRSTUVWYXZ0123456789";
	if (values.indexOf(String.fromCharCode(window.event.keyCode)) == -1)
		window.event.returnValue = false;
}

//==========================================================================================================
//--- LenMax(Value, Len): Funzione interna per la lunghezza massima [AF, SS, 16 June 2003]
function LenMax(Value, Len)
{
	if ((!(Value == "")) && (!(Value.length <= Len)))
	{
		return (0);
	}
	return (1);
}

//==========================================================================================================
//---  checkMail(TextBox): controlla campo e-mail ----------------------------------------------------------
function checkMail(TextBox)
{
	return(controllaMail(TextBox, true));
}

//--- ControllaMail(TextBox,avviso): Funzione e-mail [AF, SS, 16 June 2003]
function controllaMail(TextBox,avviso)
{
	apos=TextBox.indexOf("@");
	dotpos=TextBox.lastIndexOf(".");
	lastpos=TextBox.length-1;

	if (TextBox.value == "")
		return(1);
		if (!(LenMax(TextBox, 50))) {
		if (avviso=true) alert("Inserire al pi?50 caratteri nel campo.");
		//TextBox.focus();
		return 0;
	}
	if (apos<1 || dotpos-apos<2 || lastpos-dotpos>4 || lastpos-dotpos<2) {
		if (avviso=true) alert('Inserire un indirizzo e-mail valido');
		//TextBox.focus();
		return(0);
	}
	return 1;
}

function IsDateEmpty(dd, mm, yyyy)
{	
	//... Data vuota....................................................
	if ((dd == 0) && (mm == 0) && (IsStringEmpty(yyyy)))  return true;
	//... Else .........................................................
	return false;
}
//---  IsYearNotValid: anno non di quattro cifre
function IsYearNotValid(yyyy)
{	
	if (yyyy.search(/\d{4}/) == -1) return true; 
	//... Else ..........................................................
	return false;
}

//==========================================================================================================
//---  IsDateNotValid(dd, mm, yyyy): controlla campo e-mail ------------------------------------------------
function IsDateNotValid(dd, mm, yyyy)
{ 
 //... Data vuota: data valida ........................................
 if ((dd == 0) && (mm == 0) && (IsStringEmpty(yyyy)))  return false;
 //... Data incompleta: data non valida ...............................
 if ((dd == 0) || (mm == 0) || (IsStringEmpty(yyyy)))  return true;
 //... Anno non corretto: non valida ..................................
 if (IsYearNotValid(yyyy)) return true;
 //... Data non corretta (es. 31 giugno, 29 febbraio di anno non bisestile...): data non valida .............................
 var testDate = new Date(yyyy, mm-1, dd)
 if (!((yyyy==testDate.getFullYear()) && (mm==(testDate.getMonth()+1)) && (dd==testDate.getDate()))) return true;
 //... Else ..........................................................
 return false;
}

//==========================================================================================================
//--- IsDateGreaterThan: prima data pi?recente della seconda ----------------------------------------------
function IsDateGreaterThan(dd_1, mm_1, yyyy_1, dd_2, mm_2, yyyy_2)
{ 
 var date_1 = new Date(yyyy_1, mm_1-1, dd_1);
 var date_2 = new Date(yyyy_2, mm_2-1, dd_2);
 
 date_1 = date_1.getTime();
 date_2 = date_2.getTime();
 
 if (date_1 > date_2) return true;
 //... Else ..........................................................
 return false;
}


//--- PopUpStatistiche (documento): Apertura Pop Up statistiche [UG, MG, 21 July 2005]
function PopUpStatistiche(documento) {
	var customWindow
	customWindow = window.open(documento, "popUpStatistiche", "toolbar=no, scrollbars= yes, location=no, directories=no, status=no, menubar=no, resizable=no, width=800, height=400");
	customWindow.moveTo(100,150);
	customWindow.focus();
}

//--- checkNumAccessi(theForm): Controllo per form ultimi accessi statistiche [UG, MG, 21 July 2005]
function checkNumAccessi(theForm) {
	if (theForm.numAccessi.value== "") {
		alert("Compilare il campo.");
		theForm.numAccessi.focus();
		return;
	}
	
	if ( !(IsNumberInteger(theForm.numAccessi.value)) ) {
		alert ("E' necessario inserire un valore numerico.");
		theForm.numAccessi.value = '';
		theForm.numAccessi.focus();
		return;
	}
		
	theForm.action = "statistiche.asp?Idstep=1";
	theForm.submit();
}

//----------------------------------------------------
//--- checkNewsletter(TheForm) @fg(11598)
function checkNewsletter(TheForm)
{
	if(TheForm.txtNome.value.length<2)
	{
		alert("Inserire un nome valido");
		TheForm.txtNome.focus();
		return true;
	}
	if(TheForm.txtCognome.value.length<2)
	{
		alert("Inserire un cognome valido");
		TheForm.txtCognome.focus();
		return true;
	}
	if ((TheForm.txtEmail.value==null)||(TheForm.txtEmail.value==""))
	{
		alert("Inserire un email corretta");
		TheForm.txtEmail.focus();
		return true;
	}
	if (controllaMail(TheForm.txtEmail.value, true)==false)
	{
		TheForm.txtEmail.value="";
		TheForm.txtEmail.focus();
		return true;
	}
	if(!TheForm.rdbPrivacy[0].checked)
	{
		alert("Devi accettare le informative sulla privacy");
		TheForm.rdbPrivacy[0].focus();
		return true;
	}
	if (confirm("Attenzione: \n\nSei sicuro di voler procedere?"))
	{
		TheForm.submit();
	}
}
