/* Copyright (c) 2007, Akita Noek.  All rights reserved. */


/** Config **/
/* This controls how long a download should take. */
var ULSpeedTestUploadSize   = 102400;      /* bytes */

/** Browser Detection **/
var ULSpeedTestBrowser = "";
if (/Opera/.test(navigator.userAgent)) {
  ULSpeedTestBrowser="opera";
}
else if (/Microsoft Internet Explorer/.test(navigator.appName))
  ULSpeedTestBrowser="ie";
else if (/Adobe/.test(navigator.appName)) // We can also be in the Adobe SVG Viewer ... its probably not really correct to assume ie, but oh well. 
  ULSpeedTestBrowser="ie";
else
  ULSpeedTestBrowser="mozilla";



/* added for custom test stop */
var ULSpeedTestStop = false;


/** Code **/
function ULSpeedTest(e) { /* {{{ */
  var self = this;
  
  this.status                 = "";
  this.lastRunTime            = 0;

  this.numULReal              = 0;
  this.totalULTime = 0;

  ULSpeedTestStop = false;

  //this.updateStats();
  //this.speedTest(1048576);
  //this.speedTest(ULSpeedTestDownloadSize);
  this.uploadSpeedTest(ULSpeedTestUploadSize);
} /* }}} */

ULSpeedTest.prototype.uploadTest                  = function (size,toCallWhenDone) {
  var self = this;
  if (this.data_size == null || this.data_size != size)
    this.data = "data="+genStr(size); /* When we kick it into high gear on fast networks, lets not eat up all our ram. */
  this.data_size = size;

  var start = new Date();
  pull("http://www.vdsl.de/fileadmin/dslspeed/speedtest.php", this.data, function () {
    var end = new Date();
    self.lastRunTime = end.getTime() - start.getTime();
    var ULRate = (((size*8)/(1024))/(self.lastRunTime*0.001)).toFixed(0);
    //window.document.speedtest.setUploadSpeed(ULRate); 
    //alert ("window.document.speedtest.setUploadSpeed("+ULRate+")");

   var flash = document.getElementById('speedtest');
   flash.SetVariable("uploadSpeed", ULRate);    
  });
}

ULSpeedTest.prototype.speedTest                   = function (size) { /* {{{ */
  var self = this;
  if(ULSpeedTestStop) return;
  self.downloadTest(size, function () { });
} /* }}} */

ULSpeedTest.prototype.uploadSpeedTest                   = function (size) {
	var self = this;
	if(ULSpeedTestStop) return;
  self.uploadTest(size, function () { });	
}


/** Utility Functions {{{ **/

function getHttpRequester() { /* {{{ */
  var xmlhttp=false;
  /*@cc_on @*/
  /*@if (@_jscript_version >= 5)
   try {
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
   } catch (e) {
     try {
       xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
     } catch (E) {
       xmlhttp = false;
    }
   }
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
    xmlhttp = new XMLHttpRequest();
  }

  return xmlhttp;
} /* }}} */
function pull(src, post, callback, opt_method) { /* {{{ */
  //try {
    if (src) {
      var X = getHttpRequester();
      if (X) {
        X.open("POST",src,true);
        X.onreadystatechange=function() {
          if (X.readyState==4) {
            if (opt_method != null) {
              if (!callback[opt_method]) {
                alert(opt_method + " was not found within class");
              }
  
              callback[opt_method]();
            } else if (callback) {
              callback();
            }
          } 
        };
        X.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        X.send(post);
      } else {
        alert("pull called but the browser doesn't seem to support the XMLHttpRequest object.");
      }
    } else {
      alert("pull called with an invalid source = null");
    }
    /*
  } catch (e) {
    if (opt_method != null) {
      if (!callback[opt_method]) {
        alert(opt_method + " was not found within class");
      }

      callback[opt_method]();
    } else if (callback) {
      callback();
    } 
  }
  */
} /* }}} */

function prettySize(size) { /* {{{ */
  var html = "";

  if (typeof(size) == "string") {
    size = parseFloat(size);
  }
  if (size > 1073741824) {
     html += (size/1073741824).toFixed(1) + " GB";
  } else if (size > 1048576) {
     html += (size/1048576).toFixed(1) + " MB";
  } else if (size > 1024) {
     html += (size/1024).toFixed(1) + " KB";
  } else {
     html += size.toFixed(0) + " B";
  }

  return html;
} /* }}} */
function prettyRate(bps) { /* {{{ */
  var rate = prettySize(bps);
  rate = rate.substring(0,rate.length-1) + 'bit/s';
  return rate;
} /* }}} */
function prettyTime(pSecs) { /* {{{ */
  /* recursive for days, hours and minutes */
  switch ( true ) {
    case ( isNaN(pSecs) ):
      return "Unknown";
    case ( pSecs == Infinity ):
      return "Unknown";
    case (pSecs >= 86400):     /* days */
      return (pSecs / 86400).toFixed(0) + " days " + prettyTime(pSecs % 86400);
    case (pSecs >= 3600 ):     /* hours */
      return (pSecs / 3600).toFixed(0) + " hr " + prettyTime(pSecs % 3600);
    case (pSecs >= 60 ):       /* minutes */
      return (pSecs / 60).toFixed(0) + " min " + prettyTime(pSecs % 60);
    case (pSecs < 60):
      return (1.0*pSecs).toFixed(1) + " s "
    default:
        return "Unknown";
  }


} /* }}} */
var n_genStr = 0;
function genStr(len) { /* {{{ */
    n_genStr = (n_genStr+1)%10;
    var ret = n_genStr+"";

    while (1) {
      if (ret.length > len) {
        return ret.substr(0, len);
      }
      if (ret.length*2 >= len) {
        return ret+ret.substr(0, len-ret.length);
      }
      ret += ret;
    }

  /* IE has issues creating 'big' arrays, so we need to work around it .. and not do something like this :( */
  /* 
    n_genStr = (n_genStr+1)%10;
    var seg_size = parseInt((parseInt(len+"")/100)+"");
    var rem_size = parseInt((parseInt(len+"")%100)+"");

    return new Array(100).join(new Array(seg_size).join(n_genStr+""))+new Array(rem_size).join(n_genStr);
  */
} /* }}} */

