function CharCounter(field_id,max_chars) {
  this.field_id = field_id;
  this.max_chars = max_chars;
  this.chars_remaining = max_chars;

  if (typeof CharCounter._initialized == "undefined") {

    CharCounter.prototype.init = function () {
      var parentField = getRef(this.field_id);
      this.createCounter(parentField);

      parentField.pageObj = this;

      if (parentField.value.length) {
        this.countText(null,parentField); //if we are pre-populating
      }

      addEvent(parentField,'keypress',this.checkRemaining,false);
      addEvent(parentField,'keyup',this.countText,false);
    }

    CharCounter.prototype.createCounter = function (parentField) {
      var currCounter = document.getElementById("countDisplay");
      if (currCounter) {
        currCounter.parentNode.removeChild(currCounter);
      }

      var parentSize = this.getParentSize(parentField);
      var counterP = document.createElement('p');
      counterP.style["margin"] = "0";
      counterP.style["padding"] = "0";
      counterP.style["textAlign"] = "right";
      counterP.style["width"] = parentSize+"ex";
      counterP.id = "countDisplay";

      var currCount = document.createElement('span');
      currCount.id = "currCount";

      var startVal = document.createTextNode(this.max_chars);
      var promptText = document.createTextNode(' remaining');

      currCount.appendChild(startVal);
      counterP.appendChild(currCount);
      counterP.appendChild(promptText);

      // create sub-message space (for prompts)
      var promptP = document.createElement('p');
      promptP.style["margin"] = "0";
      promptP.style["padding"] = "0";
      promptP.style["textAlign"] = "right";
      promptP.style["width"] = parentSize+"ex";
      promptP.id = "promptP";
      insertAfter(parentField,promptP);

      insertAfter(parentField,counterP);
    }

    CharCounter.prototype.getParentSize = function (parentField) {
      switch (parentField.type) {
        case "text":
          return parentField.size;
          break;
        case "textarea":
          return parentField.cols+3;
          break;
      }
    }

    CharCounter.prototype.checkRemaining = function (e) {
      var el = null;
      el = getTarget(e);

      if (!el) {
        return;
      }
      var pageObj = (this.pageObj) ? this.pageObj : el.pageObj;

      var chars_remaining = pageObj.chars_remaining;
      //alert(chars_remaining);
      if (chars_remaining < 1) {
        var currCounter = document.getElementById("countDisplay");
        var promptP = document.getElementById("promptP");
        if (isBackSpacePressed(e)) {
          currCounter.style['color'] = 'black';
          promptP.innerHTML = "";
        } else {
          currCounter.style['color'] = 'red';
          if (promptP.innerHTML == "") {
            promptP.innerHTML = "You have reached the character limit.<br />"
                               +"Please use your backspace key to make room."
          }
          promptP.style['color'] = 'red';
          stopBubble(e);
        }
      }
    }

    CharCounter.prototype.countText = function (e,el) {
      var el = (el) ? el : null;
      el = (el) ? el : getTarget(e);

      if (!el) {
        return;
      }

      var pageObj = (this.pageObj) ? this.pageObj : el.pageObj;
      var maxValue = pageObj.max_chars;
      var chars_remaining = maxValue-el.value.length;

      pageObj.chars_remaining = chars_remaining;
      if (chars_remaining >= 0) {
        changeText("currCount",maxValue-el.value.length);
      }
    }

    CharCounter._initialized = true;
  }
}


function charCounter_init(div_id,num_chars) {
  if (!div_id) {
    return;
  }
  var num_chars = (num_chars) ? num_chars : 500;
  var myDiv = document.getElementById(div_id);
  if (myDiv) {
    // location field is only available for site admins
    var charCounter = new CharCounter(div_id,num_chars);
    charCounter.init();
  }
}



