var http = getXMLHTTPRequest();
var lastShoutId = 0;
var isNewShout = true;

var browser=navigator.appName;
var b_version=navigator.appVersion;
var version=parseFloat(b_version);


setInterval("refreshShoutbox()", 5000 );

function getXMLHTTPRequest() 
{
	try 
	{
		req = new XMLHttpRequest();
	} 
	catch(err1) 
	{
		try 
		{
			req = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (err2) 
		{
			try 
			{
				req = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (err3) 
			{
				req = false;
			}
		}
	}
	return req;
}

function refreshShoutbox() 
{
	var xmlUrl = '../xml/shoutboxXML.php';
	myRand = parseInt(Math.random()*999999999999999);
	// add random number to URL to avoid cache problems
	var newUrl = xmlUrl+"?rand="+myRand;
	http.open("GET", newUrl, true);
	http.onreadystatechange = insertShoutBox;
	http.send(null);
}

function insertShoutBox() 
{
	if (http.readyState == 4) 
	{
		if(http.status == 200) 
		{
			var xml = http.responseXML;
			var shoutCount = xml.getElementsByTagName("shout").length;
			var inner = "";
			var latestShoutId = "";
			for(var i=0; i<shoutCount; i++)
			{
				if(i==0)
					latestShoutId = xml.getElementsByTagName("id")[i].childNodes[0].nodeValue ;
				
				inner += "<p style='font:11px Helvetica, Arial, sans-serif;padding:0px;margin:0px;margin-left:3px;'><b>" 
					+ xml.getElementsByTagName("name")[i].childNodes[0].nodeValue 
					+ "</b><h6 style='font:10px Helvetica, Arial, sans-serif;padding:0px;margin:0px;color:#999999;margin-left:3px;'>"
					+ xml.getElementsByTagName("date")[i].childNodes[0].nodeValue
					+ "</h6><h5 style='font:12px Helvetica, Arial, sans-serif;padding:0px;margin:0px;color:#CE6406; margin-left:3px;'>"
					+ xml.getElementsByTagName("comment")[i].childNodes[0].nodeValue + "</h5></p><hr>";
			}
			if(latestShoutId != lastShoutId)
			{  
				lastShoutId = latestShoutId;
				if(browser == "Microsoft Internet Explorer")
					document.getElementById("shoutboxFrame").contentWindow.shoutboxInner.innerHTML = inner;
				else
					document.getElementById("shoutboxFrame").contentDocument.getElementById('shoutboxInner').innerHTML = inner;
			}
		}
	}
}
