var xmlhttps = new Array();

var products = new Array("scp","gr","imo");
var elements = new Array("scp-downloads","gr-downloads","imo-downloads");
var timer ;
var events = new Array();
var success = 0;
var loop = false;

window.onload = function (){

	timer = setTimeout( do_update, 1000 );
}

function do_update(){

	
	for( var i=0; i < products.length; i++ )
	{
		
		xmlhttps[i] = GetXmlHttpObject();
	}	
	for( var i=0; i < products.length; i++ )
	{
		update( products[i], elements[i],xmlhttps[i] );
	}
}

window.onunload = function (){

	clearInterval( timer );
}

function update( product, elem, xmlhttp )
{
	
	if (xmlhttp==null)
	{
	//	alert("it's null");
		return;
	}
	
	
	var url="downnum.php?product="+product;
	url=url+"&sid="+Math.random();
	
	xmlhttp.onreadystatechange=stateChanged;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
	
}

function stateChanged(  )
{
	
	for( var i=0; i < xmlhttps.length ; i++ )
	{
		
		xmlhttp = xmlhttps[i];
		if( xmlhttp==null ) continue;
		if (xmlhttp.readyState==4)
	  {
		
		success++;
		ret = xmlhttp.responseText;
		
		ret = ret.split(":");
		product = ret[0];
		if( ret[1]=='undefined' ) return;
		elem = document.getElementById(elements[i]);
		if( elem!=null && elem!="undefined" )
		{	
			parent = elem.parentElement;
			if( parent!=null && parent!="undefined")
				if( parent.style.visibility  =="hidden" || parent.style.visibility=="")
					parent.style.visibility   = "visible";
				
			elem.innerHTML= addCommas(ret[1]);
		}	
		xmlhttps[i] = null;
	  }
	}  
	if( success==products.length )
	{
		window.clearInterval( timer );
		success = 0;
		timer = window.setTimeout( do_update, 20000 );
		loop = true;
	}
}

function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  return new XMLHttpRequest();
  }
if (window.ActiveXObject)
  {
  // code for IE6, IE5
  return new ActiveXObject("Microsoft.XMLHTTP");
  }
return null;
}

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}