var disableHiding = false;

/*
 * Show a specific input and hide the rest
 */
function show_input(whichInput) {
	var rowContainer = document.getElementById("rowContainer");
	var rows = rowContainer.getElementsByTagName("tr");
	var x = rows.length;
	var id_prefix;
	while (x--) {
		id_prefix = rows[x].id.split("p")[0];
		if (id_prefix == "s" + whichInput || id_prefix == "g" + whichInput || whichInput == "all") {
			rows[x].style.display = "";
		} else if (rows[x].id != ''){
			rows[x].style.display = "none";
		}
	}
}

/*
 * Clean an array of empty string elements
 */
function cleaner(element, index, array) {
	return element != '';
}

/*
 * Hide all tabs on a given row
 */
function hide_row(id) {
	if (disableHiding) {
		return;
	}
	var rowIdPieces = id.match(/([a-z]+[0-9]+)/gi);
	try {
		rowIdPieces = rowIdPieces.filter(cleaner);
	} catch (error1) {
		rowIdPieces = rowIdPieces.clean("");
	}
	var rowId = rowIdPieces[0];
	if (rowIdPieces.length > 1) {
		rowId += rowIdPieces[1];
	}
	var x, count = 0;
	var optionButtons = document.getElementById('nav_' + rowId);
	for (x = 0; x < optionButtons.childNodes.length; x++) {
		if (optionButtons.childNodes[x].nodeType == 1) {
			optionButtons.childNodes[x].className = '';
			count++;
		}
	}
	try {
		document.getElementById(rowId + '_pids').style.display = 'none';
		document.getElementById(rowId + '_general').style.display = 'none';
	} catch (err) {}
	if (rowIdPieces.length < 2) {
		return;
	}
	try {
		document.getElementById(rowId + '_ca').style.display = 'none';
	} catch (error2) {}
	count -= 2; // account for the two we just hid (ca doesn't count)
	try {
		for (x = 1; x <= count; x++) {
			document.getElementById(rowId + '_ca' + x).style.display = 'none';
		}
	} catch (error3) {}
}

/*
 * Show a specific CA tab
 */
function show_ca(id, show) {
	var ca = $("#" + id.replace("ca", "_ca"));
	if (show === undefined) {
		show = ca.css("display") == 'none';
	}
	hide_row(id);
	if (show) {
		ca.css("display", "");
		$("#nav_" + id.replace("ca", "_ca")).addClass("here");
	}
}

/*
 * Show a specific General tab
 */
function show_general(id, show) {
	var general = $("#" + id + "_general");
	if (general.length > 0) {
		if (show === undefined) {
			show = general.css("display") == 'none';
		}
		hide_row(id);
		if (show) {
			general.css("display", "");
			$("#nav_" + id + "_gen").addClass("here");
		}
	}
}

/*
 * Show a specific PIDs tab
 */
function show_pids(id, show) {
	var pids = $("#" + id + '_pids');
	if (show === undefined) {
		show = pids.css("display") == 'none';
	}
	hide_row(id);
	if (show) {
		pids.css("display", "");
		$("#nav_" + id + "_pids").addClass("here");
	}
}

/*
 * Clean an array of empty string elements
 */
Array.prototype.clean = function(deleteValue) {
	for (var i = 0; i < this.length; i++) {
		if (this[i] == deleteValue) {         
			this.splice(i, 1);
			i--;
		}
	}
	return this;
};

/*
 * Show a specific tab for each row based on a given category
 */
function show_all(category) {
	var show = !$("#nav_" + category).hasClass("here");
	$("#nav_group div").each(function() {
		$(this).removeClass("here");
	});
	if (show) {
		$("#nav_" + category).addClass("here");
	}
	var rowId;
	var rows = $("div.detailOptions").children("div");
	var y = rows.length, x, z, optionButtons;
	while (y--) {
		optionButtons = $("div", rows.get(y));
		z = optionButtons.length;
		rowId = optionButtons.attr("id").replace('nav_', '').split("_")[0];
		hide_row(rowId);
		if (!show) {
			continue;
		}
		disableHiding = true;
		if (category == 'gen' || category == 'all') {
			show_general(rowId, show);
		} else if (show) {
			show_general(rowId, !show);
		}
		if (category == 'pids' || category == 'all') {
			show_pids(rowId, show);
		} else if (show) {
			show_pids(rowId, !show);
		}
		
		if (startsWith('ca', category) || category == 'all') {
			while (z-- > 2) { // ignore the last two since they're done above
				if (category == 'all' || optionButtons.get(z).id == rows.get(y).id + '_' + category) {
					show_ca(optionButtons[z].id.replace('nav_', '').replace('_', ''), show);
				}
			}
		}
		disableHiding = false;
	}
}

function pidModeChanged(pidId) {
	document.getElementById(pidId).readOnly = false;
}

function disableManualEntries() {
	$("tr.manual input").each(function() {
		$(this).attr("disabled", "disabled");
	});
	$("tr.manual select").each(function() {
		$(this).attr("disabled", "disabled");
	});
}