



// Word Count 
function wordcounter(field, countfield, maxlimit) { 
 var wordcounter = 0; 
 var wordstarted = false; 
 var whitespaceReg = /\s/; // Any whitespace character 
 var i, c; 
 for (i = 0; i < field.value.length; i++) { 
  c = field.value.charAt(i); 
  if (!wordstarted) { // looking for non-whitespace to start the word 
   if (!c.match(whitespaceReg)) { 
    wordstarted = true; 
    wordcounter++; 
    if (wordcounter > maxlimit) { 
     // We reached the limit, no more words allowed 
          // Strip everything from this character on 
          field.value = field.value.substring(0, i); 
          wordcounter--; 
          break; 
        } 
      } 
      else { 
        continue; 
      } 
    } 
    else { // looking for whitespace to end the word 
      if (c.match(whitespaceReg)) { 
        wordstarted = false; 
      } 
      else { 
        continue; 
      } 
    } 
  } 
  countfield.value =  wordcounter; 
} 
