// ***********
// WR Function
// ***********


// verifica che la data inserita sia corretta
function verData(gg,mm,yyyy)
	{ 
				
		//usato perchè il metodo getMonth lavora con indice da 0 a 11
		mm = mm-1;     
		var test = new Date(yyyy,mm,gg);

		//evito il problema del millennium bug
		var anno = "19"+test.getYear();
		if (anno.length>4) anno=anno.substring(2,anno.length);
	
		if ( (yyyy == anno) && (mm == test.getMonth()) && (gg == test.getDate()) )
			return true;      
		else
			alert('Si prega di verificare le date inserite!'); 
			return false;
	} 


// Value to be used as decimal separator
var decimalSep = ",";

// Variable for "password" type fields.
var pw_len;

// "String Response" Function for the checkField function
// lang = 0 :  italian ,  lang = 1 :  english
function respStri(lang, index)
	{
	if (index == 0)				// X select
		{
		if (lang == 0)
			return ": non è stato selezionato!";
		else if (lang == 1)
			return ": was not selected!";
		}
	else if (index == 1)		// X tutti tranne select, password e confirm
		{
		if (lang == 0)
			return ": non è stato inserito alcun valore!";
		else if (lang == 1)
			return ": can't be empty!";
		}
	else if (index == 2)		// X date
		{
		if (lang == 0)
			return ": il formato del campo non è valido! Inserire un valore del tipo [gg/mm/aaaa].";
		else if (lang == 1)
			return ": it has not a valid format! Insert a value like [dd/mm/yyyy].";
		}
	else if (index == 3)		// X number
		{
		if (lang == 0)
			return ": il valore non puo essere negativo!";
		else if (lang == 1)
			return ": the value can't be negative!";
		}
	else if (index == 4)		// X number
		{
		if (lang == 0)
			return ": il valore non puo essere decimale!";
		else if (lang == 1)
			return ": the value can't be decimal!";
		}
	else if (index == 5)		// X number
		{
		if (lang == 0)
			return ": il formato del campo non è valido! Inserire un numero valido.";
		else if (lang == 1)
			return ": it has not a valid format! Insert a valid number.";
		}
	else if (index == 6)		// X text
		{
		if (lang == 0)
			return ": il campo contiene caratteri non validi! Caratteri accettati: [a-z],[A-Z],[0-9],[_ , . \' & < > \" = ( ) - \\ /],[spazio].";
		else if (lang == 1)
			return ": the field has non valid character! Accepted characted: [a-z],[A-Z],[0-9],[_ , . \' & < > \" = ( ) - \\ /],[blankspace].";
		}
	else if (index == 7)		// X name
		{
		if (lang == 0)
			return ": il campo contiene caratteri non validi! Caratteri accettati: [a-z],[A-Z],[0-9],[_ , . \'],[spazio].";
		else if (lang == 1)
			return ": the field has non valid character! Accepted characted: [a-z],[A-Z],[0-9],[_ , . \'],[blankspace].";
		}
	else if (index == 8)		// X email
		{
		if (lang == 0)
			return ": formato non valido! Formato accettato: xxx@xxx.xxx Caratteri accettati: [a-z],[A-Z],[0-9],[_ - . @].";
		else if (lang == 1)
			return ": invalid format! Accepted format: xxx@xxx.xxx Accepted characted: [a-z],[A-Z],[0-9],[_ - . @].";
		}
	else if (index == 9)		// X web
		{
		if (lang == 0)
			return ": il campo contiene caratteri non validi! Caratteri accettati: [a-z],[A-Z],[0-9],[_ - . / \\ : ~ % ( )],[spazio].";
		else if (lang == 1)
			return ": the field has non valid character! Accepted characted: [a-z],[A-Z],[0-9],[_ - . / \\ : ~ % ( )],[blankspace].";
		}
	else if (index == 10)		// X tel
		{
		if (lang == 0)
			return ": il campo contiene caratteri non validi! Caratteri accettati: [0-9],[+ - / \\ .],[spazio].";
		else if (lang == 1)
			return ": the field has non valid character! Accepted characted: [0-9],[+ - / \\ .],[blankspace].";
		}
	else if (index == 11)		// X text_only
		{
		if (lang == 0)
			return ": il campo contiene caratteri non validi! Caratteri accettati: [a-z],[A-Z].";
		else if (lang == 1)
			return ": the field has non valid character! Accepted characted: [a-z],[A-Z].";
		}
	else if (index == 12)		// X time
		{
		if (lang == 0)
			return ": il formato del campo non è valido! Inserire un valore del tipo [hh:mm].";
		else if (lang == 1)
			return ": it has not a valid format! Insert a value like [hh:mm].";
		}
	else if (index == 13)		// X alfanum
		{
		if (lang == 0)
			return ": il formato del campo non è valido! Inserire un valore di tipo alfanumerico [a-z],[A-Z],[0-9].";
		else if (lang == 1)
			return ": it has not a valid format! Insert an alphanumeric value [a-z],[A-Z],[0-9].";
		}
	else if (index == 14)		// X password
		{
		if (lang == 0)
			return ": la password deve essere di almeno " + pw_len + " caratteri.";
		else if (lang == 1)
			return ": password length must be over " + pw_len + " characters.";
		}
	else if (index == 15)		// X confirm
		{
		if (lang == 0)
			return ": le password non sono uguali.";
		else if (lang == 1)
			return ": the passwords are not equal.";
		}
	}

