﻿var ao = new AjaxObj();
var stateChangeTime = -1;
var bandwidth_complete = false;
var TotalSeconds = 0;
var MaxSeconds = 20;
var minimumValidTime = 10;
var PacketSize = 829089;
var PacketDelta = 1310720;
var Tries = 0;
var PacketTotal = 0;
var throughput = 0;
var KbPs = 0;

function doIt(num) {
    PacketTotal = 0;
    stateChangeTime = -1;

    if (num == 1) {
        TotalSecondsElapsed = 0;

        var readyStateChange = function(reqObj) {
            if (stateChangeTime == -1)
                stateChangeTime = new Date().getTime();
        }

        strWww = new String();
        strWww += location;
        www = strWww.toLowerCase().indexOf("www") == -1 ? "" : "www.";

        //We are using mod.rewrite to redirect this request to the akamai netstorage domain
        //http://netstorage.medicmonthly.com/images/def_filler.gif
        url = "http://" + www + "emsjane.com/akamai/images/def_filler.gif?rand=" + new Date().getTime();

        ao.LoadUrl(url, Test1Callback, readyStateChange);
    }
    if (num == 2) {

    }
}


function Test1Callback(obj) {
    objImage = document.getElementById("bwImage");
    objDiv = document.getElementById("bwTestResult");

    var callBackTime = new Date().getTime();
    TotalSeconds = (callBackTime - stateChangeTime) / 1000;

    //TotalSeconds += (lastTime);
    PacketTotal += PacketSize;

    var bits = (PacketTotal * 8);   // convert Bytes to bits, 
    var kbits = bits / 1024;     // convert bits to kbits
    throughput = kbits / (TotalSeconds);
    throughput = throughput * .93;  // account for IP packet header overhead - averages about 7%

    kbx = PacketTotal / 1024;
    KbPs = throughput / 8;

    imageURL = "images/status_bad.gif";
    resultText = "Your connection speed seems to be low enough that it may interfere with your ability to " +
            "stream our videos.  We recommend using a different computer or connection, or trying again later.";

    resultText = "We detected you have approximately <b>" + Math.round(KbPs * 10) / 10 + " kbps of bandwidth available</b>.<br/><br/>";

    if (KbPs > 500) {
        imageURL = "images/status_good.gif";
        resultText += "Your internet connection is sufficient to handle our videos. You should not have issues with skipping.";
    }

    if (KbPs > 350 && KbPs < 500) {
        imageURL = "images/status_good.gif";
        resultText += "Your internet connection is sufficient for handling our videos. You may experience intermittent skipping.";
    }

    if (KbPs > 200 && KbPs < 350) {
        imageURL = "images/status_warning.gif";
        resultText += "With your current internet connection, you will receive a lower quality video and may experience intermittent skipping.";
    }

    if (KbPs < 200) {
        imageURL = "images/status_bad.gif";
        resultText += "Your internet connection is not sufficient for handling our videos. You will probably not be able to watch them using your current internet connection.";
    }
    objImage.src = imageURL;
    objDiv.innerHTML = "<p style='margin-bottom:0px; margin-top:0px; padding-top:0px; padding-bottom:0px;'>" + resultText + "</p>";

    /*obj.innerHTML  = "PT: "+PacketTotal+"<br>";
    obj.innerHTML  += "KBX: "+kbx+"<br>";
    obj.innerHTML += "KBPS: "+KbPs+"<br>";
    obj.innerHTML += "Seconds: "+TotalSeconds+"<br>";
    obj.innerHTML += "PacketSize: "+PacketSize;*/

    bandwidth_complete = true;
}

function Test2Callback() {
}