aValidator = new Array();
aRegularExpressionValidator = new Array();
aRegularExpressionValidator[ "email" ] = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
aRegularExpressionValidator[ "creditcard" ] = /^((4\d{3})|(5[1-5]\d{2})|(6011))-?\d{4}-?\d{4}-?\d{4}|3[4,7]\d{13}$/;
aRegularExpressionValidator[ "time" ] = /^([0-1][0-9]|2[0-3]):[0-5][0-9]$/;
//it's for url input aRegularExpressionValidator[ "url" ] = /^(http:\/\/)*(https:\/\/)*([\w-]+\.)+[\w-]*(/[\w- ./?%=]*)?/;
//	(http://)*(https://)*([\w-]+\.)+[\w-]*(/[\w- ./?%=]*)?
function InitializerValidation()
{
	aValidator = new Array();
	SetEnabledValidation();
	alert( "ini" );
}
function VirtualFormGetNames()
{
	var sReturnNames = "";
	for ( var i = 0; i < aValidator.length; i++ )
	{
		if ( sReturnNames.indexOf( aValidator[ i ].VirtualForm + "," ) == -1 )
			sReturnNames += aValidator[ i ].VirtualForm + ",";
	}
	sReturnNames = sReturnNames.substring( 0, sReturnNames.length-1 );
	return sReturnNames;
}
function ViewValidatorsOld()
{
	var sText = "";
	sText+= "<b>Virtual Form Distinct:</b> " + VirtualFormGetNames() + "<br>";
	sText+= "<b>Validation Enabled:</b> " + !bNotValidation_Validator + "<br>";
	sText+= "<table cellspacing=0 cellpadding=0 border=1>";
	sText+= "<tr style='font-weight:bold;'>" +
		"<td>N" +
		"</td><td>Id" +
		"<td>Control To Validate" +
		"</td><td>Virtual Form"  +
		"</td><td>Enabled" +
		"</td><td>Error Message" +
		"</td><td>Client Validation Function" +
		'</td></tr>';
	var sLastVirtualForm = "";
	var iIndexLastVirtualForm = 0;
	var aColor = new Array( "#c1e1ff","#97ccff","#71b9ff","#a7eaae","#6ddf79","#e8c2b0","#dea68b" );
	for ( var i = 0; i < aValidator.length; i++ )
	{
		if ( sLastVirtualForm != aValidator[i].VirtualForm )
		{
			sColorRow = aColor[ iIndexLastVirtualForm++ ];
			if ( iIndexLastVirtualForm == aColor.length ) iIndexLastVirtualForm = 0;
			sLastVirtualForm = aValidator[i].VirtualForm
		}
		var cvfunction = "-";
		if ( aValidator[i].ClientValidationFunction != null )
			cvfunction = aValidator[i].ClientValidationFunction;
		var iIndex = i + 1;
		sText+= '<tr bgcolor="' + sColorRow + '">' +
			"<td>" + iIndex +
			"</td><td>" + aValidator[i].Id +
			"</td><td>" + aValidator[i].ControlToValidate +
			"</td><td>" + aValidator[i].VirtualForm +
			"</td><td>" + aValidator[i].Enabled +
			"</td><td>" + aValidator[i].ErrorMessage +
			"</td><td>" + cvfunction +
			'</td></tr>';
	}
	sText+= "</table>";
	ViewInWindowValidation( sText );
}
function ViewValidators()
{
	var sText = "";
	var sTextTitle = "";
	var sForms = VirtualFormGetNames();
	sText+= "<b>Virtual Form Distinct:</b> " + sForms + "<br>";//.replace( /,/g, " - " )
	sText+= "<b>Validation Enabled:</b> " + !bNotValidation_Validator + "<br>";
	//sText+= "<table cellspacing=0 cellpadding=0 border=1>";
	sTextTitle+= "<tr style='font-weight:bold;'>" +
		//"<td>N" +
		"<td>Id" +
		"<td>Control To Validate" +
		//"</td><td>Virtual Form"  +
		"</td><td>Enabled" +
		"</td><td>Error Message" +
		"</td><td>Client Validation Fn" +
		'</td></tr>';
	var sLastVirtualForm = "";
	var iIndexLastVirtualForm = 0;
	var aColor = new Array( "#c1e1ff","#97ccff","#71b9ff","#a7eaae","#6ddf79","#e8c2b0","#dea68b" );
	var aFormsNames = sForms.split( "," );
	sForms = "," + sForms + ",";
	var aForms = new Array();
	for ( var j = 0; j <= aFormsNames.length; j++ )
		aForms[ aFormsNames[j] ] = "";
	var sTextRow = "";
	for ( var i = 0; i < aValidator.length; i++ )
	{
		sTextRow = "";
		if ( sLastVirtualForm != aValidator[i].VirtualForm )
		{
			sColorRow = aColor[ iIndexLastVirtualForm++ ];
			if ( iIndexLastVirtualForm == aColor.length ) iIndexLastVirtualForm = 0;
			sLastVirtualForm = aValidator[i].VirtualForm
		}
		//if ( sForms.indexOf( aValidator[i].VirtualForm ) != -1 )
			//aForms[ aValidator[i].VirtualForm ] = "";
		var cvfunction = "-";
		if ( aValidator[i].ClientValidationFunction != null )
			cvfunction = aValidator[i].ClientValidationFunction;
		var iIndex = i + 1;
		//sTextRow+= '<tr bgcolor="' + sColorRow + '">' +
		sTextRow+= '<tr>' +
			//"<td>" + iIndex +
			"<td>" + aValidator[i].Id +
			"</td><td>" + aValidator[i].ControlToValidate +
			//"</td><td>" + aValidator[i].VirtualForm +
			"</td><td>" + aValidator[i].Enabled +
			"</td><td>" + aValidator[i].ErrorMessage +
			"</td><td>" + cvfunction +
			'</td></tr>';
		aForms[ aValidator[i].VirtualForm ]+= sTextRow;
		//alert( aForms[ aValidator[i].VirtualForm ] );
	}
	for ( var j = 0; j < aFormsNames.length; j++ )
	{
		sText+= aFormsNames[j];
		sText+= "<table cellspacing=0 cellpadding=0 border=1 width=100% bgcolor='"+aColor[j]+"'>";
		sText+= sTextTitle;
		sText+= aForms[ aFormsNames[j] ];
		sText+= "</table>";
	}
	//sText+= "</table>";
	ViewInWindowValidation( sText );
}
function ViewInWindowValidation(t)
{
	win2=window.open( '','','scrollbars=yes,resizable=yes' );
	win2.document.writeln( '<B>Reporte</B><P>' );
	win2.document.writeln(t);
	win2.document.close()
}
function VirtualFormDestroy( idForms, bEnabled ) //public
{
	if (bEnabled == null) bEnabled = true;
	idFs = idForms + ",";
	if ( bEnabled == true )
	{
		for ( var j = 0; j < aValidator.length; j++ )
		{
			oValidator = aValidator[j];
			if ( idFs.indexOf( oValidator.VirtualForm + "," ) != -1 )
			{
				for ( var k = j; k < aValidator.length - 1; k++ )
				{
					aValidator[k] = aValidator[k+1];
				}
				j = j - 1;
				aValidator.length = aValidator.length - 1;
			}
		}
	}
	else
	{
		for ( var j = 0; j < aValidator.length; j++ )
		{
			oValidator = aValidator[j];
			if ( idFs.indexOf( oValidator.VirtualForm + "," ) == -1 )
			{
				for ( var k = j; k < aValidator.length - 1; k++ )
				{
					aValidator[k] = aValidator[k+1];
				}
				j = j - 1;
				aValidator.length = aValidator.length - 1;
			}
		}
	}
}
/*  *******************  CLASSES  ****************** */
function Class_Validator( TypeValidator, Id, ControlToValidate, ErrorMessage, Display, Enabled, VirtualForm )
{
	this.TypeValidator = TypeValidator;
	this.Id = Id;
	this.ControlToValidate = ControlToValidate;
	this.ErrorMessage = ErrorMessage;
	this.Display = Display;
	this.Enabled = Enabled;
	this.EnabledCopy = Enabled;
	this.VirtualForm = VirtualForm;
	return this;
}
function Class_RequiredFieldValidator( TypeValidator, Id, ControlToValidate, ErrorMessage, Display, Enabled, InitialValue, VirtualForm )
{
	this.base = Class_Validator
	this.base(TypeValidator, Id, ControlToValidate, ErrorMessage, Display, Enabled, VirtualForm);
	this.InitialValue = InitialValue;
	return this;
}
function Class_RequiredAtLeastOneFieldValidator( TypeValidator, Id, ControlToValidate, ErrorMessage, Display, Enabled, InitialValue, VirtualForm )
{
	this.base = Class_Validator
	this.base(TypeValidator, Id, ControlToValidate, ErrorMessage, Display, Enabled, VirtualForm);
	this.InitialValue = InitialValue;
	return this;
}
function Class_RequiredFieldCheckedValidator( TypeValidator, Id, ControlToValidate, ErrorMessage, Display, Enabled, VirtualForm )
{
	this.base = Class_Validator
	this.base(TypeValidator, Id, ControlToValidate, ErrorMessage, Display, Enabled, VirtualForm);
	return this;
}
function Class_RegularExpressionValidator( TypeValidator, Id, ControlToValidate, ErrorMessage, Display, Enabled, ValidationExpression, ValidationExpressionName, VirtualForm )
{
	this.base = Class_Validator
	this.base(TypeValidator, Id, ControlToValidate, ErrorMessage, Display, Enabled, VirtualForm);
	this.ValidationExpression = ValidationExpression;
	this.ValidationExpressionName = ValidationExpressionName;
	return this;
}
function Class_CustomValidator( TypeValidator, Id, ControlToValidate, ErrorMessage, Display, Enabled, ClientValidationFunction, VirtualForm )
{
	this.base = Class_Validator
	this.base(TypeValidator, Id, ControlToValidate, ErrorMessage, Display, Enabled, VirtualForm);
	this.ClientValidationFunction = ClientValidationFunction;
	return this;
}
function Class_CompareValidator( TypeValidator, Id, ControlToValidate, ErrorMessage, Display, Enabled, ControlToCompare, ValueToCompare, Type, Operator, Digits, GroupChar, DecimalChar, DateOrder, Century, CutOffYear, VirtualForm )
{
	this.base = Class_Validator
	this.base(TypeValidator, Id, ControlToValidate, ErrorMessage, Display, Enabled, VirtualForm);
	this.ControlToCompare = ControlToCompare;
	this.ValueToCompare = ValueToCompare;
	this.Type = Type;
	this.Operator = Operator;
	this.Digits = Digits;
	this.GroupChar = GroupChar;
	this.DecimalChar = DecimalChar;
	this.DateOrder = DateOrder;
	this.Century = Century;
	this.CutOffYear = CutOffYear;
	return this;
}
function Class_RangeValidator( TypeValidator, Id, ControlToValidate, ErrorMessage, Display, Enabled, MinimumValue, MaximumValue, Type, Digits, GroupChar, DecimalChar, DateOrder, Century, CutOffYear, VirtualForm )
{
	this.base = Class_Validator
	this.base(TypeValidator, Id, ControlToValidate, ErrorMessage, Display, Enabled, VirtualForm);
	this.MinimumValue = MinimumValue;
	this.MaximumValue = MaximumValue;
	this.Type = Type;
	this.Digits = Digits;
	this.GroupChar = GroupChar;
	this.DecimalChar = DecimalChar;
	this.DateOrder = DateOrder;
	this.Century = Century;
	this.CutOffYear = CutOffYear;
	return this;
}
/*  ****************  END  CLASSES  **************** */
/*  ************* METHODS ADD OBJECTS ************** */
function AddObj_RequiredFieldValidator( Id, ControlToValidate, ErrorMessage, Display, Enabled, InitialValue, VirtualForm )
{
	aValidator.push(new Class_RequiredFieldValidator( "RequiredFieldValidator", Id, ControlToValidate, ErrorMessage, Display, Enabled, InitialValue, VirtualForm ));
}
function AddObj_RequiredAtLeastOneFieldValidator( Id, ControlToValidate, ErrorMessage, Display, Enabled, InitialValue, VirtualForm )
{
	aValidator.push(new Class_RequiredAtLeastOneFieldValidator( "RequiredAtLeastOneFieldValidator", Id, ControlToValidate, ErrorMessage, Display, Enabled, InitialValue, VirtualForm ));
}
function AddObj_RequiredFieldCheckedValidator( Id, ControlToValidate, ErrorMessage, Display, Enabled, VirtualForm )
{
	aValidator.push(new Class_RequiredFieldCheckedValidator( "RequiredFieldCheckedValidator", Id, ControlToValidate, ErrorMessage, Display, Enabled, VirtualForm ));
}
function AddObj_RegularExpressionValidator( Id, ControlToValidate, ErrorMessage, Display, Enabled, ValidationExpression, ValidationExpressionName, VirtualForm )
{
	aValidator.push(new Class_RegularExpressionValidator( "RegularExpressionValidator", Id, ControlToValidate, ErrorMessage, Display, Enabled, ValidationExpression, ValidationExpressionName, VirtualForm ));
}
function AddObj_CustomValidator( Id, ControlToValidate, ErrorMessage, Display, Enabled, ClientValidationFunction, VirtualForm )
{
	aValidator.push(new Class_CustomValidator( "CustomValidator", Id, ControlToValidate, ErrorMessage, Display, Enabled, ClientValidationFunction, VirtualForm ));
}
function AddObj_CompareValidator( Id, ControlToValidate, ErrorMessage, Display, Enabled, ControlToCompare, ValueToCompare, Type, Operator, Digits, GroupChar, DecimalChar, DateOrder, Century, CutOffYear, VirtualForm )
{
//digits = 2;groupchar = ","; decimalchar = ".";
//dateorder = "mdy"; cutoffyear="29"; century="2000";
	aValidator.push(new Class_CompareValidator( "CompareValidator", Id, ControlToValidate, ErrorMessage, Display, Enabled, ControlToCompare, ValueToCompare, Type, Operator, Digits, GroupChar, DecimalChar, DateOrder, Century, CutOffYear, VirtualForm));
}
function AddObj_RangeValidator( Id, ControlToValidate, ErrorMessage, Display, Enabled, MinimumValue, MaximumValue, Type, Digits, GroupChar, DecimalChar, DateOrder, Century, CutOffYear, VirtualForm )
{
	aValidator.push(new Class_RangeValidator( "RangeValidator", Id, ControlToValidate, ErrorMessage, Display, Enabled, MinimumValue, MaximumValue, Type, Digits, GroupChar, DecimalChar, DateOrder, Century, CutOffYear, VirtualForm));
}
/*  ************ END METHODS ADD OBJECTS *********** */
/* ************** FUNTIONS VALIDATION ************** */
function Fn_RequiredFieldCheckedValidator( oValidator ) //private
{	//if the validation is negative
	//	  - return 1 -   or   - return 2 (and error message is show immediately) -
	//else return 0
	function OptionsListIsChecked_Validator( sControlsName )
	{
		var bIsChecked = false;var i = 0;
		var aObj = document.getElementsByName(sControlsName);				
		while ( (i < aObj.length) && (!bIsChecked) )
		{//alert( aObj[i].id + "-" +aObj[i].value );					
			bIsChecked = aObj[i].checked;
			i++;
		}
		return bIsChecked;
	}
	var iResultValidation = 0;
	if ( !OptionsListIsChecked_Validator( oValidator.ControlToValidate ) )
	{
		if ( !bShowAllErrorTogether_Validator ) ShowHideTextAllValidator( "hide" );
		ShowHideTextValidator( oValidator.Id, oValidator.Display, "show" );
		if ( bShowAllErrorTogether_Validator )
			iResultValidation = 1;
		else
		{
			if ( bShowMessageBox_Validator )
			{
				alert( oValidator.ErrorMessage );
				if ( (document.getElementsByName(oValidator.ControlToValidate)) != null )
				{
					var aObj = document.getElementsByName(oValidator.ControlToValidate);
					aObj[0].focus();
				}
			}
			iResultValidation = 2;
		}
	}
	else
		ShowHideTextValidator( oValidator.Id, oValidator.Display, "hide" );
	return iResultValidation
}
function Fn_RequiredFieldValidator( oValidator ) //private
{	//if the validation is negative
	//	  - return 1 -   or   - return 2 (and error message is show immediately) -
	//else return 0
	var iResultValidation = 0;
	var sInitialValue = ""+oValidator.InitialValue
	var sActualValue = ""+ValidatorGetValue_Validation( oValidator.ControlToValidate, oValidator.Id )
	//var sActualValue = ""+document.getElementById( oValidator.ControlToValidate ).value
	if ( sActualValue == sInitialValue )
	{
		if ( !bShowAllErrorTogether_Validator ) ShowHideTextAllValidator( "hide" );
		ShowHideTextValidator( oValidator.Id, oValidator.Display, "show" );
		if ( bShowAllErrorTogether_Validator )
			iResultValidation = 1;
		else
		{
			if ( bShowMessageBox_Validator )
			{
				alert( oValidator.ErrorMessage );
				if ( (document.getElementById(oValidator.ControlToValidate)) != null )
				{
					document.getElementById( oValidator.ControlToValidate ).focus();

					if (document.getElementById( oValidator.ControlToValidate ).tagName.toUpperCase() !="SELECT")
						document.getElementById( oValidator.ControlToValidate ).select();
				}
			}
			iResultValidation = 2;
		}
	}
	else
		ShowHideTextValidator( oValidator.Id, oValidator.Display, "hide" );
	return iResultValidation
}
function Fn_RequiredAtLeastOneFieldValidator( oValidator ) //private
{	//if the validation is negative
	//	  - return 1 -   or   - return 2 (and error message is show immediately) -
	//else return 0
	var sCharSeparator = ";";
	function AtLeastOneFieldIsOk_ById_Validator( sControlsId )
	{
		var bIsOk = false;var i = 0;
		var aObjId = sControlsId.split(sCharSeparator);				
		var sInitialValue = ""+oValidator.InitialValue
		var sActualValue = "";
		while ( (i < aObjId.length) && (!bIsOk) )
		{					
			sActualValue = "" + ValidatorGetValue_Validation( aObjId[ i ], oValidator.Id )
			bIsOk = ( sActualValue != sInitialValue );
			
			i++;
		}
		return bIsOk;
	}
	function AtLeastOneFieldIsOk_ByName_Validator( sControlsName )
	{
		var bIsOk = false;var i = 0;
		var aObj = document.getElementsByName(sControlsName);
		
		if ( aObj.length == 0 )
		{
			alert( "Run Time Error in ClientScript:\n\r - Control Name '" + sControlsName + "' not found (related to '"+oValidator.Id+"')" );
			return false;
		}
		var sInitialValue = ""+oValidator.InitialValue
		var sActualValue = "";
		while ( (i < aObj.length) && (!bIsOk) )
		{					
			sActualValue = "" + ValidatorGetValue_Validation( aObj[ i ].id, oValidator.Id )
			bIsOk = ( sActualValue != sInitialValue );
			i++;
		}
		return bIsOk;
	}
	function AtLeastOneFieldIsOk_Validator( sControls )
	{
		if ( sControls.indexOf(sCharSeparator) == -1 )
			return AtLeastOneFieldIsOk_ByName_Validator( sControls )
		else
			return AtLeastOneFieldIsOk_ById_Validator( sControls )
	}
	var iResultValidation = 0;
	if ( !AtLeastOneFieldIsOk_Validator( oValidator.ControlToValidate ) )//if ( sActualValue == sInitialValue )
	{
		if ( !bShowAllErrorTogether_Validator ) ShowHideTextAllValidator( "hide" );
		ShowHideTextValidator( oValidator.Id, oValidator.Display, "show" );
		if ( bShowAllErrorTogether_Validator )
			iResultValidation = 1;
		else
		{
			if ( bShowMessageBox_Validator )
			{
				alert( oValidator.ErrorMessage );
				var sIdCTV = oValidator.ControlToValidate;
				if ( sIdCTV.indexOf(sCharSeparator) == -1 )
				{
					var aObj = document.getElementsByName( sIdCTV );
					if ( aObj.length > 0 ) sIdCTV = aObj[ 0 ].id;
				}
				else
				{
					var aObjId = sIdCTV.split(sCharSeparator);
					if ( aObjId.length > 0 ) sIdCTV = aObjId[ 0 ];
					
				}
				if ( document.getElementById( sIdCTV ) != null )
				{
					document.getElementById( sIdCTV ).focus();
					if (document.getElementById( sIdCTV ).tagName.toUpperCase() !="SELECT")
						document.getElementById( sIdCTV ).select();
				}
			}
			iResultValidation = 2;
		}
	}
	else
		ShowHideTextValidator( oValidator.Id, oValidator.Display, "hide" );
	return iResultValidation
}
function Fn_RegularExpressionValidator( oValidator ) //private
{	//if the validation is negative
	//	  - return 1 -   or   - return 2 (and error message is show immediately) -
	//else return 0
	var iResultValidation = 0;
	re = new RegExp( oValidator.ValidationExpression )
	
	if ( oValidator.ValidationExpressionName != null )
		if ( oValidator.ValidationExpressionName != "" )
			if ( aRegularExpressionValidator[ oValidator.ValidationExpressionName.toLowerCase() ] != "undefined" )
				re = aRegularExpressionValidator[ oValidator.ValidationExpressionName.toLowerCase() ];
	var sActualValue = ValidatorGetValue_Validation( oValidator.ControlToValidate, oValidator.Id );
	if ( sActualValue != "" )
	{
		if ( !re.test( sActualValue ) )
		{
			if ( !bShowAllErrorTogether_Validator ) ShowHideTextAllValidator( "hide" );
			ShowHideTextValidator( oValidator.Id, oValidator.Display, "show" );
			if ( bShowAllErrorTogether_Validator )
				iResultValidation = 1;
			else
			{
				if ( bShowMessageBox_Validator )
				{
					alert( oValidator.ErrorMessage );
					if ( (document.getElementById(oValidator.ControlToValidate)) != null )
					{
						document.getElementById( oValidator.ControlToValidate ).focus();
						document.getElementById( oValidator.ControlToValidate ).select();
					}
				}
				iResultValidation = 2;
			}
		}
		else
			ShowHideTextValidator( oValidator.Id, oValidator.Display, "hide" );
	}
	else
		ShowHideTextValidator( oValidator.Id, oValidator.Display, "hide" );
	return iResultValidation
}
function Fn_CustomValidator( oValidator ) //private
{	//if the validation is negative
	//	  - return 1 -   or   - return 2 (and error message is show immediately) -
	//else return 0
	var iResultValidation = 0;
	str = oValidator.ClientValidationFunction + "('" + oValidator.ControlToValidate +  "')";
	bExecFn = eval( str );
	if ( !bExecFn )
	{
		if ( !bShowAllErrorTogether_Validator ) ShowHideTextAllValidator( "hide" );
		ShowHideTextValidator( oValidator.Id, oValidator.Display, "show" );
		if ( bShowAllErrorTogether_Validator )
			iResultValidation = 1;
		else
		{
			if ( bShowMessageBox_Validator )
			{
				alert( oValidator.ErrorMessage );
				if ( (document.getElementById(oValidator.ControlToValidate)) != null )
				{
					document.getElementById( oValidator.ControlToValidate ).focus();
				}
			}			
			iResultValidation = 2;
		}
	}
	else
		ShowHideTextValidator( oValidator.Id, oValidator.Display, "hide" );
	return iResultValidation
}
function Fn_CompareValidator(val)
{
	var iResultValidation = 0;
	var value = ValidatorGetValue_Validation(val.ControlToValidate, val.Id);
	if (ValidatorTrim_Validation(value).length != 0)
	{
		var compareTo = "";
		if (null == document.getElementById(val.ControlToCompare)) {
			if (typeof(val.ValueToCompare) == "string") {
				compareTo = val.ValueToCompare;
			}
		}
		else
		{
			compareTo = ValidatorGetValue_Validation(val.ControlToCompare, val.Id);
		}
		var bReturn = ValidatorCompare_Validation(value, compareTo, val.Operator, val);
	}
	else
	{
		bReturn = true;
		iResultValidation = 0;//return true;
	}
	if ( !bReturn )
	{
		if ( !bShowAllErrorTogether_Validator ) ShowHideTextAllValidator( "hide" );
		ShowHideTextValidator( val.Id, val.Display, "show" );
		if ( bShowAllErrorTogether_Validator )
			iResultValidation = 1;
		else
		{
			if ( bShowMessageBox_Validator )
			{
				alert( val.ErrorMessage );
				if ( (document.getElementById(val.ControlToValidate)) != null )
				{
					document.getElementById( val.ControlToValidate ).focus();
				}
			}		
			iResultValidation = 2;
		}
	}
	else
		ShowHideTextValidator( val.Id, val.Display, "hide" );
	return iResultValidation

}
function Fn_RangeValidator(val)
{
	var iResultValidation = 0;
	var value = ValidatorGetValue_Validation(val.ControlToValidate, val.Id);
	if (ValidatorTrim_Validation(value).length == 0)
	{
		bReturn = true;
		iResultValidation = 0;//return true;
	}
	else
	{
		bReturn = (ValidatorCompare_Validation(value, val.MinimumValue, "GreaterThanEqual", val) &&
				ValidatorCompare_Validation(value, val.MaximumValue, "LessThanEqual", val));
		
		if ( !bReturn )
		{
			if ( !bShowAllErrorTogether_Validator ) ShowHideTextAllValidator( "hide" );
			ShowHideTextValidator( val.Id, val.Display, "show" );
			if ( bShowAllErrorTogether_Validator )
				iResultValidation = 1;
			else
			{
				if ( bShowMessageBox_Validator )
				{
					alert( val.ErrorMessage );
					if ( (document.getElementById(val.ControlToValidate)) != null )
					{
						document.getElementById( val.ControlToValidate ).focus();
					}
				}
				iResultValidation = 2;
			}
		}
		else
			ShowHideTextValidator( val.Id, val.Display, "hide" );
	}
	return iResultValidation
}
/* *** FUNCTION RELATED Fn_CompareValidator & Fn_RangeValidator ***** */
function ValidatorConvert_Validation(op, dataType, val)
{
	function GetFullYear(year)
	{
		return (year + parseInt(century)) - ((year < cutoffyear) ? 0 : 100);
	}
 		digits = val.Digits;
 		groupchar = val.GroupChar;
 		decimalchar = val.DecimalChar;
		dateorder = val.DateOrder;
		cutoffyear = val.CutOffYear;
		century = val.Century;
var num, cleanInput, m, exp;
	if (dataType == "Integer") {
		exp = /^\s*[-\+]?\d+\s*$/;
		if (op.match(exp) == null) 
			return null;
		num = parseInt(op, 10);
		return (isNaN(num) ? null : num);
	}
	else if(dataType == "Double") {
		exp = new RegExp("^\\s*([-\\+])?(\\d+)?(\\" + decimalchar + "(\\d+))?\\s*$");

		m = op.match(exp);
		if (m == null)
			return null;

		for ( var hh = 1; hh < m.length; hh++){if (m[hh] == null) m[hh]=""}
		cleanInput = m[1] + (m[2].length>0 ? m[2] : "0") + "." + m[4];

		num = parseFloat(cleanInput);
		return (isNaN(num) ? null : num);            
	} 
	else if (dataType == "Currency") {
		exp = new RegExp("^\\s*([-\\+])?(((\\d+)\\" + groupchar + ")*)(\\d+)"
						+ ((digits > 0) ? "(\\" + decimalchar + "(\\d{1," + digits + "}))?" : "")
						+ "\\s*$");
		m = op.match(exp);
		if (m == null)
			return null;
		var intermed = m[2] + m[5] ;
		if (m[1]==null) m[1]="";
		for ( var hh = 1; hh < m.length; hh++){if (m[hh] == null) m[hh]=""}
		cleanInput = m[1] + intermed.replace(new RegExp("(\\" + groupchar + ")", "g"), "") + ((digits > 0) ? "." + m[7] : 0);
		num = parseFloat(cleanInput);
		return (isNaN(num) ? null : num);            
	}
	else if (dataType == "Date") {
		var yearFirstExp = new RegExp("^\\s*((\\d{4})|(\\d{2}))([-/]|\\. ?)(\\d{1,2})\\4(\\d{1,2})\\s*$");
		m = op.match(yearFirstExp);
		var day, month, year;
		var bD = true;
		if (m != null)
		{
			if ( m[2] != null)
			{
				if ((m[2].length == 4) || (dateorder == "ymd"))
				{ bD = false;

					day = m[6];
					month = m[5];
					year = (m[2].length == 4) ? m[2] : GetFullYear(parseInt(m[3], 10))
				}
			}
		}
		if ( bD ) {
			if (dateorder == "ymd"){
				return null;		
			}						
			var yearLastExp = new RegExp("^\\s*(\\d{1,2})([-/]|\\. ?)(\\d{1,2})\\2((\\d{4})|(\\d{2}))\\s*$");
			m = op.match(yearLastExp);
			if (m == null) {
				return null;
			}
			if (dateorder == "mdy") {
				day = m[3];
				month = m[1];
			}
			else {
				day = m[1];
				month = m[3];
			}

			if (m[5] != null)
				year = (m[5].length == 4) ? m[5] : GetFullYear(parseInt(m[6], 10));
			else
				year = GetFullYear(parseInt(m[6], 10));
		}
		month -= 1;
		var date = new Date(year, month, day);
		return (typeof(date) == "object" && year == date.getFullYear() && month == date.getMonth() && day == date.getDate()) ? date.valueOf() : null;
	}
	else {
		return op.toString();
	}
}
function ValidatorCompare_Validation(operand1, operand2, operator, val)
{
	var dataType = val.Type;
	var op1, op2;
	if ((op1 = ValidatorConvert_Validation(operand1, dataType, val)) == null)
		return false;    
	if (operator == "DataTypeCheck")
		return true;
	if ((op2 = ValidatorConvert_Validation(operand2, dataType, val)) == null)
		return true;
	switch (operator) {
		case "NotEqual":
			return (op1 != op2);
		case "GreaterThan":
			return (op1 > op2);
		case "GreaterThanEqual":
			return (op1 >= op2);
		case "LessThan":
			return (op1 < op2);
		case "LessThanEqual":
			return (op1 <= op2);
		default:
			return (op1 == op2);            
	}
}
function ValidatorTrim_Validation(s)
{
	var m = s.match(/^\s*(\S+(\s+\S+)*)\s*$/);
	return (m == null) ? "" : m[1];
}		
function ValidatorGetValue_Validation(id, idValidator)
{
	var control;
	if ( document.getElementById(id) != null )
	{
		control = document.getElementById(id);
		if (typeof(control.value) == "string")
		{
			return control.value;
		}
	}
	else alert( "Run Time Error in ClientScript:\n\r - Control '" + id + "' not found (related to '"+idValidator+"')" );
	return "";
}
/* *** FUNCTION RELATED Fn_CompareValidator & Fn_RangeValidator ***** */
/* *********** END FUNTIONS VALIDATION ************* */
function ShowHideTextAllValidator( action ) //private
{
	for (var i = 0; i < aValidator.length; i++)
	{
		ShowHideTextValidator( aValidator[i].Id, aValidator[i].Display, action );
	}
}
function ShowHideTextValidator( id, display, action ) //private
{
	var sDisplay = display.toLocaleLowerCase();
	var oError = document.getElementById( id )
	if ( action == "show" )
	{
		if ( sDisplay == "dynamic" )
			oError.style.display = "inline";
		else if ( sDisplay == "static" )
			oError.style.visibility = "";
	}
	else if  ( action == "hide" )
	{
		if (document.getElementById( id ))
		{
			if ( sDisplay == "dynamic" )
				oError.style.display = "none";
			else if ( sDisplay == "static" )
				oError.style.visibility = "hidden";
		}
	}
}
function SetEnabledByControlValidator( idControls, bEnabled ) //public
{
	if (bEnabled == null) bEnabled = true;
	idCs = idControls + ",";
	var oValidator;
	for ( var j = 0; j < aValidator.length; j++ )
	{
		oValidator = aValidator[j];
		if ( idCs.indexOf( oValidator.ControlToValidate + "," ) != -1 )
		{
			oValidator.Enabled = bEnabled;
			oValidator.EnabledCopy = bEnabled;
		}
	}
}
function SetEnabledVirtualFormValidator( idForms, bEnabled ) //public
{
	if (bEnabled == null) bEnabled = true;
	idFs = idForms + ",";
	var oValidator;
	for ( var j = 0; j < aValidator.length; j++ )
	{
		oValidator = aValidator[j];
		if ( idFs.indexOf( oValidator.VirtualForm + "," ) != -1 )
		{
			oValidator.Enabled = bEnabled;
			oValidator.EnabledCopy = bEnabled;
		}
	}
}
function SetErrorMessageValidator( idValidator, sErrorMessage ) //public
{
	var oValidator;
	for ( var j = 0; j < aValidator.length; j++ )
	{
		oValidator = aValidator[j];
		if ( oValidator.Id == idValidator )
		{
			oValidator.ErrorMessage = sErrorMessage;
			break;
		}
	}
}
function SetEnabledValidator( idValidators, bEnabled ) //public
{
	if (bEnabled == null) bEnabled = true;
	idVs = idValidators + ",";
	var oValidator;
	for ( var j = 0; j < aValidator.length; j++ )
	{
		oValidator = aValidator[j];
		if ( idVs.indexOf( oValidator.Id + "," ) != -1 )
		{
			oValidator.Enabled = bEnabled;
			oValidator.EnabledCopy = bEnabled;
		}
	}
}
function EnabledAllValidator(bEnabled) //public
{
	if (bEnabled == null) bEnabled = true;
	var oValidator;
	for ( var j = 0; j < aValidator.length; j++ )
	{
		oValidator = aValidator[j];
		oValidator.Enabled = bEnabled;
		oValidator.EnabledCopy = bEnabled;
	}
}
function OnlyValidation( idValidators, bEnabled ) //public
{
	/* ** new code ** */
	idValidators+="";
	oCallerObject_Validator = "vf";
	if ( idValidators == "null" )
	{
		if ( bEnabled != null )//it supposes that the second param is the caller object
		{
			oCallerObject_Validator = bEnabled;
		}
		bEnabled = true;
	}
	/* ** end new code ** */
	bOnlyValidation_Validator = true;
	var oValidator;
	if (bEnabled == null) bEnabled = true;
	if ( bEnabled == true)
		for ( var j = 0; j < aValidator.length; j++ )
		{
			oValidator = aValidator[j];
			oValidator.Enabled = !bEnabled;
			ShowHideTextValidator( oValidator.Id, oValidator.Display, "hide" );
		}
	//the next is equal to SetEnabledValidator() but it not maintains the copy of "enabled"
	idVs = idValidators + ",";
	for ( var j = 0; j < aValidator.length; j++ )
	{
		oValidator = aValidator[j];
		if ( idVs.indexOf( oValidator.Id + "," ) != -1 )
		{
			oValidator.Enabled = bEnabled;
			if ( !bEnabled ) ShowHideTextValidator( oValidator.Id, oValidator.Display, "hide" );
		}
	}
}
function OnlyByControlValidation( idControls, bEnabled ) //public
{
	/* ** new code ** */
	idControls+="";
	oCallerObject_Validator = "vf";
	if ( idControls == "null" )
	{
		if ( bEnabled != null )//it supposes that the second param is the caller object
		{
			oCallerObject_Validator = bEnabled;
		}
		bEnabled = true;
	}
	/* ** end new code ** */
	bOnlyValidation_Validator = true;
	var oValidator;
	if (bEnabled == null) bEnabled = true;
	if ( bEnabled == true)
		for ( var j = 0; j < aValidator.length; j++ )
		{
			oValidator = aValidator[j];
			oValidator.Enabled = !bEnabled;
			ShowHideTextValidator( oValidator.Id, oValidator.Display, "hide" );
		}
	//the next is equal to SetEnabledValidator() but it not maintains the copy of "enabled"
	idCs = idControls + ",";
	for ( var j = 0; j < aValidator.length; j++ )
	{
		oValidator = aValidator[j];
		if ( idCs.indexOf( oValidator.ControlToValidate + "," ) != -1 )
		{
			oValidator.Enabled = bEnabled;
			if ( !bEnabled ) ShowHideTextValidator( oValidator.Id, oValidator.Display, "hide" );
		}
	}
}
function VirtualFormValidation( idForms, bEnabled ) //public
{
	/* ** new code ** */
	idForms+="";
	oCallerObject_Validator = "vf";
	if ( idForms == "null" )
	{
		if ( bEnabled != null )//it supposes that the second param is the caller object
		{
			oCallerObject_Validator = bEnabled;
		}
		bEnabled = true;
	}
	/* ** end new code ** */
	if (bEnabled == null) bEnabled = true;
	bOnlyValidation_Validator = true;
	var oValidator;
	if ( bEnabled == true)
		for ( var j = 0; j < aValidator.length; j++ )
		{
			oValidator = aValidator[j];
			oValidator.Enabled = !bEnabled;
			ShowHideTextValidator( oValidator.Id, oValidator.Display, "hide" );
		}
	//the next is equal to SetEnabledValidator() but it not maintains the copy of "enabled"
	idFs = "," + idForms + ",";

	for ( var j = 0; j < aValidator.length; j++ )
	{
		oValidator = aValidator[j];

		if ( idFs.indexOf( "," +  oValidator.VirtualForm + "," ) != -1 )
		{
			oValidator.Enabled = bEnabled;
			if ( !bEnabled ) ShowHideTextValidator( oValidator.Id, oValidator.Display, "hide" );
		}
	}
}
function RollBackEnabled_Validator() //private
{
	var oValidator;
	for ( var j = 0; j < aValidator.length; j++ )
	{
		oValidator = aValidator[j];
		oValidator.Enabled = oValidator.EnabledCopy;
	}
}
function SetDisabledValidation() //public
{
	bNotValidation_Validator = true;
}
function SetEnabledValidation() //public
{
	bNotValidation_Validator = false;
}
function SetFunctionPostBack( sFunction, sParam )
{
	sFunctionPostBack_Validator = sFunction;
	if ( sParam != null )
		sParamPostBack_Validator = sParam;
}
function SetFunctionValidationSummary( sFunction )
{
	sFunctionValidationSummary_Validator = sFunction;
}
var bNotValidation_Validator = false; //private
var bOnlyValidation_Validator = false; //private
var oCallerObject_Validator = "vf"; //private
var sIdForm_Validator = ""; //private
var sFunctionPostBack_Validator = ""; //private
var sParamPostBack_Validator = ""; //private
var sFunctionValidationSummary_Validator = ""; //private
var bShowAllErrorTogether_Validator = true; //private NEW var - using by Summary
var bShowMessageBox_Validator = true; //private - using by Summary
var bShowSummary_Validator = false; //private - using by Summary
var sBullet_Validator = "-";//"&bull;"; //private NEW var - using by Summary
var sHeaderText_Validator = ""; //private - using by Summary
function MakeValidation()
{
	if (bNotValidation_Validator)
		return true;
	else
	{
		var sAllError = "";
		var bResultValidation = true;
		var oValidator;
		for ( var i = 0; i < aValidator.length; i++ )
		{
			oValidator = aValidator[i];
			if ( !oValidator.Enabled )
			{
				ShowHideTextValidator( oValidator.Id, oValidator.Display, "hide" );
			}
			else
			{
				switch (oValidator.TypeValidator.toLocaleLowerCase())
				{
					case "requiredfieldvalidator": //Required Field Validator
						iResultValidation = Fn_RequiredFieldValidator( oValidator )
						break;
					case "requiredatleastonefieldvalidator": //Required At Least One Field Validator
						iResultValidation = Fn_RequiredAtLeastOneFieldValidator( oValidator )
						break;
					case "requiredfieldcheckedvalidator": //Required Field Checked Validator
						iResultValidation = Fn_RequiredFieldCheckedValidator( oValidator )
						break;
					case "regularexpressionvalidator": //Regular Expression Validator
						iResultValidation = Fn_RegularExpressionValidator( oValidator )
						break;
					case "customvalidator": //Custom Validator
						iResultValidation = Fn_CustomValidator( oValidator )
						break;
					case "comparevalidator": //Compare Validator
						iResultValidation = Fn_CompareValidator( oValidator )
						break;
					case "rangevalidator": //Range Validator
						iResultValidation = Fn_RangeValidator( oValidator )
						break;
				}
				switch ( iResultValidation )
				{
					case 1:	sAllError+= sBullet_Validator+" "+oValidator.ErrorMessage+"<br>"; break;
					case 2:
						if ( bOnlyValidation_Validator )
						{
							bOnlyValidation_Validator = false;
							RollBackEnabled_Validator();
						}
						
						if ( sFunctionValidationSummary_Validator != "" )
						{
							eval( sFunctionValidationSummary_Validator + "('" + oValidator.ErrorMessage + "', '" + oValidator.ControlToValidate + "')" );
						}
						return false;
						break;
				}
			}
		}
		if ( ( sAllError != "" ) && ( sHeaderText_Validator != "" ) )
			sAllError = sHeaderText_Validator + "<br>" + sAllError;
		if ( bShowSummary_Validator )
		{
			fnShowSummary(sAllError);
		}
		if ( sAllError != "" )
		{
			if ( bShowMessageBox_Validator )
			{
				var sAllErrorBox = sAllError.replace(/<br>/g, "\n\r");
				alert( sAllErrorBox );
			}
			if (bOnlyValidation_Validator) {bOnlyValidation_Validator = false;RollBackEnabled_Validator()}
			if ( sFunctionValidationSummary_Validator != "" )
			{
				eval( sFunctionValidationSummary_Validator + "('" + sAllError + "')" );
			}
			return false;
		}
		if (bOnlyValidation_Validator) {bOnlyValidation_Validator = false;RollBackEnabled_Validator()}
		return true;
	}
}