CONNECTION_TYPE_COOKIE_NAME = "CT";
CONNECTION_TYPE_BROADBAND = "0";
CONNECTION_TYPE_DIALUP = "1";
CONNECTION_TYPE_UNKNOWN = "-2";
EXPIRATION_INTERVAL = 7;
CONNECTION_SPEED_THRESHOLD = 56;
COOKIE_PATH = "/";

destination_url = "";
cookie_domain = "";
_throughput = 0;
_connection_type = ""; 		
_semaphoreCT = false;
_semaphoreRDB = false;
		
function callback(throughput, connection_type) {
	_throughput = throughput;
	_connection_type = connection_type;
	run();
}

function set_domain_cookie( name, value, expires, path, domain, secure ) {
  var today = new Date();
  today.setTime( today.getTime() );

  if ( expires ) {
    expires = expires * 1000 * 60 * 60 * 24;
  }

  var expires_date = new Date( today.getTime() + (expires));

  document.cookie = name + "=" +escape( value ) +
  ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
  ( ( path ) ? ";path=" + path : "" ) +
  ( ( domain ) ? ";domain=" + domain : "" ) +
  ( ( secure ) ? ";secure" : "" );
}

function run() {
	set_untd_cookie();

  if(location.href.match("netzero.net") != null) 
    cookie_domain = "netzero.net";
  else if (location.href.match("juno.com") != null) 
    cookie_domain = "juno.com";
  else if (location.href.match("mybluelight.com") != null) 
    cookie_domain = "mybluelight.com";

  set_domain_cookie(CONNECTION_TYPE_COOKIE_NAME, _connection_type, EXPIRATION_INTERVAL, COOKIE_PATH, cookie_domain, false);
}
		
function set_untd_cookie() {
	var RDB_URL = window.location.protocol+'//track.untd.com/s/oasrdb?pid=SDS&profile='+_connection_type;
	exec(RDB_URL);
}
		
function exec(URL) {
	setTimeout("send_http_request('"+URL+"')", 10);
}
		
function send_http_request(requestURL) {
  var imgObj = (Image ? new Image() : undefined);
  if (imgObj != undefined) {
    imgObj.src = requestURL + "&ts=" + (new Date()).getTime();
    if (!imgObj.complete)
      pause(1000);
  }
}		
		
function pause(millis) {
	var date = new Date();
	var curDate = null;

	do { var curDate = new Date(); }
	while(curDate-date < millis);
	return;
}
		
var BandwidthChecker = {
	startTime: null,
	inProgress: false,
	downloadTimes: [],
	sizeInBytes: 3111,
	noOfTimes: 0,
	standardDownloadTime : 650,
	repeat : 3,
	connTypes: ["0", "1"],
	differenceMargin : 25,
	downloadURL: "http://webmail.netzero.net/images/headers/hdr_lg_new_accl_n.gif",
	imgObj: null,
	kbps: 0,
	cType: null,
	checkBandwidth: function(ct_type) { 
    if(typeof ct_type != "undefined" && ct_type != "") { 
      callback(ct_type, ct_type); 
      return;
    }
    else if ((typeof oClientCaps) != 'undefined' && (typeof oClientCaps.connectionType!='undefined')) { 
    /*Adding IE Client capabilities to detect connection type. For this to work, 
    a) In html tag add xml namespace {xmlns:IE} <html xmlns:IE>
	b) In head section add the following style - @media all { IE\:clientCaps {behavior:url(#default#clientCaps)}}
	c) In body section add the following line - <IE:clientCaps ID="oClientCaps" /> */
				if (oClientCaps.connectionType == "modem"){ 
					ct_type=0;
					callback(ct_type, ct_type); 
                    return;
				}else if (oClientCaps.connectionType == "lan"){
					ct_type=1;
					callback(ct_type, ct_type); 
                    return;
				}
	}
		this.inProgress = true;
		imgObj = new Image();
		imgObj.onload = this.downloaded;
		this.startTime = new Date();
		imgObj.src = this.downloadURL + "?ts=" +  (new Date()).getTime();
	},
	downloaded: function() {
		BandwidthChecker.noOfTimes += 1;
		BandwidthChecker.downloadTimes[BandwidthChecker.noOfTimes-1] = (new Date())-BandwidthChecker.startTime;
		if(BandwidthChecker.noOfTimes<BandwidthChecker.repeat) {
			BandwidthChecker.checkBandwidth();
		} else {
			BandwidthChecker.returnBandWidth();
			this.inProgress = false;
			callback(BandwidthChecker.getConnectionSpeed(), BandwidthChecker.getConnectionType());
		}
	},
	returnBandWidth: function() {
		this.downloadTimes = this.downloadTimes.sort(this.sortAscending);
		nearestNumber	= (this.downloadTimes[1] - this.downloadTimes[0])<=(this.downloadTimes[2] - this.downloadTimes[1]) ? Math.floor((this.downloadTimes[1] + this.downloadTimes[0])/2) : Math.floor((this.downloadTimes[1] + this.downloadTimes[2])/2);
		elapsedTime     =  nearestNumber / 1000;
		sizeInKBits     = (this.sizeInBytes  * 8)/1024;
		kbps       = (sizeInKBits/elapsedTime) * 0.93 ; 		this.kbps       = Math.round(kbps, 2);
		cType           = "";
		this.cType = (nearestNumber>=(this.standardDownloadTime-((this.differenceMargin/100)*this.standardDownloadTime)))?this.connTypes[0]:this.connTypes[1];
	},
	sortAscending: function(a, b) {
		return (a-b);
	},
	getConnectionType: function() {
		return this.cType;
	},
	getConnectionSpeed: function() {
		return this.kbps;
	}
}
