var request = null;
var begTime = 0;
var endTime = 0;

function createRequestObject() {
	var tmpXmlHttpObject;
    
    // depending on what the browser supports, 
    // use the right way to create the XMLHttpRequest object
    if (window.XMLHttpRequest) { 
        // Mozilla, Safari, etc
        tmpXmlHttpObject = new XMLHttpRequest();
    } else if (window.ActiveXObject) { 
        // Internet Explorer
        tmpXmlHttpObject = new ActiveXObject('Microsoft.XMLHTTP');
    }
    
    return tmpXmlHttpObject;
}

function startBandwidthTest() {
	var curDate = new Date();
	begTime = curDate.getTime();
	
	request = createRequestObject();
	request.open('get', 'detectbandwidth.256.php?curtime=' + begTime); 
	request.setRequestHeader('Cache-Control', 'no-store, no-cache, must-revalidate');
	request.setRequestHeader('If-Modified-Since', 'Sat, 1 Jan 2005 00:00:00 GMT');
	request.setRequestHeader('Expires', '-1');
    request.setRequestHeader('Pragma', 'no-cache');
	request.onreadystatechange = processResponse;
	
	request.send(null);
}

function processResponse() {
	if (request.readyState == 4) {
		var curDate = new Date();
		endTime = curDate.getTime();
		
		request = null; // is there a better way to free up memory
		
		var duration = endTime - begTime;
	    var kbps = (256 * 8) / (duration / 1000);
	    var mbps = kbps / 1000;
	    
	    if (kbps.toFixed(1) < 100) {
	        var supportValue = kbps.toFixed(1) + ' Kbps (Dialup)';
	        document.getElementById('btmysys').innerHTML = kbps.toFixed(1) + ' Kbps Dialup';
	    } else if (kbps.toFixed(1) < 300) {
	        var supportValue = kbps.toFixed(1) + ' Kbps (Broadband)';
	        document.getElementById('btmysys').innerHTML = kbps.toFixed(1) + ' Kbps Broadband';
	    } else {
	        if (mbps.toFixed(1) < 1) {
	            var supportValue = 'High Speed Network - <b>' + kbps.toFixed(1) + ' Kbps</b>';
	        } else {
	            var supportValue = 'High Speed Network - <b>' + mbps.toFixed(2) + ' Mbps</b>';
	        }
	        document.getElementById('btmysys').innerHTML = supportValue;
	    }
	    
	    if (isFaq) { return; }
	    
	    var ismin = (parseFloat(kbps.toFixed(1)) >= 48) ? 1 : 0;  // modems don't usually connect at exactly 56 Kbps
        var isrec = (parseFloat(kbps.toFixed(1)) >= 100) ? 1 : 0;
        
        if (isrec) {
            document.getElementById('bandwidthicon').innerHTML = '<img src="icon-ok.png" />';
        } else if (ismin) {
            document.getElementById('bandwidthicon').innerHTML = '<img src="icon-warning.png" />';
        } else {
            errors++; // count our errors
            document.getElementById('bandwidthicon').innerHTML = '<img src="icon-error.png" />';
        }
        
        showhowto(ismin, isrec, 'bt');
        
        // disable the ticket button when their bandwidth is too low
        // if (errors > 0) {
        //    document.getElementById('sticket').disabled = true;
        // }
        
        // create a hidden input that will be used to send data to support
        hiddenInput = document.createElement('input');
        hiddenInput.setAttribute('type', 'hidden');
        hiddenInput.setAttribute('name', 'bt');
        hiddenInput.setAttribute('value', supportValue);
        
        inputs[inputs.length] = hiddenInput;
        
        document.getElementById('support').appendChild(inputs[inputs.length - 1]);  
	}
}
