// Tabstrip version 2.0 client-side scripting 

// initialize scrolling context
var cnScrollTimer = 10;		// delay between blocks when scrolling (in milliseconds)
var cnScrollLimit = 10;		// blocks to move per "click"
var cnScrollSize  = 20;		// pixels for each "block"
var cnMinTabSize  = 500;	// pixels for minimum tab size (when reducing, block tab width)

// context variables
var m_sTabStripID, m_sScroller, m_nScrollDelta, m_nMaxScroll;
var m_oScroller, m_oScrollerSX, m_oScrollerDX, m_oScrollerContainer;
var m_bHasNavMenu;
var m_bScrollRequestStop = false;
var m_bScrolling = false;
var m_nCurrentTab = 0;
var m_nScrollLimit = 0;
var m_nLevel = 1;

// V. 1.0 Vars
// -----------
var TabStripArrayVariable = new Array();
var TabStripArrayIndex = 0;
var TabStripArrayVariableName = new Array();
var TabStripArrayVariableValue = new Array();
var TabStripArrayNVIndex = 0;

var TabStripInputActive;
var TabStripFormActive;
var TabStripIDActive;

// v. 2.0 Scripting
// ----------------
function tsInit(sTabStripID)
{
	// sets up client-side tabstrip mgmt		
	m_sTabStripID = sTabStripID;				
	m_sScroller = sTabStripID + '_Scroller';	
	
	// scoller objects
	m_oScroller = document.all(m_sScroller);
	m_oScrollerContainer = document.all(m_sScroller + 'Container');
	m_oScrollerSX = document.all(m_sScroller + 'SX');
	m_oScrollerDX = document.all(m_sScroller + 'DX');
	m_nLevel = document.all('TS_' + sTabStripID + '_LEVEL').value;
	
	// other attributes
	m_oNavMenu = document.all('navMenuContainer')
	m_bHasNavMenu = (m_oNavMenu != null);
	
	m_nMaxScroll = -(m_oScroller.offsetWidth - m_oScroller.parentElement.style.pixelWidth);		
	
	m_nScrollDelta = 0;
	m_nScrollLimit = 0;	
}

function tsResize(sTabStripID)
{
	tsInit(sTabStripID);
					
	// get controls
	var tsTabsContainer = document.all(sTabStripID + '_TabsContainer');
	var ts = document.all(sTabStripID + '_Body');
	var sTemp = '';
	
	// get margins
	sTemp = ts.style.marginLeft;	
	var nMarginLeft = new Number(sTemp.substr(0, sTemp.length - 2));
	sTemp = ts.style.marginRight;	
	var nMarginRight = new Number(sTemp.substr(0, sTemp.length - 2));
					
	// set minimum tabstrip width to cnMinTabSize	
	var nNewWidth = document.body.offsetWidth - (m_bHasNavMenu ? m_oNavMenu.offsetWidth : 0) - (nMarginLeft + nMarginRight) - (m_nLevel * (nMarginLeft + nMarginRight)*2);
	
	// ADJUST WITH MINIMUM CONTENT-SIZE
	if (nNewWidth < cnMinTabSize) 
		nNewWidth = cnMinTabSize;
	
	tsTabsContainer.style.pixelWidth = nNewWidth-32; //ts.style.pixelWidth;
	
	if (nNewWidth < ts.offsetWidth) 
		nNewWidth = ts.offsetWidth;
		
	tsTabsContainer.style.pixelWidth = nNewWidth-32;
	
	m_nMaxScroll = -(m_oScroller.offsetWidth - m_oScroller.parentElement.style.pixelWidth);
		
	if (m_oScroller.style.pixelLeft < m_nMaxScroll) 
		m_oScroller.style.pixelLeft = m_nMaxScroll;
		
	if (m_oScroller.style.pixelLeft > 0) 
		m_oScroller.style.pixelLeft = 0;
	
	tsEnableScrollButtons();	
}

