
/*******************************************************
Author :	Martin Halla
Created : 	November 23rd 2008
Description : 	Javascript functionality to limit the 
		amnount of characters in the textarea
********************************************************/
jQuery(document).ready(function(){
	
	// these are the settings
	var limit 			= 1500; 
	var init_text 		= "Characters used : 0/" + limit; 
	var message 		= "You can only type in " + limit  +  " characters ";
	var textarea		= "#comment";
	var display_div		= "#character_counter_display";
	
	// shoe the initial message
	jQuery(display_div).text(init_text);
	// bind the function to keyup
	jQuery(textarea).keyup(function(){
		
		test_char_limit(textarea,limit,display_div,message);
		
	});
}); 


/*
@name test_char_limit
@param textarea 	String 	the DOM id of the textarea to be limited
@param limit		Int 	the amount of characters to allow
@param display_div 	String	the DOM id of the div where to display the message
@param message	 	String	the message to be alerted when the user reaches the limit

note : Bedore the usage, make sure the document has the div to show the message and the textarea
	has a name or ID
*/
function test_char_limit(textarea,limit,display_div,message) {
	// get the value of the textarea
	var value = jQuery(textarea).val();
	// count its length
	var count = value.length;
	
	// if it is over the limit, display the message
	if(count > limit) {
		jQuery(textarea).val(value.substring(0,limit));
		

		// AGAIN => get the value of the textarea
		var value = jQuery(textarea).val();
		// AGAIN => count its length
		var count = value.length;
		alert(message);
		return false;
	}
	
	//update the display div with the amount of characters used
	var the_text = "Characters used : "+ count + "/" + limit; 
	jQuery(display_div).text(the_text);
}
