
var panes = new Array();
var clicked_panes = new Array ();
	
/* Setup a div element to use panes like tabs.
 *
 * @param containerId Main div which contains other <div> elements that represents the tabs.
 * @param defaultTabId First tab to be shown.
*/
function setupPanes (containerId, defaultTabId) {
	// go through the DOM, find each tab-container
	// set up the panes array with named panes
	// find the max height, set tab-panes to that height
	panes[containerId] = new Array();
	var maxHeight = 0; var maxWidth = 0;
	var container = document.getElementById (containerId);
	var paneContainer;
	var paneList;
	element = 0;
	divs = container.getElementsByTagName("div");
	do {
		paneContainer = divs[element];
		element++;
		paneList = paneContainer.childNodes;
	} while (paneList.length == 0 && element < divs.length);
	
	for (var i = 0; i < paneList.length; i++ ) {
		var pane = paneList[i];
		if (pane.nodeType != 1)
			continue;
		if (pane.offsetHeight > maxHeight)
			maxHeight = pane.offsetHeight;
		if (pane.offsetWidth > maxWidth)
			maxWidth  = pane.offsetWidth;
		panes[containerId][pane.id] = pane;
		panes.length++;
		pane.style.display = "none";
	}
	//paneContainer.style.height = maxHeight + "px";
	//paneContainer.style.width  = maxWidth + "px";
	document.getElementById(defaultTabId).onclick();
}

/*
 * Action to show a pane when clicking in a tab or link.
 *
 * @param paneId Pane identifier to be shown.
 * @param activeTab Tab where the click was done (usually you should use 'this')
 */
function showPane (paneId, activeTab) {
	// make tab active class
	// hide other panes (siblings)
	// make pane visible
	clicked_before = false;
	for (i = 0; i < clicked_panes.length; i++) {
		if (clicked_panes[i] == paneId) {
			clicked_before = true;
			break;
		}
	}
	if (! clicked_before) {
		clicked_panes[clicked_panes.length++] = paneId;
	}
	for (var con in panes) {
		activeTab.blur();
		activeTab.className = "formtab-active";
		if (panes[con][paneId] != null) { // tab and pane are members of this container
			var pane = document.getElementById(paneId);
			pane.style.display = "block";
			var container = document.getElementById(con);
			var tabs = container.getElementsByTagName("ul")[0];
			var tabList = tabs.getElementsByTagName("a")
			for (var i=0; i<tabList.length; i++ ) {
				var tab = tabList[i];
				if (tab != activeTab)
					tab.className = "formtab-disabled";
			}
			for (var i in panes[con]) {
				var pane = panes[con][i];
				if (pane == undefined)
					continue;
				if (pane.id == paneId)
					continue;
				pane.style.display = "none"
			}
		}
	}
	return false;    
}

/**
 * Shows or hide an element based on its current state.
 *
 * @param id HTML identifier of the element.
 */
function toggle_visualize (id) {
	div = document.getElementById (id);
	
	if (div.style.display == 'none') {
		div.style.display = '';
	} else {
		div.style.display = 'none';
	}
}

/**
 * Enable or disable an element based on its current state.
 *
 * @param id HTML identifier of the element.
 */
function toggle_disabled (id) {
	div = document.getElementById (id);
	
	if (div.disabled) {
		div.disabled = false;
	} else {
		div.disabled = true;
	}
}

/**
 * Check if a radio button input has been checked with any of the posibilities.
 *
 * @param input_name Radio button name.
 */
function check_radio_button (input_name) {
	radios = document.getElementsByName (input_name);
	for (i = 0; i < radios.length; i++) {
		if (radios[i].checked)
			return true;
	}
	return false;
}

/**
 * Check if an input text is a numeric value.
 *
 * @param input_name Radio button.
 */
function check_numeric_input_text (input_name) {
	texts = document.getElementsByName (input_name);
	numericExpression = /^[0-9]+$/;
	for (i = 0; i < texts.length; i++) {
		if (! texts[i].value.match (numericExpression))
			return false;
	}
	return true;
}

/**
 * Check if an input text is not empty.
 *
 * @param input_name Radio button.
 */
function check_input_text (input_name) {
	texts = document.getElementsByName (input_name);
	numericExpression = /^[0-9]+$/;
	for (i = 0; i < texts.length; i++) {
		if (texts[i].value == "")
			return false;
	}
	return true;
}

/**
 * Check if an input text is not empty.
 *
 * @param input_name Radio button.
 */
function check_select (input_name) {
	selects = document.getElementsByName (input_name);
	for (i = 0; i < selects.length; i++) {
		if (selects[i].value == -1)
			return false;
	}
	return true;
}

function html_entity_decode (str) {
	if (! str)
		return "";
	var ta = document.createElement ("textarea");
	ta.innerHTML = str.replace (/</g, "&lt;").replace (/>/g,"&gt;");
	return ta.value;
}
