//--------------------------------------------------------------------------------------------------------//
// Constructor
//
// Arguments:
// - objCaller is the form object to validate.
//
// Restrictions:
// - Controls being validated must be located on the first level of the caller's siblings.
//
function RegExpValidator(objCaller)
{
	// Public fields
	this.caller = objCaller;
	this.valid = false;


	// Public methods
	this.ValidateInput = RegExpValidator_ValidateInput;
	this.OnIncorrectNodeValue = RegExpValidator_OnIncorrectNodeValue;
	this.OnCorrectNodeValue = RegExpValidator_OnCorrectNodeValue;
	this.OnIncorrectFormValues = RegExpValidator_OnIncorrectFormValues;
	this.OnCustomValidate = RegExpValidator_OnCustomValidate;
	this.OnNeedValidate = RegExpValidator_OnNeedValidate;
}
//--------------------------------------------------------------------------------------------------------//
// Validates input of the caller
//
function RegExpValidator_ValidateInput()
{
	// Start as valid
	this.valid = true;
	
	// Cross browser support
	var all;
	
	if (this.caller.all)
	{
		all = this.caller.all;
	}
	else
	{
		all = this.caller.elements;
	}
	if (all.length == 0)
	{
		all = document.getElementsByTagName('*');
	}
	
	// Process all caller's child nodes
	for(i = 0; i < all.length ; i++)
	{
		// Take only caller's hidden child nodes with the "regex_" name prefix
		var objCurrentNode = all[i];
		var strCurrentNodeID = new String(objCurrentNode.id);

		if(0 == strCurrentNodeID.indexOf("regex_") && "hidden" == objCurrentNode.type)
		{
			// Get corresponding control to validate
			var strTargetNodeID = strCurrentNodeID.replace("regex_", "");
			var objTargetNode = document.getElementById(strTargetNodeID);
			var objRegExp = new RegExp(objCurrentNode.value);
			// Do regular expression validation and custom validation
			if(true == this.OnNeedValidate(objTargetNode))
			{
				if(null == objTargetNode.value.match(objRegExp) && true != this.OnCustomValidate(objTargetNode))
				{
					this.valid = false;
					// Sygnal of incorrect node value
					this.OnIncorrectNodeValue(objTargetNode);

					continue;
				}
			}
			// Sygnal of correct node value
			this.OnCorrectNodeValue(objTargetNode);
		}
	}
	
	// Sygnal of incorrect form values
	if(false == this.valid)
	{
		this.OnIncorrectFormValues();
	}

	return this.valid;
}
//--------------------------------------------------------------------------------------------------------//
// Sygnals of incorrect node value.
// Must be overloaded to the custom implementation.
//
function RegExpValidator_OnIncorrectNodeValue(objNode)
{
	objNode.style.background = "#FFC0CB";
	
	//alert("The \"" + objNode.id + "\" node has incorrect value of \""+ objNode.value +"\".");
}
//--------------------------------------------------------------------------------------------------------//
// Sygnals of correct node value.
// Must be overloaded to the custom implementation.
//
function RegExpValidator_OnCorrectNodeValue(objNode)
{
		objNode.style.background = "#A5FF7F";

	//alert("The \"" + objNode.id + "\" node has correct value of \""+ objNode.value +"\".");
}
//--------------------------------------------------------------------------------------------------------//
// Sygnals of incorrect form data
// Must be overloaded to the custom implementation.
//
function RegExpValidator_OnIncorrectFormValues()
{
	alert("The \"" + this.caller.id + "\" form has some incorrect fields.");
}
//--------------------------------------------------------------------------------------------------------//
// Performs custom validation
// Override to add any extra validation.
//
function RegExpValidator_OnCustomValidate(objNode)
{
	// By-default always returns true not to violate correct input data
	return true;
}
//--------------------------------------------------------------------------------------------------------//
// Allows dynamically disabling field validation if necessary
//
function RegExpValidator_OnNeedValidate(objNode)
{
	// By-default validation is needed for all attached fields
	return (objNode.type == "hidden")?false:true;
}
//--------------------------------------------------------------------------------------------------------//
