var TAB_MAX_LENGTH = 20;
ols.tabsFromFieldsets = function(id)
{
	if(this.CONTAINER = document.getElementById(id))
	{
		this.PAGES = [];
		this.TABS = [];
		
		var tabStrip = document.createElement('DIV');
		tabStrip.className = 'tabstrip';
		var ul = document.createElement('UL');
		tabStrip.appendChild(ul);
		var c = this.CONTAINER.childNodes;
		var fieldsetId = 0;
		for(var i=0;i<c.length;i++)
		{
			try{
				if(c[i].nodeName=='FIELDSET')
				{
					var legend = c[i].getElementsByTagName('LEGEND')[0];
					var legendText = new String(legend.firstChild.nodeValue);
					if(legendText.length<=TAB_MAX_LENGTH)
					{
						var txtNode = document.createTextNode(legendText);
					}
					else
					{
						var txtNode = document.createTextNode(legendText.substring(0,TAB_MAX_LENGTH-3)+'...');						
					}
					
					
					/**
					 * Doing this because of Firefox's annoying inability to style legends properly
					 */
					legend.innerHTML = '<span>'+legendText+'</span>';
					
					
					var tab = document.createElement('LI');	
								
					tab.setAttribute('title',legendText);
					c[i].TAB = tab;
					tab.FIELDSET = fieldsetId;
					tab.OWNER = this;
					tab.style.cursor = 'pointer';	
					tab.onclick = function(){
						this.OWNER.show(this.FIELDSET);
					}
					var l = document.createElement('DIV');
					l.appendChild(txtNode);
					tab.appendChild(l);
					ul.appendChild(tab);
					fieldsetId++;
					
					this.PAGES.push(c[i]);
					this.TABS.push(tab);
				}
			}catch(e){
				
			}
		}
		var clearer = document.createElement('DIV');
		clearer.className = 'bottomBorder';
		tabStrip.appendChild(clearer);
		this.CONTAINER.insertBefore(tabStrip,this.CONTAINER.firstChild);
		this.show(0);
		
		
		
		/**
		 * Create "next/previous" buttons
		 */
		for(var i=0;i<this.PAGES.length;i++)
		{
			var d = document.createElement('DIV');
			d.style.float = 'right';
			d.style.textAlign = 'right';
			this.PAGES[i].insertBefore(d,this.PAGES[i].firstChild);
			if(i>0)
			{
				// Do a previous button
				var b = document.createElement('BUTTON');
				b.innerHTML = '&laquo; Previous';				
				b.OWNER = this;
				b.SHOW = i-1;
				b.onclick = function(){this.OWNER.show(this.SHOW);return false;}
				d.appendChild(b);
			}
			if(i<this.PAGES.length-1)
			{
				// Do a previous button
				var b = document.createElement('BUTTON');
				b.innerHTML = 'Next &raquo;';
				b.OWNER = this;
				b.SHOW = i+1;
				b.onclick = function(){this.OWNER.show(this.SHOW);return false;}
				d.appendChild(b);
			}
		}
	}
}
ols.tabsFromFieldsets.prototype.show = function(fieldset)
{
	var fieldsetId = 0;
	var c = this.CONTAINER.childNodes;
	for(var i=0;i<c.length;i++)
	{
		try{
			if(c[i].nodeName=='FIELDSET')
			{
				if(fieldsetId==fieldset)
				{
					c[i].TAB.className = 'selected';
					c[i].style.display = 'block';
					c[i].style.visibility = 'visible';
					if(c[i].className)
					{
						c[i].TAB.className+=' '+c[i].className;
					}
				}
				else		
				{
					if(c[i].className)
					{
						c[i].TAB.className = c[i].className;
					}
					else
					{
						c[i].TAB.className = '';
					}
					c[i].style.display = 'none';
					c[i].style.visibility = 'hidden';
				}
				fieldsetId++;
			}
		}catch(e){
			
		}
	}
}