// stores the reference to the XMLHttpRequest object
var xmlHttp = create_XmlHttpRequest_Object();

// retrieves the XMLHttpRequest object
function create_XmlHttpRequest_Object()
{
   // will store the reference to the XMLHttpRequest object
   //var xmlHttp;
   // if running Internet Explorer
   if(window.ActiveXObject)
   {
      try
      {
         // Initialize the Microsoft XMLHTTP object
         xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (e)
      {
         xmlHttp = false;
      }
   }
   // if running Mozilla or other browsers
   else
   {
      try
      {
         xmlHttp = new XMLHttpRequest();
      }
      catch (e)
      {
         xmlHttp = false;
      }
   }
   // return the created object or display an error message
   if (!xmlHttp)
      alert("Error creating the XMLHttpRequest object.");
   else
      return xmlHttp;
}



function Send_Opt_Out(){
   document.getElementById("spinner").style.visibility="visible";
   document.getElementById("status").innerHTML="";
   if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0){
   	email=encodeURIComponent(document.getElementById("emailfield").value);
   	if (email == ""){
   		alert ("You must enter your e-mail address!");
   		return;
   	}
   	
   	xmlHttp.open("GET", "http://www.garibaldispizza.com/optout.php?email=" + email, true);
       
   	xmlHttp.onreadystatechange = Server_Response;
	xmlHttp.send(null);
    }//if
    else{
      setTimeout('Send_Opt_Out()', 1000);
   }//else
}//function Send_Opt_Out


function Send_Newsletter_Registration(choice)
{
   
   document.getElementById("spinner").style.visibility="visible";
   document.getElementById("status").innerHTML="";
   
   // proceed only if the xmlHttp object isn't busy
   if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
   {
      // retrieve the name typed by the user on the form
      if (choice == 1){
      	firstname = encodeURIComponent(document.getElementById("firstnamefield").value);
      	lastname = encodeURIComponent(document.getElementById("lastnamefield").value);
      	if (firstname == '' || lastname == ''){
	      	alert("You must provide a first and last name!");
	      	document.getElementById("spinner").style.visibility="hidden";
	      	return;
      	}//if
      }
      email=encodeURIComponent(document.getElementById("emailfield").value);
      if (email == ""){
      	alert("You must provide an e-mail address!");
      	document.getElementById("spinner").style.visibility="hidden";
      	return;
      
      }
      
      
      if (choice == 1){
        xmlHttp.open("GET", "http://www.garibaldispizza.com/signup.php?firstname=" + firstname + "&lastname=" + lastname + "&email=" + email, true);
      }
      if (choice == 2){
      	xmlHttp.open("GET", "http://www.garibaldispizza.com/optout.php?email=" + email, true);
      }
      
      
      
      
      
      
      // define the method to handle server responses
      xmlHttp.onreadystatechange = Server_Response;
      // make the server request
      xmlHttp.send(null);
   }
   else
      // if the connection is busy, try again after one second
      setTimeout('Send_Newsletter_Registration()', 1000);
}

// executed automatically when a message is received from the server
function Server_Response()
{
   // move forward only if the transaction has completed
   if (xmlHttp.readyState == 4)
   {
      // status of 200 indicates the transaction completed successfully
      if (xmlHttp.status == 200)
      {
      	document.getElementById("spinner").style.visibility="hidden";
         // extract the XML retrieved from the server
         xmlResponse = xmlHttp.responseXML;
		 if (xmlResponse != null){
	         //alert(xmlHttp.status + ", " + xmlHttp.statusText + ", " + xmlHttp.responseText);
    	     // get the first element from the document which is <response> and read its content
        	 response = xmlResponse.documentElement.firstChild.data;
		 }
		 else{
		 	response = '<response>Error accessing the database. Please try later.</response>';		 
		 }
         
         //create an object that represents the row in which the results are to be output
         var obj= document.getElementById('status');
         obj.innerHTML=response;
      }
      else
      {
         alert("There was a problem accessing the server: " + xmlHttp.statusText);
		 document.getElementById("spinner").style.visibility="hidden";
      }
   }
}