/*
 This script was specifically developed to provide added usability and
 cross-browser compatibility. It was originally designed with the W3C 
 DOM2 in mind, but browser specific code has been added, as well as 
 extensive exception handling - the idea being that if anything goes
 wrong (i.e. an exception is thrown) the user is kept blissfully 
 unaware. All validation is also completed at the server-end.

 This script was hand-coded by Nick Joyce (http://www.nickjoyce.com).
 Whilst this is a proprietary script, please feel free to adapt the 
 code (and the ideas behind it) for your own needs.

 Mail me at nick@nickjoyce.com.

 Script Version: v0.8a;

*/

try {
 var targetForm = document.getElementById("contact-form");

 targetForm.disableNavigation = function()
 {
  this.alteredElements = new Object();

  for (var counter = 0; counter < this.elements.length; counter++) {
   try {
    if (this.elements[counter].type == "submit" || this.elements[counter].type == "button") {
     this.alteredElements[this.elements[counter].id] = this.elements[counter].disabled;
     this.elements[counter].disabled = true;

    } // end if (this.elements[counter].type == "submit" || this.elements[counter].type == "button")

   } catch (e) {

   } // end try

  } // end for (var counter = 0; counter < this.elements.length; counter++)

 } // end function targetForm.disableNavigation()

 targetForm.enableNavigation = function()
 {
  if (this.alteredElements) {
   for (var counter in this.alteredElements) {
    try {
     document.getElementById(counter).disabled = targetForm.alteredElements[counter];

    } catch (e) {

    } // end try

   } // end for (var counter in targetForm.alteredElements)

  } // end if (targetForm.alteredElements)

 } // end function targetForm.enableNavigation()

 targetForm.validate = function(event)
 {
  try {
   if (!event) {
    var event = window.event;

   } // end if (!event) 

   var allow_submit = true;
   var message = "To make sure we can get in contact with you, we will need:\n\n";

   var targetElement = null;

   try {
    this.disableNavigation();

   } catch (e) {

   } // end try

   targetElement = document.getElementById("contact-name");

   if (!targetElement.disabled) {
    if (targetElement.value.length == 0) {
     if (allow_submit) {
      try {
       targetElement.focus();
       targetElement.select();

      } catch (e) {

      } // end try

     } // end if (allow_submit)

     allow_submit = false;
     message += "- Your name.\n";

    } // end if (targetElement.value.length == 0)

   } // end if (targetElement.disabled == false)

   targetElement = document.getElementById("contact-email");

   if (targetElement.disabled == false) {
    if (targetElement.value.length == 0) {
     if (allow_submit) {
      try {
       targetElement.focus();
       targetElement.select();

      } catch (e) {

      } // end try

     } // end if (allow_submit)

     allow_submit = false;
     message += "- Your email address.\n";

    } // end if (targetElement.value.length == 0)

    try {
     var reg = /^[\w-]+(?:\.[\w-]+)*@(?:[\w-]+\.)+[a-zA-Z]{2,7}$/;

    } catch (e) {
     try {
      var reg = new RegExp("^[\\w-]+(?:\\.[\\w-]+)*@(?:[\\w-]+\\.)+[a-zA-Z]{2,7}$");

     } catch (e) {

     } // end try

    } // end try

    try {
     if (allow_submit && !reg.test(targetElement.value)) {
      if (allow_submit) {
       try {
        targetElement.focus();
        targetElement.select();

       } catch (e) {

       } // end try

      } // end if (allow_submit)

      allow_submit = false;
      message += "- A valid email address (in the form some.one@example.com).\n"

     } // end if (allow_submit && !reg.test(targetElement.value))

    } catch (e) {

    } // end try

   } // end if (targetElement.disabled == false)

   if (!allow_submit) {
    message += "\nPlease try again!";

    alert(message);

   } // end if (!allow_submit)

   try {
    this.enableNavigation();

   } catch (e) {

   } // end try

   if (!allow_submit) {
    throw 'invalid';

   } // end if (!allow_submit)

   return allow_submit;

  } catch (e) {
   event.returnValue = false;

   try {
    event.preventDefault();
    event.stopPropagation();

   } catch (e) {

   } // end try

  } // end try

 } // end function targetForm.validate()

 targetForm.taintForm = function()
 {
  try {
   document.getElementById('contact-submit').disabled = false;

  } catch (e) {

  } // end try

 } // end function targetForm::taintForm()

 for (var counter = 0; counter < targetForm.elements.length; counter++) {
  try {
   targetForm.elements[counter].attachEvent("onchange", targetForm.taintForm);
   targetForm.elements[counter].attachEvent("onkeypress", targetForm.taintForm);

  } catch (e) {
   try {
    targetForm.elements[counter].addEventListener("change", targetForm.taintForm, true);
    targetForm.elements[counter].addEventListener("keypress", targetForm.taintForm, true);

   } catch (e) {

   } // end try

  } // end try

 } // end for (var counter = 0; counter < targetForm.elements.length; counter++)

} catch (e) {

} // end try

try {
 //document.getElementById("contact-name").focus();
 //document.getElementById("contact-name").select();
 document.getElementById("contact-submit").disabled = true;

} catch (e) {

} // end try

try {
 targetForm.attachEvent("onsubmit", targetForm.validate);

} catch (e) {
 try {
  targetForm.addEventListener("submit", targetForm.validate, true);

 } catch (e) {

 } // end try

} // end try
