var buttonObj;
var StatsTimer;
var currentTabName;
function UpdateTime(){
tickCount +=1;
if (tickCount>=120)
{
		tickCount=0;
	x= window.confirm("Tests are taking long time, Would you like to continue, Click \"Cancel\" to stop.");
	if(x){
		
	}
	else
	{
		stopRun=true;
		window.clearInterval(timeClock);
	}
}
}

var tickCount=0;
var timeClock;

function runTests(bobj){
	buttonObj = bobj;
window.status="...";
	setStatus(' ',0);
//timeClock = window.setInterval("UpdateTime()",1000);
//timeClock = window.setInterval("updateProgressBar()",15000);
var stopRun=false;
var urlString = "URL";
var rtdString = "RTD";
var counter=0;
var loop=true;
var sitesList="";
var urlObj,rtdObj,i,prog1,PathCell1,rowsCount,pDone,siteUrl,x,siteName;
var result=true;
var notSupported=false;
//alert("test init");
var traceRun=true;
	progress1 = document.getElementById("progress");
if (progress1!=null) 	progress1.style.display="block";


var pObj= document.getElementById("ping1");
if (pObj!=null ){ //&& az==2){
	//window.status ='Before ClearSites ';
	pObj.BrowserVersion = ie_version;


if (ie_version==6)
{
	pObj.noThreads=false;
	 var msvbvmVer;
	 var version = pObj.MSVBVersion;
       msvbvmVer = version.split(".");
   if (msvbvmVer[2] < 97) 
	{
			pObj.noThreads=true;
	}
}
	if (ie_version==7)
{
	pObj.noThreads=true;
}
	pObj.clearSites(); 
	pObj.ShowServerNames = false;
} //pObj
else
	alert("object does not exist");

	window.status ='Initialising Tests..';
var sObj = document.getElementById(currentTab.replace("Tab","StatsTable"));
if (sObj!=null){
	rows = sObj.getElementsByTagName("TR");
	rowsCount = sObj.getElementsByTagName("TR").length -1;
}
x=0;
rowsCounter=0
if (bobj!=null) bobj.disabled=true;
while (x<rowsCount && stopRun==false){
	rowId = rows[x].id;
if (rowId!="") rowsCounter +=1;
x +=1;
}

if (rowsCounter<=0) rowsCounter=1;
	prog1 = document.getElementById("prog_1");
	prog2 = document.getElementById("prog_2");

if (prog1!=null) 	prog1.style.width = "1%";
if (prog2!=null) 	prog2.style.width =  "99%";
window.status="......";
x=0;
while (x<rowsCount){
	result = true;
	rowId = rows[x].id;
	rowIdn = rowId.replace("row_","");
//alert(rowId + " -- " + (urlString + rowId.replace("row","")) );
if (rowId!=""){
	urlObj = document.getElementById(urlString + rowId.replace("row",""));
	rtdObj = document.getElementById(rtdString + rowId.replace("row",""));
		siteUrl = urlObj.title;
		siteName = urlObj.innerText;
	Games["g_" + rowIdn] = urlObj.innerText;
	i=0;
//	alert(siteUrl);
	if (siteUrl!="")
		{
		Urls[siteUrl] = rtdString + rowId.replace("row","");
		UrlPaths[siteUrl] = "g_" + rowIdn;

	 pObj.addSite (counter,siteName,siteUrl);
//		window.status =counter +'  ' + siteName + '   ' + siteUrl;
if (sitesList=="")
	sitesList = siteUrl;
else
	sitesList += "," + siteUrl;

		Run[siteUrl]=3;
			siteUrl="";
			siteName="";
//	counter +=1;
}
}

window.status +="...";


x +=1;
	counter +=1;
} //while
//alert(pObj.noThreads);
pObj.TabName = currentTabName;
pObj.RunTests();
nsObj = document.getElementById("NewStats1");
if (nsObj!=null) 	nsObj.value=NewStats;
if (!pObj.ShowDownload)
{
	window.status ='Tests Initialised';
	setStatus('Tests Initialised',0);
}
//	pObj.CloseRun();

} //function

