/**
* This function checks the length of a field against a maximum value and sets the current number of characters
* 
* displayElement: The element that will display the number of characters left; it's entire innerHTML is replaced
* textAreaElement: the field containing the value whose length we are checking
* maxLength: the maximum length of the field value
* postText: Any post text that should be put on the end of the numerical characters remaining (such as 'characters left')
*
*/
function checkLength(displayElement, textAreaElement, maxLength, postText) {
    
	if (typeof textAreaElement != "undefined") {
	
		// We have to do all of this because of the way different browsers deal with carriage returns
		countString = textAreaElement.value.replace (/\r\n/g, '\n') ;
		countString = countString.replace (/\n/g, '\n\r') ;
		
		// If they have entered past the max, make them stop by rewriting the string
		if (countString.length > maxLength) {
			if (countString.length != textAreaElement.value.length) {
				textAreaElement.value = countString.substr(0,maxLength).replace(/\n\r/g, '\n');
			} else {
				textAreaElement.value = field.value.substr (0, maxLength) ;
			}
		} else {
			displayElement.innerHTML = maxLength - countString.length + " " + postText;
		}
	}
}
