var VALIDATION_IS_REQUIRED = true;
/* define text message strings - you can move these to move i18n location if needed */
var ERROR_HEADER = "Please correct the following errors";
var FIELD_IS_REQUIRED_MESSAGE = "is a required field";

function CBvalidator()
{
	this.formname = new String();
	this.inputIsValid = true;
	this.errormessage = new String();
	this.firstErrorField = new String();
	this.requiredItems = new Array();
	this.requiredLabels = new Array();
	this.requiredEither = new Array();
	this.requiredEitherLabels = new Array();
	this.warningItems = new Array();
	this.warningLabels = new Array();
	this.requireItem = requireItem;
	this.removeRequiredItem = removeRequiredItem;
	this.requireEither = requireEither;
	this.removeRequireEither = removeRequireEither;
	this.warnItem = warnItem;
	this.isValidForm = validateForm;
	this.setFirstErrorField = setFirstErrorField;
	this.resetToTestAgain = function() {
				this.inputIsValid = true;
				this.errormessage = "";
				this.firstErrorField = "";
				}
	this.issueError = function() {
				alert(ERROR_HEADER + "\n\n------------------------------------\n\n" + this.errormessage);
				}
}

var validator = new CBvalidator();

function requireItem(itemname,itemlabel)
{
	this.requiredItems.push(itemname);
	this.requiredLabels.push(itemlabel);
}
function removeRequiredItem(itemname)
{
	for (i=0;i<this.requiredItems.length;i++)
	{
		if (this.requiredItems[i] == itemname)
		{
			this.requiredItems[i] = "";
			this.requiredLabels[i] = "";
		}
	}
}
function requireEither()
{
	var a = requireEither.arguments;
	if (a.length < 3) return false;
	var eitherOr = new Array();
	for (i=1;i<a.length;i++)
	{ eitherOr.push(a[i]); }
	this.requiredEither.push(eitherOr);
	this.requiredEitherLabels.push(a[0]);
}
function removeRequireEither(itemname)
{
	for (i=0;i<this.requiredEither.length;i++)
	{
		if (this.requiredEither[i][0] == itemname)
		{
			this.requiredEither[i] = "";
			this.requiredEitherLabels[i] = "";
		}
	}
}

function warnItem(itemname,itemlabel)
{
	this.warningItems.push(itemname);
	this.warningLabels.push(itemlabel);
}
function setFirstErrorField(fldname)
{
	if (this.firstErrorField == "")
		this.firstErrorField = fldname;
}	
 // Next series are the individual components for each type of form input
var anychar = new RegExp(/\S/);
function isEmptyText(field)
{
	if (field.type.toLowerCase() == "checkbox")
	{
		if (!field.checked)
			return true;
		return false;
	}
	if (field.value == "")
		return true;
	if (field.value.match(anychar) == null)
		return true;
	return false;
}

function isEmptySelect(field)
{
	if (field.options[field.selectedIndex].value == "")
		return true;
	return false;
}
 // Main validator
function validateForm()
{
var vformelements = document.forms[this.formname].elements;
var felement;
 // Reset this for second submission
this.resetToTestAgain();
	for (i=0;i<this.requiredItems.length;i++)
	{
		if (this.requiredItems[i] == "") // Item was removed with obj.removeRequiredItem()
			continue;
		if ((parseFloat(this.requiredItems[i]) + "") == this.requiredItems[i]) // numeric "name" value cause DOM issue - try to use ID
			felement = document.getElementById(eval("'text"+this.requiredItems[i]+"'"));
		else // default DOM document.form.element reference
			felement = vformelements[this.requiredItems[i]];
		if (!felement || (felement.toString() == "undefined")) // could not get item - continue
			continue;
		if ( isEmptyText( felement ) )
		{
			this.errormessage += this.requiredLabels[i] + " " + FIELD_IS_REQUIRED_MESSAGE + " \n";
			this.inputIsValid = false;
			this.setFirstErrorField( this.requiredItems[i] );
		}
	}
	for (i=0;i<this.requiredEither.length;i++)
	{
		var curgroup = this.requiredEither[i];
		if (curgroup == "")	continue;
		var curgroupOK = false;
		for (f in curgroup)
		{
			if ( ! isEmptyText( vformelements[curgroup[f]] ) )
				curgroupOK = true;
		}
		if (! curgroupOK)
		{
			this.errormessage += this.requiredEitherLabels[i] + " \n";
			this.inputIsValid = false;
			this.setFirstErrorField( curgroup[0] );
		}
	}
	if ( ! this.inputIsValid ) // Don't bother with warnings if there are errors
	{
		this.issueError();
		return false;
	}
	for (i=0;i<this.warningItems.length;i++)
	{
		if (! this.inputIsValid) // Once a single warning has been "Canceled" the loop should break.
			break;
		if ( vformelements[this.warningItems[i]].value == "" )
		{
			this.inputIsValid = confirm(this.warningLabels[i]);
			this.setFirstErrorField( this.warningItems[i] );
		}
	}
	return this.inputIsValid;
}
