/*
  WHAT THE HTML PAGE REQUIRES FOR validateForm() TO WORK
  Every form field that will be validated needs an additional attribute included
  called 'required' with a value of 'yes' e.g. <input required="yes">.  Radio
  button fields should only have the attribute added to the first radio field,
  not all of them.

  The script requires a form field named Email (case sensitive) for input of the
  user's email address.

*/

/*
  validateForm()
  This is a Boolean function used to validate the form data.  The best way to
  use it in a page is to include it in the form's onSubmit event handler e.g.
  <form onSubmit="return validateForm(this);">.  Note that it requires the form
  object to be passed as an argument to the function.  If the form validates it
  will be submitted and if not, no submission will take place.
*/
function validateForm(theForm)
{
    // Local Data
    // the function's return value
    var validated = false;
    // regular expression pattern for email
    var emailPat = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    // array of required field names based on the required attribute
    var fieldIDs = new Array();
    // array of field names marked as not validated
    var markedIDs = new Array();
    // counters for the arrays
    var fieldIDsCount = 0;
    var markedIDsCount = 0;

	if (document.getElementById || document.all)
	{
    // Populate an array of required field name values from the form if the
    // field's attribute has a required=yes value.
    for (i = 0; i < theForm.elements.length; i++)
    {
      if (theForm.elements[i].getAttribute("required") == "yes")
      {
        fieldIDs[fieldIDsCount] = theForm.elements[i].name;
        fieldIDsCount ++;
      }
    }

    // Check for empty required fields that should stop form submission
    // and populate the marked fields array.
    for (i = 0; i < fieldIDs.length; i++)
    {
      var element = theForm.elements[fieldIDs[i]];
      if (element.value == "" || element.selectedIndex == 0)
      {
        markedIDs[markedIDsCount] = fieldIDs[i];
        markedIDsCount++;
      }
    }// end for

    // Display error mark for unvalidated form fields.
    if (markedIDsCount > 0)
    {
      // First, clear any previously displayed error layers.
      for (i = 0; i < fieldIDs.length; i++)
      {
        hideById(fieldIDs[i]);
      }
      // Then, display new error layers.
      for (i = 0; i < markedIDsCount; i++)
      {

        showById(markedIDs[i]);
      }
      alert("FORM ERROR\nRequired Information Missing\n\n" +
      "Fields marked with an asterisk must be\ncompleted in order to submit " +
      "the form.\n\nPlease Make Corrections And Try Again.");
  	}
  	// check for valid email syntax
  	else if (theForm.email.value != "" && !emailPat.test(theForm.email.value))
  	{
  		alert("FORM ERROR\n\n" +
  					"The Email Address Contains Syntax Errors.\n\n" +
  					"Please Make Corrections And Try Again.");
      // First, clear any previously displayed error layers.
      for (i = 0; i < fieldIDs.length; i++)
      {
        hideById(fieldIDs[i]);
      }
  		showById('email');
  		theForm.email.focus();
  	}
    // require complete address information if some info is omitted
    else if ((theForm.Street.value == "" || theForm.City.value == "" || theForm.State.value == "" ||
      theForm.Zipcode.value == "") && (!theForm.Street.value == "" || !theForm.City.value == "" ||
      !theForm.State.value == "" || !theForm.Zipcode.value == ""))
    {
      alert("FORM ERROR\nIncomplete Address Information Provided.\n\n" +
            "This information is optional but if you include your\naddress " +
            "the information must be complete.\n\n" +
            "Please Make Corrections And Try Again.");
    }
    // error when incomplete address information provided & contact by mail is checked
    else if ((theForm.Street.value == "" && theForm.City.value == "" && theForm.State.value == "" &&
      theForm.Zipcode.value == "") && theForm.reply.selectedIndex == 3)
    {
      alert("FORM ERROR\nInappropriate Option Selected.\n\n" +
          "You have indicated that you would like us to contact you by\n" +
          "regular mail but have not completed the necessary address\ninformation.\n\n" +
          "Please Make Corrections And Try Again.");
    }
    // error when no telephone number provided & contact by telephone is checked
    else if (theForm.Telephone.value == "" && theForm.reply.selectedIndex == 2)
    {
      alert("FORM ERROR\nInappropriate Option Selected.\n\n" +
          "You have indicated you would like us to contact you\n" +
          "by telephone but have not included a number to call.\n\n" +
          "Please Make Corrections And Try Again.");
    }
    else
    {
      validated = true;
    }
 }// end if/else
    return validated;
} // end validateForm()

/*
  This function will capitalize single and multi-line text.  The
  argument passed to the function is the text to be capitalized.
*/
function capitalizeText(theString)
{
  // The function's return value.
  var stringValue = "";
  // Capitalize the string if there is input.
  if (theString.length > 0)
  {
    theString = theString.toLowerCase();
    theString = theString.replace(/^[a-z]|\b[a-z]/g,
      function (subString)
      {
        return subString.toUpperCase();
      });
   stringValue = theString;
  }
  return stringValue;
}// end capitalizeText()

function writeSpan(anID, text)
{
	if (document.getElementById || document.all)
	{
		document.write('<span id="' + anID + '" style="font-family: Times New Roman; font-size: 14pt; font-style: normal; color: red; font-weight: normal; visibility: hidden">' + text + '<\/span>');
	}
} // end writeSpan()

function hideById(id)
{
	if (document.getElementById)
	{
		document.getElementById(id).style.visibility = "hidden";
	}
	else if (document.all)
	{
		document.all.item(id).style.visibility = "hidden";
	}
}

function showById(id)
{
	if (document.getElementById)
	{
		document.getElementById(id).style.visibility = "visible";
	}
	else if (document.all)
	{
		document.all.item(id).style.visibility = "visible";
	}
}
