//holds an instance of XMLHttpRequest
var xmlHttp = createXmlHttpRequestObject();

//creates an XMLHttpRequest instance
function createXmlHttpRequestObject() {
	
	var xmlHttp;

	//for all browsers except IE6 and older
	try
	{
		//try to create XMLHttpRequest object
		xmlHttp = new XMLHttpRequest();
	}
	catch (e)		
	{
		//assume IE6 or older
		var xmlHttpVersions = new Array (
												'MSXML2.XMLHTTP.6.0',
												'MSXML2.XMLHTTP.5.0',
												'MSXML2.XMLHTTP.4.0',
												'MSXML2.XMLHTTP.3.0',
												'MSXML2.XMLHTTP',
												'Microsoft.XMLHTTP'												
												);
		// try every prog id until one works
		for (var i=0; i<xmlHttpVersions.length && !xmlHttp ; i++)
		{		
			try
			{
				xmlHttp = new ActiveXObject (xmlHttpVersions[i]);
			}
			catch (e)	{ } // ignore potential error
		}
	}
	// return the created object or display an error message
	if (!xmlHttp) alert ("Error creating the XMLHttpRequest object.");
	else return xmlHttp;
}

//called to read a file frmo the server
function process(cnt)
{
	var hotdeal_cat = document.hotdeal.hotdeal_cat.value;

	//only continue if xmlHttp isn't void
	if (xmlHttp)
	{
		//try to connect to the server
		try
		{
			//initiate reading the random.php file from the server
			xmlHttp.open ("GET","random.php?cat=" + hotdeal_cat + "&cnt=" + cnt + "&a=" + new Date().getTime(),true);
			xmlHttp.onreadystatechange = handleRequestStateChange;
			xmlHttp.send (null);
		}
		catch (e)
		{
			//display the error in case of failure
			alert ("Can't connect to the server:\n" + e. toString());
		}
	}
}

cnt = 0
function runRandomImage()
{
cnt = cnt + 1;
process(cnt); // Runs image function once
setTimeout("runRandomImage()",7000);
}

//function that handles the HTTP response
function handleRequestStateChange()
{
	if (xmlHttp.readyState == 4)
	{
		//continue only if HTTP status is OK
	    if (xmlHttp.status == 200)
		{
			try
			{
				handleServerResponse();
			}
			catch (e)
			{
				alert ("Error reading the response: " + e.toString());
			}
		 }
		 else {
			 //display status message
			 alert ("There was a problem retrieving the date:\n" + xmlHttp.statusText);
		 }
	}
}
function handleServerResponse()
{
	myDiv1 = document.getElementById("div_main_title");
	myDiv2 = document.getElementById("div_main_price");
	myDiv3 = document.getElementById("div_main_pic");

    var xmlResponse = xmlHttp.responseXML;
	//obtain the XML's document element
	xmlRoot = xmlResponse.documentElement;
	
	x_id = xmlRoot.getElementsByTagName("id").item(0).firstChild.data;
	x_title = xmlRoot.getElementsByTagName("title").item(0).firstChild.data;
	x_price = xmlRoot.getElementsByTagName ("price").item(0).firstChild.data;
	x_pic = xmlRoot.getElementsByTagName ("pic").item(0).firstChild.data;

	// writes the image files output to the div </font>  #6D82A0
	myDiv1.innerHTML = x_title;
	myDiv2.innerHTML = '<FONT color=#000000><B>Our Price:</B> </FONT><FONT color=#ce0100 size=3><B>$' + x_price + '</div></B></FONT> </A>';
	myDiv3.innerHTML = '<A href="http://www.aerocooler.com/shop.cart?action=ITEM&prod_id=' + x_id + '"><IMG height=180 src="' + x_pic + '?a=' + new Date().getTime() + '" width=180 border=0></A>';
}
