	/************************************************************************************************************
	(C) www.dhtmlgoodies.com, October 2005
	
	This is a script from www.dhtmlgoodies.com. You will find this and a lot of other scripts at our website.	
	
	Terms of use:
	You are free to use this script as long as the copyright message is kept intact. However, you may not
	redistribute, sell or repost it without our permission.
	
	Updated:
		
		March, 14th, 2006 - Create new tabs dynamically
		March, 15th, 2006 - Dynamically delete a tab
		
	Thank you!
	
	www.dhtmlgoodies.com
	Alf Magne Kalleland
	
	************************************************************************************************************/		
	var textPadding = 3; // Padding at the left of tab text - bigger value gives you wider tabs
	var strictDocType = true; 
	var tabView_maxNumberOfTabs = 100;	// Maximum number of tabs
	
	/* Don't change anything below here */
	var dhtmlgoodies_tabObj = new Array();
	var activeTabIndex = new Array();
	var MSIE = navigator.userAgent.indexOf('MSIE')>=0?true:false;
	
	var regExp = new RegExp(".*MSIE ([0-9]\.[0-9]).*","g");
	var navigatorVersion = navigator.userAgent.replace(regExp,'$1');
	
	var ajaxObjects = new Array();
	var tabView_countTabs = new Array();
	var tabViewHeight = new Array();
	var tabDivCounter = 0;
	var closeImageHeight = 8;	// Pixel height of close buttons
	var closeImageWidth = 8;	// Pixel height of close buttons
	
	
	function setPadding(obj,padding){
		var span = obj.getElementsByTagName('SPAN')[0];
		span.style.paddingLeft = padding + 'px';	
		span.style.paddingRight = padding + 'px';	
	}
	function showTab(parentId,tabIndex)
	{
		var parentId_div = parentId + "_";
		if(!document.getElementById('tabView' + parentId_div + tabIndex)){
			return;
		}
		if(activeTabIndex[parentId]>=0){
			if(activeTabIndex[parentId]==tabIndex){
				return;
			}
	
			var obj = document.getElementById('tabTab'+parentId_div + activeTabIndex[parentId]);
			if(obj!=null)
			{
			obj.className='tabInactive';
			document.getElementById('tabView' + parentId_div + activeTabIndex[parentId]).style.display='none';
			}
		}
		
		var thisObj = document.getElementById('tabTab'+ parentId_div +tabIndex);	
			
		thisObj.className='tabActive';
		
		document.getElementById('tabView' + parentId_div + tabIndex).style.display='block';
		activeTabIndex[parentId] = tabIndex;
		

		var parentObj = thisObj.parentNode;
		var aTab = parentObj.getElementsByTagName('DIV')[0];
		countObjects = 0;
		var startPos = 2;
		var previousObjectActive = false;
		while(aTab){
			if(aTab.tagName=='DIV'){
				if(previousObjectActive){
					previousObjectActive = false;
					startPos-=2;
				}
				if(aTab==thisObj){
					startPos-=2;
					previousObjectActive=true;
					setPadding(aTab,textPadding+1);
				}else{
					setPadding(aTab,textPadding);
				}
				
				aTab.style.left = startPos + 'px';
				countObjects++;
				startPos+=2;
			}			
			aTab = aTab.nextSibling;
		}
		
		return;
	}
	
	function tabClick()
	{
		var idArray = this.id.split('_');		
		showTab(this.parentNode.parentNode.id,idArray[idArray.length-1].replace(/[^0-9]/gi,''));
		
	}
	
	function rolloverTab()
	{
		if(this.className.indexOf('tabInactive')>=0){
			this.className='inactiveTabOver';
		}
		
	}
	function rolloutTab()
	{
		if(this.className ==  'inactiveTabOver'){
			this.className='tabInactive';
		}
		
	}
	
	function hoverTabViewCloseButton()
	{
		this.src = this.src.replace('close.gif','close_over.gif');
	}
	
	function stopHoverTabViewCloseButton()
	{
		this.src = this.src.replace('close_over.gif','close.gif');
	}
	function setTabSize(mainContainerID,width,height)
	{
		if(width!=0)
		{
		document.getElementById(mainContainerID).style.width=width+"px";
		}
		if(height!=0)
		{
		document.getElementById(mainContainerID).style.height=height+"px";
		}
		
		var divs = dhtmlgoodies_tabObj[mainContainerID].getElementsByTagName('DIV');

		
		for(var no=0;no<divs.length;no++){
			if(divs[no].className=='dhtmlgoodies_aTab'){
				divs[no].style.width=(width-12)+"px";
			}
		}
	for(var no=0;no<=tabView_countTabs[mainContainerID];no++){
	TuneHeight("TabWindowName"+no,"TabWindowID"+no,mainContainerID)
	}
	
	}
	function initTabs(mainContainerID,tabTitles,activeTab,width,height,closeButtonArray,additionalTab)
	{
		if(!closeButtonArray)closeButtonArray = new Array();
		if(!tabTitles)tabTitles = new Array();
		if(!additionalTab || additionalTab=='undefined'){			
			dhtmlgoodies_tabObj[mainContainerID] = document.getElementById(mainContainerID);
			width = width + '';
			if(width.indexOf('%')<0)width= width + 'px';
			dhtmlgoodies_tabObj[mainContainerID].style.width = width;
						
			height = height + '';
			if(height.length>0){
				if(height.indexOf('%')<0)height= height + 'px';
			//	dhtmlgoodies_tabObj[mainContainerID].style.height = height;
			}
			

			tabViewHeight[mainContainerID] = height;
			
			var tabDiv = document.createElement('DIV');		
			var firstDiv = dhtmlgoodies_tabObj[mainContainerID].getElementsByTagName('DIV')[0];	
			
			dhtmlgoodies_tabObj[mainContainerID].insertBefore(tabDiv,firstDiv);	
			tabDiv.className = 'dhtmlgoodies_tabPane';	
			
			tabView_countTabs[mainContainerID] = 0;

		}else{
			var tabDiv = dhtmlgoodies_tabObj[mainContainerID].getElementsByTagName('DIV')[0];
			var firstDiv = dhtmlgoodies_tabObj[mainContainerID].getElementsByTagName('DIV')[1];
			height = tabViewHeight[mainContainerID];
			activeTab = tabView_countTabs[mainContainerID];		
	
			
		}
		
		
		
		for(var no=0;no<tabTitles.length;no++){
			var aTab = document.createElement('DIV');
			aTab.id = 'tabTab' + mainContainerID + "_" +  (no + tabView_countTabs[mainContainerID]);
			aTab.onmouseover = rolloverTab;
			aTab.onmouseout = rolloutTab;
			aTab.onclick = tabClick;
			aTab.className='tabInactive';
			aTab.src=tabTitles[no];
			
			var span = document.createElement('SPAN');
			span.innerHTML = tabTitles[no];
			span.style.position = 'relative';
			aTab.appendChild(span);
			
			if(closeButtonArray[no]){
				var closeButton = document.createElement('IMG');
				closeButton.src =filePath+ 'manage/images/close.gif';
				closeButton.height = closeImageHeight + 'px';
				closeButton.width = closeImageHeight + 'px';
				closeButton.setAttribute('height',closeImageHeight);
				closeButton.setAttribute('width',closeImageHeight);
				closeButton.style.position='absolute';
				closeButton.style.top = '6px';
				closeButton.style.right = '0px';
				closeButton.onmouseover = hoverTabViewCloseButton;
				closeButton.onmouseout = stopHoverTabViewCloseButton;
				
				//span.innerHTML = span.innerHTML + '&nbsp;&nbsp;&nbsp;&nbsp;';	
				
				var deleteTxt = span.innerHTML+'';

				closeButton.onclick = function(){ deleteTab(this.parentNode.id) };
				aTab.appendChild(closeButton);
			}
			
			tabDiv.appendChild(aTab);
			
		}

		var tabs = dhtmlgoodies_tabObj[mainContainerID].getElementsByTagName('DIV');
		var divCounter = 0;
		for(var no=0;no<tabs.length;no++){
			if(tabs[no].className=='dhtmlgoodies_aTab' && tabs[no].parentNode.id == mainContainerID){
			//	if(height.length>0)tabs[no].style.height = height;
				tabs[no].style.display='none';
				tabs[no].id = 'tabView' + mainContainerID + "_" + divCounter;
				divCounter++;
			}			
		}	
		tabView_countTabs[mainContainerID] = tabView_countTabs[mainContainerID] + tabTitles.length;	
		showTab(mainContainerID,activeTab);
		return activeTab;
	}	
	
	function showAjaxTabContent(ajaxIndex,parentId,tabId)
	{
		var obj = document.getElementById('tabView'+parentId + '_' + tabId);
		obj.innerHTML = ajaxObjects[ajaxIndex].response;		
	}
	
	function resetTabIds(parentId)
	{
		var tabTitleCounter = 0;
		var tabContentCounter = 0;
		
		
		var divs = dhtmlgoodies_tabObj[parentId].getElementsByTagName('DIV');

		
		for(var no=0;no<divs.length;no++){
			if(divs[no].className=='dhtmlgoodies_aTab'){
				divs[no].id = 'tabView' + parentId + '_' + tabTitleCounter;
				tabTitleCounter++;
			}
			if(divs[no].id.indexOf('tabTab')>=0){
				divs[no].id = 'tabTab' + parentId + '_' + tabContentCounter;	
				tabContentCounter++;
			}	
			
				
		}
	
		tabView_countTabs[parentId] = tabContentCounter;
	}

	
	function createNewTab(parentId,tabTitle,tabContent,tabContentUrl,closeButton)
	{		
	
		if(tabView_countTabs[parentId]>=tabView_maxNumberOfTabs)return;	// Maximum number of tabs reached - return
		var div = document.createElement('DIV');
		div.className = 'dhtmlgoodies_aTab';
		dhtmlgoodies_tabObj[parentId].appendChild(div);		

		var tabId = initTabs(parentId,Array(tabTitle),0,'','',Array(closeButton),true);
		if(tabContent)div.innerHTML = tabContent;
		if(tabContentUrl){		
			var ajaxIndex = ajaxObjects.length;
			ajaxObjects[ajaxIndex] = new sack();
			ajaxObjects[ajaxIndex].requestFile = tabContentUrl;	// Specifying which file to get

			ajaxObjects[ajaxIndex].onCompletion = function(){ showAjaxTabContent(ajaxIndex,parentId,tabId); };	// Specify function that will be executed after file has been found
			ajaxObjects[ajaxIndex].runAJAX();		// Execute AJAX function	
		
		}
				
	}
	function createTabWindow(parentId,tabTitle,fileUrl)
	{
	
	    var index=getTabIndexBySrc(fileUrl);
		if(index!=-1)
		{
	
		showTab(index[0],index[1]);
		}else
		{
		if(tabView_countTabs[parentId]>=tabView_maxNumberOfTabs)return;	// Maximum number of tabs reached - return
		var div = document.createElement('DIV');
		div.className = 'dhtmlgoodies_aTab';
		dhtmlgoodies_tabObj[parentId].appendChild(div);		
		var tabId = initTabs(parentId,Array(tabTitle),0,'','',Array(true),true);
		 setTabSrc(parentId,tabId,fileUrl);
		tabContent="<div id=\"loadTabWindowID"+tabId+"\" style=\"text-align:center;padding-top:50px;padding-bottom:50px;\"><img src=\""+filePath+"manage/images/loading.gif\" alt=\"\" width=\"94\" height=\"17\" /></div><iframe style=\"display:none\" onLoad=\"{loadTabWindow('TabWindowName"+tabId+"','TabWindowID"+tabId+"','"+parentId+"','"+tabId+"');}\" src=\""+fileUrl+"\" name=\"TabWindowName"+tabId+"\" id=\"TabWindowID"+tabId+"\" width=\"740\" height=\"200\" marginwidth=\"0\" marginheight=\"0\" frameborder=\"0\" scrolling=\"no\"></iframe>";
		div.innerHTML = tabContent;
		}
	}
	
	function openTabWindow(parentId,tabTitle,fileUrl)
	{
	if(tabView_countTabs[parentId]<=0||activeTabIndex[parentId]==0){
	createTabWindow(parentId,tabTitle,fileUrl);
	return;
	}
	 setTabTitle(parentId,activeTabIndex[parentId],tabTitle);
	if(document.getElementById('tabTab' + parentId + '_' + activeTabIndex[parentId])){
		var obj = document.getElementById('tabView' + parentId + '_' + activeTabIndex[parentId]);
		setTabSrc(parentId,activeTabIndex[parentId],fileUrl);
		tabContent="<div id=\"loadTabWindowID"+activeTabIndex[parentId]+"\" style=\"text-align:center;padding-top:50px;padding-bottom:50px;\"><img src=\""+filePath+"manage/images/loading.gif\" alt=\"\" width=\"94\" height=\"17\" /></div><iframe style=\"display:none\" onLoad=\"{loadTabWindow('TabWindowName"+activeTabIndex[parentId]+"','TabWindowID"+activeTabIndex[parentId]+"','"+parentId+"','"+activeTabIndex[parentId]+"');}\" src=\""+fileUrl+"\" name=\"TabWindowName"+activeTabIndex[parentId]+"\" id=\"TabWindowID"+activeTabIndex[parentId]+"\" width=\"740\" height=\"200\" marginwidth=\"0\" marginheight=\"0\" frameborder=\"0\" scrolling=\"no\"></iframe>";
		obj.innerHTML = tabContent;
				}
	}
	function setTabSrc(mainContainerID,tabId,fileUrl)
	{
	document.getElementById('tabTab' + mainContainerID + "_" +  tabId).src=fileUrl;
	}
	
	function loadTabWindow(fm_name,fm_id,mainContainerID,tabID)
	{
	
	var div=document.getElementById('tabView' + mainContainerID + "_" + tabID);
	if(div!=null)
	{
	div.getElementsByTagName('DIV')[0].style.display='none';
	div.getElementsByTagName('IFRAME')[0].style.display='block';
	}
	    var frm=document.getElementById(fm_id);  
    if(frm==null)
    return;
   
    var subWeb=document.frames?document.frames[fm_name].document:frm.contentDocument;  
    if(frm != null && subWeb != null){ 
    setTabTitle(mainContainerID,tabID,subWeb.title);
     }
	TuneHeight(fm_name,fm_id,mainContainerID);
	
	}
	
	function TuneHeight(fm_name,fm_id,parentId){  
    var frm=document.getElementById(fm_id);  
    if(frm==null)
    return;
   
    var subWeb=document.frames?document.frames[fm_name].document:frm.contentDocument;  
    if(frm != null && subWeb != null){ 
       frm.style.height = subWeb.documentElement.scrollHeight+"px"; 
       width=document.getElementById(parentId).style.width.replace("px","")-12;
       frm.style.width =width+"px";
    }  
    }
    
   
		function getTabIndexBySrc(src)
	{
	
		for(var prop in dhtmlgoodies_tabObj){
			var divs = dhtmlgoodies_tabObj[prop].getElementsByTagName('DIV');
			for(var no=0;no<divs.length;no++){
				if(divs[no].src==src){
				
				var tmpId = divs[no].id.split('_');						
				return Array(prop,tmpId[tmpId.length-1].replace(/[^0-9]/g,'')/1);
					
			}
		}
		}
		return -1;
		
	}


    function   StrLenthByByte(str)   
  {   
  var   len;   
  var   i;   
  len   =   0;   
  for   (i=0;i<str.length;i++)   
  {   
  if   (str.charCodeAt(i)>255)   len+=2;   else   len++;   
  if(len>12)
  return i;
  }   
  return   str.length;   
  } 
	function setTabTitle(mainContainerID,tabID,title)
	{
	var a=document.getElementById('tabTab' + mainContainerID + "_" +  tabID).getElementsByTagName('SPAN')[0];
	a.innerHTML=title.substring(0,StrLenthByByte(title));
	a.title=title;
	}
	
	function getTabIndexByID(id)
	{
	
		for(var prop in dhtmlgoodies_tabObj){
			var divs = dhtmlgoodies_tabObj[prop].getElementsByTagName('DIV');
			for(var no=0;no<divs.length;no++){
				if(divs[no].id==id){
				
				var tmpId = divs[no].id.split('_');					
				return Array(prop,tmpId[tmpId.length-1].replace(/[^0-9]/g,'')/1);
					
			}
		}
		}
		return -1;
		
	}
	/* Call this function if you want to display some content from external file in one of the tabs 
	Arguments: Title of tab and relative path to external file */
	
	function addAjaxContentToTab(tabTitle,tabContentUrl)
	{
		var index = getTabIndexByTitle(tabTitle);
		if(index!=-1){
			var ajaxIndex = ajaxObjects.length;
			
			tabId = index[1];
			parentId = index[0];
			
			
			ajaxObjects[ajaxIndex] = new AJAX();
			ajaxObjects[ajaxIndex].requestFile = tabContentUrl;	// Specifying which file to get

			ajaxObjects[ajaxIndex].onCompletion = function(){ showAjaxTabContent(ajaxIndex,parentId,tabId); };	// Specify function that will be executed after file has been found
			ajaxObjects[ajaxIndex].runAJAX();		// Execute AJAX function				
			
		}	
	}

	
	
	function deleteTab(tabID,tabIndex,parentId)
	{

		if(tabID){
			var index=getTabIndexByID(tabID);
			if(index!=-1){
				deleteTab(false,index[1],index[0]);
			}
			
		}else if(tabIndex>=0){

			if(document.getElementById('tabTab' + parentId + '_' + tabIndex)){
				var obj = document.getElementById('tabTab' + parentId + '_' + tabIndex);
				var id = obj.parentNode.parentNode.id;
				obj.parentNode.removeChild(obj);
				var obj2 = document.getElementById('tabView' + parentId + '_' + tabIndex);
				obj2.parentNode.removeChild(obj2);
				resetTabIds(parentId);
				//alert(tabIndex);
			if(activeTabIndex[parentId]==tabIndex)
			{
			
				if(tabIndex>0)
				{
				activeTabIndex[parentId]=tabIndex;
				showTab(parentId,tabIndex-1);
				}
				else
				{
				activeTabIndex[parentId]=-1;
				showTab(parentId,"0");
          
				}
				}	
				
//					if(tabView_countTabs[parentId]==0)
//				{
//				createTabWindow(parentId,"welcome","welcome.aspx")
//				}	
			}	
			
	
		}
		

			
		
		
	}
	
	