/*
  -------------------------------------------------------------------------
	                    JavaScript Form Validator 
                                Version 2.0.2
	Copyright 2003 JavaScript-coder.com. All rights reserved.
	You use this script in your Web pages, provided these opening credit
    lines are kept intact.
	The Form validation script is distributed free from JavaScript-Coder.com

	You may please add a link to JavaScript-Coder.com, 
	making it easy for others to find this script.
	Checkout the Give a link and Get a link page:
	http://www.javascript-coder.com/links/how-to-link.php

    You may not reprint or redistribute this code without permission from 
    JavaScript-Coder.com.
	
	JavaScript Coder
	It precisely codes what you imagine!
	Grab your copy here:
		http://www.javascript-coder.com/
    -------------------------------------------------------------------------  
*/

/// FormValidator class.
function FormValidator(formName)
{
  this.form = document.forms[formName];
	if (!this.form)	{
		alert('BUG: There is no form named "'+formName+'"');
		return;
	}

	if (this.form.onSubmit) {
		this.form.old_onsubmit = this.form.onSubmit;
		this.form.onSubmit     = null;
	} else
		this.form.old_onsubmit = null;

	/// Adds a inputItem.for validation.
	this.add = function(inputItemName, parameter, errorMessage)
	{
		if (!this.form)
		{
			alert('BUG: Create first a FormValidator object for the form containing the input item "'+inputItemName+'"');
			return;
		}
	
		var inputItem = this.form[inputItemName];
		if (!inputItem)
		{
			alert('BUG: There is no input item named "'+inputItemName+'"');
			return;
		}
	
		if (!inputItem.validators)
			inputItem.validators = new FormValidatorSet(inputItem);
		inputItem.validators.add(parameter, errorMessage);
	}

	/**
	 * Returns if the form inputItem. are valid.
	 *
	 * @return Boolean true if valid; otherwise false
	 */
	this.isValid = function()
	{
		for(var itr = 0; itr < this.form.elements.length; itr++)
		{
			if (this.form.elements[itr].validators &&
					!this.form.elements[itr].validators.isValid())
				return false;
		}
		return true;
	}
}

/// Form validator set.
function FormValidatorSet(inputItem)
{
	this.set       = new Array();
	this.inputItem = inputItem;

	this.add = function(parameter, errorMessage)
	{
		this.set[this.set.length] = new FormValidatorItem(this.inputItem, parameter, errorMessage);
	}

	this.isValid = function()
	{
		 for(var i=0; i < this.set.length; i++) {
			 if (!this.set[i].isValid())
				 return false;
		 }
		 return true;
	}
}

