addLoadListener(init);

function init() // Attach checkform to form onsubmit events.
{
  document.forms[0].onsubmit = checkform;
  return true;
}

/* ************************* */
/* Form validation function. */
/* DAC 12/25/2006            */
/* ************************* */
function checkform()
  { 
  var strNameAlert = "";
  var strEmailAlert = "";
  var strInvalidEmail = "";
  var strMsgAlert = "";
  var blnKicker = false;  
  // These are the required form fields.
  var strNameValue = document.getElementById('name').value;
  var strEmailValue = document.getElementById('email').value;
  var strMsgValue = document.getElementById('msg').value;    

  // The regular expression to check for whitespace
  // was adapted from The Javascript Anthology
  // by James Edwards and Cameron Adams
  // Site Point, isbn 0-9752402-6-9 chapter 6 pg 114.
  if (strNameValue == "" || /^\s+$/.test(strNameValue))
    {
    strNameAlert = ("Please enter your name.");
    blnKicker = true;
    }
  if (strEmailValue == "" || /^\s+$/.test(strEmailValue))
    {
    strEmailAlert = ("Please enter your email address.");
    blnKicker = true;    
    }   
  if (!/^[\w\.\-]+@([\w\-]+\.)+[a-zA-Z]+$/.test(strEmailValue))
    {
    strInvalidEmail = ("The email address you entered appears to be invalid.");
    blnKicker = true;
    } 
  if (strMsgValue == "" || /^\s+$/.test(strMsgValue))
    {
    strMsgAlert = ("Please enter a message.");
    blnKicker = true;    
    }
      
  if (blnKicker == false)  
    {
    return true; // Return true to submit the form.
    }
      
  else
    {
    // Create a global variable to hold a reference
    // to the div where error messages are written to.
    InsertPoint = document.getElementById("errormsg");
        
    // Remove any existing error messages.
    while (InsertPoint.hasChildNodes())
    {
      InsertPoint.removeChild(InsertPoint.lastChild);
    }
    
    // Call InsertMsg() to create and insert 
    // error messages for the empty form fields.
    if (!(strNameAlert == ""))
    {
      InsertMsg(strNameAlert);       
    }
    if (!(strEmailAlert == ""))
    {
      InsertMsg(strEmailAlert); 
    }
    if (!(strInvalidEmail == ""))
    {
      InsertMsg(strInvalidEmail); 
    }
    if (!(strMsgAlert == ""))
    {
      InsertMsg(strMsgAlert);
    }   
    return false; // Display errors and return false to supress form submission.
    }
  }

// InsertMsg() creates p element and text
// nodes, then appends them to the div.
function InsertMsg(strInlineMsg)
{
  var ParagraphNode = document.createElement("p");    
  var TextNode = document.createTextNode(strInlineMsg);
  ParagraphNode.appendChild(TextNode);
  var NewNode = InsertPoint.appendChild(ParagraphNode);
}
/* ************************ */ 
/* End the form validation. */
/* ************************ */  
