function format_as_uppercase(e) {
  e = (e) ? e : event;
  var trg   = (e.target) ? e.target : ((e.srcElement) ? e.srcElement : null);
  var cc    = (e.charCode) ? e.charCode : ((e.keyCode) ? e.keyCode : ((e.which) ? e.which : 0));
  trg.value = trg.value.toUpperCase();
  // only proceed if we have a target
  return true;
}

function uppercaseFormat(value) {
  return value.toUpperCase();
}

function sentencecaseFormat(value)
{
   // todo: make this respond to events properly!!! see addresscaseFormat below
  var words = value.split(" ");
  //this probably isn't a complete list of words that shouldn't be capitalized
  var special_words = new Array('and', 'the', 'to', 'for', 'is', 'in', 'a', 'at', 'an', 'from', 'by', 'if', 'of');
  for (var i=0;i<words.length;i++) {
    if (i === 0) {
      //always capitalize the first word
      words[i] = (words[i].substring(0,1)).toUpperCase() + words[i].substring(1);
    }
    else if(special_words.indexOf(words[i]) < 0) { 
      words[i] = (words[i].substring(0,1)).toUpperCase() + words[i].substring(1);
    }
  }
  return words.join(' '); 
}

function namecaseFormat(val) {
  val = val.toLowerCase();
}

function addresscaseFormat(e) {
  e = (e) ? e : event;
  var trg   = (e.target) ? e.target : ((e.srcElement) ? e.srcElement : null);
  var cc    = (e.charCode) ? e.charCode : ((e.keyCode) ? e.keyCode : ((e.which) ? e.which : 0));
  var val = trg.value.toLowerCase();
  val = val.replace(/\,/g, " "); // delete commas - replace by space (essential!)
  val = val.replace(/( o\'|^o\')/gi, "O'¶ "); // change o' to O'+space (O' Reilly) (force uppercase)
  val = val.replace(/\'([a-z]{2,})/gi, "'¶ $1"); // change o' to O'+space (O' Reilly) (force uppercase)
  val = val.replace(/-([a-z]{2,})/gi, "-¶ $1"); // change - to -¶space 
  val = val.replace(/(\s|\.|^)(mc)([a-z])/g, "$1mc¶ $3"); // add ¶space after Mc (force uppercase)
  val = val.replace(/(\D)\./g, "$1.¶ "); // add space after literal . (B. M. Smith > uppercase)
  val = val.replace(/^\s+|\s+$/g,""); // trim spaces
  val = val.replace(/(\d+)([a-z]{3,})/gi, "$1 $2"); // add space after numbers when 3+ alphachars follow
  val = val.replace(/(\d+)([.{2,}$])/gi, " $1$2"); // add 4 spaces before numbers not at end of string
  val = val.replace(/(\S*)\"(\D+)\"(\S*)/g, '$1 "$2" $3'); // put spaces around "string" (force uppercase)
  val = val.replace(/\-([a-z])/g, " - $1"); // add spaces around hyphens (force uppercase)
  val = val.replace(/(^\s+)/, ""); // remove spaces at start of string
  val = val.replace(/\s{4,}/g, " "); // remove excessive spaces > 4

  var special_words = new Array('and', 'the', 'to', 'for', 'is', 'in', 'a', 'at', 'an', 'from', 'by', 'if', 'of');
  var words = val.split(" ");
  var len = words.length;

  for (var k=0; k<len; k++) {
    var word = words[k];
    if(special_words.indexOf(word) < 0 || k === 0 ) { // don't bother for special words
      if (/\d/.test(word)) {word = word.toUpperCase();}
      var wordLen = word.length;
      var posn = word.search(/[a-zA-Z]/);
      var firstChars = word.substring(0, posn+1).toUpperCase();
      var postString = word.substring(posn+1,wordLen);
      word = firstChars + postString;
      word = word.replace ( /C\/O\b/g, "C\/o" );
      word = word.replace ( /Po\b/g, "PO" );
      words[k] = word;      
    }
  }

  var newline = words.join(" ");
  newline = newline.replace (/¶ /g, ""); // remove faux spaces
  newline = newline.replace (/¶/g, ""); // remove trailing ¶
  newline = newline.replace (/ {2,}/g, " "); // remove multiple spaces
  trg.value = newline;
}
