
var BPtestObj = null;

function BPtest(url, filesize) {
	this.speed = 0;
	this.url = url;
	this.filesize = filesize;
	this.started = 0;
	this.octets_seconds = 0;
	this.formatted_speed = "";
	BPtestObj = this;
}
// 547 / 15 = 1000 / x           x = 547 / 15000
//
//			547ms 15Mo
//			15 * 1000 / 547

BPtest.prototype.onUpdate = function() {}

BPtest.prototype.responseReceived = function() {
	// TODO : check erreur 404
	
	var newDate = new Date();
	var diffms = (newDate - this.started);
	var kos = diffms / 1000;
	kos = parseInt(this.filesize / kos);
	this.octets_seconds = kos;
	
	this.formatted_speed = this.formatResponse();
	this.onUpdate();

}

BPtest.prototype.formatResponse = function() {
	var base = this.octets_seconds;
	var res = 0;
	if (base > 1000) astr = (Math.round(base / 1000 * 100) / 100) + " ko/s";
	if (base > 1000000) astr = (Math.round(base / 1000000 * 100) / 100) + " Mo/s";
	return astr;
}

BPtest.prototype.startTest = function() {
	this.started = new Date();
	this.aSyncRequestGET(this.url, "?rnd="+Math.random(), "BPtestObj.responseReceived()");
	//this.speed = 
}




BPtest.prototype.aSyncRequestGET =  function(req,data,jstoexecuteafter) {
	if (document.all)
		var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	else
	  var xmlhttp =  new XMLHttpRequest();
	  //alert(1);
	xmlhttp.onreadystatechange = function() {
	  if (xmlhttp.readyState == 4) {
	      var rez = xmlhttp.responseText;  
		//  alert(2);
		//alert(rez);
	      if (jstoexecuteafter) eval(jstoexecuteafter);
	  }
	}
   xmlhttp.open('GET',req+data, true);  
   xmlhttp.send("");
}



