
/************************************************************************************************************************************************
 *										FormValidator                                                                                                             *
 * V 1.0.0 25/09/2002 By Bit                                                                                                                    *
 * V 1.0.1 14/11/2006 By Bit                                                                                                                    *
 * V 2.0.0 22/03/2007 By Bit                                                                                                                    *
 *                                                                                                                                              *
 *	Se 'ChkGroup' e' != da "" allora valida il gruppo applicando 'ChkGroupMode'                                                                 *
 *                                      ChkType                                                                                                 *
 *  1  Non vuoto                                                                                                                                *
 *		Se 'Param1' e' != da "" allora vale come minimo di caratteri da accettare                                                                 *
 *		Se 'Param2' e' != da "" allora vale come massimo di caratteri da accettare                                                                *
 *		Se 'Param3' e' != da "" allora indica i caratteri da accettare                                                                            *
 *	2 Solo numerico range min indicato in 'Param1' range max indicato in 'Param2'                                                               *
 *	3 Solo numericoIntero range min indicato in 'Param1' range max indicato in 'Param2'                                                         *
 *	4 PopUp first item not selectable                                                                                                           *
 *  5 Radio chk                                                                                                                                 *
 *  6 ChkBox chk                                                                                                                                *
 *  7 indica le estensioni da abilitare (.jpg .mov)                                                                                             *
 *  8 Piva chk                                                                                                                                  *
 *  9 CodFis chk                                                                                                                                *
 * 10 E-mail chk                                                                                                                                *
 * 11 Date chk                                                                                                                                  *
 *                                                                                                                                              *
 * FieldProp = {                                                                                                                                *
 *			FieldID:"xxxx",					// Id del campo da controllare.                                                                                 *
 *			Enabled:xxxx,						// se false il campo non passa dal controllo automatico.                                                        *
 *			ChkType:xxxx,						// Tipo di controllo da fare (1 - 12).                                                                          *
 *			Params:["xxxx","xxxx",],	// Parametri utente.                                                                                          *
 *      RealTypeChk:xxx					// Indica se fare un chk al change true, false                                                                  *
 *      ErrorDysplayMode:xxx		// 0 non visualizza il messaggio di errore, 1 Lo visualizza al realchk, 2 lo visualizza al real chk e alsubmit. *
 *			ErrorMex:"xxxx"					// Messaggio di errore tornato                                                                                  *
 *			ErrorTabID:"xxxx"				// ID del tab da evidenziare.                                                                                   *
 *		};                                                                                                                                        *
 *                                                                                                                                              *
 * RelationProp = {                                                                                                                             *
 *			FieldsID:["xxxx","xxxx"],	// lista di id di campi da mettere in relazione.                                                              *
 *			RelType:"xxx",						// Tipo di relazione da applicare                                                                             *
 *																	OR fa un controllo in or                                                                                    *
 *																	AND fa un controllo in and                                                                                  *
 *																	SUM somma i valori dei campi indicati, l' ultimo campo è il campo risultante                                *
 *		};                                                                                                                                        *
 ************************************************************************************************************************************************/

function	ChkFormCreateChecker	(FormId)
{
	MyElemID (FormId).ChkObj = new FormChecker (FormId);
	alert("Validazione form");
	return (MyElemID (FormId).ChkObj);
}

function	FormChecker	(FormID)
{
	this.FormRef = MyElemID (FormID);
	this.FieldList = new Array ();
	this.RelationList = new Array ();
	this.CheckFunctionList = new Array ();
	this.ChkMode = 1;	// 1 Check obbligatorio, 2 Avvisa esistenza campi non completi e chiede cosa fare.
	this.ErrorFieldClass = " FormError";
	this.ErrorTabClass = "TabFormError";
	this.OldOnSubmit = this.FormRef.onsubmit;
	
	// *** Start Register Check function ***
	
	this.CheckFunctionList[this.CheckFunctionList.length] = null;	// Dummy
	
	this.CheckFunctionList[this.CheckFunctionList.length] = this.ExecuteChk1;
	this.CheckFunctionList[this.CheckFunctionList.length] = this.ExecuteChk2;
	this.CheckFunctionList[this.CheckFunctionList.length] = this.ExecuteChk3;
	this.CheckFunctionList[this.CheckFunctionList.length] = this.ExecuteChk4;
	this.CheckFunctionList[this.CheckFunctionList.length] = this.ExecuteChk5;
	this.CheckFunctionList[this.CheckFunctionList.length] = this.ExecuteChk6;
	this.CheckFunctionList[this.CheckFunctionList.length] = this.ExecuteChk7;
	this.CheckFunctionList[this.CheckFunctionList.length] = this.ExecuteChk8;
	this.CheckFunctionList[this.CheckFunctionList.length] = this.ExecuteChk9;
	this.CheckFunctionList[this.CheckFunctionList.length] = this.ExecuteChk10;
	this.CheckFunctionList[this.CheckFunctionList.length] = this.ExecuteChk11;
	
	// *** End Register Check function ***
	
	this.FormRef.onsubmit = this.ValidateForm;
}

