﻿
Framework.IncludeJavaScript("/Client/Javascript/Library/CookieHelper.js");

function ConnectionSpeedChecker() {
}

ConnectionSpeedChecker.Debug = false;

ConnectionSpeedChecker.Startup = function()
    {
        
	    tempCookieValue = CookieHelper.ReadCookie('TempCookie','');
    	
	    if (tempCookieValue!=null && (tempCookieValue.indexOf("Broadband") != -1 || tempCookieValue.indexOf("Dialup") != -1)) 
	    {		
	        //	If cookie exists, do not do a bandwidth check 
		    // do nothing
            if (ConnectionSpeedChecker.Debug) alert('No need to test for connection speed, it is already in cookie.');
	    }
	    else
	    {
            if (ConnectionSpeedChecker.Debug) alert('About to start connection speed test.');
            
	        // check bandwidth
		    var randNum = Math.random();
		    ConnectionSpeedChecker.ImageDownloadStartTime = (new Date()).getTime(); /* capture time just bfor the image starts downloading 
														    client machine */
		    /**************
		    //Dialup test
		    for(var i = 1; i <= 6000000; i++) {}
		    /***************/
	        var imgConnType = new Image();
		    imgConnType.src = "/images/connectiontype_tracker.jpg?randomnum="+randNum;		
    	    imgConnType.onload = ConnectionSpeedChecker.ImageDownloadCompleteHandler
	    }
    }
    
ConnectionSpeedChecker.ImageDownloadCompleteHandler = function()
    {
        var conntype = "Unknown";
        var cutoff = 2400;	// imgConnType size = 23kb and cutoff speed = 10Mbps
        var diff = ((new Date()).getTime() - ConnectionSpeedChecker.ImageDownloadStartTime);  	
			
        /* = time taken for the image to load in milli seconds  */
        if(diff > 0 && diff < cutoff)
        {
            conntype = "Broadband";
        }					
        else if(diff >= cutoff)
        {
            conntype = "Dialup";
        }	

        //set the image src for NetTracker reporting 
        var imgTracker = new Image()
        imgTracker.src = "/marketing/asi/connectiontracker.aspx?Connection="+conntype;
        if (ConnectionSpeedChecker.Debug) alert("Connection check complete, hit made to: " + imgTracker.src);	
    }