// Validate Froms Functions

/***
   Check if field is not empty.
***/
function notEmpty( field ) {
   if ( field.value == "" ) {
      alert( field.name + " cannot be empty." );
      return false;
   }
   return true;
}


/***
   Check if field is empty.
***/
function isEmpty( field ) {
   if ( field.value == "" ) {
      return true;
   }
   return false;
}


/***
   Check if the syntax of an email is valid.
***/
function validEmail( email ) {
   invalidChars = "/:,;";
   
   if ( email == "" ) {
      alert( "You must enter an email address." );
      return false;
   }
   for ( i=0; i<invalidChars.length; i++ ) {
      badChar = invalidChars.charAt( i );
      if ( email.indexOf( badChar, 0 ) > -1 ) {
         alert( "Your email address is not formatted correctly." );
         return false;  
      }  
   }  
   
   atPos = email.indexOf( "@", 1 );
   if ( atPos == -1 ) {
      alert( "Your email address is not formatted correctly." );
      return false;
   }
   if ( email.indexOf( "@", atPos+1 ) > -1 ) {
      alert( "Your email address is not formatted correctly." );
      return false;
   }
   
   periodPos = email.indexOf( ".", atPos );
   if ( periodPos == -1 ) {
      alert( "Your email address is not formatted correctly." );
      return false;  
   }
   if ( periodPos+3 > email.length ) {
      alert( "Your email address is not formatted correctly." );
      return false;  
   }
   
   return true;
}


/***
   Makes sure that email and email2 matches.
***/
function emailMatches( email1, email2 ) {
   if ( email1 == email2 ) {
      return true;   
   }
   else {
      alert( "Emails entered do not match." );
      return false;
   }  
}


/***
   Check if the syntax of a zip is valid.
***/
function validZip( zip ) {
   re = /\d{5}/g;
   OK = re.test( zip );
   if ( zip == "" ) {
      alert( "Zip code cannot be empty." );
      return false;
   }
   else if ( !OK ) {
      alert( "Zip code entered is not valid." );
      return false;
   }
   return true;  
}


/***
   Check if at least one area of interest is selected.
***/
function checkSelections( selectionsArray ) {
   for( i=0; i<selectionsArray.options.length; i++ ) {
      if ( selectionsArray.options[i].selected == true ) {
         return true;   
      } 
   }
   
   alert( "Please select at least one area of interest." );
   return false;
}


/***
   Check that the textarea's number of characters does not exceed 250.
***/
function checkLength( comments ) {
   if ( comments.length <= 250 ) {
      return true;   
   }
   else {
      alert( "Comments cannot be longer than 250 characters." );
      return false;
   }  
}
