 window.onload = function() {
  if(document.forms[1]) {
      var f = document.forms[1];
      for(i=0;i<f.length;i++) {
         if(f.elements[i].name.match(/^(phone)/)) {                 
           f.elements[i].onblur = function () {FormatPhone(this);};
         }
         if(f.elements[i].type == "textarea" && f.elements[i].title) {             
           f.elements[i].onkeypress = function() {checkEntryLength(this, parseInt(this.title));};
           f.elements[i].onblur = function() {checkEntryLength(this, parseInt(this.title) + 1);};
           f.elements[i].onfocus = function() {toggleCSSProperty(this, 'backgroundColor', '#fff');};
         }
      }
   }
 }

 function checkEntryLength(field, max) {
     var val = field.value.replace(/^\s+|\t/g, "");
     val = val.replace(/\s{3,}/g, " ");
     val = val.replace(/\n{3,}/g, /\n\n/);
     if(val.length >= max) {
         alert("Please limit your response to " + field.title + " or fewer.");
         val = val.substring(0, max - 1);  
         toggleCSSProperty(field, 'backgroundColor', '#ffff00');
     }   
     field.value = val;
 }

  function toggleCSSProperty(id, property, value) {
  if(document.getElementById(id)) {
   var str = "document.getElementById('" + id + "').style." + property + "='" + value + "';";
  } else {
   var str = "id.style." + property + "='" + value + "';";
  }
  eval(str);
}

