function formValidator(frmName)
{
	this.validations = new Array();				//the elements that require validation
	this.formObj = document.forms[frmName];		//form object

	if(!this.formObj)
	{
		alert("BUG: couldnot get Form object "+frmName);
		return;
	}

	//validate function
	//must be added to onsubmit attribute of the FORM tag
	//example: onsubmit="return frmValidationObject.validate;"
	this.validate = function () 
	{
		for(var itr=0; itr < this.validations.length; itr++)
		{
			formElement = this.validations[itr][0];
			//popUpProperties(validations[itr]);
			objValue = this.formObj.elements[formElement]; //form element; 'this' object is the form object
			switch(this.validations[itr][1][0]) // validation option
			{
			//case required 				
				case "req": 
				case "required": 
				{ 
					if(eval(objValue.value.length) == 0) 
					{ 
						window.alert(this.validations[itr][2]); // show alert
						objValue.focus();
						return false; 
					}
				break;             
				}
				//case maxlen
				//second element of the options array is the maximum allowed length
				case "maxlength": 
				case "maxlen": 
				{ 
					if(eval(objValue.value.length) > this.validations[itr][1][1]) 
					{ 
						window.alert(this.validations[itr][2]); // show alert
						objValue.focus();
						return false;
					}
				break; 
				}
				//case minlen
				//second element of the options array is the minimum allowed length
				case "minlength": 
				case "minlen": 
				{ 
					if(eval(objValue.value.length) < this.validations[itr][1][1]) 
					{ 
						window.alert(this.validations[itr][2]); // show alert
						objValue.focus();
						return false; 
					}
				break; 
				}
				//only alphabetic and numeric characters
				case "alphanum": 
				case "alphanumeric": 
				{ 
					var charpos = objValue.value.search("[^A-Za-z0-9]"); 
					if(objValue.value.length > 0 && charpos >= 0) 
					{ 
						window.alert(this.validations[itr][2]); // show alert
						objValue.focus();
						return false; 
					}
				break; 
				}
				//only numbers
				case "num": 
				case "numeric": 
				{ 
					var charpos = objValue.value.search("[^0-9]"); 
					if(objValue.value.length > 0 &&  charpos >= 0) 
					{ 
						window.alert(this.validations[itr][2]); // show alert
						objValue.focus();
						return false; 
					}
				break;               
				}
				//only alphabetic characters
				case "alpha": 
				case "alphabetic": 
				{ 
					var charpos = objValue.value.search("[^A-Za-z]"); 
					if(objValue.value.length > 0 &&  charpos >= 0) 
					{ 
						window.alert(this.validations[itr][2]); // show alert
						objValue.focus();
						return false; 
					}
				break; 
				}
				//only alphabetic, numeric and hyphen characters
				case "alnumhyphen":
				{
					var charpos = objValue.value.search("[^A-Za-z0-9\-_]"); 
					if(objValue.value.length > 0 &&  charpos >= 0) 
					{ 
						window.alert(this.validations[itr][2]); // show alert
						objValue.focus();
						return false; 
					}
				break;
				}
				//email format
				case "email": 
				{
					str = objValue.value;
					if (str.search("^([a-zA-Z0-9_]|\\-|\\.)+@(([a-zA-Z0-9_]|\\-)+\\.)+[a-zA-Z]{2,4}\$") == -1)
					{ 
						window.alert(this.validations[itr][2]); // show alert
						objValue.focus();
						return false; 
					}
				break; 
				}
				//number less than a certain value
				//second element of the options array is the maximum allowed value
				case "lt": 
				case "lessthan": 
				{ 
					if(eval(objValue.value) >= this.validations[itr][1][1]) 
					{ 
						window.alert(this.validations[itr][2]); // show alert
						objValue.focus();
						return false; 
					}
				break; 
				}
				//number bigger than a certain value
				//second element of the options array is the minimum allowed value
				case "gt": 
				case "greaterthan": 
				{ 
					if(eval(objValue.value) <= this.validations[itr][1][1]) 
					{ 
						window.alert(this.validations[itr][2]); // show alert
						objValue.focus();
						return false; 
					}
				break; 
				}
				//match against a regular expression
				//second element of the options array is the regular expression
				case "regexp": 
				{ 
					if(objValue.value.length > 0)
					{
						if(!objValue.value.match(this.validations[itr][1][1])) 
						{ 
							window.alert(this.validations[itr][2]); // show alert
							objValue.focus();
							return false; 
						}
					}
				break; 
				}
				//mandatory selection from a list
				//second element of the index of the null element
				case "dontselect": 
				{ 
					if(objValue.selectedIndex == null) 
					{ 
						alert("BUG: dontselect command for non-select Item"); 
						objValue.focus();
						return false; 
					} 
					if(objValue.selectedIndex == this.validations[itr][1][1]) 
					{ 
						window.alert(this.validations[itr][2]); // show alert
						return false; 
					} 
				break; 
				}
			}
		}
		return true;
	}

	//add validation rule to the form validation object
	this.addValidation = function(formElement, validationOptions, errorMessage)
	{
		if(!this.formObj)
		{
			alert("BUG: the form object is not set properly");
			return;
		}//if
		var itemObj = this.formObj[formElement];
		if(!itemObj)
		{
			alert("BUG: Couldnot get the input object named: " + formElement);
			return;
		}
		this.validations[this.validations.length] = new Array(formElement, validationOptions, errorMessage);
	}

	this.clearAllValidations = function()
	{
		for(var itr=0; itr < this.validations.length; itr++)
		{
			this.validations[itr] = null;
		}
	}
}


