
var currentSpan = "";

function Validator(frmName)
{
	this.frmObj = document.getElementById(frmName);

	if(!this.frmObj)
	{
		alert("Could not get " + frmName + " object." );
			return;
	}

	if(this.frmObj.onsubmit)
	{
		this.frmObj.old_onsubmit = this.frmObj.onsubmit;
		this.frmObj.onsubmit = null;
	}
	else
	{
		this.frmObj.old_onsubmit = null;
	}

	this.frmObj.onsubmit = _SubmitEventHandler;
	this.AddValidator = AddValidator;
	this.ExtraValidatorFunction = _ExtraValidatorFunction;
	this.clearValidators = _clearValidators;
}

function _ExtraValidatorFunction(functionname)
{
  this.frmObj.ExtraValidatorFunction = functionname;
}

function _clearValidators()
{
	for(var i=0;i < this.frmObj.elements.length;i++)
	{
		this.frmObj.elements[i].ValidatorCollection = null;
	}
}

function _SubmitEventHandler()
{
	var someFails = false;
	var validationResult = true;

	for(var i = 0; i < this.elements.length; i++)
	{
		if(this.elements[i].ValidatorCollection)
			someFails = this.elements[i].ValidatorCollection.validate();

		if (someFails)
			validationResult = false;
	}

	if(validationResult && this.ExtraValidatorFunction)
     	validationResult = eval(this.ExtraValidatorFunction + "()");

	return validationResult;
}

function AddValidator(item, validatorFunction, validateParam, errorMsg, errorDisplayMode, onErrorFocus, style)
{
  	if(!this.frmObj)
	{
	     alert("The form object is not set properly");
	     return;
	}

	var frmField = this.frmObj[item];

 	if(!frmField)
	{
	     alert("Could not get the " + item + " form element.");
	     return;
	}

	if(!frmField.ValidatorCollection)
	{
		frmField.ValidatorCollection = new ValidatorCollection(frmField);
	}

	frmField.ValidatorCollection.add(validatorFunction, validateParam, errorMsg, errorDisplayMode, onErrorFocus, style);
}

function ValidatorCollection(inputitem)
{
	this.ValidArray = new Array();
	this.add = _AddValidator;
	this.validate = Validate;
	this.frmField = inputitem;
}

function _AddValidator(validatorFunction, validateParam, errorMsg, errorDisplayMode, onErrorFocus, style)
{
	this.ValidArray[this.ValidArray.length] =
		new AddValidationControl(this.frmField, validatorFunction, validateParam, errorMsg, errorDisplayMode, onErrorFocus, style);
}

function AddValidationControl(item, validatorFunction, validateParam, errorMsg, errorDisplayMode, onErrorFocus, style)
{
	this.errorDisplayMode = errorDisplayMode;
	this.errorMsg = errorMsg;
	this.item = item;
	this.validator = validatorFunction;
	this.validateParam = validateParam;
	this.validate = _Validate;
	this.onErrorFocus = onErrorFocus;
	this.style = style;
}

function Validate()
{
	// this routine validate all controls in the form
	var someFails = false;

	for(var i = 0; i < this.ValidArray.length; i++)
	{
		if(!someFails)
		{
	           if (this.ValidArray[i].errorMsg != null && this.ValidArray[i].errorMsg != "")
	                 currentSpan = _CreateSpanError(this.ValidArray[i]);

	           if(!this.ValidArray[i].validate())
	                 someFails = true;
	      }
	}

	if (someFails)
	{
		return true;
	}

	else
	{
		currentSpan.style.visibility = "hidden";
		return false;
	}
}

function _Validate()
{
	if(!this.validator(this.item, this.validateParam))
	{
		switch(this.errorDisplayMode)
		{
			case 'left':
			case 'right':
				//currentSpan.innerHTML = this.errorMsg;
			//	currentSpan.style.visibility = "visible";
				var tem  = document.getElementById("searchValueError");
				tem.innerHTML=this.errorMsg;

				break;

			case 'alert':
				alert(this.erroMsg);
				break;
		}

		if (this.onErrorFocus == true)
			this.item.focus();

		return false;
	}

	return true;
}

function _CreateSpanError(item)
{
	var errorSpan = eval("document.getElementById('" + item.item.id + "Val')");

	if (errorSpan == null)
	{
		var errorSpan = document.createElement("span");
		errorSpan.id = item.item.id + 'Val';

		var curEl = document.getElementById(item.item.id);
		var parent = curEl.parentNode;

		if (item.errorDisplayMode == 'right')
			parent.insertBefore(errorSpan, curEl.nextSibling);

		else
			parent.insertBefore(errorSpan, curEl);

		// Manage the style;
		var styles = item.style.split(";");
		var styleEl = "";
		var i = 0;
		while (i < styles.length - 1)
		{
			styleEl = styles[i].split(":");

			if (styleEl[0] == "className")
			{
				eval("errorSpan." + styleEl[0] + "= '" + styleEl[1] + "';");
				break;
			}

			else
				eval("errorSpan.style." + styleEl[0] + "= '" + styleEl[1] + "';");
			i += 1;
		}
	}

	return errorSpan;
}

//***********************************************************************//
// Validators: you can modify the name as well as the functionality of   //
// single validators according to your exigencies.								 //
// Remember to change the name of the called function into html page.	 //
//***********************************************************************//
function RequiredFieldValidator(item, param)
{
	// Supply a min value for the length of the field
   var trimmed = item.value.replace(/^\s+|\s+$/g, '') ;

	if(trimmed.length <= param)
	{
		return false;
	}

	return true;
}

function CompareFieldValidator(item, param)
{
	// Supply an input field name in the param value

	var param = eval("document.getElementById(param)");

	if(item.value != param.value)
	{
		return false;
	}

	return true;
}

function MaxLengthFieldValidator(item, param)
{
	// Supply the max length that control should not override

	if(item.value.length > param)
	{
		return false;
	}

	return true;
}

function CheckBoxFieldValidator(item, param)
{
	// Param isn't used;

	if(item.checked == true)
	{
		return true;
	}

	return false;
}

function NumericFieldValidator(item, param)
{
	// Supply a valid regular expression param
	if (param == null)
		param = "[^0-9]";

	if (item.value.search(param) >= 0)
		return false;

	return true;
}

function AlphaFieldValidator(item, param)
{
	// Supply a valid regular expression param
	if (param == null)
		param = "[^A-Za-z]";

	if (item.value.search(param) >= 0)
		return false;

	return true;
}

function AlphaNumericFieldValidator(item, param)
{
	// Supply a valid regular expression param

	if (param == null)
		param = "[^A-Za-z0-9]";

	if (item.value.search(param) >= 0)
		return false;

	return true;
}

function SelectedIndexFieldValidator(item, param)
{
	// Param isn't used;

	if (item.selectedIndex == -1)
		return false;

	return true;
}

function EmailFieldValidator(item, param)
{
	if (param == null)
		param = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

	//var re = new RegExp(param);

	return param.test(item.value);
}

function ComboBoxFieldValidator(item, param)
{
	if (param == null)
		param = 0;

	if (item.selectedIndex == param)
		return false;

	return true;
}

/*
	Copyright 2006 www.web-ma.com. All rights reserved.
*/