//alert('3');
function displayPath(Id){
//alert(CurrentPath + " != " + Id + " -- " + currentTab.replace("Tab","PathCell"));
	if (CurrentPath!=Id){
var pathObj = document.getElementById(currentTab.replace("Tab","PathCell"));
var pathNameObj = document.getElementById(currentTab.replace("Tab","PathName"));
if (Games[Id]!=null){
if (pathObj!=null){
	if (Paths[Id]!=null){
		if (pathNameObj!=null) pathNameObj.innerText="Path (" + Games[Id] +")";
			pathObj.innerHTML = "<font size=2 color=white>" + Paths[Id] + "</font>";
	}
}
} //pathobj
	} //currentpath
} //function
//alert('4');
function setStatus(msg,opt){
var stObj = document.getElementById("status_span");
if (stObj!=null)
{
	if (opt==0)
	{
	stObj.innerText = msg;
	}
	else
	stObj.innerText += msg;
	}

if (stObj.innerText.length>80)
{
		stObj.innerText = stObj.innerText.substr(0,80);
}
}
function XMLHTTP_send() {
	var DataToSend = "";
	nsObj = document.getElementById("NewStats1");
	if (nsObj!=null) 	{
		DataToSend = "statdata=" + nsObj.value;
var pData="";

	pData = pathData;
	//	alert('in http send ' + pData);
DataToSend += "&pathdata=" + pData;
		}
		if (DataToSend==""){
	DataToSend = "statdata=No Data";
	DataToSend += "&pathdata=No Data" ;
		}
//		alert(DataToSend);
var pObj1 = document.getElementById("postData");
if (pObj1!=null) pObj1.value = DataToSend;
	var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	xmlhttp.Open("POST","http://www.broadbandbenchmark.com/sdata.php",false);
	xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlhttp.send(DataToSend);
//	abc=window.open("","a1","");
//	abc.document.write('Response:   ' + xmlhttp.responseText);
//	alert(DataToSend);
window.status="Ready....";
}
//alert('5');
function showTable(Obj){
var tObj,tableObj,tableObj1,sObj;
tObj = document.getElementById(currentTab);
if (tObj!=null){
	//alert(currentTab.replace("Tab","d"));
tableObj = document.getElementById(currentTab.replace("Tab","d"));
tableObj1 = document.getElementById(Obj.id.replace("Tab","d"));
sObj = document.getElementById("site_Type");
if (tableObj!=null && tableObj1 !=null){
tableObj.style.display="none";
tableObj1.style.display="block";
tObj.className="passiveTab";
Obj.className="activeTab";
currentTab = Obj.id;
currentTabName = Obj.innerText;
if (sObj!=null) sObj.innerText = Obj.innerText;
}
//alert(Obj.id);

} //tObj
} //function

//alert('6');
function addStat(u,t){
updateProgressBar();
var	rtdObj = document.getElementById(Urls[u]);
//alert ( u + '   ' + t + '   ' + Urls[u] + '    ' + rtdObj);
if (rtdObj!=null)
{
	if (!isNaN(t)){
//		alert(parseInt(t));
		if (parseInt(t)<2200)
				rtdObj.innerText = t;
	}
}
	if (NewStats=="")
			NewStats += u + "#" + t;
				else
			NewStats += "," + u + "#" + t;

var nsObj = document.getElementById("NewStats1");
if (nsObj!=null) 	nsObj.value=NewStats;
updateRun(u,1);

}
//alert('7');
function addPath(u,t){
	updateProgressBar();
//alert ( "url:" + u  + "   ---   " + UrlPaths[u]);
				Paths[UrlPaths[u]]=t;
				pathData += "<pathfor>" + u + "</pathfor>" + t;
				updateRun(u,2);
}

//alert('8');
function updateRun(u,type){
sendData=true;
Run[u] -= type;
//alert("run length: " + Run.length + "  --- " + u + "   --  " + Run[u]);
for (key in Run)
{
	//alert(key + "  --  " + Run[key]);
	if (parseInt(Run[key])>0)
	{
		sendData=false;
		break;
	}
}
//alert("Senddata: " + u + " --- " + sendData);
if (sendData)
{
//alert("Ready to Send");
XMLHTTP_send();
}
}
//alert('9');

