/**
 * registerCustomer.js
 * @author Nick Kreeger <nick@mymodernmedia.com>
 *
 * @dependencies:
 *   - ajaxBase.js
 */
 
var customerForm = null;
 
/**
 * Attempt to add a customer, provide feedback
 * if the user already exists, or if there was
 * a problem saving the customer.
 */
function attemptCustomerRegistration(aForm)
{
  // Make sure an email address is specified:
  if (aForm.email.value == "")
  {
    alert("Please enter an email address.");
    return;
  }

  customerForm = aForm;
  initAJAX('data/addCustomer.php', 'POST', 'application/x-www-form-urlencoded');

  var str = getFormValues(aForm, "validate");
  lockForm();
  sendAjaxRequest(str, customerRegistrationCallback);
}

function attemptMeasureRegistration(aForm)
{
  // Make sure an email address is specified:
  if (aForm.email.value == "")
  {
    alert("Please enter an email address.");
    return;
  }

  customerForm = aForm;
  initAJAX('data/addCustomer_requestmeasure.php', 'POST', 'application/x-www-form-urlencoded');

  var str = getFormValues(aForm, "validate");
  lockForm();
  sendAjaxRequest(str, customerRegistrationCallback);
}

function attemptShopRegistration(aForm)
{
  // Make sure an email address is specified:
  if (aForm.email.value == "")
  {
    alert("Please enter an email address.");
    return;
  }

  customerForm = aForm;
  initAJAX('data/addCustomer_shopathome.php', 'POST', 'application/x-www-form-urlencoded');

  var str = getFormValues(aForm, "validate");
  lockForm();
  sendAjaxRequest(str, customerRegistrationCallback);
}

function attemptContactRegistration(aForm)
{
  // Make sure an email address is specified:
  if (aForm.email.value == "")
  {
    alert("Please enter an email address.");
    return;
  }

  customerForm = aForm;
  initAJAX('data/addCustomer_contact.php', 'POST', 'application/x-www-form-urlencoded');

  var str = getFormValues(aForm, "validate");
  lockForm();
  sendAjaxRequest(str, customerRegistrationCallback);
}

function attemptTestRegistration(aForm)
{
  // Make sure an email address is specified:
  if (aForm.email.value == "")
  {
    alert("Please enter an email address.");
    return;
  }

  customerForm = aForm;
  initAJAX('data/addCustomer_requestmeasure_test.php', 'POST', 'application/x-www-form-urlencoded');

  var str = getFormValues(aForm, "validate");
  lockForm();
  sendAjaxRequest(str, customerRegistrationCallback);
}

function customerRegistrationCallback()
{
  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("There was a problem with the request. Please try again.");
      unlockForm();
    }
  }
}

function lockForm()
{
  customerForm.firstName.disabled = true;
  customerForm.lastName.disabled = true;
  customerForm.address.disabled = true;
  customerForm.city.disabled = true;
  customerForm.state.disabled = true;
  customerForm.zip.disabled = true;
  customerForm.email.disabled = true;
  customerForm.phone.disabled = true;
  
  customerForm.submitButton.disabled = true;
  customerForm.submitButton.value = "Loading...";
  customerForm.resetButton.disabled = true;
}

function unlockForm()
{
  customerForm.firstName.disabled = false;
  customerForm.lastName.disabled = false;
  customerForm.address.disabled = false;
  customerForm.city.disabled = false;
  customerForm.state.disabled = false;
  customerForm.zip.disabled = false;
  customerForm.email.disabled = false;
  customerForm.phone.disabled = false;
  
  customerForm.submitButton.disabled = false;
  customerForm.submitButton.value = "Submit";
  customerForm.resetButton.disabled = false;
}

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

function showThanks()
{
  var replaceForm = document.getElementById('formDiv');
  replaceForm.innerHTML = 'Thanks ' + customerForm.firstName.value + ' ' + customerForm.lastName.value + ', ' +
                          'we have received your information and someone from Weber Carpet will contact you shortly.';
}

function showError(errorStr)
{
  var hiddenDiv = document.getElementById('errorDiv');
  hiddenDiv.style.visibility = 'visible';
  hiddenDiv.innerHTML = errorStr;
}
