
function countWords(container,maxNumWords,returnCount) {
  
  //trap missing value for returnNum and returnTo
  if (returnCount == undefined) { returnCount = false; }
  
  //split string on spaces to count words.
  containerSplit = container.value.split(" ");
  //alert(containerSplit.length);
  //don't count trailing spaces unless followed by a word
  if (containerSplit[(containerSplit.length-1)] == "") { containerSplit.pop();}
  //alert(containerSplit.length);
  
  //update inline word count field
  if (returnCount) { returnCount.value = maxNumWords - containerSplit.length; }
 
  //test size of split array; array size = word count
  if (containerSplit.length > maxNumWords) {
     
     //string equals or exceeds maximum allowed word count
     
     //reduce text to max words
     container.value = "";
     for(i=0;i<=(maxNumWords - 1);i++) { 
       container.value = container.value + containerSplit[i] + " ";
       }
     
     //trim white space from end of string
     while (container.value.substr((container.value.length - 1),(container.value.length - 1)) == " ") {
       container.value = container.value.substr(0,(container.value.length - 1));
       }
     
     //string exceeds maximum word count
     if (containerSplit.length > maxNumWords) { return false; }
     else { return true; }
     
     }
     
  else { return true; }

  }



//-----------------------------------------------------------------------------------------//


function checkTel(field){
  
  format = field.value.split("-");

  if (format.length != 3) {
     alert("Phone Number Error")
     field.select();
     field.focus();
     return false;
     }
  
  if (format[0].length != 3 || format[1].length != 3 || format[2].length != 4) { 
     alert("Phone Number Error");
     field.select();
     field.focus();
     return false;
     }


  invalidChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ`!@#$%^&*()=\][/.,';";

  for (i=0; i < invalidChars.length; i++) {
      badChar = invalidChars.charAt(i)
      if (field.value.indexOf(badChar,0) > -1) { 
         alert("Invalid character in a telephone field");
         field.select();
         field.focus();
         return false;
         }
      }
  
  return true;

}



//-----------------------------------------------------------------------------------------//



function checkEmail(email) {
  
  invalidChars = " /;:,`=][*"
  emailVal = email.value
    
  // Condition 1
  //deleted

  // Condition 2
  for (i=0; i < invalidChars.length; i++) {
       badChar = invalidChars.charAt(i)
       if (emailVal.indexOf(badChar,0) > -1) { 
          alert("Invalid characters in email field")
          email.select();
          email.focus();
          return false;
          }
       }
  
  // Condition 3
  atPos = emailVal.indexOf("@",1)
  if (atPos == -1) { 
     alert("Invalid email");
     email.select();
     email.focus();
     return false;    
     }
  
  if (emailVal.indexOf("@",atPos+1) > -1) {
     alert("Invalid email")
     email.select();
     email.focus();
     return false;
     }
    
  // Condition 4
  periodPos = emailVal.indexOf(".",atPos)
  if (periodPos == -1) { 
     alert("Invalid email")
     email.select();
     email.focus();
     return false;
     }
  
  if (periodPos+4 > emailVal.length) { 
     if (confirm("Are you sure this email is correct?\n\nClick\"OK\" to continue\nClick \"Cancel\" to edit email")) { return true;}
     email.select();
     email.focus();
     return false;
     }
    
  return true;

}



//-----------------------------------------------------------------------------------------//




function checkDate(year,month,day) {
  
  leapYears = new String();
  
  for (i=32;i>0;i--) { leapYears += (2036 - (i*4)) + "," }
  
  // ---- verify number of days in the month ---- //
  if ("4,6,9,11,04,06,09".indexOf(month) > -1 && day > 30) {
     return false;
     }
  
  // ---- verify number of days in Feb, based on leap years ---- //
  if (leapYears.indexOf(year) < 0 && (month == "02" || month == "2") && day > 28) {
     return false;
     }
  
  if (leapYears.indexOf(year) > -1 && (month == "02" || month == "2") && day > 29) {
     return false;
     }
  
  // ---- verify valid 4-number year ----//
  if (year.length != 4) { return false; }
  
  return true;
  
  }




//-----------------------------------------------------------------------------------------//




function maskPhone(field,off) {
  
  var fullFieldWidth = 10;
  if (off == undefined) { off = false; }
  
  
  numbers = "";
  for(i=0;i<field.value.length;i++) {
     
     if (!isNaN(field.value.charAt(i))) { numbers += field.value.charAt(i); }
     
     } 
  
  if (numbers.length <= fullFieldWidth) { 
     
     //apply mask
     
     if (numbers.length < fullFieldWidth) { 
        
        for(i=0;fullFieldWidth-numbers.length > 0;i++) { numbers += "_"; }
        
        }
     
     numbers = "(" + numbers.substr(0,3) + ") " + numbers.substr(3,3) + "-" + numbers.substr(6,4);
     alert(numbers);
     field.value = "(111) 2223333";
     }
     
     
  
  return true;
  
  }




//-----------------------------------------------------------------------------------------//



function cursorTo(field) {

  field.select();
  field.focus();
  
}




//-----------------------------------------------------------------------------------------//



     
function toggleOption(selectField) {
  
  //alert(selectField);
  
  for (i=0;i<selectField.length;i++) {
      if (selectField.options[i].selected) {
         
         alert(selectField.options[i].value);
         
         }
      }
  
  //alert(document.edit.testnewpriv.options[document.edit.testnewpriv.selectedIndex].value);
  
  }