FormChecker.prototype.AddFieldToList = function	(FieldID,ChkType,Params,RealTypeChk,ErrorDysplayMode,ErrorMex,ErrorTabID)
{	
	this.FieldList[this.FieldList.length] = {
																						FieldID:FieldID,
																						Enabled:true,
 																						ChkType:ChkType,
  																					Params:Params,
  																					RealTypeChk:RealTypeChk,
  																					ErrorDysplayMode:ErrorDysplayMode,
  																					ErrorMex:ErrorMex,
 																						ErrorTabID:ErrorTabID
																					};
}

FormChecker.prototype.RemoveAllFieldFromList = function	()
{
	this.FieldList = new Array ();
}

FormChecker.prototype.RemoveFieldFromList = function	(FieldID)
{
	var	NewFList = new Array ();
	
	for (var i = 0;i < this.FieldList.length;i++)
	{
		if (this.FieldList[i].FieldID != FieldID)
		{
			NewFList[NewFList.length] = this.FieldList[i];
		}
	}
	this.FieldList = NewFList;
}

FormChecker.prototype.AddRelationToList = function	(FieldsID,RelType)
{
	var	FieldIdx;
	
	this.RelationList[this.RelationList.length] = {FieldsID:FieldsID,RelType:RelType};
	for (var i = 0;i < FieldsID.length;i++)
	{
		FieldIdx = this.FieldInCheckList (FieldsID[i]);
		if (FieldIdx != false)
		{
			this.FieldList[FieldIdx].Enabled = false;
		}
	}
}

FormChecker.prototype.RemoveAllRelationFromList = function	()
{
	for (var i = 0;i < this.RelationList.length;i++)
	{
		FieldIdx = this.FieldInCheckList (this.RelationList[i].FieldsID);
		if (FieldIdx != false)
		{
			this.FieldList[FieldIdx].Enabled = true;
		}
	}
	this.RelationList = new Array ();
}

FormChecker.prototype.RemoveRelationFromList = function	(FieldID)
{
	var	NewRelationList = new Array ();
	
	for (var i = 0;i < this.RelationList.length;i++)
	{
		if (this.RelationList[i].FieldID != FieldID)
		{
			NewRelationList[NewRelationList.length] = this.RelationList[i];
		}
		else
		{
			FieldIdx = this.FieldInCheckList (FieldID);
			if (FieldIdx != false)
			{
				this.FieldList[FieldIdx].Enabled = true;
			}
		}
	}
	this.FieldList = NewRelationList;
}

FormChecker.prototype.SetChkMode = function	(Mode)
{
	this.ChkMode = Mode;
}

// *************************** Internal ***************************

FormChecker.prototype.ValidateForm = function	()
{
	var	FormChecker = this.ChkObj;	
	var	RetErrorStatus = {IsValid:true,FieldInErrorList:new Array ()};
	var	CheckInfo = null;	
	
	if (FormChecker.FieldList != null)
	{		
		// *** Validate single field ***
		for (var FormFieldCount = 0;FormFieldCount < FormChecker.FormRef.length;FormFieldCount++)
		{			
			FieldIdx = FormChecker.FieldInCheckList (FormChecker.FormRef[FormFieldCount].id);			
			if (FieldIdx !== false)
			{
				CheckInfo = FormChecker.FieldList[FieldIdx];				
				if (CheckInfo.Enabled == true)
				{
					FormChecker.ValidateField (CheckInfo,RetErrorStatus);
				}
			}
		}
		
		// *** Applay relations ***
		
		for (var i = 0;i < FormChecker.RelationList.length;i++)
		{
			var	RelInfo = FormChecker.RelationList[i];
			var	IsValid = (RelInfo.RelType == "AND") ? true : false;
			
			if (RelInfo.RelType != "SUM")
			{
				for (var j = 0;j < RelInfo.FieldsID.length;j++)
				{
					FieldIdx = FormChecker.FieldInCheckList (RelInfo.FieldsID[j]);			
					if (FieldIdx !== false)				
					{
						CheckInfo = FormChecker.FieldList[FieldIdx];
						
						switch (RelInfo.RelType)
						{
							case "OR":
								IsValid |= FormChecker.ValidateField (CheckInfo);
								break;
							case "AND":
								IsValid &= FormChecker.ValidateField (CheckInfo);
								break;
						}
					}
				}
			}
		}
	}		
	
	if (RetErrorStatus.IsValid == false)
	{
		FormChecker.DisplayErrors (RetErrorStatus.FieldInErrorList);
	}
	return (RetErrorStatus.IsValid);
}

