var currentBandwidth = 2048;
var currentBandwidthUnit = 1000;
var currentSize = 100;
var currentSizeUnit = 1048576;

function sizeOnInput(input) {
	var size = parseFloat(document.getElementById('size').value);
	if (!isNaN(size)) {
		currentSize = size;
	}
	updateOutput();
}

function bandwidthOnInput(input) {
	var bandwidth = parseFloat(document.getElementById('bandwidth').value);
	if (!isNaN(bandwidth)) {
		currentBandwidth = bandwidth;
	}
	updateOutput();
}

function sizeUnitOnInput(input) {
	var radiosSizeUnit = document.getElementsByName('sizeUnit');
	for (var i = 0; i < radiosSizeUnit.length; i++) {
		if (radiosSizeUnit[i].checked) {
			currentSizeUnit = parseInt(radiosSizeUnit[i].value);
			break;
		}
	}
	updateOutput();
}

function bandwidthUnitOnInput(input) {
	var radiosBandwidthUnit = document.getElementsByName('bandwidthUnit');
	for (var i = 0; i < radiosBandwidthUnit.length; i++) {
		if (radiosBandwidthUnit[i].checked) {
			currentBandwidthUnit = parseInt(radiosBandwidthUnit[i].value);
			break;
		}
	}
	updateOutput();
}

function updateOutput() {
	var seconds = (currentSize * currentSizeUnit) / (currentBandwidth * currentBandwidthUnit / 8);
	var units = { second : 60,
	              minute : 60,
	              hour : 24,
	              day : 365,
	              year : Number.MAX_VALUE };
	var time = '';
	var array = [];
	if (seconds > 0 && seconds < .001) {
		time = '< 1 ms';
	} else if (seconds < 1) {
		time = Math.round(seconds * 1000) + ' ms';
	} else if (seconds < 60) {
		time = seconds.toFixed(2) + ' seconds';
	} else {
		seconds = Math.round(seconds);
		for (var unit in units) {
			var n = seconds % units[unit];
			seconds -= n;
			seconds /= units[unit];
			array.unshift(n + ' ' + unit + (n != 1 ? 's' : ''));
			if (seconds == 0) {
				break;
			}
		}
		time = array.slice(0, 3).join(', ');
	}
	document.getElementById('outputTime').innerHTML = time;
}

window.onload = function() {
	updateOutput();
}


