function checkRequired(theForm){
  var field;
  var pass = true; // At return from function, pass = true means form OK, pass = false means not OK
  for(var i=0; i<theForm.elements.length;i++){
    field=theForm.elements[i];
    if(field.name.substring(0,3)=="req"){
      if (field.value==""){
        pass=false;
        break;
      } // end if
    } // end if
  } // end for
  if(!pass){
    alert("Please complete all required fields before submitting.");
  }	
  return pass;
} // end function

 
function check(theForm) {
  //Declare variables to serve as flags for tests
  var reqOK, zipOK, emailOK;
  
	//Initialize the flags to true meaning that the validation was successful	
  var reqOK = zipOK = emailOK = true; // If any of these becomes false the form is bad.

  //Invoke the checkRequired function, passing it the form as a parameter
  reqOK = checkRequired(theForm);

  //If the checkRequired function succeeded, validate the zip code and email address using the provided regular expressions
  if(!reqOK) { // Performs following checks only if form passed checkRequired test (i.e., reqOK is true)
    return false;
	}
  else{
	  /*Check Postal Code*/    
		var re5digit=/^\d{5}$/;
    if(theForm.reqzip.value.search(re5digit)==-1) {
      alert("Please enter a valid 5 digit postal code.");
      zipOK = false;
    }
	  else {
	    zipOK = true;
	  }

	
    /*Check Email*/
    var emailRE =/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
    if(emailRE.test(theForm.reqemail.value)) {
      emailOK = true;
	  }
    else {
      alert("Please enter a valid email address.");
      emailOK = false;
	  }
  }	  
	  

	//Check the different flags, if any is false, return the value false; otherwise, return the value true	
  if(reqOK&&zipOK&&emailOK){
    return true;
  }
  else{
    return false;
  }
 
}