function createTabset(theTabset, location){

	var theTabsetTitle = theTabset;
	
	var theTabsetNode = document.getElementById(theTabset);
	setClass( theTabsetNode, 'tabSet_normal');
	
	// Create UL for tab navigation
	var tabNavigation = document.createElement('UL');
	tabNavigation.id = theTabsetTitle + '_tabNavigationUL';
	setClass( tabNavigation, 'navigationUL');
	if (location == 'none'){
		tabNavigation.style.display = 'none';
	}
	theTabsetNode.parentNode.insertBefore(tabNavigation, theTabsetNode);

	var theTabs = theTabsetNode.childNodes;
	var firstTab = '';
				
	for(var i = 0; i < theTabs.length; i++){
	
		var currentTab = theTabs.item(i);
						
		if (currentTab.nodeName == 'DT'){
			var currentTitle = trimString(currentTab.innerHTML);
			setClass( currentTab, 'tabTitle');
			
			createTabButton(theTabsetTitle, currentTitle);
			if (firstTab == ''){
				var firstTab = currentTitle;
			}
		}
		
		if (currentTab.nodeName == 'DD'){
			currentTab.id = currentTitle;
			setClass( currentTab, 'tabBody_inactive');
		}

	}
	
	viewTab(theTabsetTitle, firstTab)
				
}


function createTabButton(theTabset, theTabTitle){

	tabButtonDiv = document.getElementById(theTabset + '_tabNavigationUL');
	
	tabLI = document.createElement('LI');
	
	var tabButton_L = document.createElement('div');
	setClass( tabButton_L, 'tabButton_L');

	var tabButton_C = document.createElement('div');
	setClass( tabButton_C, 'tabButton_C');

	var tabButton_R = document.createElement('div');
	setClass( tabButton_R, 'tabButton_R');

	var tabButton = document.createElement('a');
	tabButton.id = theTabTitle;
	setClass( tabButton, 'tabButton');
	tabButton.setAttribute('href', 'javascript:viewTab(\'' + theTabset + '\',\'' + theTabTitle + '\');tooltip_close();');
	
	var tabButtonText = document.createTextNode(theTabTitle);

	tabButton.appendChild(tabButton_L);
	tabButton.appendChild(tabButton_C);
	tabButton_C.appendChild(tabButtonText);
	tabButton.appendChild(tabButton_R);

	tabLI.appendChild(tabButton);
	tabButtonDiv.appendChild(tabLI);

}

function viewTab(theTabset, theTab){

	var theTabsetTitle = theTabset;

	// Select desired tab body.
	var theTabset = document.getElementById(theTabset);
	var theTabs = theTabset.childNodes;

	for(var i = 0; i < theTabs.length; i++){
	
		var currentTab = theTabs.item(i);
						
		if (currentTab.nodeName == 'DD' && currentTab.id == theTab){
			currentTab.setAttribute('class', 'tabBody_selected');
			currentTab.setAttribute('className', 'tabBody_selected');
		}
		else if (currentTab.nodeName == 'DD'){
			currentTab.setAttribute('class', 'tabBody_hidden');
			currentTab.setAttribute('className', 'tabBody_hidden');
		}

	}
	
	// Select desired tab navigation button.
	var tabNavigation = document.getElementById(theTabsetTitle + '_tabNavigationUL');
	var tabNavigationTabs = tabNavigation.childNodes;
	
	for(var i = 0; i < tabNavigationTabs.length; i++){

		var currentTab = tabNavigationTabs.item(i).firstChild;
	
		if (currentTab.id == theTab ) {
			setClass( currentTab, 'tabButton_selected');
		}
		else {
			setClass( currentTab, 'tabButton_normal');
		}

	}

}


function destroyTabset(theTabset){

	var theTabsetTitle = theTabset;

	var theTabset = document.getElementById(theTabset);
	setClass( theTabset, 'tabSet_full');
	
	var theTabNavigation =  document.getElementById(theTabsetTitle + '_tabNavigationUL');
	theTabset.parentNode.removeChild(theTabNavigation);


}


/* Utilities */
function trimString(str) {
	return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}


function setClass( theElement, theClass) {
	theElement.setAttribute('class', theClass);
	theElement.setAttribute('className', theClass);
}
