// FUNZIONI VALIDAZIONE LATO CLIENT (REGULAR EXPRESSION)
//il campo che deve essere validato necessita di un attributo
 //'validator' come TAG HTML

var PatternsDict = new Object();

PatternsDict.zipPat = /^\d{5}$/;
PatternsDict.zipPat.testo = "Il campo deve contenere 5 cifre";

PatternsDict.currencyPat = /^\$\d{1,3}(,\d{3})*\.\d{2}$/;
// matches $17.23 or $14,281,545.45 or ...

PatternsDict.DatePat=/^(0[1-9]|1[0-9]|2[0-9]|3[0-1])\/(0[1-9]|1[0-2])\/\d{4}$/;
PatternsDict.DatePat.testo = "Data errata";

PatternsDict.timePat=/^([0-9]|0[0-9]|1[0-9]|2[0:3]):([0-9]|0[0-9]|1[0-9]|2[0-9]|3[0-9]|4[0-9]|5[0-9])$/;
PatternsDict.timePat.testo = "Il campo deve essere nel formato hh:mm";

PatternsDict.anzianitaPat=/^(|[0-9]AA|1[0-9]AA|2[0-9]AA|3[0-9]AA)(| | [0-9]MM| 1[0-1]MM|[0-9]MM|1[0-1]MM)$/;
PatternsDict.anzianitaPat.testo = "Il campo deve essere nel formato [0-39]AA [0-11]MM (č possibile specificare solo AA o solo MM)";

PatternsDict.eMailPat = /[\w+]+[\@][\w]+([\w]|[\-])+[\.]+[\w]/;
PatternsDict.eMailPat.testo = "Il campo deve essere nel formato:'xxx@xxx.xxx'";

PatternsDict.PercentPat=/^(0[0-100])\/\d{4}$/;
PatternsDict.PercentPat.testo = "Percentuale non valida!";

PatternsDict.CampoNumerico =/^[0-9]+$/;
PatternsDict.CampoNumerico.testo = "Il campo deve contenere solo numeri.";

PatternsDict.Telefono = /^[\+]{0,1}([\d]+[\-\.\s\/\\]{0,1})+$/;
PatternsDict.Telefono.testo = "Il campo deve contenere solo numeri.";

PatternsDict.Contratto = /^[\w]+([^\\^\/^\:^\*^\?^\"^\<^\>^\|]+$)/;
PatternsDict.Contratto.testo = "Il campo non puņ contenere i caratteri /\:*?|";

//funzione di validazione da chiamare nell'evento onChange del text field

function validateSingleForm(campo)
{  
	var v = document.forma[campo].validator;     //  validator
	var c = document.forma[campo].nomeCampo;     //  nomeVisualizzato
	var val = document.forma[campo].value;       //  valore campo
	var txt = PatternsDict[v].testo;
	var thePat = PatternsDict[v];

	var gotIt = thePat.exec(val);
	if (!val.length==0)
	{
		if(!gotIt)
		{
			document.forma[campo].focus();
			document.forma[campo].select();
			alert(c + ": " + txt);

			//if (v == "DatePat")
				document.forma[campo].value = "";

			return false;
		}
		if (v == "DatePat")
			if (!(checkData(val)))
			{
				document.forma[campo].focus();
				return false;
			}
	}
	return true;
}


// stessa funzione con loop dei campi del form da chiamare nel SUBMIT

function validateForm(theForm)
{      // return true if all is well
	var elArr = theForm.elements;       // get all elements of the form into array
	for(var i = 0; i < elArr.length; i++)
		with(elArr[i])
		{                    // for each element of the form...
			var v = elArr[i].validator;       // get validator, if any
			var c = elArr[i].nomeCampo;       // get validator, if any
			if(!v)
				continue;                  // no validator property, skip
			var thePat = PatternsDict[v];     // select the validating regular expr
			var txt = PatternsDict[v].testo;
			var val = elArr[i].value;
			var gotIt = thePat.exec(val);   // run it on value of elArr[i]

			if (!elArr[i].value.length==0)
			{
				if(!gotIt)
				{
					//alert(name + ": errore nella digitazione " + v + " to " + value); return false;
					elArr[i].focus();
					alert(c + ": " + txt);
					return false;
				}
				if (v == "DatePat")
					if (!(checkData(elArr[i].value)))
					{
						elArr[i].focus();
						return false;
					}
			}
		}
	return true;
}


function checkData(MyDate)
{
//	var f=document.FORMA;
	
//	var GIORNO = f.RII_GIORNO_DI_NASCITA.value;
//	var MESE = f.RII_MESE_DI_NASCITA.value;
//	var ANNO = f.RII_ANNO_DI_NASCITA.value;
	var GIORNO = MyDate.substr(0,2);
	var MESE = MyDate.substr(3,2);
	var ANNO = MyDate.substr(6,4);

//	alert("'" + MyDate + "' " + GIORNO + " " + MESE + " " + ANNO);
	
	//controlla mesi
	if ((MESE==4 || MESE==6 || MESE==9 || MESE==11) && (GIORNO > 30 || GIORNO < 1))
	{
		alert("Data inesistente!");
		return false;
	}
	
	//controlla febbraio
	if (MESE==2)
	{
	if (LeapYear(ANNO)==true)
		{
		if (GIORNO > 29)
			{
			alert("giorno errato!");
			return false;
			}
		}
		else 
		{
			if (GIORNO > 28)
			{
			alert("giorno errato!");
			return false;
			}
		}
	}
		return true;
}

function LeapYear(intYear) 
{
	if (intYear % 100 == 0) 
	{
	if (intYear % 400 == 0) 
		{ 
		return true; 
		}
	}
	else {
	if ((intYear % 4) == 0) 
	{ return true; }
	}
	return false;
}

