function divTab(dId, tId, aCss, nCss, selected)
{
	var self = this;
	this.tabId = tId;
	this.divId = dId;
	this.activeCss = aCss;
	this.normalCss = nCss;
	this.isSelected = selected;
	this.div;
	this.tab;
	
	this.getDiv = function()
	{
		if (self.div == null || self.div == 'undefined')
			self.div = document.getElementById(self.divId);
		return self.div;
	}

	this.getTab = function()
	{
		if (self.tab == null || self.tab == 'undefined')
			self.tab = document.getElementById(self.tabId);
		return self.tab;
	}
	
	this.hoverTab = function(aCss)
	{
		if (self.isSelected == true)
			return;
		self.getTab().className = self.activeCss;
	}

	this.offHoverTab = function(nCss)
	{
		if (self.isSelected == true)
			return;
		self.getTab().className = self.normalCss;
	}
	
	this.hide = function()
	{
		if (self.isSelected == false)
			return;
		self.getDiv().style.display = 'none';
		self.getTab().className = self.normalCss;
		self.isSelected = false;
	}

	this.show = function()
	{
		if (self.isSelected == true)
			return;
		self.getDiv().style.display = 'block';
		self.getTab().className = self.activeCss;
		self.isSelected = true;
	}	
}

function divTabCollection(aCss, nCss)
{
	var self = this;
	this.activeCss = aCss;
	this.normalCss = nCss;
	this.tabs = new Array();
	
	this.add = function(dId, tId)
	{
		var isFirst = false;
		if (self.tabs.length == 0)
			isFirst = true;
		self.tabs[self.tabs.length] = new divTab(dId, tId, self.activeCss, self.normalCss, isFirst);
	}
	
	this.hover = function(tab)
	{
		var count = 0;
		for (count = 0; count < self.tabs.length; ++count)
		{
			if (self.tabs[count].tabId != tab.id)
				self.tabs[count].offHoverTab();
			else
				self.tabs[count].hoverTab();
		}
	}

	this.hoverOff = function(tab)
	{
		var count = 0;
		for (count = 0; count < self.tabs.length; ++count)
		{
			if (self.tabs[count].tabId == tab.id)
				self.tabs[count].offHoverTab();
		}
	}
	
	this.click = function(tab)
	{
		var count = 0;
		for (count = 0; count < self.tabs.length; ++count)
		{
			if (self.tabs[count].tabId != tab.id)
				self.tabs[count].hide();
			else
				self.tabs[count].show();
		}
	}
}

function toggleTab(tId, dId, aCss, nCss, alreadyOn)
{
	var self = this;
	this.selected = alreadyOn;
	this.tabId = tId;
	this.divId = dId;
	this.tab;
	this.div;
	this.activeCss = aCss;
	this.normalCss = nCss;
	
	this.getTab = function()
	{
		if (self.tab == null || self.tab == 'undefined')
			self.tab = document.getElementById(self.tabId);
		return self.tab;
	}

	this.getDiv = function()
	{
		if (self.div == null || self.div == 'undefined')
			self.div = document.getElementById(self.divId);
		return self.div;
	}
	
	this.hoverTab = function()
	{
		if (self.selected)
			return;
		self.getTab().className = self.activeCss;		
	}
	
	this.outTab = function()
	{
		if (self.selected)
			return;
		self.getTab().className = self.normalCss;
	}
	
	this.click = function()
	{
		if (self.selected)
		{
			self.getDiv().style.display = 'none';
			self.getTab().className = self.normalCss;
		}
		else
		{
			self.getDiv().style.display = 'block';
			self.getTab().className = self.activeCss;
		}
		self.selected = !self.selected;
	}
}

function InitTable(objTable)
{
	var mainList = document.getElementById(objTable);
	if(mainList)
	{
		var rows = mainList.getElementsByTagName('tr');

		if(mainList.textContent) ie = false;
		
		for (x=1; x<rows.length; x++)
		{
			// Hide sub rows
			if(rows[x].cells[1].innerHTML.length<=emptyCellLength)
			{
				rows[x].style.display='none';
			}
			// If there is a next row and it is not a sub row then change current folder to a dot...
			if(rows[x+1])
			{
				if(rows[x+1].cells[1].nodeType==1 && rows[x+1].cells[1].innerHTML.length>emptyCellLength)
				{
					if(rows[x].cells[1].getElementsByTagName('img')[0])
					{
						// Show dot...
						rows[x].cells[1].getElementsByTagName('img')[0].src="?a=41834";
						rows[x].cells[1].getElementsByTagName('img')[0].style.padding = '0.3em';
						rows[x].cells[1].getElementsByTagName('img')[0].className="dot";
					}
				}
			}
			else
			{
				if(rows[x].cells[1].getElementsByTagName('img')[0])
				{
					// Show dot...
					rows[x].cells[1].getElementsByTagName('img')[0].src="?a=41834";
					rows[x].cells[1].getElementsByTagName('img')[0].style.padding = '.3em';
					rows[x].cells[1].getElementsByTagName('img')[0].className="dot";
				}
			}
			
			if(ie)
			{
				rows[x].onclick = new Function( 'toggleRow(this, window.event);return false;' );
			}
			else
			{
				rows[x].addEventListener('click',function (e)
				{
					toggleRow(this,e);
				},false);
			}
		}
	}
}

