/*
   Usage:  for every phone field that needs to be in the format
			(###) ###-####
   set the onpropertychange event handler to phone(this).
   
   <INPUT NAME="PHONE" LENGTH=14 MAXLENGTH=14 onpropertychange="phone(this);">
   
   The script will force compliance to the above phone format.
   
   When checking phone formats during an onsubmit event handler (or any
   other time) create an array of field names, then send that array to
   the phoneVerify method.
   
   function verify(){
      var phones = ["cPH1", "cPH2", "cPAG", "cCELL", "cFAX1", "cFAX2"]
      if (! phoneVerify(phones)) return false;
   }
   
*/
var phoneFormat = /^\(\d{3}\) \d{3}-\d{4}/;
var lastval = "";
var lastfield = "";
var working = false;

function phone(field){
   var val = field.value;
   var len = val.length;
   var re;
   
   if (field != lastfield){
      lastfield = field;
      lastval = "";
   }
   
   if (len ==0) return;
   
   if (len < lastval.length){
      if (lastval.substr(0,len) == val){ 
         lastval = val;
         return;
      }
   }
   
   if (! working){
      working = true;
      re = /[^0-9\(\)\- ]/;
      if (re.test(val)){
         field.value = lastval;
         working = false;
         return;
      }
      switch (len){
         case 1:
            re = /\(?\d?/;
            break;
         case 2:
            re = /\(?\d{1,2}/;
            break;
         case 3:
            re = /\(?\d{2,3}/;
            break;
         case 4:
            re = /^\(\d{3}/;
            break;
         case 5:
            re = /^\(\d{4}/;
				if (re.test(val)){
               val = val.substring(0,4) + ") " + val.substring(4);
               re = /^\(\d{3}\) \d/;
            } else {
               re = /^\(\d{3}\)/;
            }
            break;
         case 6:
            re = /^\(\d{3}\)\d/;
				if (re.test(val)){
               val = val.substring(0,5) + " " + val.substring(5);
               re = /^\(\d{3}\) \d/;
            } else {
               re = /^\(\d{3}\) /;
            }
            break;
         case 7:
            re = /^\(\d{3}\) \d/;
            break;
         case 8:
            re = /^\(\d{3}\) \d{2}/;
            break;
         case 9:
            re = /^\(\d{3}\) \d{3}/;
            break;
         case 10:
            re = /^\d{10}/;
				if (re.test(val)){
               val = "(" + val.substring(0,3) + ") " + val.substring(3, 6) + "-" + val.substring(6);
               re = /^\(\d{3}\) \d{3}-\d{4}/;
            } else {
               re = /^\(\d{3}\) \d{4}/;
					if (re.test(val)){
                  val = val.substring(0, 9) + "-" + val.substring(9);
                  re = /^\(\d{3}\) \d{3}-\d/;
               } else {
                  re = /^\(\d{3}\) \d{3}-/;
               }
            }
            break;
         case 11:
            re = /^\(\d{3}\) \d{3}-\d/;
            break;
         case 12:
            re = /^\(\d{3}\) \d{3}-\d{2}/;
            break;
         case 13:
            re = /^\(\d{3}\) \d{3}-\d{3}/;
            break;
         case 14:
            re = /^\(\d{3}\) \d{3}-\d{4}/;
            break;
         default:
            field.value = lastval;
            working = false;
            return;
      }
		if (re.test(val)){
         if (len < 4){
            if (val.substring(0,1) != "(") val = "(" + val;
         } else if (len == 4){
            val += ") ";
         } else if (len == 9){
            val += "-";
         }
         field.value = lastval = val;
      } else {
         field.value = lastval;
      }
      working = false;
   }
}
function phoneVerify(fieldNames){
   var valid = true;
   var val;
   var i;
   for (i=0;i<fieldNames.length;i++){
      val = document.all[fieldNames[i]].value;
      if (val.length > 0){
      	if (val.length != 14){
            	valid = false;
            	break;
      	} else if (! phoneFormat.test(val)){
            	valid = false;
            	break;
      	}
      }
   }
   if (! valid){
      alert("Incomplete phone fields are not allowed!");
      document.all[fieldNames[i]].focus();
      return false;
   } else {
      return true;
   }
}