/*
 * 
 * Textarea Word Count Jquery Plugin 
 * Version 1.0
 * 
 * Copyright (c) 2008 Roshan Bhattarai
 * website : http://roshanbh.com.np
 * 
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 * 
*/

jQuery.fn.wordCount = function(params){
	var p = {
		counterElement:"display_count"
	};
	var total_words;
	
	if(params) {
		jQuery.extend(p, params);
	}
	var words;
	//for each keypress function on text areas
	this.keypress(function() { 
		if(this.value.split(" ").join("") != "") {
			words = this.value.split(/[\,\?]+/,5);
			total_words=this.value.split(/[\,\?]+/).length;
			jQuery('#'+p.counterElement).html("Total Keywords : " + total_words);	
		}
		else {
			jQuery('#'+p.counterElement).html("");	
		}
		if(total_words >= 6) {
			alert("You cant allow to enter more words");
			this.value = words.join(",");
			words = this.value.split(/[\,\?]+/);
			total_words=this.value.split(/[\,\?]+/).length;
			jQuery('#'+p.counterElement).html("Total Keywords : " + total_words);
			return false
		}			
	});	
	this.blur(function() { 
		if(this.value.split(" ").join("") != "") {
			words = this.value.split(/[\,\?]+/,5);
			total_words=this.value.split(/[\,\?]+/).length;
			jQuery('#'+p.counterElement).html("Total Keywords : " + total_words);	
		}
		else {
			jQuery('#'+p.counterElement).html("");	
		}
		if(total_words >= 6) {
			this.value = words.join(",");
			words = this.value.split(/[\,\?]+/);
			total_words=this.value.split(/[\,\?]+/).length;
			jQuery('#'+p.counterElement).html("Total Keywords : " + total_words);
			return false
		}			
	});	
};
jQuery.fn.charCount = function(params){
	var p = {
		counterElement:"display_charcount"
	};
	var total_words;
	
	if(params) {
		jQuery.extend(p, params);
	}
	var words;
	//for each keypress function on text areas
	this.keyup(function() { 
		if(this.value.length > decsr_limit) {
			this.value = this.value.substring(0,decsr_limit);
		}
		jQuery('#'+p.counterElement).html(decsr_limit-this.value.length + " / " + decsr_limit);			
	});	
	this.blur(function() { 
		if(this.value.length > decsr_limit) {
			this.value = this.value.substring(0,decsr_limit); 
		}
		jQuery('#'+p.counterElement).html(decsr_limit-this.value.length + " / " + decsr_limit);
	});	
};
