/* 
*  Author:  David Gailey
*  File name: ValidationHelper.js
*
*  provides functions for use in js form validation
*  			
*  REQUIRES ErrorMessages.js and className.js   
*
*  resetValidation - 	removes all error messages and validation styles in a page
*						element ids of validation elements in the page need to have a 'val_' prefix
*						error message html also has to be properly setup as per that function
*         
*  massAddClassName - 	accepts a single id or an array of ids then adds an array of classes to each element with that id   
*						if no array of classes is specified, the validationBorderCSSClasses array is used
*
*  textLimit -			puts a limit on the number of characters a user can type into a field\
*  						usage: <textarea cols="20" rows="2" value="" onkeypress="javascript:textLimit(this, 5);" name="test" id="test"></textarea>
*
*  validateRequired - 	makes sure something is contained in an input field, also checks for spaces and other characters
*
*/

	
//place the css class name(s) in this array
var validationBorderCSSClasses = new Array('validation_border','extra_padding','no_left_margin');


function resetValidation(classes)
{
	var i,n;
	
	//first remove any old error messages
	removeErrorMessages();
	
	//loop thru and remove any validationBorderCSSClasses classNames
	//these will be on <table>, <td>, and <div> elements with an id with a substring of 'val_' in it
	var valElms = new Array;
	
	//add all applicable <table>'s to array
	var x = document.getElementsByTagName('table');
	for(i=0;i<x.length;i++)
	{
		if(x[i].id.match('val_'))
		{
			valElms.push(x[i].id);
		}
	}
	
	//add all applicable <td>'s to array
	var x = document.getElementsByTagName('td');
	for(i=0;i<x.length;i++)
	{
		if(x[i].id.match('val_'))
		{
			valElms.push(x[i].id);
		}
	}
	//add all applicable <div>'s to array
	var x = document.getElementsByTagName('div');
	for(i=0;i<x.length;i++)
	{
		if(x[i].id.match('val_'))
		{
			valElms.push(x[i].id);
		}
	}
	
	//default to validationBorderCSSClasses array if a classes array is not supplied
	if(typeof(classes) == 'undefined') classes = validationBorderCSSClasses;
	
	//remove classNames from each element that has an id in the classes array
	for(i=0;i<valElms.length;i++)
	{
		for(n=0;n<classes.length;n++)
		{
			removeClassName(valElms[i],classes[n]);
		}
	}
}

function massAddClassName(elementIds,classes)
{
	//chk if elementIds is an array or not so we have the option of 
	//just passing one id into elementIds as a string when the function is called
	var ilength,i,n;;
	
	//console.info('constructor: ',elementIds.constructor.toString());
	if (elementIds.constructor.toString().indexOf('Array') == -1)
	{
		//console.info('elementIds: 1 :',elementIds);
		elementIds = new Array(elementIds); //make into array 
	}
	ilength = elementIds.length;
	//console.info('elementIds: 2 :',elementIds);	
	//console.info(elementIds.length,' : ',ilength);
	
	
	//default to validationBorderCSSClasses array if a classes array is not supplied
	if(typeof(classes) == 'undefined') classes = validationBorderCSSClasses;

	for(i=0;i<ilength;i++)
	{
		for(n=0;n<classes.length;n++)
		{
			addClassName(elementIds[i],classes[n]);
			//console.info('elementIds: 3 :',i,' :',n,' :',elementIds);
		}
	}
}

function textLimit(field, maxlen) 
{
	if (field.value.toString().length > maxlen)	field.value = field.value.substring(0, maxlen);
}

function validateRequired(fieldValue)
{
	if(false)
	{
		//function to better validate input fields
	}
}









