// www.paychex.com Form Validation
// 
// Version: 1.0
// Revision Date: 1/9/2003
 
function ClearFocus(thisArray,focusArray) {
 
  var n = 0;
  var m = thisArray.length;
  
  while (n < m) {
   focusArray[thisArray[n]] = 0;
 n = n + 1;
  }
  return (focusArray);
}
 

function validateForm(formName) {
	var emailArray;
	var emailDomainArray;
  /*var NoIndustry = 0;
  var NumIndustry = 4;
  var ChkIndustry = 0;
  var IndustryArray = new Array(NumIndustry);
  var NoTray = 0;
  var NumTray = 2;
  var ChkTray = 0;
  var TrayArray = new Array(NumTray);
  var NoPlastic = 0;
  var NumPlastic = 3;
  var ChkPlastic = 0;
  var PlasticArray = new Array(NumPlastic);
  */
  
  var FocusChk = new Array(1000);
  var msg;
  var errors="";
  
  for (var i=0; i<formName.length; i++) {
    currField=formName.elements[i];
    switch(currField.name) {
		
   case 'fullname':
  if (currField.value.length < 2) {
   errors += "- Name not provided in full.\n";
   FocusChk[i] = 1;
  }
  break;
  
   case 'company':
  if (currField.value.length < 2) {
   errors += "- Company Name not provided in full.\n";
   FocusChk[i] = 1;
  }
  break;
  
   case 'address':
    if (currField.value.length < 2) {
   errors += "- Address not entered in full.\n";
   FocusChk[i] = 1;
  }
  break;
  
   case 'city':
    if (currField.value.length < 2) {
   errors += "- City not identified.\n";
   FocusChk[i] = 1;
  }
  break;
  
   case 'state':
    if (currField.value.length < 2) {
   errors += "- State not properly identified.\n";
   FocusChk[i] = 1;
  }
  break;
  
  case 'zip':
    if (currField.value.length < 5) {
		errors += "- ZIP Code not properly entered.\n   (Must contain at least 5 digits.)\n";
		FocusChk[i] = 1;
	}else{
		for (k = 0; k < currField.value.length; k++) {
    		if (((currField.value.charAt(k) >= "0") && (currField.value.charAt(k) <= "9")) || (currField.value.charAt(k) == "-")) {
    		} else {
      			errors += "- ZIP Code not properly entered.\n   (Must contain at least 5 digits.)\n";
			  	FocusChk[i] = 1;
     			k = currField.value.length+1;
    		}
		}
  }
  break;
  
  case 'email':
  	emailArray = currField.value.split("@");
	if(emailArray[1]!=null && emailArray[0].length > 0 ){
		emailDomainArray=emailArray[1].split(".");
		if(emailDomainArray[1]!=null && emailDomainArray[0].length > 0 && emailDomainArray[1].length > 0){
			
		}else{
			errors += "- Please enter a valid email address. \n";
			FocusChk[i] = 1;
		}
	}else{
		errors += "- Please enter a valid email address. \n";
		FocusChk[i] = 1;
	}
  break;
  
  case 'phonenum':
   if (currField.value.length < 10) {
   errors += "- Phone Number not entered correctly.\n";
   FocusChk[i] = 1;
  } else {
   for (k = 0; k < currField.value.length; k++) {
    if (((currField.value.charAt(k) >= "0") && (currField.value.charAt(k) <= "9")) || (currField.value.charAt(k) == "-")) {
    } else {
     errors += "- Phone Number not entered correctly.\n";
     FocusChk[i] = 1;
     k = currField.value.length+1;
    }
   }
  }
  break;
  
  /*case 'industry':
  IndustryArray[ChkIndustry] = i;
  FocusChk[i] = 1;
  ChkIndustry++;
  if (currField.checked) {
   NoIndustry++;
  }
  if (ChkIndustry == NumIndustry) {
   if (NoIndustry == 0) {
    errors += "- Industry type not indicated.\n";
   } else {
    FocusChk = ClearFocus(IndustryArray,FocusChk);
   }
  }
  break;
  
  case 'traytype':
  TrayArray[ChkTray] = i;
  FocusChk[i] = 1;
  ChkTray++;
  if (currField.checked) {
   NoTray++;
  }
  if (ChkTray == NumTray) {
   if (NoTray == 0) {
    errors += "- Type of tray not indicated.\n";
   } else {
    FocusChk = ClearFocus(TrayArray,FocusChk);
   }
  }
  break;
  
    case 'quantity':
    if (currField.value.length < 1) {
   errors += "- Quantity not completed.\n";
   FocusChk[i] = 1;
  } else {
   for (k = 0; k < currField.value.length; k++) {
    if (((currField.value.charAt(k) >= "0") && (currField.value.charAt(k) <= "9")) || (currField.value.charAt(k) == "-")) {
    } else {
     errors += "- Quantity not entered correctly.\n";
     FocusChk[i] = 1;
     k = currField.value.length+1;
    }
   }
  }
  break;
  
  case 'plastictype':
  PlasticArray[ChkPlastic] = i;
  FocusChk[i] = 1;
  ChkPlastic++;
  if (currField.checked) {
   NoPlastic++;
  }
  if (ChkPlastic == NumPlastic) {
   if (NoPlastic == 0) {
    errors += "- Type of plastic not indicated.\n";
   } else {
    FocusChk = ClearFocus(PlasticArray,FocusChk);
   }
  }
  break;
  */
    }
  }
 
  for (var j = 0; j < 1000; j++) {
   if (FocusChk[j] == 1) {
  formName.elements[j].focus();
  j = 1000;
 }
  }
  
  if (!errors) {     
 return true;
  }
  msg = "__________________________________________________\n\n";
  msg += "This form was not submitted due to the error(s) listed below.\n";
  msg += "Please correct them and resubmit the form.\n";
  msg += "__________________________________________________\n\n";
  msg += errors;
  alert(msg);
  return false;
}