function $(elm) {
	return document.getElementById(elm);
}

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			if (oldonload) {
				oldonload();
			}
			func();
		}
	}
}

var ROGERS = { speeds: ["none", "lite", "express", "extreme", "lite_discounted", "express_discounted"] };

addLoadEvent(function() {
	if ($("speed_extreme")) {
		updateInstallOptions();
		calculatePrice();

		for (i = 0; i < ROGERS.speeds.length; i++) {
			var item = "speed_" + ROGERS.speeds[i];
			if ($(item)) {
				$(item).onchange = calculatePrice;
				$(item).onclick = calculatePrice;
			}
		}

		$("technician").onchange = updateInstallOptions;
		$("courier").onchange = updateInstallOptions;
		$("pickup").onchange = updateInstallOptions;

		$("technician").onclick = updateInstallOptions;
		$("courier").onclick = updateInstallOptions;
		$("pickup").onclick = updateInstallOptions;
		
		$("standard_rental").onclick = calculatePrice;
		$("standard_purchase").onclick = calculatePrice;
		$("gateway_rental").onclick = calculatePrice;
		$("gateway_purchase").onclick = calculatePrice;
	} else if ($("comp01")) {
		$("comp01").onclick = popupComparison;
	}
});

function updateInstallOptions() {
	if ($("technician").checked) {
		$("install_date_wrapper").style.display = "block";
	} else {
		$("install_date_wrapper").style.display = "none";
	}
}

function setFee(speed) {
	for (i = 0; i < ROGERS.speeds.length; i++) {
		var item = "fee_" + ROGERS.speeds[i];
		if ($(item)) {
			$(item).style.display = "none";
		}
	}
	
	if (speed)
		$("fee_" + speed).style.display = "inline";
	else
		$("fee_none").style.display = "inline";
}

function calculatePrice() {
	for (i = 0; i < ROGERS.speeds.length; i++) {
		var item = "speed_" + ROGERS.speeds[i];
		if ($(item) && $(item).checked) {
			setFee(ROGERS.speeds[i]);
			return;
		}
	}

	setFee();
}
