/**
 * infoRequest.js
 * @author Nick Kreeger <nick.kreeger@park.edu>
 *
 * @dependencies:
 *   - ajaxBase.js
 */
 
var contactForm = null;
var errorDiv = document.getElementById('errorDiv');
 
/**
 * Send the information request.
 * @param aContactForm The HTML form that contains the form data.
 */
function submitInfoRequest(aContactForm)
{
  contactForm = aContactForm;
  
  hideErrorDiv();
  
  if (contactForm.firstName.value == "")
  {
    showError('Please enter your first name');
    return;
  }
  
  if (contactForm.lastName.value == "")
  {
    showError('Please enter your last name');
    return;
  }
  
  if (contactForm.email.value == "")
  {
    showError('Please enter your email address');
    return;
  }
  
  initAJAX('data/requestInfo.php', 'POST', 'application/x-www-form-urlencoded');
  
  var str = getFormValues(aContactForm, "validate");
  //alert(str);
  lockForm();
  sendAjaxRequest(str, infoRequestCallback);
}

/**
 * Reset the form and hide the error div.
 * @param aContactForm The HTML contact form.
 */
function resetForm(aContactForm)
{
  contactForm = aContactForm;
  contactForm.reset();
  hideErrorDiv();
}

/**
 * Handle the information 'onreadystatechange' events.
 */
function infoRequestCallback()
{
  if (gHttpRequest.readyState == 4)
  {
    if (gHttpRequest.status == 200)
    {
      // All responses will come back in an XML response with the 
      // root node named '<request>'. The root node will contain at
      // least one node named '<status>' that contains the boolean
      // response to the success of the request. If a request fails,
      // then a '<reason>' node will be added containing the reason 
      // for the failure.
      var xmldoc = gHttpRequest.responseXML;
      var requestNode = xmldoc.getElementsByTagName('request');
      var isSuccess = 'false';  // assume failure
      
      for (var i = 0; i < requestNode.length; i++)
      {
        try
        {
          isSuccess = requestNode[i].getElementsByTagName('status')[0].childNodes[0].nodeValue;
        }
        catch (e) { }
        
        if (isSuccess == 'true')
        {
          showThanks();
        }
        else
        {
          var reason = "";
          try
          {
            reason = requestNode[i].getElementsByTagName('reason')[0].childNodes[0].nodeValue;
          }
          catch (e) { }
          
          showError(reason);
        }
      }
      
      unlockForm();
    }
    
    else
    {
      alert("Thank you. Your message has been sent");
      unlockForm();
	  resetForm();
    }
  }
}

function lockForm()
{
  for (var i = 0; i < contactForm.elements.length; i++)
    contactForm.elements[i].disabled = true;
    
  contactForm.submitButton.value = "Loading...";
  contactForm.submitButton.style.color = '#fff';
}

function unlockForm()
{
  for (var i = 0; i < contactForm.elements.length; i++)
    contactForm.elements[i].disabled = false;
    
  contactForm.submitButton.value = "Submit";
  contactForm.submitButton.style.color = '#000';
}

function validate(aVar)
{
  //XXX add some validation.
}

/*function showThanks()
{
  var thanksDiv = document.getElementById('thanksDiv');
  thanksDiv.style.visibility = 'visible';
  thanksDiv.innerHTML = 'Thanks ' + contactForm.firstName.value + ' ' + contactForm.lastName.value + ', ' +
                        'we have received your information. Someone with The Village of Loch Lloyd will contact you shortly.';
                          
  var formDiv = document.getElementById('formDiv');
  formDiv.style.visibility = 'hidden';
  
  window.location = 'information_request.html#top';
}*/

function showThanks()
{
  var replaceForm = document.getElementById('formDiv');
  replaceForm.innerHTML = 'Thanks ' + contactForm.firstName.value + ' ' + contactForm.lastName.value + ', ' +
                        'we have received your information. Someone with The Village of Loch Lloyd will contact you shortly.';
}

function hideErrorDiv()
{
  var errorDiv = document.getElementById('errorDiv');
  var errorDiv2 = document.getElementById('errorDiv2');
  errorDiv.style.visibility = 'hidden';
  errorDiv2.style.visibility = 'hidden';
  errorDiv.style.padding = "0px";
  errorDiv2.style.padding = "0px";
  errorDiv.innerHTML = '';
  errorDiv2.innerHTML = '';
}

function showError(aText)
{
  var errorDiv = document.getElementById('errorDiv');
  var errorDiv2 = document.getElementById('errorDiv2');
  errorDiv.style.visibility = 'visible';
  errorDiv2.style.visibility = 'visible';
  errorDiv.style.padding = "5px";
  errorDiv2.style.padding = "5px";
  errorDiv.innerHTML = aText;
  errorDiv2.innerHTML = aText;
}
