// $Id: counters.js,v 1.2 2006/03/27 16:24:11 chrisgraham Exp $

/*
*	Each counter in the array is an array of counterId, fieldId, counterMax
*/
function initializeCounters(counters)
{
	// Show the javascript counters
	counterDivs = getElementsByClassName('counterJavascript');

	for (i = 0; i < counterDivs.length; i++)
	{
		counterDivs[i].style.display = "inline";
	}

	// Initialize the counters based on their initial values
	for (var key in counters)
	{
		updateCounter(counters[key]['counterId'], counters[key]['fieldId'], counters[key]['counterMax']);
	}
}

function updateCounter(counterId, fieldId, counterMax)
{
	newValue = counterMax - document.getElementById(fieldId).value.length;

	if (newValue < 0)
	{
		document.getElementById(fieldId).value = document.getElementById(fieldId).value.substring(0, counterMax);
		newValue = 0;
	}
	
	if (counterId != null)
	{
		document.getElementById(counterId).value = newValue;
	}
}

function getElementsByClassName(classname)
{
	elements = new Array();
	alltags = document.all? document.all : document.getElementsByTagName('*');

	inc = 0;

	for (i = 0; i < alltags.length; i++)
	{
		if (alltags[i].className == classname)
		{
			elements[inc++] = alltags[i];
		}
	}

	return elements;
}

// $Log: counters.js,v $
// Revision 1.2  2006/03/27 16:24:11  chrisgraham
// Added a check to updateCounter to see if the counterId had been given (not null) before attempting to update its value
//
// Revision 1.1  2006/03/27 11:08:26  chrisgraham
// Initial revision
//