//
// Variabili globali
//
var SQLKeyWords = new Array('SELECT', 'INSERT INTO', 'UPDATE', 'DELETE', 'TRUNCATE', 'DROP');
var BadChars = "<>#\"";
var Numbers = "0123456789.,-";

////////////////////////////////////////////////////////////////////////////////
/////////////////FUNZIONE DEFINITIVA PER LE FORM////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
function ValidateInputs(nform, form_campi, form_nomi, form_notnull, form_numeric, form_filterSQL, form_filterBad)
{	 
	var color = "#C1C4DC";
	var previous_color = "#FFFFFF";
	//
	//
	//
	for(i=0; i<form_campi.length; i++)
	{		
		campo = document.forms[nform].elements[form_campi[i]];
		valore = campo.value;
		campo.style.backgroundColor = previous_color;
		//
		// Se l'array form_notnull contiene un 1 allora non deve essere nullo il suo contenuto
		//
		if(form_notnull[i] == 1 && valore == "")
		{
			campo.style.backgroundColor = color;
			alert("Errore, il campo '" + form_nomi[i] + "' deve essere compilato.", 1);
			campo.focus();
			return;
		}
		//
		// Se l'array form_isnumeric contiene un 1 allora deve contenere solo numeri
		//
		if(form_numeric[i] == 1)
		{
			var errore = 0;			
			for (j=0; j<=valore.length-1; j++) 
			{
				if (Numbers.indexOf(valore.charAt(j)) == -1) 
				{
					errore = 1;
				}
			}
			if (errore)
			{
				campo.style.backgroundColor = color;					
				alert("Errore, il campo '" + form_nomi[i] + "' deve contenere solo numeri.", 1);
				campo.focus();				
				return;
			}
		}		
		//
		// Se l'array form_filterSQL contiene un 1 allora non deve contenere parole chiave SQL
		//
		if(form_filterSQL[i] == 1)
		{
			var errore = 0;			
			for (j=0; j<SQLKeyWords.length; j++) 
			{
				keyword = SQLKeyWords[j];
				valoretmp = valore.toUpperCase();
				if(valoretmp.search(keyword) > -1)
				{
					errore = 1; 
					break;
				}
			}
			if (errore)
			{
				campo.style.backgroundColor = color;		
				messaggio = "Error, field '" + form_nomi[i] + "' must not contains SQL keyword:";
				for (j=0; j<=SQLKeyWords.length-1; j++)
					messaggio += "\n  -  '" + SQLKeyWords[j] + "'";
				alert(messaggio,1);
				campo.focus();				
				return;
			}
		}
		//
		// Filtra i caratteri cattivi
		//
		if(form_filterBad[i] == 1)
		{
			var errore = 0;			
			for (j=0; j<=valore.length-1; j++) 
			{
				if (BadChars.indexOf(valore.charAt(j)) != -1) 
				{
					errore = 1;
				}
			}
			if (errore)
			{
				campo.style.backgroundColor = color;		
				messaggio = "Error, field '" + form_nomi[i] + "' must not contains following characters:\n";
				for (j=0; j<=BadChars.length-1; j++)
					messaggio += BadChars.charAt(j) + " ";
				alert(messaggio,1);
				campo.focus();
				return;
			}
		}		
	}
	return(1);
}

////////////////////////////////////////////////////////////////////////////////
//function per aprire pop-up
////////////////////////////////////////////////////////////////////////////////
function apri(path,name,feat)
{
	window.open(path,name,feat);
}

////////////////////////////////////////////////////////////////////////////////
//function per cancellare 
////////////////////////////////////////////////////////////////////////////////
function cancella(file,nome)
{
	if(confirm("Sei sicuro di voler cancellare: \n\""+nome+'\"?'))
	{
		document.location.href = file;
	}
	return;
}

