/*
Filename: \js\strings.js
Programmer: Carlos Vazquez (carlos@mojointeractive.com)
Date:
Purpose: A Collection of javscript functions that perform operations on strings
Document Location:
*/

/* Trims space from the beginning and end of a string */
function trimAll(sString)
{
	while (sString.substring(0,1) == ' ')
	{
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' ')
	{
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}

/* Returns a word count of text passed in. Removes any html tags first */
function GetWordCount(gText)
{
	var mystring = gText.replace(/<[^>]*>/g, '');
	mystring = mystring.replace(/&nbsp;/g, '');
	mystring = mystring.replace("  ", ' ');
	mystring = mystring.replace("   ", ' ');
	mystring = trimAll(mystring);
	var stringArray = mystring.split(" ");
	for (var lw = 0; lw < stringArray.length; lw++)
	{
		if (stringArray[lw].length == 0)
		{
			stringArray.splice(lw,1);
		}
	}
	var wordCount = stringArray.length;
	return (wordCount);
}

/* Limits the text in a field to a specified amount. Accepts the fields name and prevents anymore typing once the limit is reached. */
function limitText(limitField, limitCount, limitNum)
{
	if (limitField.value.length > limitNum)
	{
		limitField.value = limitField.value.substring(0, limitNum);
	}
	else
	{
		limitCount.value = limitNum - limitField.value.length;
	}
}

/* Return the number of words in a document element passed, with the max words expected */
function TextAreaWordCount(element, DisplayField, MaxWords)
{
	var NumberOfWords = GetWordCount(element.value);
	if (NumberOfWords > MaxWords)
	{
		element.value = element.value.substring(0, (element.value.length - 1));
		NumberOfWords = GetWordCount(element.value);
	}
	DisplayField.value = NumberOfWords;
}

function TextAreaCharCount(element, DisplayField, MaxChars)
{
	var NumberOfChars = element.value.length;
	if (NumberOfChars > MaxChars)
	{
		element.value = element.value.substring(0, MaxChars);
		NumberOfChars = element.value.length;
	}
	DisplayField.value = NumberOfChars;
}

