/* author: Juan Londono */
/* Creation date: 29/04/2003 */

/*
  ----------------------------------------------------------------
   Procedures to count words typed
  ----------------------------------------------------------------
*/

if (!limit) {
	var limit = 100;
}

function update() {
  document.data.counter.value=count_words(document.data.text.value);
  if(document.data.counter.value > limit) {
    errorSpace.innerHTML="Ad must not exceed more than "+limit+" words";
    if(document.styleSheets) {
      document.data.counter.style.fontWeight = 'bold';
      document.data.counter.style.color = '#ff0000';
    }
  }
  else if(document.data.counter.value <= limit
    && document.styleSheets ) {
    document.data.counter.style.fontWeight = 'normal';
    document.data.counter.style.color = '#000000';
    errorSpace.innerHTML="&nbsp;";
  }
}

function count_words(phrase) {
  var words;
  var ch=0;
  
  if (phrase.length==0) {
    words=0;
  } else {
    words=1;
  }
  while(ch<phrase.length) {
    if ((phrase.substring(ch,ch+1) == " ") && (phrase.substring(ch-1,ch) != " ")) {
      words++;
	  ch++;
    }
    if ((phrase.substring(ch,ch+1) == "\n") && (phrase.substring(ch-2,ch-1) != "\n") &&
	 (phrase.substring(ch-2,ch-1) != "")) {
      words++;
      ch++;
    }
    ch++;
  }
  if ((phrase.length>0) && 
    ((phrase.substring(phrase.length-1,phrase.length) == " ") || 
	(phrase.substring(phrase.length-1,phrase.length) == "\n"))) {
	words--;
  }
  return words;
}