function tsInitialize(sTabStripID)
{
	tsInit(sTabStripID);
	
	// set scrolling position
	m_oScroller.style.pixelLeft = document.all('TS_' + sTabStripID + '_SCROLL').value;
	
	// call resizer twice to ensure context vars initialization.
	tsResize(sTabStripID);
	tsResize(sTabStripID);
}

function tsClick(sTabName, sPage)
{
	var sPageVarName = sTabName + '_TABINDEX';
	var oCurrentPage = document.all(sPageVarName);
	
	oCurrentPage.value = sPage;	
	oCurrentPage.form.submit();
}

function tsEnableScrollButtons()
{
	var bActive = false;
	
	nScroll = m_oScroller.style.pixelLeft;
	
	if (nScroll > m_nMaxScroll)
	{
		bActive = true;
		m_oScrollerDX.className = "tsScrollerActive";
	}
	else
		m_oScrollerDX.className = "tsScrollerInactive";

	if (nScroll < 0)
	{
		bActive = true;
		m_oScrollerSX.className = "tsScrollerActive";
	}
	else
		m_oScrollerSX.className = "tsScrollerInactive";
		
	// show/hide container
	m_oScrollerContainer.style.visibility = (bActive ? 'visible' : 'hidden');
}

function tsScrollTimer()
{		
	// do the actual scrolling
	var nOldScroll = m_oScroller.style.pixelLeft;
	var nNewScroll = nOldScroll + m_nScrollDelta;		
	
	if ((--m_nScrollLimit) == 0)
	{
		m_bScrolling = false;
		return;
	}								

	// do not scroll if already at start-of-tabstrip
	if ((nNewScroll > 0) && (nOldScroll == 0))
	{
		m_bScrolling = false;
		return;
	}
		
	if (nNewScroll > 0) 
		nNewScroll = 0;		
	
	// do not scroll if already at end-of-tabstrip
	if ((nNewScroll < m_nMaxScroll) && (nOldScroll == m_nMaxScroll))
	{
		m_bScrolling = false;
		return;
	}
		
	if (nNewScroll < m_nMaxScroll)
		nNewScroll = m_nMaxScroll;
		
	m_oScroller.style.pixelLeft = nNewScroll;
	document.all('TS_' + m_sTabStripID + '_SCROLL').value = nNewScroll;
	
	tsEnableScrollButtons();
			
	if (!m_bScrollRequestStop)
		window.setTimeout(tsScrollTimer, cnScrollTimer);
	else
		m_bScrolling = false;
}

function tsScrollStop() 
{
	// stop the scroll, please.
	m_bScrollRequestStop = true;
}

function tsScroll(sTabStripID, nScrollDir)
{
	if (m_bScrolling) 			
		return;
		
	tsInit(sTabStripID);
	
	m_nScrollDelta = nScrollDir * cnScrollSize;
	m_nScrollLimit = cnScrollLimit;
							
	m_bScrollRequestStop = false;
	
	window.setTimeout(tsScrollTimer, cnScrollTimer);
	
	m_bScrolling = true;
}

function tsAdjustTip(oTab)
{
	var oImg = document.all(oTab.id + '_TT');
	
	if (oImg != null)
	{
		oImg.style.pixelWidth = oTab.offsetWidth;		
		oImg.style.pixelHeight = oTab.offsetHeight;
	}
}

function tsHover(oTab, bHighlight)
{	
	if (oTab.className != 'tsActive')
	{
		oTab.style.cursor = 'hand';
		
		if (bHighlight)
			oTab.style.textDecoration = 'underline';
		else
			oTab.style.textDecoration = 'none';	
	}
	
	tsAdjustTip(oTab);
}

// V. 1.0 scripting 
// ----------------
		
