/*
Using the onsubmit event handler from form tag:
onSubmit="return checkRequiredFields(this,'theForm','name|name2|title|street|city|state|zip|country','Your first name|Your last name|Your title|Your street address|City|State|Zip|Country')"

Using a submit button and the onclick event handler:
onClick=" return checkRequiredFields(document.theForm,'theForm','Name|Email|Church|City|State|Country|userfile','Your name|Your email address|Your church|Your city|Your state|Your Country|Picture to upload')"
*/

function checkRequiredFields(input,fname,fields,messages) {
  
  formobj = eval("document." + fname);
    
  //Create arrays of field names and associated alert messages
  var requiredFields = fields.split("|");
  var fieldNames = messages.split("|");
  
  //If the number of required fields is not the same as
  //the number of alert messages, alert and return false.
  //For debugging, mainly.
  if (requiredFields.length != fieldNames.length) {
    alert ("I'm sorry, but the number of required fields is not equal to the number of alert messages. Please revise.");
    return false;
  }
  
  //First check the group_id field to make sure code is valid
  if (formobj.Group_ID && formobj.Group_ID.value != '') {
    if (groupidcheck(formobj.Group_ID) == false) {
      return false;
     }
  }
  
  //Check if there is a field "email" and execute validEmail() if so
  for (var i=0; i<requiredFields.length;  i++) {
    if (requiredFields[i] == "email" || requiredFields[i] == "Email") {
      emailobj = eval ("document." + fname + "." + requiredFields[i]);
      if (validEmail(emailobj) == false) {
        return false;
      }
    }
  }

  var fieldCheck   = true;
  var fieldsNeeded = "\nPlease fill in the following required field(s):\n\n\t";
    
  var first_field = ""; //Will be assigned name of first empty text field.

  //A flag set to determine if a text field is the first field found empty, 
  //and so can set focus on it. If a drop-down menu is chosen first and displayed
  //as first to user in alert box list, can't set focus to it.
  var a = 0;
    
  for (var fieldNum=0; fieldNum < requiredFields.length; fieldNum++) {
    
    radiofield = eval ("document." + fname + "." + requiredFields[fieldNum]);
    
    //Check drop-down menus here; must have valid value to not be put on the list of
    //"empty" fields shown to user; can't be null or contain only spaces.
    
    if (input.elements[requiredFields[fieldNum]].type == "select-one") {
      if (input.elements[requiredFields[fieldNum]].options[input.elements[requiredFields[fieldNum]].selectedIndex].value == "" || input.elements[requiredFields[fieldNum]].options[input.elements[requiredFields[fieldNum]].selectedIndex].value == " ") { 
        fieldsNeeded += fieldNames[fieldNum] + "\n\t";
        fieldCheck = false;
        if (a == 0) {
          a = 1; 
        }
      }
    }
    
    //Check radio buttons here.
    //radiofield[0].type would equal "radio", but not radiofield.type!
    //But radiofield[0].type is useless to test select-one, text, and other types of fields.
    else if (radiofield.type != "select-one" && 
        radiofield.type != "text" && 
        radiofield.type != "password" &&  
        radiofield.type != "textarea" &&  
        radiofield.type != "file") { 
      var notchecked = true;
      for (i=0; i < radiofield.length; i++) {
        if (radiofield[i].checked) { 
          notchecked = false;
          break;
        }
      }
      if (notchecked == true) {
        fieldsNeeded += fieldNames[fieldNum] + "\n\t";
        fieldCheck = false;
        if (a == 0) {
          a = 1; 
        }
      }
    }  
          
    //Check regular text fields here (also types of "file" and "textarea") 
    else if (input.elements[requiredFields[fieldNum]].value == "" || 
             input.elements[requiredFields[fieldNum]].value == " ") {
      fieldsNeeded += fieldNames[fieldNum] + "\n\t";
      fieldCheck = false;
      if (first_field == "" && a == 0) { 
        //a == 0 means drop-down menu field not chosen ahead of this field, 
        //so can set focus on this text field, since it will show up first 
        //on the list of "empty" fields shown to user in alert box.
        first_field = requiredFields[fieldNum];
      }
    }
  }
  
  if (fieldCheck == true) {
    return true;
  }
  else {
    alert(fieldsNeeded);
    if (first_field != "") {
      fieldobj = eval("document." + fname + "." + first_field);
      fieldobj.focus();
    }
    return false;
  }
}

function validEmail(eForm) { 
  if (eForm.value == "") {
    return true;
  }
  var eAddr = eForm.value;
  var eName = eForm.name;
  
  var goodAddr = false;
  var ndxAt = ndxDot = 0;
  ndxAt  = eAddr.indexOf("@");
  ndxDot = eAddr.lastIndexOf(".");
  
  if ( (ndxDot < 0) || (ndxAt < 0) ) {
    alert("Your email address lacks '.' or '@'.\n\nThe format is 'you@dom.suf'");
  }  
  
  else if (ndxDot < ndxAt) {
    alert("You entered your email address incorrectly.\nThe '.' should come after the '@'.\n\nThe format is 'you@dom.suf'");
  }
  
  else if ( (ndxDot - 2) <= ndxAt) {
    alert("You may be missing your domain name.\n\nThe format is 'you@dom.suf'");
  }
  
  else if ( eAddr.length < (ndxDot + 3) ) {
    alert("You need at least two characters after the 'dot'.\n\nThe format is 'you@dom.suf'");
  } 
  
  else { goodAddr = true; }
   
  //if (goodAddr == false) {
  //  eObj = eval("document.calcform." + eName);
  //  eObj.focus();
  //}
  return (goodAddr);
} 

function digitsonly(thisref) {
  if (thisref.value.search(/[^0-9]/) > -1) { 
    alert ('Please enter digits only.'); thisref.value = thisref.value.replace(/[^0-9]/g,''); 
  }
}