jQuery.fn.limitWords = function(params){
  var p = {
    infodiv:"display_words",
    display_counter: true,
    limit: 150
  };

  var total_words;
  var chars_in_range;

  if(params) {
    jQuery.extend(p, params);
  }

  //for each keypress function
  this.keyup(function()
  {
    total_words = this.value.split(/[\s\.\?]+/).length;
    var text = this.value;
    if(total_words > p.limit) {
      $('#' + p.infodiv).html(p.limit + ' word limit');
      // Get total # of chars and restrict
      this.value = text.substr(0,chars_in_range);
    }else{
      chars_in_range = this.value.length;
      if (p.display_counter){
	$('#' + p.infodiv).html(p.limit - total_words);
      }
      return true;
    }
  });
};


jQuery.fn.limitChars = function(params){

  var p = {
    infodiv:"display_chars",
    limit: 255
  };

  if(params) {
    jQuery.extend(p, params);
  }

  this.keyup( function() {

    var text = this.value;
    var textlength = text.length;

    if(textlength > p.limit) {
      $('#' + p.infodiv).html(p.limit + ' character limit');
      this.value = text.substr(0,p.limit);
      return false;
    }
    else {
      $('#' + p.infodiv).html(p.limit - textlength);
      return true;
    }

  });

};