/// Form validator item
function FormValidatorItem(inputItem, parameter, errorMessage)
{
	this.inputItem    = inputItem;
  this.parameter    = parameter;
	this.errorMessage = errorMessage;

	/// Returns if the content of the inputItem.is valid.
	this.isValid = function()
	{
		if (!this.isValidData()) {
			this.inputItem.focus();
			return false;
		}
		return true;
	}

	this.isValidData = function() 
	{ 
		var epos         = this.parameter.search("=");
		var command      = "";
		var commandValue = "";
		if (epos >= 0) { 
			command      = this.parameter.substring(0, epos); 
			commandValue = this.parameter.substr(epos + 1); 
		} else
			command = this.parameter; 
	
		switch (command) { 
			case "req": 
			case "required": 
				if (eval(this.inputItem.value.length) == 0) { 
					if (!this.errorMessage || this.errorMessage.length == 0) 
						this.errorMessage = this.inputItem.name + ': Required'; 
					alert(this.errorMessage); 
					return false; 
				}
				break;             
			case "maxlength": 
			case "maxlen": 
				if (eval(this.inputItem.value.length) >  eval(commandValue)) { 
					if (!this.errorMessage || this.errorMessage.length == 0) 
						this.errorMessage = this.inputItem.name + ': '+commandValue+' characters maximum'; 
					alert(this.errorMessage); 
					return false; 
				}
				break; 
			case "minlength":
			case "minlen":
				if (eval(this.inputItem.value.length) <  eval(commandValue)) { 
					if (!this.errorMessage || this.errorMessage.length == 0) 
						this.errorMessage = this.inputItem.name + ': ' + commandValue + ' characters minimum'; 
					alert(this.errorMessage); 
					return false;                 
				}
				break; 
			case "alnum": 
			case "alphanumeric": 
				var charpos = this.inputItem.value.search("[^A-Za-z0-9]"); 
				if (this.inputItem.value.length > 0 &&  charpos >= 0) { 
					if (!this.errorMessage || this.errorMessage.length == 0) 
						this.errorMessage = this.inputItem.name+': Only alpha-numeric characters allowed'; 
					alert(this.errorMessage); 
					return false; 
				}
				break; 
			case "num": 
			case "numeric": 
				var charpos = this.inputItem.value.search("[^0-9]"); 
				if (this.inputItem.value.length > 0 &&  charpos >= 0) { 
					if (!this.errorMessage || this.errorMessage.length == 0) 
						this.errorMessage = this.inputItem.name+': Only digits allowed'; 
					alert(this.errorMessage); 
					return false; 
				}
				break;               
			case "alphabetic": 
			case "alpha": 
				var charpos = this.inputItem.value.search("[^A-Za-z]"); 
				if (this.inputItem.value.length > 0 &&  charpos >= 0) { 
					if (!this.errorMessage || this.errorMessage.length == 0) 
						this.errorMessage = this.inputItem.name+': Only alphabetic characters allowed'; 
					alert(this.errorMessage); 
					return false; 
				}
				break; 
			case "alnumhyphen":
				var charpos = this.inputItem.value.search("[^A-Za-z0-9\-_]"); 
				if (this.inputItem.value.length > 0 &&  charpos >= 0) { 
					if (!this.errorMessage || this.errorMessage.length == 0) 
						this.errorMessage = this.inputItem.name+': characters allowed are A-Z,a-z,0-9,- and _'; 
					alert(this.errorMessage); 
					return false; 
				}
				break;
			case "email": 
				if (!this.isValidEmail()) { 
					if (!this.errorMessage || this.errorMessage.length == 0) 
						this.errorMessage = this.inputItem.name+': Enter a valid Email address';
					alert(this.errorMessage); 
					return false;
				}
				break; 
			case "lt": 
			case "lessthan": 
				if (isNaN(this.inputItem.value)) { 
					alert(this.inputItem.name+": Should be a number "); 
					return false;
				}
				if (eval(this.inputItem.value) >=  eval(commandValue)) { 
					if (!this.errorMessage || this.errorMessage.length == 0)
						this.errorMessage = this.inputItem.name+': value should be less than '+ commandValue; 
					alert(this.errorMessage); 
					return false;
				}
				break; 
			case "gt": 
			case "greaterthan": 
				if (isNaN(this.inputItem.value)) { 
					alert(this.inputItem.name+": Should be a number "); 
					return false;
				}
				if (eval(this.inputItem.value) <=  eval(commandValue)) { 
					if (!this.errorMessage || this.errorMessage.length == 0) 
						this.errorMessage = this.inputItem.name + ': value should be greater than '+ commandValue; 
					alert(this.errorMessage); 
					return false;                 
				}
				break; 
			case "regexp": 
				if (this.inputItem.value.length > 0
						&& !this.inputItem.value.match(commandValue)) { 
					if (!this.errorMessage || this.errorMessage.length == 0) 
						this.errorMessage = this.inputItem.name+': Invalid characters found'; 
					alert(this.errorMessage); 
					return false;                   
				}
				break; 
			case "dontselect": 
				if (this.inputItem.selectedIndex == null) { 
					alert('BUG: You add a dontselect validator for a non-select input item'); 
					return false; 
				} 
				if (this.inputItem.selectedIndex == eval(commandValue)) { 
					if (!this.errorMessage || this.errorMessage.length == 0) 
						this.errorMessage = this.inputItem.name+': Please Select one option'; 
					alert(this.errorMessage); 
					return false;                                   
				} 
				break; 
		}
		return true; 
	}

	/// Returns if the given email address is valid.
	this.isValidEmail = function()
	{
		if (this.inputItem.value.length <= 0)
			return true;
	
		var splitted = this.inputItem.value.match('^(.+)@(.+)$');
		if (splitted == null
				|| splitted[1] == null
				|| splitted[2] == null)
			return false;

		if (splitted[1].match(/^\"?[\w-_\.]*\"?$/) == null)
			return false;

		if (splitted[2].match(/^[\w-\.]*\.[A-Za-z]{2,4}$/) == null
			  && splitted[2].match(/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/) == null)
			return false;

		return true;
	}
}

