/*
// Ajax functions to hijack the contact form
// will need to add php mail function to formlogic.php file 
*/
window.onload = prepareForm;

function prepareForm() {
	// check for functionality
	if(!document.getElementById) {   
	   return;
	   }
	// check to match sure theform element exists
	if(!document.getElementById("theform")) {   
	   return
	   }
	// if it does
	document.getElementById("theform").onsubmit = function() {   
	   var data = "";
		// as long as you are in the form element, you can loop thru all its fields by using the elements attribute
		for(var i = 0; i < this.elements.length; i++) {   
		   data += this.elements[i].name;
			data += "=";
			data += escape(this.elements[i].value);
			data += "&";
		   }
		// do not allow the form to be submitted
		return !sendData(data);
	   };  // the semi-colon is necessary for the unnamed function declared for onsubmit event	
   }
	
function sendData(data) {
	// call function that creates an AJAX request object
	var request = getHTTPObject();
	if(request) {   
		// create an anonymous function to parse the response
	   request.onreadystatechange = function() {
			// callback function that returns data(html etc) from server
	   	parseResponse(request);
	      };
		request.open("POST","form_logic.php",true);
		request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		request.send(data);
		return true;
	   }
	else {   
	   return false;
	   }
   }
	
function parseResponse(request) {
	if(request.readyState == 4) {   
	   if(request.status == 200 || request.status == 304) {   
	      var primaryContent = document.getElementById("primaryContent");
	      primaryContent.innerHTML = request.responseText;
			prepareForm();
			}
	   }
   }		
	
function getHTTPObject() {
	var xhr = false;
	if(window.XMLHttpRequest) {   
	   xhr = new XMLHttpRequest;
	   }
	else if(window.ActiveXObject) {   
		try {   
		   xhr = new ActiveXObject("Msxml2.XMLHTTP");
		   }
		catch(e) {   
		   try {   
		      xhr = new ActiveXObject("Microsoft.XMLHTTP");
		      }
			catch(e) {   
			   xhr = false;
			   }
		   }
	   }
   return xhr;
	}	
	