/*
  -------------------------------------------------------------------------

	Web-MA JavaScript Form Validator Version 1.0.5
	URL: http://www.web-ma.com/code/scroller
	
	Copyright (C) 2006 Web-MA Soluzioni Informatiche
	http://www.web-ma.com

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	The GNU license to which this software is referering is available at:
	http://www.gnu.org/licenses/gpl.txt

  -------------------------------------------------------------------------

	Version history:

  	* 1.0.0 - 04/02/2006 - Initial release;

	* 1.0.1 - 04/04/2006 - Minor bug fix;

	* 1.0.2 - 04/08/2006 - Added new small function for validate input
				     fields;

	* 1.0.3 - 04/19/2006 - Added support for CSS ClassName property;

	* 1.0.4 - 05/05/2006 - Minor bug fix: while re-submitting form, if a
				     previous error has been generated and then
				     fixed, error message wasn't clear.

	* 1.0.5 - 03/02/2007 - Minor bug fix: while re-submitting form, if a
				     previous error has been generated and then
				     fixed, forms doesn't submitted to the action
				     page.
  -------------------------------------------------------------------------
*/

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";

				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

	if(item.value.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 == 0)
		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;
}
         	

function RadioFieldValidator(item, param)
{
	if(item.checked){
		return true;
	}

	CheckArray = document.getElementsByName(item.name)

	for(i = 0; i < CheckArray.length; i++){
		if(CheckArray[i].checked){
			return true;
		}
	}
	//if (param == null)
		//param = 0;

	//if (item.selectedIndex == param)
		//return false;

	return false;
}

/*
	Copyright 2006 www.web-ma.com. All rights reserved.
*/