/* Javascript for verifying form submission. */

/******************************************************************************/
/*   Function name:    checkForm ()                                           */
/*   Function desc:    Form validation to verify that the required fields     */
/*                     are not empty and that email format is correct.        */
/*   Input:            none.                                                  */
/*   Returns:          False if the is a error in the form validation.        */
/******************************************************************************/       
function checkForm ()
{
   // Get the body of the document.
   var indexBody   = document.getElementsByTagName ("body") [0];
   
   // Get the set of tables in the document.
   var tables      = indexBody.getElementsByTagName ("table");
   
   // Get the "index" table which is the second table in this form.
   var indexTable  = tables [1];
   
   // Get the set of table cell elements for the "index" table.
   var indexCells  = indexTable.getElementsByTagName ("td");
   
   // Get the subject, comments, and email label cells.  These will be
   // highlighted if a error occurs.
   var subjectLbl  = indexCells [0];
   var commentsLbl = indexCells [2];
   var emailLbl    = indexCells [4];
   
   // Verify that the "subject" input field is not empty.  If it is empty, then
   // Display an error message, highlight it's label, and focus on the field.
   var subject     = document.getElementById ("subject");
   var subjectStr  = subject.value.trim ();
   if (subjectStr == "")
   {
   	  subjectLbl.style.color = "maroon";
   	  alert ("A subject is required.");
   	  subject.focus();
   	  return false;
   }
   else
      subjectLbl.style.color = "black";

   // Verify that the "comments" input field is not empty.  If it is empty, then
   // Display an error message, highlight it's label, and focus on the field.
   var comments    = document.getElementById ("comments");
   var commentsStr = comments.value.trim ();
   if (commentsStr == "")
   {
   	  commentsLbl.style.color = "maroon";
   	  alert ("Comments are required.");
   	  comments.focus();
   	  return false;
   }
   else
      commentsLbl.style.color = "black";

   // Verify that the "email address" format is correct.  If the format is bad,
   // then display an error message, highlight it's label, and focus on the field.
   var email    = document.getElementById ("email");
   var emailStr = email.value.trim ();
   if (emailStr == "")
   {
      emailLbl.style.color = "maroon";
      alert ("An email address is required.");
      email.focus ();
      return false;
   }
   else if (validateEmail (emailStr) == false)
   {
         emailLbl.style.color = "maroon";
         alert ("Invalid e-mail address");
         email.focus();
         return false;
   }
   else
   {
      var emailArray = emailStr.split ("@");
      if (!(emailArray [1] == "ulm.edu" || emailArray [1] == "tribe.ulm.edu"))
      {
         emailLbl.style.color = "maroon";
         alert ("ulm.edu or tribe.ulm.edu e-mail address required.");
         email.focus ();
         return false;
      }
      else
      {
         emailLbl.style.color = "black";
      }
   }
}

String.prototype.trim = function ()
{
   return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

function validateEmail (email)
{
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   if(reg.test (email) == false)
      return false;
}