var errorFieldRef = null;

if(document.getElementById) {
  window.alert = function(txt) {
    showCustomAlert(txt);
  }
}

function showCustomAlert(txt) {
    var obj = document.getElementById("alertBox");
    obj.getElementsByTagName("p")[0].innerHTML = txt;
    obj.style.visibility = "visible";
    obj.getElementsByTagName("a")[0].focus();
}

function hideCustomAlert(autofocus) {
    var obj = document.getElementById("alertBox");
    obj.style.visibility = "hidden";
    if (errorFieldRef) {
        if (autofocus) errorFieldRef.focus();
        errorFieldRef = null;
    }
}

function showThankYou() {
    var obj = document.getElementById("thankYouBox");
    obj.style.visibility = "visible";
    obj.getElementsByTagName("a")[0].focus();
}

function hideThankYou() {
    var obj = document.getElementById("thankYouBox");
    obj.style.visibility = "hidden";
}

function validateForm(theForm) {
  if ((theForm.name.value == "") || (theForm.name.value == "Name")) {
    alert("Please enter a value in the \"Name\" field.");
    errorFieldRef = theForm.name;
    return (false);
  }

  if ((theForm.email.value == "") || (theForm.email.value == "E-mail")) {
    alert("Please enter a value in the \"E-mail\" field.");
    errorFieldRef = theForm.email;
    return (false);
  }

  var invalidChars = '\/\'\\ ";:?!()[]\{\}^|';
  for (var i=0; i<invalidChars.length; i++) {
    if (theForm.email.value.indexOf(invalidChars.charAt(i),0) > -1) {
        alert("The \"E-mail\" field contains an invalid character.");
        errorFieldRef = theForm.email;
        return (false);
    }
  }
  
  var ptr = theForm.email.value.indexOf('@',0);
  if (ptr < 1) {
    alert("The \"E-mail\" field does not contain a valid email address.");
    errorFieldRef = theForm.email;
    return (false);
  }
  
  if (theForm.email.value.indexOf('.',ptr) == -1) {
    alert("The \"E-mail\" field does not contain a valid email address.");
    errorFieldRef = theForm.email;
    return (false);
  }

  if ((theForm.phone.value == "") || (theForm.phone.value == "Phone")) {
    alert("Please enter a value in the \"Phone\" field.");
    errorFieldRef = theForm.phone;
    return (false);
  }

  if ((theForm.address.value == "") || (theForm.address.value == "Address")) {
    alert("Please enter a value in the \"Address\" field.");
    errorFieldRef = theForm.address;
    return (false);
  }

  if ((theForm.message.value == "") || (theForm.message.value == "Message")) {
    alert("Please enter a value in the \"Message\" field.");
    errorFieldRef = theForm.message;
    return (false);
  }
  
  return (true);
}