function updateProgressBar(){
	var enableButton=false;
var pObj = document.getElementById("ping1");
	if (pObj!=null)
	{
	
	progress1 = document.getElementById("progress");
//if (progress1!=null) 	progress1.style.display="block";
if (rowsCounter<=0) rowsCounter=1;
	prog1 = document.getElementById("prog_1");
	prog2 = document.getElementById("prog_2");

//if (prog1!=null) 	prog1.style.width = "1%";
//if (prog2!=null) 	prog2.style.width =  "99%";

rowsCounter = pObj.TotalSites;
counter = pObj.SitesinPingProcess;
tracecounter = pObj.SitesinTraceProcess;
//tracecounter =0; //added to remove trace status

//alert(counter + " / " + rowsCounter);
if (counter>0)
{
if (progress1!=null) 	progress1.style.display="block";
	pDone = ((rowsCounter-counter)/rowsCounter) * 100;
	if (pDone>100) pDone=100;
	if (prog1!=null) 	prog1.style.width =pDone + "%";
	if (prog2!=null) 	prog2.style.width = (100-pDone) + "%";
window.status +="...";
	setStatus('...',1);
}

else
		{
			//Progress Bar update
			progress1 = document.getElementById("progress");	
			if (progress1!=null) 	progress1.style.display="none";
//			if (buttonObj!=null) buttonObj.disabled=false;
			enableButton=true;
			window.status ="RTD tests completed.";
				setStatus('RTD tests completed.',0);
		}
		if (tracecounter>0)
		{
			if (counter<=0)
			{
				enableButton=false; //temporarily made true to override trace
				window.status +="    Trace running (" + tracecounter  + " remaining)";
				setStatus("    Trace running (" + tracecounter  + " remaining)",1);
			}
		}
		else
		{
			window.status +="    Trace completed...Processing Stats...";
			setStatus("    Trace completed.",1);
	//		window.clearInterval(timeClock);
			window.clearInterval(StatsTimer);
		}
	//	enableButton=true;
		if (enableButton)
		{
			if (buttonObj!=null) buttonObj.disabled=false;
		}
}
} //function




/*
   name - name of the cookie
   value - value of the cookie
   [expires] - expiration date of the cookie
     (defaults to end of current session)
   [path] - path for which the cookie is valid
     (defaults to path of calling document)
   [domain] - domain for which the cookie is valid
     (defaults to domain of calling document)
   [secure] - Boolean value indicating if the cookie transmission requires
     a secure transmission
   * an argument defaults when it is assigned null as a placeholder
   * a null placeholder is not required for trailing omitted arguments
*/

function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}


/*
  name - name of the desired cookie
  return string containing value of specified cookie or null
  if cookie does not exist
*/

function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}


/*
   name - name of the cookie
   [path] - path of the cookie (must be same as path used to create cookie)
   [domain] - domain of the cookie (must be same as domain used to
     create cookie)
   path and domain default if assigned null or omitted if no explicit
     argument proceeds
*/

function deleteCookie(name, path, domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

// date - any instance of the Date object
// * hand all instances of the Date object to this function for "repairs"

function fixDate(date) {
  var base = new Date(0);
  var skew = base.getTime();
  if (skew > 0)
    date.setTime(date.getTime() - skew);
}
var statCount=0;
function startTimer(){
	//StatsTimer = window.setInterval("checkData()",5000);	
}
function checkData(){
var obj2= document.getElementById("errorDiv");

var obj1= document.getElementById("ping1");
	if (obj1!=null){
		statCount +=1;
		window.status="Checking Statistics...." + statCount;
//alert('check');
		obj1.checkStats();
	if (obj2!=null){
//abc = window.open("","stat","");
		//obj2.innerText=obj1.ErrorData;
	//	abc.document.writeln(obj1.ErrorData);
	}

	}


}

function ShowDownloadLink(){
//var link="http://www.microsoft.com/downloads/details.aspx?FamilyId=7B9BA261-7A9C-43E7-9117-F673077FFB3C";
var obj1 = document.getElementById("downloadDiv");
if (obj1.style.display=="none")
{
	obj1.style.display="block";
}
else
	obj1.style.display="none";
}