function tsAction(sTabstrip, strID) {
	
	var count;
	var strval;
	var num;
	var obj;
	var strVarValue;
	var sInner='';

	// get objects
	var objinput = document.all('TS_' + sTabstrip + '_ID');	
	var objform  = document.all('TabStripForm_' + sTabstrip);
	var sHistory = '|';
	var sKey='';
	
	// replace value for "new tabstrip page"
	objinput.value = strID;	
	
	// obtain inner form content
	sInner = objform.innerHTML;	
	
	// store this-form-items on "history".	
	for (count=0; count < objform.childNodes.length; count++)
		sHistory += objform.childNodes[count].id + '|';
		
	// save all dictionary values as hidden fields
	for (count=0;count<TabStripArrayIndex;count++) 
	{			
		strval = '';
		sKey = TabStripArrayVariable[count].toUpperCase();
						
		if (sHistory.indexOf('|' + sKey + '|') < 0) 
		{	
			obj = document.all.item(TabStripArrayVariable[count]);
			
			if (obj !=null) 
			{							
				switch (obj.type) 
				{
					case 'checkbox':
						if (obj.checked) 
							strval = '<input type="hidden" id="ID' + TabStripArrayVariable[count] + '" name="' + TabStripArrayVariable[count] + '" value="' + obj.value + '">';
						else
							strval = '<input type="hidden" id="ID' + TabStripArrayVariable[count] + '" name="' + TabStripArrayVariable[count] + '" value="">';
						break;
						
					case 'select-one':
						if(obj.selectedIndex>=0){
						strVarValue=obj.options(obj.selectedIndex).value;
						strval = '<input type="hidden" id="ID' + TabStripArrayVariable[count] + '" name="' + TabStripArrayVariable[count] + '" value="' + strVarValue + '">';						
						}	
						break;
						
					case 'select-multiple':
						var txttxt;
						txttxt=null;
						for (num=0;num<obj.length;num++) {
							if (obj.options(num).selected==true) {
								if (txttxt!=null) {
									txttxt += ',';
									txttxt += obj.options(num).value;
								} else {
									txttxt = obj.options(num).value;
								}
							}
						}
						strval = '<input type="hidden" id="ID' + TabStripArrayVariable[count] + '" name="' + TabStripArrayVariable[count] + '" value="' + txttxt + '">';
						break;
						
					case null:					
						//è una collezione di oggetti
						switch (obj(0).type) 
						{
							case 'radio':
								obj=document.all.item(TabStripArrayVariable[count]);
								for (num=0; num<obj.length;num++) 
								{
									if (obj(num).checked)
										strval = '<input type="hidden" id="ID' + TabStripArrayVariable[count] + '" name="' + TabStripArrayVariable[count] + '" value="' + obj(num).value + '">';
								}
								break;
								
							default:
								strval = '<input type="hidden" id="ID' + TabStripArrayVariable[count] + '" name="' + TabStripArrayVariable[count] + '" value="' + obj.value + '">';
								break;
						}
						break;
						
					default:
						strVarValue='';
						if (obj.length > 1) 
							strVarValue=obj(0).value;
						else
							strVarValue=obj.value;

						strval = '<input type="hidden" id="ID' + TabStripArrayVariable[count] + '" name="' + TabStripArrayVariable[count] + '" value="' + strVarValue + '">';
						break;
				}				
										
				// remember history
				sHistory += sKey + '|';
				
				// append new "hidden" field
				sInner += strval;
			}					
		}
	}
	
	TabStripArrayIndex=0;
	
	for (count=0;count<TabStripArrayNVIndex;count++) 
	{	
		strval = '<input type="hidden" id="' + TabStripArrayVariableName[count] + '" name="' + TabStripArrayVariableName[count] + '" value="' + TabStripArrayVariableValue[count] + '">';
		sInner += strval;		
	}
		
	TabStripArrayNVIndex=0;
	
	// replace inner form!
	objform.innerHTML = sInner;
	objform.submit();
	
	return;
}


// Add a new variable before submitting a form 
function TabStripAddVariable(txtname,txtvalue) {
	TabStripArrayVariableName[TabStripArrayNVIndex] = txtname;
	TabStripArrayVariableValue[TabStripArrayNVIndex] = txtvalue;
	TabStripArrayNVIndex ++;
	return;
}


// Submits a tabstrip
function TabStripSubmit() {
	tsAction(TabStripFormActive.id.substr(13),TabStripInputActive.value);
	return;
}

// set the minimum tab size (default 500px)
function SetMinTabSize(txtminsize) {
	cnMinTabSize = txtminsize;
}

