// Validate against empty fields or lists not selected
function Inforequired(which) 
{
  var pass=true;
  var tempobj;
  if (document.images) 
  {
    for (i=0;i<which.length;i++) 
	{
      tempobj=which.elements[i];
      if (tempobj.name.substring(0,8)=="required") 
	  {
        if (((tempobj.type=="text"||tempobj.type=="textarea")&&
        tempobj.value=='')||(tempobj.type.toString().charAt(0)=="s"&&
        tempobj.selectedIndex==0)) 
		{
          pass=false;
          break;
        }
      }
    }
  }
    if (!pass) {
    shortFieldName=tempobj.name.substring(8,30).toUpperCase();
    alert("Please make sure the "+shortFieldName+" field is completed.");
	
	if (tempobj.type=="text"||tempobj.type=="textarea")
		  	tempobj.select();
	
 	tempobj.focus();

    return false;
  }
  else
  return true; 
}
//validate against characters NUMBERS ONLY!
function validate(field) {
var valid = "0123456789"
var ok = "yes";
var temp;
for (var i=0; i<field.value.length; i++) {
temp = "" + field.value.substring(i, i+1);
if (valid.indexOf(temp) == "-1") ok = "no";
}
if (ok == "no") {
alert("THIS FIELD REQUIRES NUMBERS ONLY");
field.focus();
field.select();
   }
}
//validate e-mail field
function checkEmail(field) {
  if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(field.value)){
    return (true)
   } else {
   		alert("Invalid E-mail Address! Please re-enter.")
		field.focus();
		field.select();
    	return (false);
   }
}


//validate Date fields
function validDate(field,required)
{
  var result = true;

  if (required && !validRequired(field))
    result = false;
  
   if (result)
   {
     var elems = field.value.split("/");
     
     result = (elems.length == 3); // should be three components
     
     if (result)
     {
       var month = parseInt(elems[0],10);
        var day = parseInt(elems[1],10);
       var year = parseInt(elems[2],10);
      result = !isNaN(month) && (month > 0) && (month < 13) &&
            !isNaN(day) && (day > 0) && (day < 32) &&
            !isNaN(year) && (elems[2].length == 4);
     }
     
      if (!result)
     {
       alert('Not a date format. Try mm/dd/yyyy');
       field.focus();    
    }
  } 
  
  return result;
}

 // Validate phone number fields
  // This is not particularly strict, but it's hard to do
  // real regular expression matching in JavaScript.
  function validatePhone3(textfield) {
     phoneOK = true; // phoneOK is GLOBAL
     var digits = 0;
     // Check that the phone number only contains
     // the characters "() -0-9" and contains the right
     // number of digits total
     for (var i=0; i < textfield.value.length; i++) {
        var theChar = textfield.value.charAt(i);
        if ((theChar >= "0") && (theChar <= "9")) {
           digits++;
           continue;
        }
        if (theChar == " ") continue;
        if (theChar == "-") continue;
        if (theChar == "(") continue;
        if (theChar == ")") continue;
        phoneOK = false;
     }

     // The string is OK if it contains only the allowed
     // characters and the number of digits in the
     // phone number is 10 total (area code + number)
     phoneOK = phoneOK && (digits == 3);
     if (!phoneOK) {
        alert("Please enter a phone number in the format (999)-999-9999");
        textfield.focus();
        textfield.select();
     }

     return phoneOK;
  }
 
 // Validate phone number fields
  // This is not particularly strict, but it's hard to do
  // real regular expression matching in JavaScript.
  function validatePhone4(textfield) {
     phoneOK = true; // phoneOK is GLOBAL
     var digits = 0;
     // Check that the phone number only contains
     // the characters "() -0-9" and contains the right
     // number of digits total
     for (var i=0; i < textfield.value.length; i++) {
        var theChar = textfield.value.charAt(i);
        if ((theChar >= "0") && (theChar <= "9")) {
           digits++;
           continue;
        }
        if (theChar == " ") continue;
        if (theChar == "-") continue;
        if (theChar == "(") continue;
        if (theChar == ")") continue;
        phoneOK = false;
     }

     // The string is OK if it contains only the allowed
     // characters and the number of digits in the
     // phone number is 10 total (area code + number)
     phoneOK = phoneOK && (digits == 4);
     if (!phoneOK) {
        alert("Please enter a phone number in the format (999)-999-9999");
        textfield.focus();
        textfield.select();
     }

     return phoneOK;
  }
  
  
var isNN = (navigator.appName.indexOf("Netscape")!=-1);
function autoTab(input,len, e) {
var keyCode = (isNN) ? e.which : e.keyCode; 
var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
if(input.value.length >= len && !containsElement(filter,keyCode)) {
input.value = input.value.slice(0, len);
input.form[(getIndex(input)+1) % input.form.length].focus();
}
function containsElement(arr, ele) {
var found = false, index = 0;
while(!found && index < arr.length)
if(arr[index] == ele)
found = true;
else
index++;
return found;
}
function getIndex(input) {
var index = -1, i = 0, found = false;
while (i < input.form.length && index == -1)
if (input.form[i] == input)index = i;
else i++;
return index;
}
return true;
}
