var http = createRequestObject();
var theresp;
var lasturl;
var lastrstate;
var lbto;
var timesfailed = 0;

function softreboot()
{
	http = createRequestObject();
}
function createRequestObject()
{
	var ro;
	var browser = navigator.appName;
	if (browser == "Microsoft Internet Explorer")
	{
		ro = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else
	{
		ro = new XMLHttpRequest();
	}
	return ro;
}
function rebootajax()
{
	lastrstate = null;
	http = createRequestObject();
	http.open('GET',lasturl,1);
	http.send(null);
	http.onreadystatechange = handleResponse;
	showloading();	
}
function simpleajax(url)
{
	softreboot();
	http.open('GET',url,0);
	http.send(null);
	return(http.responseText);
}
function ajax(url,resp,noload)
{
	softreboot();
	lasturl = url;
	theresp = resp;
	http.open('GET',url,1);
	http.send(null);
	http.onreadystatechange = handleResponse;
	if(!noload) showloading();
}
function handleResponse()
{
	if (http.readyState == 1)
	{
		if (lastrstate == 1)
		{
			timesfailed++;
			if (timesfailed <= 10)
				setTimeout("rebootajax();",1000);
			return false;
		}
	}
	lastrstate = http.readyState;
	if (http.readyState == 4)
	{
		if (http)
		{
			try 
			{
				if (http.status == 200)
				{
					timesfailed = 0;
					hideloading();
					theresp(http.responseText);
				}
				else
				{
					hideloading();
					theresp('');
				}
			}
			catch(err)
			{
				timesfailed++;
				if(timesfailed <= 10)
					setTimeout("rebootajax();",1000);
				return false;
			}
		}
	}
}
function hideloading()
{
	if (document.getElementById('loadingbar'))
	{
		document.getElementById('loadingbar').style.display = 'none';
		clearTimeout(lbto);
	}
}
function showloading()
{
	if (document.getElementById('loadingbar'))
	{
		document.getElementById('loadingbar').style.display = '';
	}
	else
	{
		temp = document.createElement('div');
		temp.setAttribute('id','loadingbar');
		temp.innerHTML += '<table width="100%" cellspacing=0 cellpadding=0><tr><td align=right valign=top><table width=75 cellspacing=0 cellpadding=3><tr><td align=center bgcolor="#cc0000"><font face="Arial" size="-1" color="#ffffff">Loading...</font></td></tr></table></td></tr></table>';
		temp.style.position = 'absolute';
		temp.style.top = document.body.scrollTop;
		temp.style.left = document.body.scrollLeft;
		document.body.appendChild(temp);
	}
	fixloadingpos();
}
function fixloadingpos()
{
	if (document.getElementById('loadingbar'))
	{
		document.getElementById('loadingbar').style.top = document.body.scrollTop + 5;
		document.getElementById('loadingbar').style.left = document.body.scrollLeft + 5;
	}
	clearTimeout(lbto);
	lbto = setTimeout('fixloadingpos()',300);
}