/**
 * Holds the tab logic for the TabContainer class
 */
function TabContainer()
{
	this.ItemArray = Array();
	
	this.AddLinkItem = function(IDGroup,ID,Url)
	{
		this.ItemArray.push(new TabItem(IDGroup,ID,'LINK',Url));
	}
	//will setup our internal array for state keeping and other niceties.
	this.AddObjectItem = function(IDGroup,ID)
	{
		this.ItemArray.push(new TabItem(IDGroup,ID,'OBJECT'));
	}
	this.AddHtmlItem = function(IDGroup,ID)
	{
		this.ItemArray.push(new TabItem(IDGroup,ID,'HTML'));
	}
	this.AddAjaxItem = function(IDGroup,ID,AjaxUrl,ActionText,ErrorText,Cache)
	{
		this.ItemArray.push(new TabItem(IDGroup,ID,'AJAX',AjaxUrl,ActionText,ErrorText,Cache));
	}
	
	this.MakeDirty = function(ID)
	{
		var tab=this.FindTabByID(ID);
		if(tab != null)
			tab.IsDirty=true;
		
		//if we are currently active and we are an ajax tab, we need to refresh!
		if(tab.IsActive)
			this.ClickTab(tab);		
	}
	
	this.OnClick = function(IDGroup,ID)
	{
		this.UnClick(IDGroup);
		this.ClickTab(this.FindTabByID(ID));
	}
	
	this.FindTabByID = function(ID)
	{
		for(var c=0; c < this.ItemArray.length; c++)
		{
			if(this.ItemArray[c].ID==ID)
				return this.ItemArray[c];
		}
	}
	
	this.UnClick = function(IDGroup)
	{
		//loop through the options and find the active one.
		for(var c=0; c < this.ItemArray.length; c++)
		{
			if(this.ItemArray[c].IDGroup==IDGroup && this.ItemArray[c].Active==true)
				this.UnClickTab(this.ItemArray[c]);
		}
	}
	
	this.ClickTab = function(obj)
	{
		var tmp = document.getElementById('tab_'+obj.ID+'_content');
		if(tmp != null)
		{
			tmp.className='tab_content_visible';
		}
		
		tmp = document.getElementById('tab_'+obj.ID+'_item');
		if(tmp != null)
		{
			tmp.className="tab_item_active";
		}		

		if(obj.Type=='AJAX')
		{
			if(!obj.Cached || !obj.Cache || obj.IsDirty)
			{
				var tabname='tab_'+obj.ID+'_content';
				this.AjaxCall(obj.Url,tabname,obj.ActionText,obj.ErrorText,'POST');
			}
		}
		
		obj.Active	= true;
		obj.IsDirty	= false;
		obj.Cached	= true;
	}
	
	this.UnClickTab = function(obj)
	{
		var tmp = document.getElementById('tab_'+obj.ID+'_content');
		if(tmp != null)
		{
			tmp.className='tab_content_hidden';
		}

		tmp = document.getElementById('tab_'+obj.ID+'_item');
		if(tmp != null)
		{
			tmp.className="tab_item_inactive";
		}
		obj.Active=false;
	}
	
	this.AjaxCall = function(url, page_element, call_message, error_message, method, params)
	{	
	    if (params == undefined)
	        params = null;
	
		var target_page_elem = document.getElementById(page_element);
		
		//if we have a valid target then do the request
		if (target_page_elem != null)
		{
		    document.getElementById(page_element).innerHTML = call_message;    
		     
			//here we are creating the ajax call processing object and giving it the call back function
		    var call_processing = new CallProcessing
			(
				url,
				function(response_text,output_flag)
				{
					document.getElementById(page_element).innerHTML = response_text;
				},
				error_message,
				true
			);
		
			call_processing.perform_get_post(method,params,true);
			return true;	
		}
	
		//the target was invalid so we will return false
		return false;
	}
	
	function TabItem(IDGroup,ID,Type,Url,ActionText,ErrorText,Cache)
	{
		this.IDGroup= IDGroup;
		this.ID		= ID;
		this.Type	= Type;
		this.Url	= Url;
		this.Active	= false;
		
		//for ajax
		this.ActionText	= ActionText;
		this.ErrorText 	= ErrorText;
		this.Cache		= Cache;
		this.Cached		= false;
		this.IsDirty	= false;
	}
}