function doEverything()
   {
      function pageManagement()
         {

            // =========================================================================
            // Parse and Validate Contact Form

            function contactFormManagement()
               {

                  var documentObjectPath = window.document;

                  if (documentObjectPath.getElementById("contactForm")) // Makes sure to run this function only if an ID of "contactForm" is present.
                     {


                        // ============ Declares and assigns values to local variables =============
                        // =========== that match IDs in the corresponding HTML document. ==========
                        var contactForm = documentObjectPath.getElementById("contactForm");
                        var formNameField = documentObjectPath.getElementById("formNameField");
                        var formEmailField = documentObjectPath.getElementById("formEmailField");
                        var formSubjectField = documentObjectPath.getElementById("formSubjectField");
                        var formMessageField = documentObjectPath.getElementById("formMessageField");
                     //   var formCField = documentObjectPath.getElementById("formCField");
                     //   var formCButton = documentObjectPath.getElementById("formCButton");
                        var formSubmitButton = documentObjectPath.getElementById("formSubmitButton");
                        var formResetButton = documentObjectPath.getElementById("formResetButton");

                        // ====================== Validate the Contact Form ========================
                        function validate(formID)
                           {

                              var inputFields = new Array("formNameField",
                                                          "formEmailField",
                                                          "formSubjectField",
                                                          "formMessageField");
                              var counter;
                              var fieldID;
                              var msg = "Please complete the following fields:\n\n";
                              var badFields = "";

                              for (counter = 0; counter < inputFields.length; counter++)
                                 {
                                    fieldID = inputFields[counter];

                                    if (formID.elements[fieldID].value.length == 0)
                                       {

                                          if (fieldID == "formNameField")
                                             {
                                                badFields = badFields + "  - Name \n";
                                             }

                                          else if (fieldID == "formEmailField")
                                             {
                                                badFields = badFields + "  - Email \n";
                                             }

                                          else if (fieldID == "formSubjectField")
                                             {
                                                badFields = badFields + "  - Subject \n";
                                             }

                                          else if (fieldID == "formMessageField")
                                             {
                                                badFields = badFields + "  - Message \n";
                                             }

                                 //         else if (fieldID == "formCField")
                                 //            {
                                 //              badFields = badFields + "  - Type the two characters from the image. \n";
                                 //            }

                                       }
                                 }

                              if (badFields.length != 0)
                                 {
                                    alert(msg + badFields);
                                    return false;
                                 }

                              if (formID.formEmailField.value.length > 0)
                                 {
                                    return emailCheck(formID.formEmailField.value);
                                 }

// Note to self: Make a function to check the number of characters in the capcha field.

                              else
                                 {
                                    return true;
                                 }

                           }

                        // ============== Check text in fields for correct structure ===============
                        function emailCheck(emailStr)
                           {

                              var emailPat=/^(.+)@(.+)$/;
                              var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
                              var validChars="\[^\\s" + specialChars + "\]";
                              var quotedUser="(\"[^\"]*\")";
                              var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
                              var atom=validChars + '+';
                              var word="(" + atom + "|" + quotedUser + ")";
                              var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
                              var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
                              var matchArray=emailStr.match(emailPat);

                              if (matchArray==null)
                                 {
                                    alert("The email address appears to be incorrect (check @ and .'s)");
                                    return false;
                                 }

                              var user=matchArray[1];
                              var domain=matchArray[2];

                              if (user.match(userPat)==null)
                                 {
                                    alert("The email username doesn't appear to be correct.");
                                    return false;
                                 }

                              var IPArray=domain.match(ipDomainPat);

                              if (IPArray!=null)
                                 {
                                    for (var i=1;i<=4;i++)
                                       {
                                          if (IPArray[i]>255)
                                             {
                                                alert("The email destination IP address is invalid.");
                                                return false;
                                             }
                                       }

                                    return true;
                                 }

                              var domainArray=domain.match(domainPat);

                              if (domainArray==null)
                                 {
                                    alert("The email domain name doesn't appear to be correct.");
                                    return false;
                                 }

                              var atomPat=new RegExp(atom,"g");
                              var domArr=domain.match(atomPat);
                              var len=domArr.length;

                              if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>3)
                                 {
                                    alert("The email address must end in a three-letter domain, or two-letter country.");
                                    return false;
                                 }

                              if (len<2)
                                 {
                                    var errStr="The email address is missing a hostname.";
                                    alert(errStr);
                                    return false;
                                 }

                              return true;
                           }

                        // ============= Execute limited captcha image reloads on button click. ==============
                   //     var reloadsLeft = 5;
                   //     var captchaImage = documentObjectPath.getElementById("cImage");

                   //     function captchaReloadRestrict()
                   //        {
                   //           function refreshCaptcha()
                   //              {
                   //                 captchaImage.setAttribute("src",captchaImage.src+'?count='+reloadsLeft);
                   //                 reloadsLeft--;
                   //              }
                   //           if (reloadsLeft > 3)
                   //              {
                   //                 refreshCaptcha();
                   //              }
                   //           else if (reloadsLeft > 2)
                   //              {
                   //                 refreshCaptcha();
                   //                 alert("Only " + reloadsLeft + " more reloads allowed.");
                   //              }
                   //           else if (reloadsLeft > 1)
                   //              {
                   //                 refreshCaptcha();
                   //                 alert("Only " + reloadsLeft + " more reload allowed.");
                   //              }
                   //           else if (reloadsLeft > 0)
                   //              {
                   //                 refreshCaptcha();
                   //                 alert("Last reload used.");
                   //              }
                   //           else
                   //              {
                   //                 alert("No more reloads allowed.");
                   //              }
                   //           return false;
                   //        }

                        // ============= Execute the Contact Form validation function ==============
                        function executeValidate()
                           {
                              return validate(contactForm);
                           }


                        // Uses previously declared local variables to assign event handlers
                        // to execute the previously defined functions upon XHTML objects.
                        contactForm.onsubmit = executeValidate;
                   //     formCButton.onclick = captchaReloadRestrict;


                     }


               }


            contactFormManagement();
         }

      pageManagement();
   }


// Waits until the "window" object and all of its children have been created
// before trying to manipulate those children objects using the above functions.
// This works because the "window" object is high enough in the object hierarchy
// to always have already loaded before this JavaScript file is even read.

// NOTE: By using this technique, no event handlers
// (and therefore, no associated JavaScript), exist within the actual XHTML file
// itself. This keeps the scripting and markup completely separate from each
// other and conveniently modular. The XHTML document is then manipulated only
// through its object tree with the use of element IDs attached to the XHTML
// tags.
window.onload = doEverything;
