/*window.onload = function() {
  alert("This crap is being called when the page loads!");
  //wire the submit form button
  //document.getElementById("SubmitButton").onclick = submitForm;

  //wire the onblur events for validating the entered form information
  document.getElementById("firstname").onblur = validateNonEmpty(firstname, document.getElementById('firstname_help'));
  document.getElementById("lastname").onblur = validateNonEmpty(this, document.getElementById('lastname_help'));
  document.getElementById("email").onblur = validateEmail(this, document.getElementById('email_help'));
  document.getElementById("phone").onblur = validatePhone(this, document.getElementById('phone_help'));
}*/

      function validateNonEmpty(inputField, helpText) {
        //See if the input field contains any content
 	if (inputField.value.length == 0) {
	  //The data is invalid, so set the help message
          if (helpText != null)
            helpText.innerHTML = "Please enter some text for this required field.";
          return false;
        }
	else {
	  //The data is OK, so clear the help message
	  if (helpText != null)
	    helpText.innerHTML = "";
	  return true;
        }
      }
      
      function validateRegEx(regex, inputStr, helpText, helpMessage) {
        //See if the inputStr data validates OK
        if (!regex.test(inputStr)) {
          //The data is invalid , so set the help message and return false
          if (helpText != null)
            helpText.innerHTML = helpMessage;
     	  return false;
	}
	else {
	  //The data is OK, so clear the help message and return true
          if (helpText != null)
            helpText.innerHTML = "";
          return true;
	}
      }
      
      function validateEmail(inputField, helpText) {
        //See if the input value contains data
        if (!validateNonEmpty(inputField, helpText))
          return false;
        //Then see if the input value is a valid e-mail address
	return validateRegEx(/^[\w\.-_\+]+@[\w-]+(\.\w{2,4})+$/, inputField.value, helpText, "Please enter a valid e-mail address (for example, johndoe@abc.com).");
      }
      
    /*    function validatePhone(inputField, helpText) {
        //See if the input value contains data
        if (inputField.value.length != 0) {
          //Then see if the input value is a valid phone number
          return validateRegEx(/^\(?\d{3}(\)|-|\s|\.)?\d{3}(-|\s|\.)?\d{4}$/, inputField.value, helpText, "Please enter a valid phone number (for example, 123-456-7890).");
        }
        else {
          //The data is OK, so clear the help message and return true
          if (helpText != null)
            helpText.innerHTML = "";
          return true;
        }
      }
      */
    
     function validatePhone(inputField, helpText) {
        //See if the input value contains data
        if (inputField.value.length != 0) {
          //strip out acceptable non-numeric characters
          inputField = inputField.value.replace(/[\(\)\.\-\ ]/g, '');
        if (isNaN(parseInt(inputField))) {
            helpText.innerHTML = ("The phone number contains illegal characters.");
            return false;
            }
        if (!(inputField.length == 10)) {
	  helpText.innerHTML = ("The phone number is the wrong length. Make sure you include an area code.");
	  return false;
	}
	if (helpText != null) {
          helpText.innerHTML = "";
          return true;
          }
	}
	else {
          //The data is OK, so clear the help message and return true
          if (helpText != null)
            helpText.innerHTML = "";
          return true;
        }
     }
      
      function submitForm(form) {
        if (validateNonEmpty(form["firstname"], document.getElementById('name_help')) &&
          validateNonEmpty(form["lastname"], document.getElementById('name_help')) &&
          validateEmail(form["email"], document.getElementById('email_help')) &&
          validatePhone(form["phone"], document.getElementById('phone_help'))) {
        //Submit the form to the server
        alert("Thank you for successfully entering your contact information!");
        form.submit();
        } else
          alert("I'm sorry but there is something wrong with the form information.");
      }