// We need to check the length of the request at the point when the user submits the form
    // as it is possible for the user to paste more than 4000 characters into the field in Firefox v3
    // where the clipboard is not available.
    function checkRequestLength(txtRequest)
    {
        var errorMessage = "";
        
        if (document.getElementById(txtRequest).value.length > maxChars)
        {
            errorMessage += "You have entered " + document.getElementById(txtRequest).value.length + " characters into the \"Your request\" field. The request must be less than " + maxChars + " characters." + "\n";
        }
        
        return errorMessage;
    }
        
    //Method that detects if the length of the field is more than the allowable amount in the text area.
    //If so, a warning message is made visible
    function maxLength(field,maxCharacters,labelfield,e)
    { 
       var txtRequestObject = document.getElementById(field);
       var labelFieldObject = document.getElementById(labelfield);
    
       if(txtRequestObject.value.length >= maxCharacters) 
       {                
          labelFieldObject.style.display = "inline"; 
          if (window.event) // IE
          {
            event.returnValue=false;
          }
          else // Other browser (i.e. Firefox)
          {
            if (e.keyCode != 8 && e.keyCode != 46) // Backspace or delete character
            {
                e.preventDefault();
            }
            else
            {
                return true;
            }
          }   
          return false;
       }
       else
       {
          labelFieldObject.style.display = "none";          
       }
       return true;
    }  
     
     //Method that detects if the length of the field is less than the allowable amount in the text area.
     //If so, a warning message is made invisible.
     function detectBelowMaxLength(field,maxCharacters,labelfield,remainingfield,remaininglabel)
     {
           var txtRequestObject = document.getElementById(field);
           var labelFieldObject = document.getElementById(labelfield);
           var txtCharactersLeft = document.getElementById(remainingfield);
           var lblCharactersLeft = document.getElementById(remaininglabel);
           
           if(txtRequestObject.value.length < maxCharacters) 
           {             
                labelFieldObject.style.display = "none";          
           }
           else
           {
                labelFieldObject.style.display = "inline";                    
           }
           
           if (maxCharacters - txtRequestObject.value.length < 500)
           {
                txtCharactersLeft.style.display = "inline";
                lblCharactersLeft.style.display = "inline";
                txtCharactersLeft.value = maxCharacters - txtRequestObject.value.length;
           }
           else
           {
                txtCharactersLeft.style.display = "none";
                lblCharactersLeft.style.display = "none";
           }            
     }
     
     // Method that sets the red star (indicating required field) when the response required checkbox is selected.
     function makeEmailMandatory(responseRequired,emailLabel)
     {    
        var emailLabelObject = document.getElementById(emailLabel);
        emailLabelObject.innerHTML = emailAddress;
        
        var redStar = "<em>*</em>";
        if (document.getElementById(responseRequired).checked)
        {
            emailLabelObject.innerHTML += redStar;
        }
        else
        {
            emailLabelObject.innerHTML = emailAddress;
        }
        // We do not have a red star against any field if the response required checkbox is not checked.
    }
    // Loads the XML document
    function loadXMLDoc(dname)
    {
        var xmlDoc;
        if (window.XMLHttpRequest)
        {
            xmlDoc=new window.XMLHttpRequest();
            xmlDoc.open("GET",dname,false);
            xmlDoc.send("");
            return xmlDoc.responseXML;
        }
        // IE 5 and IE 6
        else if (ActiveXObject("Microsoft.XMLDOM"))
        {
            xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
            xmlDoc.async=false;
            xmlDoc.load(dname);
            return xmlDoc;
        }
        alert("Error loading document");
        return null;
    } 
    // If the value is blank, we add to the error message by pulling it from the StringResources XML document.
    function AddToErrorMessage(fieldToValidate, validationString, xmlDoc)
    {
        var errorMessage = "";
        var x;
        
        if (document.getElementById(fieldToValidate).value == "")
        {
            for(var i=0; i < xmlDoc.getElementsByTagName("data").length; i++)
            {
                x = xmlDoc.getElementsByTagName("data")[i];
                if (x.getAttributeNode("name").value == "FOI_Request_vld" + validationString + "_ErrorMessage")
                {
                    x = xmlDoc.getElementsByTagName("value")[i];
                    errorMessage += x.childNodes[0].nodeValue + "\n";
                }   
            }
        }
        
        return errorMessage;
    }
    
    // Checks for a valid email address using a regular expression.
    // Adds to the error message if the email is invalid.
    function CheckValidEmail(txtEmail, xmlDoc)
    {
        var errorMessage = "";
        var x;
        
        pat = /^([a-zA-Z0-9-\u0027-_]+\.)*[a-zA-Z0-9-\u0027-_]+\@([a-zA-Z0-9-_]+\.)+[a-zA-Z0-9]+[a-zA-Z0-9]+$/;
        
        if(!pat.test(txtEmail))
        {
            for(var i=0; i < xmlDoc.getElementsByTagName("data").length; i++)
            {
                x = xmlDoc.getElementsByTagName("data")[i];
                if (x.getAttributeNode("name").value == "FOI_Request_vldEmail_ErrorMessage")
                {
                    x = xmlDoc.getElementsByTagName("value")[i];
                    errorMessage += x.childNodes[0].nodeValue + "\n";
                }   
            } 
        }
        return errorMessage;
    }
    // Called to validate the fields on the client when the submit button is clicked.
    // If any field values are invalid an error message is displayed and the request is not submitted.
    function validateFields(responseRequired,txtTitle,txtFirstName,txtLastName,txtAddress,txtPhone,txtEmail,txtRequest,txtPostCode)
    {
        var errorMessage = "";
        var x;
        var emailError = "";
        var requestError = "";
        
        xmlDoc=loadXMLDoc("/XMLData/StringResources.xml");
        
        errorMessage += AddToErrorMessage(txtTitle, "Title", xmlDoc);
        errorMessage += AddToErrorMessage(txtFirstName, "FirstName", xmlDoc);
        errorMessage += AddToErrorMessage(txtLastName, "LastName", xmlDoc);
        
        if (document.getElementById(responseRequired).checked == true)
        {
            emailError = AddToErrorMessage(txtEmail, "Email", xmlDoc);
            errorMessage += emailError;
        }
        
        if (emailError == "" && document.getElementById(txtEmail).value != "") 
        {
            // Validate the email address using a regular expression
            errorMessage += CheckValidEmail(document.getElementById(txtEmail).value, xmlDoc);
        }
        requestError = AddToErrorMessage(txtRequest, "Request", xmlDoc);
        
        if (requestError == "")
        {
            errorMessage += checkRequestLength(txtRequest);
        }
        else
        {
            errorMessage += requestError;
        }
        
        if (errorMessage == "")
        {
            return true;
        }
        else
        {
            alert(errorMessage);
            return false;
        }
    }
