/* popsite for PHP - http://sourceforge.net/projects/popsite/ */

var xmlHttp;
var xmlHttptarget;
var xmlHttptimeout;

// Update element(target) with url(content) every waitms ms

function autoUpdate(target,url,waitms) {	
	clearTimeout(xmlHttptimeout);
	bgUpdate(target,url);
	xmlHttptimeout = setTimeout("autoUpdate('"+target+"','"+url+"','"+waitms+"')",waitms);
}

// Update element(target) with url(content)
function bgUpdate(target,url) {
	xmlHttptarget=target;
	xmlHttp=prepareXmlHttp();
	if (xmlHttp==null) {
		document.getElementById(xmlHttptarget+'err').innerHTML="Sorry, your browser does not support XMLHttpRequest().";
        return false;
	}
	// Prevent cache
	url=url+"&sid="+Math.random();
	// bgloadid is used for partial updates (changes since last polling)
//	if (typeof document.getElementById('bgloadid') != "undefined") url = url+'&bgid='+parseInt(document.getElementById('bgloadid'));
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	return true;
}

function stateChanged() {

	if (xmlHttp.readyState == 4 || xmlHttp.readyState=="complete") {
		if (xmlHttp.status == 200) {
			document.getElementById(xmlHttptarget).innerHTML=xmlHttp.responseText;
        } else {
            document.getElementById(xmlHttptarget+'err').innerHTML="Error: "+xmlHttp.statusText;
        }
	}
}

function prepareXmlHttp() {
	var xmlHttp=null;
	try {
		xmlHttp=new XMLHttpRequest();
	}
	catch (e) {
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        }
		catch (e) {
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}

