function jq(myid)
{
	return '#' + myid.replace(/:/g,"\\:").replace(/\./g,"\\.");
}

function show(element, speed)
{
	if (speed === undefined)
		speed = 'normal';

	$(jq(element)).slideDown(speed);
}

function hide(element, speed)
{
	if (speed === undefined)
		speed = 'normal';

	if (speed == 0)
		$(jq(element)).hide();
	else
		$(jq(element)).slideUp(speed);
}

function showsection(table, column, value, speed)
{
	var section = table + "." + column;
	show(section, speed);
}

function hidesection(table, column, value, speed)
{
	var section = table + "." + column;
	hide(section, speed);
}

function showhide(table, column, showvalue, hidevalue, speed)
{
	var showsect = showvalue + "." + table + "." + column;
	var hidesect = hidevalue + "." + table + "." + column;
	show(showsect, speed);
	hide(hidesect, speed);
}

function hidehide(table, column, value, speed)
{
	if ((table == "db.webgui.logview" ||
	     table == "db.webgui.netsniff_selection") &&
	    column == "ipproto_sel")
	{
		hide("tcp." + table + "." + column, speed);
		hide("icmp." + table + "." + column, speed);
	}
	if (table == "db.sip.reply_config")
	{
		hide(table + "." + column, speed);
	}
}

function alwayssection(table, column, value, speed)
{
	switch(table)
	{
		case "db.webgui.editcol_settings":
			hidesection(table, column, value, speed);
			break;
		case "db.firewall.master_logclass":
			showsection(table, column, value, speed);
			break;
	}
}

function toggle(table, column, value, speed)
{
	switch(value)
	{
	case "on":
		showsection(table, column, value, speed);
		break;
	case "fallback":
		showsection(table, column, value, speed);
		break;
	case "configured":
		showsection(table, column, value, speed);
		break;
	case "always":
		alwayssection(table, column, value, speed);
		break;
	case "radius":
		showsection(table, column, value, speed);
		break;
	case "other":
		showsection(table, column, value, speed);
		break;
	case "sometimes":
		showsection(table, column, value, speed);
		break;
	case "if_transfer_target_rings":
		showsection(table, column, value, speed);
		break;
	case "if_transferer_hangs_up":
		showsection(table, column, value, speed);
		break;
	case "allow_tls":
		showsection(table, column, value, speed);
		break;
	case "only_tls":
		showsection(table, column, value, speed);
		break;
	case "recurse":
		showsection(table, column, value, speed);
		break
	case "blind":
		showsection(table, column, value, speed);
		break;
	case "both":
		showsection(table, column, value, speed);
		break;
	case "icmp":
		showhide(table, column, value, "tcp", speed);
		break;
	case "tcp":
		showhide(table, column, value, "icmp", speed);
		break;
	case "udp":
		showhide(table, column, "tcp", "icmp", speed);
		break;
	case "esp":
		hidehide(table, column, value, speed);
		break;
	case "all":
		hidehide(table, column, value, speed);
		break;
	case "numeric":
		hidehide(table, column, value, speed);
		break;
	case "selected":
		if (table == "db.webgui.logview" ||
		    table == "db.webgui.netsniff_selection")
		{
			showsection(table, column, value, speed);
		}
		break;
	default:
		hidesection(table, column, value, speed);
		break;
	}
}

function hideinit()
{
	$("#dialoguecontent :radio:checked[onclick]").each(function() {
		var parts = this.id.split(".");
		var value = parts[(parts.length-1)];
		var column = parts[(parts.length-2)];
		var table = parts[0];
		for (var j = 1; j < (parts.length-2); j++)
		{
			table = table.concat(".", parts[j]);
		}
		toggle(table, column, value, 0);
	});
}

function toggle_child_checkbox(event)
{
	this.firstChild.checked = !this.firstChild.checked;
}

function stop_propagation(event)
{
	event.stopPropagation();
}

function clickable_td()
{
	var checkbox = $('#dialoguecontent .tdtable > tbody > tr > td > :checkbox:only-child');
	checkbox.parent().click(toggle_child_checkbox);

	checkbox.parent().hover(
		function() { $(this).addClass("clickabletd") },
		function() { $(this).removeClass("clickabletd") });

	checkbox.click(stop_propagation);
}

$(document).ready(hideinit);
$(document).ready(clickable_td);

$(document).ready(function() {
	$('#dialoguecontent .needjavascript').removeClass('needjavascript');
});
