// $Revision: 1 $ $Date: 2009-08-10 16:19:35+01:00 $ $Archive: /TRUNK/UAF/TribalTech.UI/TribalTech.UI.Web/tt_common/scripts/TT_ClientTab.js $
var ttclientTab_state=[];
var equalOperator =0;
var leftTop = "LeftTop";

function ttclientTab_init(clientId) 
{		
	if (ttclientTab_state[clientId]==null)
	    var tabControl = new ttclient_tab(clientId,eval("TT_"+clientId+"_Tab"));	
}

function ttclient_tab(clientId,props)
{
//alert(clientId);
	ttclientTab_state[clientId]=this;		
	this.ControlId = props[0];	
	this.Url = props[1];
	this.NumberOfTabs = props[2];
	this.TabOrientation = props[3];
	this.AfterSelectedTabChange = props[4];
	this.BeforeSelectedTabChange = props[5];	
	this.AdditionalParameter = props[6];
	
}

function ttclientTab_tabClick(clientId, index)
{
    ttclientTab_init(clientId);

	// set hidden input field
	document.getElementById(clientId).value=index;

	var tabControl = ttclientTab_state[clientId];
	var tabName = "";
	
	if (tabControl.BeforeSelectedTabChange!="")
	{
    	//fire the beforeselectedtabchange event it there is one
		var result = eval(tabControl.BeforeSelectedTabChange+"('"+index+"','" + tabControl.AdditionalParameter + "')");
		if(result==false)
		{
		    //cancel the selection of the tab
		    return result;
		}
	}
	// Loop through all tabs and show/hide as appropriate
	// Also change style of tabtops
	for(var i =0;i<tabControl.NumberOfTabs;i++)
	{
		var tabDiv = document.getElementById(clientId+"Tab"+i);
		var tabTop = document.getElementById(clientId+"Top"+i);
		
		if (tabDiv!=null&&tabTop!=null)
		{
			if (i==index)
			{
				switch (tabControl.TabOrientation)
				{
					case leftTop:
						tabTop.className ="tableftsel";
						break;
					default:
						tabTop.className ="tabsel";
						break;
				}
				tabDiv.style.display="block";
				tabName = tabTop.innerText;
				ttclientTab_selectFirstElement(tabDiv);
			}
			else if (tabTop.className=="tabsel"|| //do nothing with disabled tabs
					 tabTop.className=="tableftsel")	
			{
				switch (tabControl.TabOrientation)
				{
					case leftTop:
						tabTop.className ="tableft";
						break;
					default:
						tabTop.className ="tab";
						break;
				}
				
				tabDiv.style.display="none";
			}
		}
	}
	
	if (tabControl.AfterSelectedTabChange!="")
	{
		eval(tabControl.AfterSelectedTabChange+"('"+index+"','"+tabName+"', '" + tabControl.AdditionalParameter + "')");
	}
	
	tabControl = null;
	
	return true;
}

function ttclientTab_selectFirstElement(obj){
    //find the first standard control and attempt to set focus
    var tables = obj.getElementsByTagName("table");
    for(var i=0;i<tables.length;i++){
        if((tables[i].standard!=null && tables[i].selfirst!=null) || tables[i].minisearch!=null){
            var inputs = tables[i].getElementsByTagName("input");
            for(var j=0;j<inputs.length;j++)
                if(inputs[j].type!="hidden") //exclude hidden field types
                    if(inputs[j].type=="text") //include only text fields
                        if(inputs[j].style.visibility!="hidden" && inputs[j].style.display!="none")
                            if(inputs[j].readOnly==false){//ensure visible
                                try{
                                    inputs[j].focus();                            
                                    break;
                                }catch(e){}//move to next field                                
                            }
        }    
    }
}
