function ActiveTableChange() {
	var _s = document.getElementById('active_table').value;
	if (_s == 'None') {
		$("#MPEG").css("display", "none");
		$("#DVB").css("display", "none");
		$("#ATSC").css("display", "none");
	} else if (_s == 'MPEG') {
		$("#MPEG").css("display", "");
		$("#DVB").css("display", "none");
		$("#ATSC").css("display", "none");
	} else if (_s == 'DVB') {
		$("#MPEG").css("display", "");
		$("#DVB").css("display", "");
		$("#ATSC").css("display", "none");
	} else if (_s == 'ATSC') {
		$("#MPEG").css("display", "");
		$("#DVB").css("display", "none");
		$("#ATSC").css("display", "");
	}
}

function NitDeliveryDescChange() {
	var sel = $("#delivery_descriptor").val();
	if (sel == 'Cable') {
		$("#NitCableAndSatellite").css("display", "");
		$("#NitSatellite").css("display", "none");
		$("#NitTerrestrial").css("display", "none");
	} else if (sel == 'Satellite') {
		$("#NitCableAndSatellite").css("display", "");
		$("#NitSatellite").css("display", "");
		$("#NitTerrestrial").css("display", "none");
	} else if (sel == 'Terrestrial') {
		$("#NitCableAndSatellite").css("display", "none");
		$("#NitSatellite").css("display", "none");
		$("#NitTerrestrial").css("display", "");
	}
}

function expandContent(id) {
	$("#" + id + " tr").each(function() {
		if ($(this).hasClass("header")) {
			var link = $(this).find("a");
			if (link.html() == "hide") {
				link.html("show");
			} else {
				link.html("hide");
			}
			link.blur();
		} else {
			if ($(this).css("display") == "none") {
				$(this).css("display", "");
			} else {
				$(this).css("display", "none");
			}
		}
	});
}

function init() {
	ActiveTableChange();
	NitDeliveryDescChange();
	$(".inner .header").each(function() {
		$(this).click(function () {
			expandContent($(this).parent().parent().attr("id"));
		});
	});
}

function validate_udd_info(index, id, data) {
	if (!/^[a-fA-F0-9]{2}$/.test(id)) {
		alert("The UDD tag at index " + index + " is invalid.");
		try {
			$("input[name=udd_id" + index + "]")[0].focus();
		} catch (err) {
			$("input[name=udd_id]")[0].focus();
		}
		return false;
	}
	if (!/^([a-fA-F0-9]{2})+$/.test(data)) {
		alert("The UDD data at index " + index + " is invalid.");
		try {
			$("input[name=udd_data" + index + "]")[0].focus();
		} catch (error) {
			$("input[name=udd_data]")[0].focus();
		}
		return false;
	}
	return true;
}

function add_udd() {
	var index = $("input[name=udd_index]").val();
	var id = $("input[name=udd_id]").val();
	var data = $("input[name=udd_data]").val();
	if (validate_udd_info(index, id, data)) {
		var params = "udd_index=" + index + "&udd_id=" + id + "&udd_data=" + data + "&action=add_udd";
		loadPage("/asimux/tables.py", params);
	}
}

function remove_udd(index) {
	var params = "udd_index=" + index + "&action=remove_udd";
	loadPage("/asimux/tables.py", params);
}

function update_udd(index) {
	var id = $("input[name=udd_id" + index + "]").val();
	var data = $("input[name=udd_data" + index + "]").val();
	if (validate_udd_info(index, id, data)) {
		var params = "udd_index=" + index + "&udd_id=" + id + "&udd_data=" + data + "&action=add_udd";
		loadPage("/asimux/tables.py", params);
	}
}