	// Created by: james.hutt@nienclub.co.uk
	// Created in: November 2009
	//
	// PURPOSE:
	// Generic functions that deal with TEXTAREA form elements 'only'.
	// 1: replaceCarriageReturn.
	// This function, as the name implies, removes carriage returns from TEXTAREA form elements.
	//
	// 2: TEXTAREA - MAXLENGTH.
	// Normally, the MAXLENGTH attribute is not valid. However, this small section of jQuery code
	// ensures that a user cannot type or paste more that they are allowed to into a TEXTAREA form element.

	function replaceCarriageReturn(textarea, replaceWith) {
		var area = escape(textarea); //encode all characters in text area to find carriage return character
		if(area.indexOf("%0D%0A") > -1) {
			// Windows encodes returns as \r\n hex
			area=area.replace(/0D%0A*/g,replaceWith)
		} else if(area.indexOf("%0A") > -1) {
			// Unix encodes returns as \n hex
			area=area.replace(/%0A*/g,replaceWith)
		} else if(area.indexOf("%0D") > -1) {
			// Macintosh encodes returns as \r hex
			area=area.replace(/%0D*/g,replaceWith)
		}
		return unescape(area);
	}

	$(document).ready( function() {
		// Ensure that the maxlength attribute is forced on all textareas.
		$('textarea[maxlength]').keyup(function() {  
			// Get the limit from maxlength attribute  
			var limit = parseInt($(this).attr('maxlength'));  
			// Get the current text inside the textarea  
			var text = $(this).val();  
			// Count the number of characters in the text  
			var chars = text.length;  
			
			// Check if there are more characters then allowed  
			if(chars > limit) {  
				// And if there are use substr to get the text before the limit  
				var new_text = text.substr(0, limit);  
				
				// And change the current text with the new text  
				$(this).val(new_text);  
			}  
		});
	});