// JavaScript Document

// This function controls all the others when the page Loads
//=====================================================================

//Creation of request object
var request = null;
try {
  request = new XMLHttpRequest();
} catch (trymicrosoft) {
  try {
    request = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (othermicrosoft) {
    try {
      request = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (failed) {
      request = null;
    }
  }
}

if (request == null)
  alert("Error creating request object!");


function addLoadListener(fn)
{
  if (typeof window.addEventListener != 'undefined')
  {
    window.addEventListener('load', fn, false);
  }
  else if (typeof document.addEventListener != 'undefined')
  {
    document.addEventListener('load', fn, false);
  }
  else if (typeof window.attachEvent != 'undefined')
  {
    window.attachEvent('onload', fn);
  }
  else
  {
    var oldfn = window.onload;
    if (typeof window.onload != 'function')
    {
      window.onload = fn;
    }
    else
    {
      window.onload = function()
      {
        oldfn();
        fn();
      };
    }
  }
};



addLoadListener(formReset);
addLoadListener(submitForm);
addLoadListener(showConfirmation);


//
// function getCustomerInfo() {
//     var phone = document.getElementById("phone").value;
//	 if (validatePhone(phone) == false) {
//			return; 
//	 }
//     var url = "lookupCustomer.php?phone=" +
//               escape(phone);
//     request.open("GET", url, true);
//     request.onreadystatechange = updatePage;
//     request.send(null);
//   }
//
//   function updatePage() {
//     if (request.readyState == 4) {
//       if (request.status == 200) {
//         /* Get the response from the server */
//         var customerAddress = request.responseText;
//
//         /* Update the HTML web form */
//         document.getElementById("address").value =
//           customerAddress;
//       } else
//         alert("Error! Request status is " + request.status);
//     }
//  }


 function formReset() {
	document.forms[0].reset();	 
 }
 
   
  function submitForm() {
	
	  
	var name = document.getElementById("name").value; 
	var email = document.getElementById("email").value; 
	var comment = document.getElementById("comment").value; 
	
	
	var url = "submitform.php";
	
	request.open("POST", url, true);
	request.onreadystatechange = showConfirmation;
	request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	request.send("name=" + escape(name) +
				 "&email=" + escape(email) +
				 "&comment=" + escape(comment)); 
  }
  
  function showConfirmation () {
	  if (request.readyState == 4) {
		if (request.status == 200) {
			var response = request.responseText;
			
			//Locate form on page
			var mainDiv = document.getElementById("ajax-response");
			//var orderForm = document.getElementById("order-form");
			  
			//Create some confirmation text
			pElement = document.createElement("p");
			textNode = document.createTextNode(response);
			//textNode = document.createTextNode("Your order should arrive within " + response + " minutes. Enjoy your pizza!");
			
			pElement.appendChild(textNode);
			
			//Insert the confirmation into the Div
			mainDiv.appendChild(pElement);
			formReset();	
				
		} else {
			var message = request.getResponseHeader("Status"); //Get the value of the Status response header if there is one
			
			if ((message.length <=0) || (message.length == null)) {
				alert("Error! Request status is " + request.status);		
		} else {
				alert(message); //If server returned status, then show that to the customer
		}
		}
	  }
  }
