var xmlHttp;

function createXMLHttpRequest(){
	if(window.ActiveXObject){
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}else if(window.XMLHttpRequest){
		xmlHttp = new XMLHttpRequest();
	}
}

function startRequest(){

	createXMLHttpRequest();
	xmlHttp.onreadystatechange = update;
	xmlHttp.open("GET", "http://www.violinbridges.co.uk/pumkin/controllers/download_stats_counter_updater.php?rand=' + Math.random()*4", true);
	xmlHttp.send(null);
	
}


function updateCounters(id){window.location = "file_downloader.php?id=" + id; /*pollServer()*/;}

/*function pollServer(){startRequest();}*/

function handleReply(){update();}
	
function update(){
	if(xmlHttp.readyState == 4){
		if(xmlHttp.status == 200){
			 xmlDoc = xmlHttp.responseXML;
			 xmlHttp = null;
			 var repliesNode = xmlDoc.getElementsByTagName("replies")[0];
			var entriesNodes = repliesNode.getElementsByTagName("entry");
	
			for (var i = 0; i < entriesNodes.length; i++){
				 idNode = entriesNodes[i].getElementsByTagName("id")[0];//entriesNodes.childNodes[1];
				 countNode = entriesNodes[i].getElementsByTagName("count")[0];
				 id = idNode.firstChild.data;
				 count = null;
				 count = countNode.firstChild.data;
				 //document.getElementById(id).innerHTMl = count;
				 counter_el = document.getElementById(id);
				 var new_node = document.createTextNode(count);
				 counter_el.removeChild(counter_el.firstChild);
				 counter_el.appendChild(new_node);
			}
			setTimeout("pollServer()", 5000);
		}
	}
}

function doStart(){
	createXMLHttpRequest();
	xmlHttp.onreadystatechange = startCallBack;
	xmlHttp.open("GET", "http://www.violinbridges.co.uk/pumkin/controllers/download_stats_counter_updater.php", true);
	xmlHttp.send(null);
}

function startCallBack(){
	
	if(xmlHttp.readyState == 4){
		if(xmlHttp.status == 200){
			setTimeout("pollServer()", 5000);
		}
	}
}

function pollServer(){
	createXMLHttpRequest();
	xmlHttp.onreadystatechange = update;
	xmlHttp.open("GET", "http://www.violinbridges.co.uk/pumkin/controllers/download_stats_counter_updater.php?rand=' + Math.random()*4", true);
	xmlHttp.send(null);
}