function ExpandAll(objTable)
{
	var mainList = document.getElementById(objTable);
	if(mainList)
	{
		var rows = mainList.getElementsByTagName('tr');

		if(mainList.textContent) ie = false;
		
		for (x=1; x<rows.length; x++)
		{
			// Show row
			rows[x].style.display='';
		}
		
		for(x=0; x < mainList.getElementsByTagName('img').length; x++){
			if(mainList.getElementsByTagName('img').item(x).className=='plus_button')
				mainList.getElementsByTagName('img').item(x).src='?a=41741';
		}
		document.getElementById('expand-all').style.display='none';
		document.getElementById('collapse-all').style.display='block';
	}
}

function CollapseAll(objTable)
{
	var mainList = document.getElementById(objTable);
	if(mainList)
	{
		var rows = mainList.getElementsByTagName('tr');

		if(mainList.textContent) ie = false;
		
		for (x=2; x<rows.length; x++)
		{
			// Hide sub rows
			if(rows[x].cells[1].innerHTML.length<=emptyCellLength)
			{
				rows[x].style.display='none';
			}
		}
		for(x=0; x < mainList.getElementsByTagName('img').length; x++){
			if(mainList.getElementsByTagName('img').item(x).className=='plus_button')
				mainList.getElementsByTagName('img').item(x).src='?a=41742';
		}
		document.getElementById('collapse-all').style.display='none';
		document.getElementById('expand-all').style.display='block';
	}
}

function toggleRow(itemClicked, evt)
{	
	var process = false;
	
	var target = evt.target ? evt.target : evt.srcElement;
	
	// Find the column for the level we are expanding/collapsing
	var colNo = 1;
    for (var cell = itemClicked.firstChild, i = 0; cell != null; cell = cell.nextSibling)
    {
        if (cell.nodeType == 1)
        {
			if(cell.innerHTML.length>emptyCellLength)
			{
				colNo = i;
				break;
			}
			i++;
        }
	}
	
	// If this is a level 4 then do nothing
	if(colNo!=4)	
	{
		processRow = itemClicked.nextSibling;
		var hide = null;

		while(processRow != null)
		{
			if (processRow.nodeType == 1)
			{
				// If we have reached another row at the same level or higher then break
				for(iCol=0;iCol<=colNo;iCol++)
				{
					if(processRow.cells[iCol].innerHTML.length>emptyCellLength)
					{
						return;
					}
				}
				// Set if we are hiding or showing the first time we are here
				if(hide==null)
				{
					if(processRow.style.display!='none')
						hide = true;
					else
						hide = false;
				}
				if(hide)
				{
					processRow.style.display = 'none';
					itemClicked.getElementsByTagName('img').item(0).src = '?a=41742';
					if(processRow.getElementsByTagName('img').item(0).className=='plus_button')
						processRow.getElementsByTagName('img').item(0).src = '?a=41742';
				}
				else if(processRow.cells[colNo+1].innerHTML.length>emptyCellLength)
				{
					processRow.style.display = '';
					itemClicked.getElementsByTagName('img').item(0).src = '?a=41741';
				}
			}
			processRow = processRow.nextSibling;
		}
	}

	if(ie)
	{
		evt.cancelBubble = true;
	}
	else
	{
		evt.stopPropagation();
	}
}

function toggleDisplay(item)
{
	if(document.getElementById(item).style.display=='none'){document.getElementById(item).style.display='block';}
	else{document.getElementById(item).style.display='none';}
}

function showRegions()
{
	InitTable('tblRegionBreakdown'); if(document.getElementById('tblRegionBreakdown')){document.getElementById('tblRegionBreakdown').style.display='block';}
}

function showAssets()
{
	InitTable('tblSectorBreakdown'); if(document.getElementById('tblSectorBreakdown')){document.getElementById('tblSectorBreakdown').style.display='block';}
}

