var xmlHttp;
var imageid;
var newitem;
var browser;
function defcon1() {
	xmlHttp.abort();
	document.getElementById('time').innerHTML = "Cache page timeout. Retrying.";
	clearTimeout(stopfetch);
	setTimeout("ajaxUpdater('xmlparse.php');",5000);
}
function displayTime(timestamp,serverTime) {
	currentTime = new Date();
	if(currentTime.getHours() < 10) {
		var hours = '0' + currentTime.getHours();
	}
	else {
		var hours = currentTime.getHours();
	}
	if(currentTime.getMinutes() < 10) {
		var minutes = '0' + currentTime.getMinutes();
	}
	else {
		var minutes = currentTime.getMinutes();
	}
	if(currentTime.getSeconds() < 10) {
		var seconds = '0' + currentTime.getSeconds();
	}
	else {
		var seconds = currentTime.getSeconds();
	}
	if(serverTime < timestamp) {
		serverTime = timestamp;
		document.getElementById('servertime').innerHTML = serverTime;
	}
	var secOld = Math.round(eval(serverTime - timestamp));
	if(secOld > 90) {
		secOld = 90;
	}
	return 'Cache is about ' + secOld + ' second(s) old. (' + hours + ':' + minutes + ':' + seconds + ')';
}
function elementFix(elementTagName) {
	if (elementTagName == '<b>') {
		if ((browser != "IE") && (navigator.appName != "Microsoft Internet Explorer")) return '<b>';
		else return '<B>';
	}
	if (elementTagName == '</b>') {
		if ((browser != "IE") && (navigator.appName != "Microsoft Internet Explorer")) return '</b>';
		else return '</B>';
	}
	if ((browser != "IE") && (navigator.appName != "Microsoft Internet Explorer")) return elementTagName;
	else return "woot:" + elementTagName;
}
function ajaxUpdater(urlFetch)  {
	var wootCache;
	try
	{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
		browser = "notIE";
	}
	catch (e)
	{
		// Internet Explorer
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
			browser = "IE";
		}
		catch (e)
		{
			try
			{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
				browser = "IE";
			}
			catch (e)
			{
				alert("Your browser does not support AJAX!  You are being redirected to a page better suited for your browser.");
				window.location = "/noajax.php"
				return false;
			}
		}
	}
	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState==4)
		{

			stopfetch = setTimeout('defcon1();',15000);
			if (xmlHttp.responseXML.getElementsByTagName('title')[0].firstChild.nodeValue) {
				if(imageid) {
					if((imageid != xmlHttp.responseXML.getElementsByTagName('forceimagerefresh')[0].firstChild.nodeValue) && (document.getElementById('title').innerHTML != (elementFix('<b>') + xmlHttp.responseXML.getElementsByTagName('title')[0].firstChild.nodeValue + elementFix('</b>')))) {
						imageid = xmlHttp.responseXML.getElementsByTagName('forceimagerefresh')[0].firstChild.nodeValue;
						newitem=document.getElementById('newitem');
						newitem.Play();
					}
				}
				else { imageid = xmlHttp.responseXML.getElementsByTagName('forceimagerefresh')[0].firstChild.nodeValue; }
			}
			else { 
				defcon1();
				return 0;
			}
			wootCache=xmlHttp.responseXML.getElementsByTagName('title')[0].firstChild.nodeValue;
			if((wootCache.length > 0) && (xmlHttp.responseXML.getElementsByTagName('WOOT')[0].childNodes.length >= 10))
			{
				document.getElementById('wootoff').innerHTML = '';
				document.getElementById('title').innerHTML='<b>' + xmlHttp.responseXML.getElementsByTagName('title')[0].firstChild.nodeValue + '</b>';
				document.getElementById('price').innerHTML=xmlHttp.responseXML.getElementsByTagName(elementFix('price'))[0].firstChild.nodeValue;
				document.getElementById('condition').innerHTML='<b>Condition: </b>' + xmlHttp.responseXML.getElementsByTagName(elementFix('condition'))[0].firstChild.nodeValue;
				if(xmlHttp.responseXML.getElementsByTagName(elementFix('soldout'))[0].firstChild.nodeValue == 'True')
				{
					document.getElementById('purchaseurl').innerHTML='<div class="soldout"><b>Item is Sold Out!</b></div>';
				}
				else
				{
					if(xmlHttp.responseXML.getElementsByTagName(elementFix('soldoutpercentage')).length == 0) {
						document.getElementById('purchaseurl').innerHTML='<a class="buylink" href="' + xmlHttp.responseXML.getElementsByTagName(elementFix('purchaseurl'))[0].firstChild.nodeValue + '">I want one!</a>';
					}
					else {
						if(xmlHttp.responseXML.getElementsByTagName(elementFix('soldoutpercentage'))[0].firstChild.nodeValue >= .90) {
							document.getElementById('purchaseurl').innerHTML='<a class="buylink" href="' + xmlHttp.responseXML.getElementsByTagName(elementFix('purchaseurl'))[0].firstChild.nodeValue + '">I want one!<br />(They\'re almost gone!)</a>';
						}
						else {
							document.getElementById('purchaseurl').innerHTML='<a class="buylink" href="' + xmlHttp.responseXML.getElementsByTagName(elementFix('purchaseurl'))[0].firstChild.nodeValue + '">I want one!</a>';
						}
					}
				}
				if(xmlHttp.responseXML.getElementsByTagName(elementFix('wootoff'))[0].firstChild.nodeValue == 'True')
				{
					var percentleft = Math.round(eval(1 - xmlHttp.responseXML.getElementsByTagName(elementFix('soldoutpercentage'))[0].firstChild.nodeValue) *100) + '% left';
					document.getElementById('wootoff').innerHTML = 'Woot-off!<br /><h2>' + percentleft + '</h2>';
					document.title = percentleft + ' Woot-off! - ' + wootCache;
				}
				else
				{
					document.title = wootCache;
				}
				document.getElementById('discussionurl').innerHTML='<a class="forumlink" href="' + xmlHttp.responseXML.getElementsByTagName(elementFix('discussionurl'))[0].firstChild.nodeValue + '">Discuss this product</a>';
				var products = xmlHttp.responseXML.getElementsByTagName(elementFix('product'));
				document.getElementById('itemlist').innerHTML = '';
				for (var i=0;i<products.length;i++)
				{
					var items = document.createElement('div');
					items.className = 'items';
					var item = document.createElement('p');
					item.className = 'item';
					var boldqty = document.createElement('b');
					boldqty.appendChild(document.createTextNode("Quantity: "));
					var productquantity = document.createTextNode(products[i].getAttribute('quantity'));
					var addbr=document.createElement('br');
					var boldproduct=document.createElement('b');
					boldproduct.appendChild(document.createTextNode("Product: "));
					var productdesc = document.createTextNode(products[i].firstChild.nodeValue);
					item.appendChild(boldqty);
					item.appendChild(productquantity);
					item.appendChild(addbr);
					item.appendChild(boldproduct);
					item.appendChild(productdesc);
					items.appendChild(item);
					document.getElementById('itemlist').appendChild(items);
					document.getElementById('shipping').innerHTML='+ $5 shipping';
				}
				if(document.getElementById('image')) {
					document.getElementById('image').innerHTML = '<img src="image.jpg?' + xmlHttp.responseXML.getElementsByTagName('forceimagerefresh')[0].firstChild.nodeValue + '" alt="' + wootCache + '" />';
				}
				document.getElementById('time').innerHTML = 'Cache is updated every ' + xmlHttp.responseXML.getElementsByTagName('refresh')[0].firstChild.nodeValue + ' seconds.  ' + displayTime(xmlHttp.responseXML.getElementsByTagName('timestamp')[0].firstChild.nodeValue,document.getElementById('servertime').innerHTML);
				clearTimeout(stopfetch);
				setTimeout('wootrefresh();',eval(xmlHttp.responseXML.getElementsByTagName('refresh')[0].firstChild.nodeValue * 1000));
			}
			else {
				clearTimeout(stopfetch);
				setTimeout("ajaxUpdater('xmlparse.php');",5000);
				document.getElementById('time').innerHTML = "Cache failed to update.  Refreshing again in 5 seconds.";
			}
		}
	}
	xmlHttp.open("GET",urlFetch,true);
	xmlHttp.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
	xmlHttp.send(null);
}