// ************************************************************************
// Function to check the field of a Form.
// Return FALSE if error, TRUE if ok.
// For any kind of field there are some options.
//
// Field Type	Field Description			Options		Description
// ------------------------------------------------------------------------
//  select		Select field				req			selectedIndex <> 0
//											noa			no alert
//
//  date		Date field in format:		req			required
//				dd/mm/yyyy					noa			no alert
//
//  number		Number field for value		req			required
//				integer, decimal and		dec			decimal
//				negative					neg			negative
//											noa			no alert
//
//  text		Text field that accept		req			required
//				only the character:			noa			no alert
//				[a-z], [A-Z], [0-9],
//				[_ , . ' & < > " = ( )],
//				[- \ /], [spazio]
//
//  name		Text field that accept		req			required
//				only the character:			noa			no alert
//				[a-z], [A-Z], [0-9],
//				[_ , . '], [spazio]
//
//  email		Text field that accept		req			required
//				only the character:			noa			no alert
//				[a-z], [A-Z], [0-9],
//				[_ - . @] with the foramt
//				xxx@xxx.xxx
//
//  web			Text field that accept		req			required
//				only the character:			noa			no alert
//				[a-z], [A-Z], [0-9],
//				[_ - . / \ : ~ % ( )],
//				[spazio]
//
//  tel			Text field that accept		req			required
//				only the character:			noa			no alert
//				[0-9], [+ - / \ .], [spazio]
//
//  text_only	Text field that accept		req			required
//				only the character:			noa			no alert
//				[a-z], [A-Z]
//
//  time		Time field in format:		req			required
//				hh:mm						noa			no alert
//
//  alfanum		Text field that accept		req			required
//				only alfanumeric char.:		noa			no alert
//				[a-z], [A-Z], [0-9]
//
//  all    		Text field that accept		req			required
//				any character				noa			no alert
//
//  password	Text field that accept		pw_len		number that
//				only string longer than the				specify the
//				specified length						string length
//											noa			no alert
//
//  confirm		Text field that accept		fld_cmp		input text
//				a value equal to the					field object
//				field specified							to compare
//											noa			no alert
//
// Usage:
//		checkField('Prezzo', theFrom.prezzo, 'number', 'reqdec', 'it')
//		checkField('Confirm Pw', theFrom.pw2, 'confirm', theFrom.pw1, 'en')
// ************************************************************************