FormChecker.prototype.ValidateField = function	(CheckInfo,RetStatus)
{
	//alert ("kkkkkkkkkk "+RetStatus.IsValid);
	
	RetStatus.IsValid = this.CheckFunctionList[CheckInfo.ChkType] (CheckInfo.FieldID,CheckInfo.Params);
	if (RetStatus.IsValid == false)
	{
		RetStatus.FieldInErrorList[RetStatus.FieldInErrorList.length] = CheckInfo;
	}
}

FormChecker.prototype.DisplayErrors = function	(FieldInError)
{
	var	ErrorMessage = "";
	var	i = null;
	
	for (i = 0;i < FieldInError.length;i++)
	{
		ErrorMessage += FieldInError[i].ErrorMex + "\n";
		
		MyElemID (FieldInError[i].FieldID).className += this.ErrorFieldClass;			
		
		if (FieldInError[i].ErrorTabID != "")
		{
			MyElemID (FieldInError[i].ErrorTabID).className += this.ErrorTabClass;
		}
	}
	alert ("I campi contrassegnati in giallo sono obbligatori.");
}

FormChecker.prototype.ExecuteChk1 = function	(FieldID,Params)
{
	var	IsValid = true;
	var	FieldObj = MyElemID (FieldID);
	var	MinLen = 1;
	var	MaxLen = FieldObj.value.length;
	
	if (Params != "")
	{
		var	MinLen = Params[0];
		var	MaxLen = Params[1];
	}
	
	if ((FieldObj.value.length < MinLen) || (FieldObj.value.length > MaxLen))
	{		
		IsValid = false;
	}
	return (IsValid);
}

FormChecker.prototype.ExecuteChk2 = function	(FieldID,Params)
{
	var	IsValid = true;
	var	FieldObj = MyElemID (FieldID);
	var	MinRng = FieldObj.value;
	var	MaxRng = FieldObj.value;
	
	if (Params != "")
	{
		var	MinRng = Params[0];
		var	MaxRng = Params[1];
	}
	
	if ((FieldObj.value.length == 0) || (isNaN (Number (FieldObj.value))))
	{
		IsValid = false;
	}
	else if ((parseFloat (FieldObj.value) < MinRng) || (parseFloat (FieldObj.value) > MaxRng))
	{
		IsValid = false;
	}
	
	return (IsValid);
}

FormChecker.prototype.ExecuteChk3 = function	(FieldID,Params)
{
	var	IsValid = true;
	var	FieldObj = MyElemID (FieldID);
	var	MinRng = FieldObj.value;
	var	MaxRng = FieldObj.value;
	
	if (Params != "")
	{
		var	MinRng = Params[0];
		var	MaxRng = Params[1];
	}
	
	if ((FieldObj.value.length == 0) ||  (isNaN (Number (FieldObj.value))) || (parseInt (FieldObj.value) != parseFloat (FieldObj.value)))
	{
		IsValid = false;
	}
	else if ((parseFloat (FieldObj.value) < MinRng) || (parseFloat (FieldObj.value) > MaxRng))
	{
		IsValid = false;
	}
	
	return (IsValid);
}

FormChecker.prototype.ExecuteChk4 = function	(FieldID,Params)
{
	var	IsValid = true;
	var	FieldObj = MyElemID (FieldID);
	
	if (FieldObj.selectedIndex == 0)
	{
		IsValid = false;
	}
	return (IsValid);
}

FormChecker.prototype.ExecuteChk5 = function	(FieldID,Params)
{
	var	IsValid = true;
	var	FieldObj = MyElemID (FieldID);
	
	if (FieldObj.checked == false)
	{
		IsValid = false;
	}
	return (IsValid);
}

FormChecker.prototype.ExecuteChk6 = function	(FieldID,Params)
{
	var	IsValid = true;
	var	FieldObj = MyElemID (FieldID);
	
	if (FieldObj.checked == false)
	{
		IsValid = false;
	}
	
	return (IsValid);
}

FormChecker.prototype.ExecuteChk7 = function	(FieldID,Params)
{
	var	IsValid = true;
	var	FieldObj = MyElemID (FieldID);
	
	if (FieldObj.value.length > 0)
	{
		var	Pices = FieldObj.value.split (".");
		var	ErrCnt = 0;
		
		for (var i = 0;i < CheckInfo.Param1.length;i++)
		{
			if (CheckInfo.Params[0][i] != Pices[Pices.length - 1])
			{
				++ErrCnt;
			}
		}
		if (ErrCnt != CheckInfo.Params[0].length)
		{
			IsValid = false;
		}
	}		
	return (IsValid);
}

