// Define the timeout for loading a tab container.
var tabContainerLoadTimeout = 30000;

// Define an array to hold tab names we have already loaded.
var tabContentLoaded = new Array();

function tabExists(tabName, tabGroup)
	{
	if (tabName && tabGroup)
		{
		for (var i = 0; i < tabGroup.length; i++)
			{
			if (tabGroup[i] === tabName)
				{
				return true;
				}
			}
		}
	return false;
	}

function getTabContent(tabName, sku)
	{
	// Don't retrieve the tab contents if we already have.
	if (!tabContentAlreadyLoaded(tabName))
		{
		// Set the call data.
		var callMetaData = {
			callback: loadTabContainerData,
			arg: tabName + '_container',
			timeout: tabContainerLoadTimeout,
			errorHandler: tabContainerLoadError
			};

		try
			{
			// Get the content for this tab.
			eval('DwrItemPage.getItem' + startWordWithCaps(tabName) + 'Content(sku, callMetaData);');

			// Only mark this tab as loaded if there were no issues.
			tabContentLoaded[tabContentLoaded.length] = tabName;
			}
		catch (e)
			{
			// Load an error message.
			dwr.util.setValue(tabName + '_container', '<div class="content"><h2 style="color: #880000;">We are having difficulty retrieving the contents for this tab. Please try again later.</h2></div>', {escapeHtml: false});
			}
		}
	}

function startWordWithCaps(word)
	{
	// Capitalize the first letter of this word.
	return word.charAt(0).toUpperCase() + word.substr(1).toLowerCase();
	}

function tabContentAlreadyLoaded(tabName)
	{
	// Check to see if this tab name has already been collected.
	for (var i = 0; i < tabContentLoaded.length; i++)
		{
		if (tabContentLoaded[i] === tabName)
			{
			// Found the tab name.
			return true;
			}
		}

	// Tab name has not been collected yet.
	return false;
	}

function loadTabContainerData(tabContainerData, tabContainerId)
	{
	// Chech the content first.
	if (tabContainerData !== null && tabContainerData !== '')
		{
		// Replace the content of this tab container.
		dwr.util.setValue(tabContainerId, tabContainerData, {escapeHtml: false});
		}
	}

function tabContainerLoadError(message)
	{
	// Show an error message.
	window.status = message;
	}