////////////////////////////////////////////////////////////////////////////////	
//function per controllo campi delle form
//	parametri: nome della form, array dei campi da controllare,
//  array dei nomi dei campi da visualizzare
////////////////////////////////////////////////////////////////////////////////
// SOSTITUITO DALLA FUNZIONE VALIDATEINPUTS
/*
function invia(nform, campi, ncampi, numerici, nnumerici, date, ndate)
{
//	try
//	{
		var color = "#C1C4DC";
		var previous_color = "#CEDCEC";
		//
		// controllo che siano compilatiti tutti gli input inseriti nel vettore 'campi'
		//
		for(i=0; i<campi.length; i++)
		{
			campo = eval("document." + nform + "." + campi[i]);
			valore = eval("document." + nform + "." + campi[i] + ".value");
			campo.style.backgroundColor = previous_color;				
			if(valore == "")
			{
				campo.style.backgroundColor = color;
				campo.focus();
				alert("Attenzione, il campo '" + ncampi[i] + "' non è stato compilato");
				return;
			}
		}
		//
		// controllo che tutti gli input inseriti nel vettore 'numerici' contengano solo numeri
		//
		if(numerici && nnumerici)
		{		
			for(j=0; j<numerici.length; j++)
			{
				campo = eval("document." + nform + "." + numerici[j]);
				TheNumber = eval("document." + nform + "." + numerici[j] + ".value");			
				var valid = 1;
				var GoodChars = "0123456789";
				for (i =0; i <= TheNumber.length-1; i++) 
				{
					if (GoodChars.indexOf(TheNumber.charAt(i)) == -1) 
					{
						valid = 0;
					}
				}
				if (valid==0)
				{
					campo.style.backgroundColor = color;		
					campo.focus();				
					alert("Attenzione: Il campo '" + nnumerici[j] + "' deve contenere solo numeri.");
					return;
				}
			}	
		}
		//
		// controllo che gli input specificati contengano delle date
		//
		if(date && ndate)
		{			
			for(j=0; j<date.length; j++)
			{
				campo = eval("document." + nform + "." + date[j]);
				valore = eval("document." + nform + "." + date[j] + ".value");			
				dPart = valore.split('-');
				if(dPart.length==3)
				{
					if ( (!(dPart[0]>0 && dPart[0]<=31)) || (!(dPart[1]>0 && dPart[1]<=12)) || (!(dPart[2]>1900 && dPart[2]<2010)) )
					{
						campo.style.backgroundColor = color;		
						campo.focus();				
						alert("Attenzione: Il campo '" + ndate[j] + "' deve contenere una data nel formato gg-mm-aaaa.");
						return;
					}
				}
				else 
				{
					campo.style.backgroundColor = color;		
					campo.focus();				
					alert("Attenzione: Il campo '" + ndate[j] + "' deve contenere una data nel formato gg-mm-aaaa.");
					return;
				}
			}		
		}
		invia = eval("document." + nform);
		invia.submit();
//	}
//	catch(e)
//	{
//		alert("Exception:" + e.Message);
//	}
}	

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
function ValidateInputs(nform, campi, ncampi, numerici, nnumerici, date, ndate)
{
	var color = "#C1C4DC";
	var previous_color = "#CEDCEC";
	//
	// controllo che siano compilatiti tutti gli input inseriti nel vettore 'campi'
	//
	for(i=0; i<campi.length; i++)
	{
		campo = eval("document." + nform + "." + campi[i]);
		valore = eval("document." + nform + "." + campi[i] + ".value");
		campo.style.backgroundColor = previous_color;				
		if(valore == "")
		{
			campo.style.backgroundColor = color;
			campo.focus();
			alert("Attenzione, il campo '" + ncampi[i] + "' non è stato compilato");
			return;
		}
	}
	//
	// controllo che tutti gli input inseriti nel vettore 'numerici' contengano solo numeri
	//
	if(numerici && nnumerici)
	{		
		for(j=0; j<numerici.length; j++)
		{
			campo = eval("document." + nform + "." + numerici[j]);
			TheNumber = eval("document." + nform + "." + numerici[j] + ".value");			
			var valid = 1;
			var GoodChars = "0123456789";
			for (i =0; i <= TheNumber.length-1; i++) 
			{
				if (GoodChars.indexOf(TheNumber.charAt(i)) == -1) 
				{
					valid = 0;
				}
			}
			if (valid==0)
			{
				campo.style.backgroundColor = color;		
				campo.focus();				
				alert("Attenzione: Il campo '" + nnumerici[j] + "' deve contenere solo numeri.");
				return;
			}
		}	
	}
	//
	// controllo che gli input specificati contengano delle date
	//
	if(date && ndate)
	{			
		for(j=0; j<date.length; j++)
		{
			campo = eval("document." + nform + "." + date[j]);
			valore = eval("document." + nform + "." + date[j] + ".value");			
			dPart = valore.split('-');
			if(dPart.length==3)
			{
				if ( (!(dPart[0]>0 && dPart[0]<=31)) || (!(dPart[1]>0 && dPart[1]<=12)) || (!(dPart[2]>1900 && dPart[2]<2010)) )
				{
					campo.style.backgroundColor = color;		
					campo.focus();				
					alert("Attenzione: Il campo '" + ndate[j] + "' deve contenere una data nel formato gg-mm-aaaa.");
					return;
				}
			}
			else 
			{
				campo.style.backgroundColor = color;		
				campo.focus();				
				alert("Attenzione: Il campo '" + ndate[j] + "' deve contenere una data nel formato gg-mm-aaaa.");
				return;
			}
		}		
	}
	return(1);
}
*/