FormChecker.prototype.ExecuteChk8 = function	(FieldID,Params)
{
	var	IsValid = true;
	var	FieldObj = MyElemID (FieldID);
	
	if (FieldObj.value.length != 11)
	{
		var	EnabledChar = "0123456789";
		var	Chk1 = 0;
		var	Chk2 = null;
		
		for(i = 0;i < 11;i++ )
		{
			if( EnabledChar.indexOf (pi.charAt (i)) == -1)
			{
				IsValid = true;
			}
		}
		for (i = 0;i <= 9;i += 2)
		{
			Chk1 += pi.charCodeAt (i) - '0'.charCodeAt (0);
		}
		for(i = 1;i <= 9;i += 2)
		{
			Chk2 = 2 * ( pi.charCodeAt (i) - '0'.charCodeAt (0));
			if ( Chk2 > 9 )  Chk2 = Chk2 - 9;
			Chk1 += Chk2;
		}
		if ((10 - Chk1 % 10 ) % 10 == pi.charCodeAt (10) - '0'.charCodeAt (0))
		{
			IsValid = false;
		}
	}	
	return (IsValid);
}

FormChecker.prototype.ExecuteChk9 = function	(FieldID,Params)
{
	var	IsValid = true;
	var	FieldObj = MyElemID (FieldID);
	
	var	validi = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
	var	set1 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var set2 = "ABCDEFGHIJABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var	setpari = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var	setdisp = "BAKPLCQDREVOSFTGUHMINJWZYX";
	var	i;
	var s = 0;
	
	FieldObj.value = FieldObj.value.toUpperCase ();
	if (FieldObj.value.length != 16)
	{
		IsValid = false;
	}
	for( i = 0; i < 16; i++ )
	{
		if (validi.indexOf (FieldObj.value.charAt (i)) == -1)
		{
			IsValid = false;
		}
	}
	for (i = 1;i <= 13;i += 2)
	{
		s += setpari.indexOf (set2.charAt (set1.indexOf (FieldObj.value.charAt (i))));
	}
	for (i = 0;i <= 14;i += 2)
	{
		s += setdisp.indexOf (set2.charAt (set1.indexOf (FieldObj.value.charAt (i))));
	}
	if (s%26 != FieldObj.value.charCodeAt (15)-'A'.charCodeAt (0))
	{
		IsValid = false;
	}
	
	return (IsValid);
}

FormChecker.prototype.ExecuteChk10 = function	(FieldID,Params)
{
	var	IsValid = true;
	var	FieldObj = MyElemID (FieldID);
	
	if (FieldObj.value.match (/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,2}))$)\b/gi) == null)
	{
		IsValid = false;
	}
	
	return (IsValid);
}

FormChecker.prototype.ExecuteChk11 = function	(FieldID,Params)
{
	// copia di ExecuteChk1
	var	IsValid = true;
	var	FieldObj = MyElemID (FieldID);
	var	MinLen = 1;
	var	MaxLen = FieldObj.value.length;
	
	if (Params != "")
	{
		var	MinLen = Params[0];
		var	MaxLen = Params[1];
	}
		
	if ((FieldObj.value.length < MinLen) || (FieldObj.value.length > MaxLen))
	{		
		IsValid = false;
	}
	return (IsValid);
}

FormChecker.prototype.CleanAndTrimField = function	(MakeClear,MakeTrim)
{
	for (var i = 0;i < this.FormRef.length;i++)
	{
		if (this.FormRef[i].id != "")
		{
			var	ObjRef = MyElemID (this.FormRef[i].id);
			
			switch (ObjRef.type)
			{
				case "text":
					if (MakeTrim == true)
					{
						ObjRef.value = TrimData (ObjRef.value);
					}
					if (MakeClear == true)
					{
						ObjRef.className = ObjRef.className.replace (this.ErrorFieldClass,"");
					}
					break;
				case "password":
				case "textarea":
				case "select-one":
				case "file":
				case "checkbox":
				case "radio":
					if (MakeClear == true)
					{
						ObjRef.className = ObjRef.className.replace (this.ErrorFieldClass,"");
					}
					break;
			}
		}
	}
}

FormChecker.prototype.FieldInCheckList = function	(FieldID)
{
	for (var i = 0;i < this.FieldList.length;i++)
	{
		if (this.FieldList[i].FieldID == FieldID)
		{
			return (i);
		}
	}
	return (false);
}
