// ===========================================
//  Created by: John-Erik Omland -- JEOWebProductions.com
// ===========================================

/* -- USAGE:    validateFormFields(NAME_OF_FORM_OBJ, REQUIRED_FIELDS_LIST) 
     -- EXAMPLE: validateFormFields(this.form, requiredFieldnamesList) 
     -- NOTE:      must define var requiredFieldnamesList[] before validateFormFields() called
  */
  
// =================
 function inList( check, theList ) {
    for(i=0; i<theList.length; i++) {
      if( check==theList[i] ) return true; 
    }
    return false;
 }
// =================
 function validateFormFields( formObj, requiredFieldNamesList ) {
//    alert('in validate form fields');
    
    for( i=0; i<requiredFieldNamesList.length; i++) {
       if( inList( formObj.elements[i].name, requiredFieldNamesList ))  {
//    alert("checking field " +formObj.elements[i].name);  // DEBUG ###
          if( formObj.elements[i].value == "" || formObj.elements[i].value == null ) {
             alert("Must suply a valid, non-blank entry for form [" +formObj.elements[i].name +"] field. Close this window and try again.");
             formObj.elements[i].focus();
             return false;
          }
       }
    }
    return true;
 }
// =================

/* -- USAGE:     checkEmail(EMAIL_FIELD_VALUE) 
     -- EXAMPLE: checkEmail(this.value) 
     -- NOTE:      
  */
function checkEmail(email) {
   if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email)){
      return (true)
   }
   // alert("Invalid E-mail Address! Please re-enter.")
   return (false)
}
// =================

/* -- USAGE:     emailFieldValid(EMAIL_FIELD_NAME_OBJ) 
     -- EXAMPLE: emailValid(this) 
     -- NOTE:      
  */
  
function emailValid(emailFieldObj)
{
   var email = emailFieldObj.value;
// a very simple email validation checking. 
// you can add more complex email checking if it helps 
    if(email.length <= 0)
	{
	  return false;
	}
    var splitted = email.match("^(.+)@(.+)$");
    if(splitted == null) { emailFieldObj.focus(); return false; }
    
    if(splitted[1] != null ) {
      var regexp_user=/^\"?[\w-_\.]*\"?$/;
      if(splitted[1].match(regexp_user) == null) { emailFieldObj.focus(); return false; }
    }
    
    if(splitted[2] != null) {
      var regexp_domain=/^[\w-\.]*\.[A-Za-z]{2,4}$/;
      if(splitted[2].match(regexp_domain) == null) {
	    var regexp_ip =/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
	    if(splitted[2].match(regexp_ip) == null) { emailFieldObj.focus(); return false; }
      }// if
   alert("email valid = " +email); // DEBUG ###
      return true;
    }
{ emailFieldObj.focus(); return false; };
}
// ===========================================
