// Written by Martin Krolik (martin@krolik.net)
// You may want to use a much larger file size (3 to 4 MB) to more
// accurately test FASTER networks.

var iFileSize = 12288;//268945; // size of image (test_speed.gif) in bytes
var objImage = new Image();
//objImage.onload = StopWatch;
objImage.onload = autoStopWatch;
var objDate = new Date();
var objStartTime = objDate.getTime();
var objStopTime = objDate.getTime();
var objTimerHandle = 0;
var iIterations = 0;

function Working()
{
  document.outform.output.value = document.outform.output.value + ".";
  iIterations++;
  if (iIterations < 3600)
    {
    objTimerHandle = setTimeout("Working();", 500);
    }  
  else 
    {
    document.outform.output.value = document.outform.output.value + "\nERROR:  Either the test 'load test' file could not be found, or your network is too slow or not functioning properly. (Took more than 30 minutes to complete.)";
    }
}

function StartWatch()
{
  document.outform.output.value = "Testing in progress  ..."
  objDate = new Date()
  objStartTime = objDate.getTime();
  objImage.src = "test_speed.gif?Epoch=" + objStartTime ;
  objTimerHandle = setTimeout("Working();", 500);
}

function StopWatch()
{
  objDate = new Date()
  objStopTime = objDate.getTime();
  if (objTimerHandle) 
    {
    clearTimeout(objTimerHandle);
    objTimerHandle=0;
    }
  var iDuration = (objStopTime - objStartTime) / 1000;  // the number of seconds it took
  var iThroughPut = (iFileSize / iDuration) / 1024;
  var iThroughPutBits = ((iFileSize * 8) / iDuration) / 1024;
  iThroughPut = (Math.round(iThroughPut * 100)) / 100
  iThroughPutBits = (Math.round(iThroughPutBits * 100)) / 100
  document.outform.output.value = document.outform.output.value + "\nTest completed.";
  document.outform.output.value = document.outform.output.value + "\nNetwork throughput is " + iThroughPut + " kilobytes per second. \n(" + iThroughPutBits + " kilobits per second)";
  iIterations = 0;
}


// Automatical test

function autoWorking()
{
  document.getElementById('test_speed').innerHTML = "Testing in progress for " + iIterations + "sec";
  iIterations++;
  if (iIterations < 3600) objTimerHandle = setTimeout("autoWorking();", 500);
  else document.getElementById('test_speed').innerHTML = "ERROR:  Either the test 'load test' file could not be found, or your network is too slow or not functioning properly. (Took more than 30 minutes to complete.)";
}

function autoStartWatch()
{
  document.getElementById('test_speed').innerHTML = "Testing in progress";
  objDate = new Date()
  objStartTime = objDate.getTime();
  objImage.src = "test_speed.gif?Epoch=" + objStartTime ;
  objTimerHandle = setTimeout("autoWorking();", 500);
}

function autoStopWatch()
{
  objDate = new Date()
  objStopTime = objDate.getTime();
  if (objTimerHandle) {
    clearTimeout(objTimerHandle);
    objTimerHandle=0;
  }
  
  var iDuration = (objStopTime - objStartTime) / 1000;  // the number of seconds it took
  var iThroughPut = (iFileSize / iDuration) / 1024;
  var iThroughPutBits = ((iFileSize * 8) / iDuration) / 1024;
  iThroughPut = (Math.round(iThroughPut * 100)) / 100
  iThroughPutBits = (Math.round(iThroughPutBits * 100)) / 100
  document.getElementById('test_speed').innerHTML = iThroughPut + " kilobytes\/sec (" + iThroughPutBits + " kilobits\/sec)";
  document.getElementById('drmt_speed').value = iThroughPut + " kilobytes\/sec (" + iThroughPutBits + " kilobits\/sec)";
  iIterations = 0;
}