function checkField(fieldName, theField, fieldType, options, language)
	{
	var correct = true;
	var lang;
	var fieldVal, rExpr, ris_rExpr;
	var noalert;
	
	// Flag for the alert popup
	if (fieldType != "password" && fieldType != "confirm")
		{
		if (options.indexOf("noa") != -1)
			noalert = true;
		else
			noalert = false;
		}
	
	if (language == "it")
		lang = 0;
	else if (language == "en")
		lang = 1;
	else
		{
		alert('Error: language parameter is wrong!')
		return false;
		}
	
	if (fieldType == "select")
		{
		if (theField.options(0).selected)
			{
			if (options.indexOf("req") != -1)
				{
				if (!noalert)
					{
					alert(fieldName + respStri(lang,0));
					//theField.focus();
					}
				correct = false;
				}
			}
		}
	
	if (fieldType == "date")
		{
		fieldVal = theField.value;
		if (fieldVal.length > 0)
			{
			// Regular expression that match a string like:
			//   dd/mm/yyyy or dd/mm/yy
			rExpr = /^(\d{1,2})\/(\d{1,2})\/(\d{2,4})$/;
			ris_rExpr = fieldVal.match(rExpr);
			//Day check (ris_rExpr[1] - captured from regular expression)
			//It must be a valure between 1 and 31
			//Month check (ris_rExpr[2] - captured from regular expression)
			//It must be a valure between 1 and 12
			correct = (ris_rExpr != null) && (ris_rExpr[1] >= 1) && (ris_rExpr[1] <= 31) && (ris_rExpr[2] >= 1) && (ris_rExpr[2] <= 12);
			// If Year is under 100 then that means that his real value is 20xx
			if (!correct)
				{
				// Regular expression that match a string like:
				//   ddmmyyyy or ddmmyy
				rExpr = /^(\d{1,2})(\d{1,2})(\d{2,4})$/;
				ris_rExpr = fieldVal.match(rExpr);
				//Day check (ris_rExpr[1] - captured from regular expression)
				//It must be a valure between 1 and 31
				//Month check (ris_rExpr[2] - captured from regular expression)
				//It must be a valure between 1 and 12
				correct = (ris_rExpr != null) && (ris_rExpr[1] >= 1) && (ris_rExpr[1] <= 31) && (ris_rExpr[2] >= 1) && (ris_rExpr[2] <= 12);
				// If Year is under 100 then that means that his real value is 20xx
				if (!correct)
					{
					if (!noalert)
						{
						alert(fieldName + respStri(lang,2));
						//theField.focus();
						}
					}
				else
					{
					if (ris_rExpr[3] < 100)
						theField.value=ris_rExpr[1] + '/' + ris_rExpr[2] + '/20' + ris_rExpr[3];
					else
						theField.value=ris_rExpr[1] + '/' + ris_rExpr[2] + '/' + ris_rExpr[3];
					}
				}
			else
				if (ris_rExpr[3] < 100)
					theField.value=ris_rExpr[1] + '/' + ris_rExpr[2] + '/20' + ris_rExpr[3];
			}
		else
			{
			if (options.indexOf("req") != -1)
				{
				if (!noalert)
					{
					alert(fieldName + respStri(lang,1));
					//theField.focus();
					}
				correct = false;
				}
			}
		}
	
	if (fieldType == "number")
		{
		fieldVal = theField.value;
		if (fieldVal.length > 0)
			{
			// Regular expression that match a string like:
			if (decimalSep == ".")
			//   [-]nnn[.nnn]
				rExpr = /^(-?)\d+(\.\d+)?$/;
			else if (decimalSep == ",")
			//   [-]nnn[,nnn]
				rExpr = /^(-?)\d+(\,\d+)?$/;
			ris_rExpr = fieldVal.match(rExpr)
			correct = (ris_rExpr != null);
			if (!correct)
				{
				if (!noalert)
					{
					alert(fieldName + respStri(lang,5));
					//theField.focus();
					}
				}
			else
				{
				//Negative check (ris_rExpr[1] - captured from regular expression)
				//It must be a positive value if "neg" isn't in the
				//option parameter
				if (options.indexOf("neg") == -1 && ris_rExpr[1] == "-")
					{
					if (!noalert)
						{
						alert(fieldName + respStri(lang,3));
						//theField.focus();
						}
					correct = false;
					}
				//Decimal check (ris_rExpr[2] - captured from regular expression)
				//It must be an integer value if "dec" isn't in the
				//option parameter
				if (options.indexOf("dec") == -1 && ris_rExpr[2] != "")
					{
					if (!noalert)
						{
						alert(fieldName + respStri(lang,4));
						//theField.focus();
						}
					correct = false;
					}
				}
			}
		else
			{
			if (options.indexOf("req") != -1)
				{
				if (!noalert)
					{
					alert(fieldName + respStri(lang,1));
					//theField.focus();
					}
				correct = false;
				}
			}
		}
	
	if (fieldType == "text")
		{
		fieldVal = theField.value;
		if (fieldVal.length > 0)
			{
			// Regular expression that match a string with
			// the following character: [a-z], [A-Z],
			// [0-9], [_ , . ' & < > " = ( ) - \ /], [spazio]
			rExpr = /^(\w|[,.'&<>"=()-\\/ ])+$/i;
			ris_rExpr = fieldVal.match(rExpr);
			correct = (ris_rExpr != null);
			if (!correct)
				{
				if (!noalert)
					{
					alert(fieldName + respStri(lang,6));
					//theField.focus();
					}
				}
			}
		else
			{
			if (options.indexOf("req") != -1)
				{
				if (!noalert)
					{
					alert(fieldName + respStri(lang,1));
					//theField.focus();
					}
				correct = false;
				}
			}
		}
	
	if (fieldType == "name")
		{
		fieldVal = theField.value;
		if (fieldVal.length > 0)
			{
			// Regular expression that match a string with
			// the following character: [a-z], [A-Z],
			// [0-9], [_ , . '], [spazio]
			rExpr = /^(\w|[,.' ])+$/i;
			ris_rExpr = fieldVal.match(rExpr);
			correct = (ris_rExpr != null);
			if (!correct)
				{
				if (!noalert)
					{
					alert(fieldName + respStri(lang,7));
					//theField.focus();
					}
				}
			}
		else
			{
			if (options.indexOf("req") != -1)
				{
				if (!noalert)
					{
					alert(fieldName + respStri(lang,1));
					//theField.focus();
					}
				correct = false;
				}
			}
		}
	
	if (fieldType == "email")
		{
		fieldVal = theField.value;
		if (fieldVal.length > 0)
			{
			// Regular expression that match a string with
			// the following character: [a-z], [A-Z],
			// [0-9], [_ - . @] and the format xxx@xxx.xxx
			//               xxx       @         xxx     .     [xx|xxx]
			rExpr = /^\w(\w|\.|-)*\w\@\w(\w|\.|-)*\w\.[a-z]{2,4}$/i;
			ris_rExpr = fieldVal.match(rExpr);
			correct = (ris_rExpr != null);
			if (!correct)
				{
				if (!noalert)
					{
					alert(fieldName + respStri(lang,8));
					//theField.focus();
					}
				}
			}
		else
			{
			if (options.indexOf("req") != -1)
				{
				if (!noalert)
					{
					alert(fieldName + respStri(lang,1));
					//theField.focus();
					}
				correct = false;
				}
			}
		}
	
	if (fieldType == "web")
		{
		fieldVal = theField.value;
		if (fieldVal.length > 0)
			{
			// Regular expression that match a string with
			// the following character: [a-z], [A-Z],
			// [0-9], [_ - . / \ : ~ % ( )], [spazio]
			rExpr = /^(\w|[-./\\:~ %\(\)])+$/i;
			ris_rExpr = fieldVal.match(rExpr);
			correct = (ris_rExpr != null);
			if (!correct)
				{
				if (!noalert)
					{
					alert(fieldName + respStri(lang,9));
					//theField.focus();
					}
				}
			}
		else
			{
			if (options.indexOf("req") != -1)
				{
				if (!noalert)
					{
					alert(fieldName + respStri(lang,1));
					//theField.focus();
					}
				correct = false;
				}
			}
		}
	
	if (fieldType == "tel")
		{
		fieldVal = theField.value;
		if (fieldVal.length > 0)
			{
			// Regular expression that match a string with
			// the following character: [0-9], [- / \ .], [spazio]
			rExpr = /^\+?(\d|[-/\\. ])+$/i;
			ris_rExpr = fieldVal.match(rExpr);
			correct = (ris_rExpr != null);
			if (!correct)
				{
				if (!noalert)
					{
					alert(fieldName + respStri(lang,10));
					//theField.focus();
					}
				}
			}
		else
			{
			if (options.indexOf("req") != -1)
				{
				if (!noalert)
					{
					alert(fieldName + respStri(lang,1));
					//theField.focus();
					}
				correct = false;
				}
			}
		}
	
	if (fieldType == "text_only")
		{
		fieldVal = theField.value;
		if (fieldVal.length > 0)
			{
			// Regular expression that match a string with
			// the following character: [a-z], [A-Z]
			rExpr = /^[a-z]+$/i;
			ris_rExpr = fieldVal.match(rExpr);
			correct = (ris_rExpr != null);
			if (!correct)
				{
				if (!noalert)
					{
					alert(fieldName + respStri(lang,11));
					//theField.focus();
					}
				}
			}
		else
			{
			if (options.indexOf("req") != -1)
				{
				if (!noalert)
					{
					alert(fieldName + respStri(lang,1));
					//theField.focus();
					}
				correct = false;
				}
			}
		}
	
	if (fieldType == "time")
		{
		fieldVal = theField.value;
		if (fieldVal.length > 0)
			{
			// Regular expression that match a string like:
			//   hh:mm
			rExpr = /^(\d{1,2}):(\d{1,2})$/;
			ris_rExpr = fieldVal.match(rExpr);
			//Hours check (ris_rExpr[1] - captured from regular expression)
			//It must be a valure between 0 and 23
			//Minutes check (ris_rExpr[2] - captured from regular expression)
			//It must be a valure between 0 and 59
			correct = (ris_rExpr != null) && (ris_rExpr[1] >= 0) && (ris_rExpr[1] <= 23) && (ris_rExpr[2] >= 0) && (ris_rExpr[2] <= 59);
			if (!correct)
				{
				rExpr = /^(\d{1,2})$/;
				ris_rExpr = fieldVal.match(rExpr);
				//Hours check (ris_rExpr[1] - captured from regular expression)
				//It must be a valure between 0 and 23
				correct = (ris_rExpr != null) && (ris_rExpr[1] >= 0) && (ris_rExpr[1] <= 23);
				if (!correct)
					{
					if (!noalert)
						{
						alert(fieldName + respStri(lang,12));
						//theField.focus();
						}
					}
				else
					theField.value=ris_rExpr[1] + ':00';
				}
			}
		else
			{
			if (options.indexOf("req") != -1)
				{
				if (!noalert)
					{
					alert(fieldName + respStri(lang,1));
					//theField.focus();
					}
				correct = false;
				}
			}
		}
	
	if (fieldType == "alfanum")
		{
		fieldVal = theField.value;
		if (fieldVal.length > 0)
			{
			// Regular expression that match a string with
			// the following character: [a-z], [A-Z], [0-9]
			rExpr = /^(\w)+$/i;
			ris_rExpr = fieldVal.match(rExpr);
			correct = (ris_rExpr != null);
			if (!correct)
				{
				if (!noalert)
					{
					alert(fieldName + respStri(lang,13));
					//theField.focus();
					}
				}
			}
		else
			{
			if (options.indexOf("req") != -1)
				{
				if (!noalert)
					{
					alert(fieldName + respStri(lang,1));
					//theField.focus();
					}
				correct = false;
				}
			}
		}
	
	if (fieldType == "all")
		{
		fieldVal = theField.value;
		if (fieldVal.length == 0)
			{
			if (options.indexOf("req") != -1)
				{
				if (!noalert)
					{
					alert(fieldName + respStri(lang,1));
					//theField.focus();
					}
				correct = false;
				}
			}
		}
	
	if (fieldType == "password")
		{
		fieldVal = theField.value;
		if (fieldVal.length < options)
			{
			pw_len = options;
			alert(fieldName + respStri(lang,14));
			//theField.focus();
			correct = false;
			}
		}
	
	if (fieldType == "confirm")
		{
		fieldVal = theField.value;
		if (fieldVal != options.value)
			{
			alert(fieldName + respStri(lang,15));
			//theField.focus();
			correct = false;
			}
		}
	
	return correct;
	}