function loadConnSpeed() {	
	var x = getConnType();		
	//window.alert("loadConnSpeed - x:" + x);	
}
function getConnType(){	
	var tp = GetConnCookie("ConnParam");
	if (!tp)  { // no cookie
		drawImgConn();
		return ''; 
	} else {
		return tp;
	}
}
function drawImgConn() {        
    var isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
    if ( isIE == true) {
        drawCSImageTag("http://video.alberghieturismo.it/_conn_test/conn_ie.bmp", // Image filename
                  5194);        
    } else {
         drawCSImageTag("http://video.alberghieturismo.it/_conn_test/conn_firefox.gif", // Image filename
                  42160);
    }
} 

function drawCSImageTag( fileLocation, fileSize) {	
	var loc = fileLocation + '?t=' + escape((new Date()).getTime());
	var imtest = new Image();
	imtest.src = loc;
	try{
		if(  !(navigator.userAgent.indexOf('Mac')>-1 && navigator.appVersion.indexOf('MSIE')>-1)   ){	
			var start = (new Date()).getTime();		
			imtest.onload = function compute(){
				computeConnectionSpeed( start, fileSize );
			}
		}else{
		  var start = (new Date()).getTime();
			eval('imtest.onload = new function compute(){computeConnectionSpeed( start, fileSize );}');
		}
	}catch(e){
		// per baco IE5: esegue la funzione sull'onload ma lancia comunque un eccezione
	}

	return;
}

function computeConnectionSpeed( start, fileSize ) {	
	// This function returns the speed in kbps of the user's connection,
	// based upon the loading of a single image.  It is called via onload
	// by the image drawn by drawCSImageTag() and is not meant to be called
	// in any other way.  You shouldn't ever need to call it explicitly.
	var end = (new Date()).getTime();
	var speed = (Math.floor((((fileSize * 8) / ((end - start) / 1000)) / 1024) * 10) / 10);	

  // scadenza cookie a 7 giorni
	var mai = new Date();
	mai.setTime(mai.getTime() + 7*24*60*60*1000);

  	var conn_value = connectionType( speed ) ;
	SetConnCookie ("ConnParam", conn_value, mai, "/", null, null);
}

function connectionType(speed) {
	// These are constants which define the base speeds
	// for a number of different connections.  They are
	// measured in units of kbps.
	SLOW_MODEM = 56;
	MEDIUM_MODEM = 350;
	
	if (speed) {
		if (speed < SLOW_MODEM) {
			return "2"; //"SLOW";
		} else {
			if (speed < MEDIUM_MODEM) {
			return "1"; // "MEDIUM";
		}
		
		else {
			return "0"; // "FAST";
		}
	}
	} else {
		return "2";
	}
}

function getConnCookieVal (offset) {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)
    endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}

function GetConnCookie (name) {
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg)
    {
      return getConnCookieVal (j);
    }
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break;
  }
  return null;
}

function SetConnCookie (name,value,expires,path,domain,secure) {
  document.cookie = name + "=" + escape (value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}