/**
 * init()
 *
 * // Replaced by addEvent function
 * window.onload = init;
 */
function init()
{
  // document.getElementById("notify").action = "javascript:void();";
  document.getElementById("notify").onsubmit = saveData;
}

/**
 *
 * saveData()
 *
 */
function saveData()
{
 var ajaxRequest = getHTTPObject(); // Create the HTTP Object

 var email = document.getElementById("email").value;
 var url = "http://get.fitwith.us/notify.php";

  // Create a function that will receive data sent from the server
  ajaxRequest.onreadystatechange = function()
  {
    if (ajaxRequest.readyState == 4)
    {
      document.getElementById("notifyStatus").innerHTML = ajaxRequest.responseText;

      // Re-enable the form on error
      document.getElementById("notify").disabled = false;

      if (document.getElementById("success"))
      {
        // document.getElementById("notify").style.display = 'none';
        var el = document.getElementById('notify');
        el.parentNode.removeChild(el);
      }
    }

    else
    {
      document.getElementById("notifyStatus").innerHTML = '<b>Submitting address...</b><br /><img src="./img/loading.gif" alt="Submitting address..." title="Submitting address..." />';

      // Disable the form so they can't submit it twice
      document.getElementById("notify").disabled = true;
    }
  }

  // Send the POST request
  ajaxRequest.open("POST", url, true);
  ajaxRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  ajaxRequest.send('email=' + email);

  // Prevent the form from being submited
  return false;
}

/**
 *
 * getHTTPObject()
 *
 */
function getHTTPObject()
{
 var xmlhttp;

 // Use IE's ActiveX items to load the file.
 if(typeof ActiveXObject != 'undefined')
 {
  try
  {
   xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  }

  catch (e)
  {
   try
   {
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
   }
   catch (E)
   {
    xmlhttp = false;
   }
  }
 // If ActiveX is not available, use the XMLHttpRequest of Firefox/Mozilla etc. to load the document.
 } else if (XMLHttpRequest)
 {
  try
  {
   xmlhttp = new XMLHttpRequest();
  }
  catch (e)
  {
   xmlhttp = false;
  }
 }
 else
 {
  xmlhttp = false;
 }
 return xmlhttp;
}

/**
 *
 * addEvent()
 *
 */
function addEvent(objObject, strEventName, fnHandler)
{
  // DOM-compliant way to add an event listener
  if (objObject.addEventListener)
  {
    objObject.addEventListener(strEventName, fnHandler, false);
  }

  // IE/windows way to add an event listener
  else if (objObject.attachEvent)
  {
    objObject.attachEvent('on' + strEventName, fnHandler);
  }
}

addEvent(window,'load',init);