// A threadsafe implementation for XMLHTTPRequest


var req;

function submitemail(email, name, phone, inquiry, response)
{
  var url, callback;
  if (response != '')
  { 
    // Response mode
	//alert("response mode");
    divtochange = document.getElementById("home_submit");
	if (response != 0)
	{
		//alert("response=new title");
		divtochange.innerHTML = '<a class="submit_btn" href="javascript:submitform();">Submit</a><span class="warning"><br />Thank You!</span>';
		document.emailform.email.value = "";
		document.emailform.name.value = "";
		document.emailform.phone.value = "";
		document.emailform.inquiry.value = "";
	}
	else
	{ 
		// do nothing 
		alert("** ERROR - message not sent**");
	}
		
  }else{
    // Input mode
	//alert("input mode");
	if(phone == "") phone = "not entered";
	if(inquiry == "") inquiry = "not entered";
	var trimmedEmail = trim(email);
	var encodedName = urlencode(trim(name));
	var encodedPhone = urlencode(trim(phone));
	var encodedInquiry = urlencode(trim(inquiry));
    url  = 'submitemail.php?email=' + trimmedEmail + '&name=' + encodedName + '&phone=' + encodedPhone + '&inquiry=' + encodedInquiry;
	callback = true;
	ajaxSend(url, callback);
	//alert("end of input mode");
  }

}

// threadsafe asynchronous XMLHTTPRequest code
function ajaxSend(url, callback)
{
	// we use a javascript feature here called "inner functions"
	// using these means the local variables retain their values after the outer function
	// has returned. this is useful for thread safety, so 
	// reassigning the onreadystatechange function doesn't stomp over earlier requests.
	//alert("in ajaxSend");
	function ajaxBindCallback()
	{
		//alert("in ajaxBindCallback");
		if (ajaxRequest.readyState == 4)
		{
			if (ajaxRequest.status == 200) 
			{
				if (ajaxCallback)
				{
					//alert("in ajaxCallback true section");
      				response  = ajaxRequest.responseXML.documentElement;
      				method    = response.getElementsByTagName('method')[0].firstChild.data;
      				input1    = response.getElementsByTagName('input1')[0].firstChild.data;
      				input2    = response.getElementsByTagName('input2')[0].firstChild.data;
      				input3    = response.getElementsByTagName('input3')[0].firstChild.data;
      				result    = response.getElementsByTagName('result')[0].firstChild.data;
					//alert("method: "+method);
					//alert("input1: "+input1);
					//alert("result: "+result);
      				eval(method + '(input1)');
					//alert("end ajaxCallback true section after eval");
				}
				else 
				{
					// do nothing
					//alert('no callback defined');
				}
			}
			else alert("There was a problem retrieving the xml data:\n" + ajaxRequest.status + ":\t" + ajaxRequest.statusText + "\n" + ajaxRequest.responseText);
		}
	}
	// use a local variable to hold our request and callback until the inner function is called...
	var ajaxRequest = null;
	var ajaxCallback = callback;
	// bind our callback then hit the server...
	if (window.XMLHttpRequest)
	{
		// moz et al
		//alert("in moz et al");
		ajaxRequest = new XMLHttpRequest();
		ajaxRequest.onreadystatechange = ajaxBindCallback;
		ajaxRequest.open("GET", url, true);
		ajaxRequest.send(null);
	} 
	else if (window.ActiveXObject) 
	{
		// ie
		//alert("in ie");
		ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
		if (ajaxRequest) 
		{
			ajaxRequest.onreadystatechange = ajaxBindCallback;
			ajaxRequest.open("GET", url, true);
			ajaxRequest.send();
		}
	}
}

