// JavaScript Document

//function to hide inactive tabs and associate an onClick event:
//$(document).ready(displaySableTabItems);// JavaScript Document

function displaySableTabItems()
{
	"use strict";
	var sableTabId;
	var sableTab, 		sableTabs 		= document.getElementsByClassName("sable-tab");
	var sableTabItem, 	sableTabItems 	= document.getElementsByClassName("sable-tab-item");
	var sableItemDisplay;
	for (var i = 0; i < sableTabs.length; i++)
	{
		sableTab 			= sableTabs[i];
		sableTabItem 		= sableTabItems[i];
		if (sableTabItem !== undefined)
		{
			sableItemDisplay 	= sableTabItem.classList.contains('sable-tab-active') ? 'block' : 'none';
			sableTabItem.style.display = sableItemDisplay;
			sableTabId = 'sable-tab-'+(i+1);
			sableTab.id = sableTabId;
			sableTab.onclick = function(){openSableTab(this.id);};
		}
	}
}

function openSableTab(id) 
{
	"use strict";
    var i;
	
	//highlight the correct tab
    var sableTab, sableTabs = document.getElementsByClassName("sable-tab");
	var sableTabItem, sableTabItems = document.getElementsByClassName("sable-tab-item");
    for (i = 0; i < sableTabs.length; i++) 
	{
		sableTab 		= sableTabs[i];
		sableTabItem 	= sableTabItems[i];
		
		if ((sableTab !== undefined) && (sableTabItem !== undefined))
		{		
			sableTab.className 		= 'sable-tab';
			sableTabItem.className 	= 'sable-tab-item';
			if (sableTab.id === id)
			{
				sableTab.className 		+= ' sable-tab-active';
				sableTabItem.className 	+= ' sable-tab-active';
			}
		}
	}
	displaySableTabItems();
	
	//if there is more to be done add the attribute sable-click to the button with the function:
	var fld = document.getElementById(id);
	if (fld !== null)
	{
		var onclk = fld.getAttribute('sable-click');
		eval(onclk);	
	}
}

function openSableTabByTag(tag, value)
{
	"use strict";
    var i, id;
	
	//highlight the correct tab
	var sableTab, sableTabs = document.getElementsByClassName("sable-tab");
	id = null;
    for (i = 0; i < sableTabs.length; i++) 
	{
		sableTab = sableTabs[i];
		if (sableTab.getAttribute(tag) === value)
		{
			id = sableTab.id;
			break;
		}
	}
	if (id) {openSableTab(id);}
}

function getActiveSableTabId()
{
	"use strict";
    var i, id, idx;
	var sableTab, sableTabs = document.getElementsByClassName("sable-tab");
	id = null;
    for (i = 0; i < sableTabs.length; i++) 
	{
		sableTab = sableTabs[i];
		idx = (sableTab.className).indexOf('sable-tab-active');
		if (idx > 0)
		{
			id = sableTab.id;
			break;
		}
	}
	return id;
}