﻿var Path = "/Images/TestSpeed.gif"
var timer = 0;
var HostList;
var sHost = 0;

function TestSpeed()
{
    var obj=this;
    var n = obj.id;
    var timer=new Date().getTime();
    var img=new Image();
    img.src="http://"+obj.url+Path+"?ran="+Math.random();
    
    img.onload=function()
    {
	    obj.size=22009;
	    obj.time=new Date().getTime()-timer;
	    obj.speed=Math.round(obj.size*1.02*10/obj.time)/10;
    }
    img.onerror=function()
    {
	    var idx = "show" + n;
	    var showx = document.getElementById(idx);
	    showx.innerHTML=("<div style=margin-top:2px;color:red>连接超时</div>");
    }	
}
function Host(id,url)
{
    this.id = id;
    this.url = url;
    this.size =0;// size;
    this.time =0;// time;
    this.speed =0;// speed;
    this.TestSpeed = TestSpeed;
}
function CreateHostList(count)
{
    HostList = new Array(count);
}
function PushToTestArray(id,url,size,time,speed)
{
    var eHost = new Host(id,url,size,time,speed);
    HostList[id] = eHost;
}
function DisplayResult(flag)
{
    var maxspeed = document.getElementById("maxspeed");
    try{
    for (loop=0; loop<HostList.length; loop++)
    {
	    if (HostList[loop].speed>maxspeed.value) 
	    {
		    maxspeed.value = HostList[loop].speed;
	    }
    }
    for (loop=0; loop<HostList.length; loop++)
    {
	    var idx = "show"+loop;
	    var showx = document.getElementById(idx);
	    if (HostList[loop].speed != 0)
	    {	
		    showx.innerHTML=("<div style=margin-top:4px><table width='100%' cellspacing='0' cellpadding='0' border='0'><tr><td width='"+Math.ceil(HostList[loop].speed/maxspeed.value*80)+"%' style='background:url(Images/pic_ft_process.gif) repeat-x;height:10px;font-size:1px;border:1px solid #85bc00'>&nbsp;</td><td style='height:10px;font-size:1px;'>&nbsp;</td></tr></table></div>");
	    }
    }
    }catch(e){}
}
function DisplayClose()
{
    var maxspeed = document.getElementById("maxspeed");
    try{
    for (loop=0; loop<HostList.length; loop++)
    {
	    if (HostList[loop].speed>maxspeed.value) 
	    {
		    maxspeed.value = HostList[loop].speed;
	    }
	    var idx = "show"+loop;
	    var showx = document.getElementById(idx);
	    if (HostList[loop].speed != 0)
	    {
		    showx.innerHTML=("<div style=margin-top:4px><table width='100%' cellspacing='0' cellpadding='0' border='0'><tr><td width='"+Math.ceil(HostList[loop].speed/maxspeed.value*80)+"%' style='background:url(Images/pic_ft_process.gif) repeat-x;height:10px;font-size:1px;border:1px solid #85bc00'>&nbsp;</td><td style='height:10px;font-size:1px;'>&nbsp;</td></tr></table></div>");
	    }
	    else
	    {
	        showx.innerHTML="<div style=margin-top:2px;color:red>连接超时</div>";
	    }
    }
    }catch(e){}
    document.getElementById("infodiv").style.display="";
    document.getElementById("infoloading").style.display="none";
    //document.getElementById("infobtn").disabled = false;
    document.getElementById("finish").value = "1";
    
    }
    
function TryArray()
{
    try
    {
	    if (timer>=HostList.length) return 0;
	    else
	    {
		    HostList[timer].TestSpeed();
		    setTimeout(TryArray,900);
	    }
    }
    catch(e){}
    timer++;
}

