/* $Id: ajax.js,v 1.1.1.1 2006/04/22 15:32:18 Jonathan Exp $ */

// global variable to store the ajax request in
var http_request = false;

/***
* make_request - make an xml ajax request
***/
function make_request(url, handler, error_url)
{
	// First of all, do some checking of browsers
	try
	{
		if (window.XMLHttpRequest)
		{ 
			// Mozilla, Safari,...
			http_request = new XMLHttpRequest();
		}
		else if (window.ActiveXObject)
		{
			// IE
			http_request = new ActiveXObject("Microsoft.XMLHTTP");
		}
		else
		{
			// ajax not supported
		}
	}
	catch (e)
	{
		window.top.location = error_url;
	}
	
	// assign the handler passed in to the onreadystatechange event
	http_request.onreadystatechange = handler;
	
	// open the request
	http_request.open('GET', url, true);
	
	// and send it (with no data)
	http_request.send(null);
} 


/** SOME EXAMPLE/DEBUG HANDLERS **/

function alert_contents()
{
	if (http_request.readyState == 4)
	{
		if (http_request.status == 200)
		{
			alert(http_request.responseText);
		}
		else
		{
			alert('There was a problem with the request.');
		}
	}
}


function onreadystatechange_receipt()
{
	/* If XMLHR object has finished retrieving the data */
	if (http_request.readyState == 4)
	{
		/* If the data was retrieved successfully */
		if (http_request.status == 200)
		{
			write_details();
		}
		/* IE returns a status code of 0 on some occasions, so ignore this case */
		else if (http_request.status != 0)
		{
			alert("There was an error while retrieving the URL: " + http_request.statusText);
		}
	}

	return true;
}


function write_details()
{
	// get the http_request response text
	var receipt = document.getElementById("main");

	var response_status = http_request.responseXML.getElementsByTagName("status")[0].childNodes[0].nodeValue;
	
	if (response_status == "Success")
	{
		receipt.childNodes[0].nodeValue = http_request.responseXML.getElementsByTagName("ev_mkt_id")[0].childNodes[0].nodeValue;
	}
	else
	{
		main_text = "There was an error: " + http_request.responseXML.getElementsByTagName("reason")[0].childNodes[0].nodeValue
		receipt.childNodes[0].nodeValue = main_text;
	}
	
	return true;
}
	
