/*
* Holds the ID of the location
* Only used if a response is not recieved and GetLocation needs to be called again
*/
var locationID;
/*
* Holds the location number
* Only used if a response is not recieved and GetLocation needs to be called again
*/
var locationNumber;

/*
* Creates the proper request object based on the browser type
* From http://jibbering.com/2002/4/httprequest.html
*/
function createRequestObject() {
	var xmlhttp=false;
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	// JScript gives us Conditional compilation, we can cope with old IE versions.
	// and security blocked creation of the objects.
	try {
	xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
	try {
	xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	} catch (E) {
	xmlhttp = false;
	}
	}
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
			xmlhttp=false;
		}
	}
	if (!xmlhttp && window.createRequest) {
		try {
			xmlhttp = window.createRequest();
		} catch (e) {
			xmlhttp=false;
		}
	}
	return xmlhttp;
}

/*
* Get the Request Object
*/
var objRequest = createRequestObject();

/*
* Calls the file that will retrieve the location details
* Based on http://www.phpbuilder.com/columns/kassemi20050613.php3?aid=923
*/
function GetLocation(location_id, number) {
	locationID = location_id;
	locationNumber = number;
	/* Create the request. The first argument to the open function is the method (POST/GET),
	and the second argument is the url...
	*/
	objRequest.open('get', 'get-location.php?yard_id=' + location_id + "&number=" + number);
	/* Define a function to call once a response has been received.  */
	objRequest.onreadystatechange = HandleLocation;
	/* Send the data. We use something other than null when we are sending using the POST method. */
	objRequest.send(null);
}

/*
* Handles the response from the file that retrieves the location details
*/
function HandleLocation() {
	/* Make sure that the transaction has finished. The XMLHttpRequest object
	has a property called readyState with several states:
	0: Uninitialized
	1: Loading
	2: Loaded
	3: Interactive
	4: Finished */
	if(objRequest.readyState == 4){
		//Finished loading the response
		/* We have got the response from the server-side script,
		let's see just what it was. using the responseText property of
		the XMLHttpRequest object. */

		var response = objRequest.responseText;

		/*
		* If a response is not recieved (most likely becuase the mouse movement is too fast for the script to keep up),
		* then try again
		*/
		if(response.length > 0) {
			/* And now we want to change the information about the location. */
			var location = new getObj('location');
			location.style.backgroundColor = "#C3C3C3";
			location.obj.innerHTML = response;
		}
		else {
			GetLocation(locationID, locationNumber);
		}
	}
}

function GetHomePageRandomImage() {
	objRequest.open('get', 'home-page-image.php');
	/* Define a function to call once a response has been received.  */
	objRequest.onreadystatechange = HandleHomePageRandomImage;
	/* Send the data. We use something other than null when we are sending using the POST method. */
	objRequest.send(null);
	
}

function HandleHomePageRandomImage() {
	if(objRequest.readyState == 4){
		//Finished loading the response
		/* We have got the response from the server-side script,
		let's see just what it was. using the responseText property of
		the XMLHttpRequest object. */

		var response = objRequest.responseText;

		if(response.length > 0) {
			/* And now we want to change the information about the location. */
			var image = new getObj('home-image');

			image.obj.innerHTML = response;
		}
		else {
			GetLocation(locationID, locationNumber);
		}
	}
}

function getObj(name)
//FROM http://www.quirksmode.org/js/dhtmloptions.html
{
	if (document.getElementById)
	{
		this.obj = document.getElementById(name);
		this.style = document.getElementById(name).style;
	}
	else if (document.all)
	{
		this.obj = document.all[name];
		this.style = document.all[name].style;
	}
	else if (document.layers)
	{
		this.obj = document.layers[name];
		this.style = document.layers[name];
	}
}