//WARNING: TAB SYSTEM WITH SCROLLING CONTENT IS A SINGLE INSTANCE VERSION

////////////////////////////////////// SCROLL PANE CODE
//Scroll Pane by TelSim Software
var spScrollAmount          = 1;      //Amount to scroll per tick in pixels
var spScrollTick            = 50;     //Tick interval in milliseconds
var spInterval              = 0;      //Handle to the interval
var spStartPoint            = "0px";  //Starting point of scroll pane
var scrollIsNeeded          = false;  //Boolean value for whether or not the scrolling is necessary (bounds exceeded)
var spBuffer                = 50;     //Extra space in pixels at the end of a scroll before it starts over again (this used 2x)
var spStartHandle						= 0;			//Handle for timeout for first starting the scrolling session
var spStartTimeout					= 1000;		//Ticks for start timeout

//Quick function that returns the handle of an element
function GetElement(szElementName)
{
  return document.all ? document.all[szElementName] : document.getElementById(szElementName);
}

function initStartPoint()
{
  var spw = GetElement("scroll-pane-window");
  var spm = GetElement("scroll-pane-mask");
  
  spw.style.top = spStartPoint;
  
  //Only if the text is greater than...
  scrollIsNeeded = spw.offsetHeight > spm.offsetHeight;
}

function ScrollIt(iAmount)
{

    //Short-named element references
    var spw = GetElement("scroll-pane-window");
    var spm = GetElement("scroll-pane-mask");

    //Private function - using the interval method, this function scrolls the pane
    var previousY = String(document.getElementById("scroll-pane-window").style.top);
    previousY = parseInt(previousY.substring(0,previousY.length-2));

    //Do the scrolling
    spw.style.top = String((previousY + parseInt(iAmount)) + "px");

    //Determine the positions
    var curPos = ~parseInt(GetElement("scroll-pane-window").style.top);
    var maxPos = (spw.offsetHeight + spBuffer);
    
    //No debug in this VERSION
    
    //Check to see if current position is greater than or equal to maximum position
    if ( curPos >= maxPos )
    {
      //Start over because we have reached the end of the scrolling session
      spw.style.top = String((spm.offsetHeight + spBuffer) + "px")
    }
}

function ClearScrollInterval(spDirection)
{
		if ( spInterval != 0 )
		{
		  //Clear the interval because the direction is 0
		  clearInterval(spInterval);
		  spInterval = 0;
		}
}

function ScrollPane(spDirection)
{
  //Check to see if the bounds were exceeded
  if ( scrollIsNeeded )
  {
  		ClearScrollInterval();
  		
      //Public function
      if ( spDirection != 0 )
      {
        //Start the interval
        spInterval = setInterval("ScrollIt("+(spDirection*spScrollAmount)+")", spScrollTick);
      }
      else
      {
				
      }  
  }
}

////////////////////////////////////// TAB CONTROL CODE
//Tab Control by TelSim Software
var thisTab;
var thisTab = []; thisTab[0] = -1, thisTab[1] = -1; 

var TC_CREATED = 0;
var isMoz = ( (typeof(XMLHttpRequest) != 'undefined') && !document.all ? true : false );
var isIE = ((window.ActiveXObject && !navigator.__ice_version)? true : false);

var today = new Date();
var hour = today.getHours();
var day = today.getDay();
var lns = 0;
var PLAY = 1, PAUSE = 2, DISABLE = 0; //TC autotab flags
var TC=new Array();

function AddTabControl(iFile,par)
{
	TC.push(new TabControl(iFile,par)); 
}
function getFileObj()
{
	if( isMoz )
	{
		try 
		{
			var r = new XMLHttpRequest();
			if ( r.overrideMimeType ){ r.overrideMimeType('text/xml'); }
			return r;
		}
		catch(e)
		{
			outError(e);
		}
	}		
	else
	{
		var axo = ['Msxml2.XMLHTTP.6.0', 'Msxml2.XMLHTTP.4.0', 'Msxml2.XMLHTTP.3.0', 'Msxml2.XMLHTTP', 'Microsoft.XMLHTTP'], i;
		for( i=0; i < axo.length; i++ )
		{
			try 
			{ 
				return new ActiveXObject(axo[i]); 
			}
			catch(e)
			{
				outError("Attempting to use "+axo[i]+" - "+e);
			}
		} 
	}
	return null;
}

function getSingleNode(nd,nn)
{
	try {
		var q = isIE?nd.selectSingleNode(nn):nd.getElementsByTagName(nn)[0];
		return q;
	} catch ( e ){ return null;}
}

function retAttrib(nd,att,def)
{
	if (typeof(def)=="undefined") def=null;
	try {
		var q = isIE?nd.attributes.getNamedItem(att).text:nd.getAttribute(att);
		return q ;
	} catch ( e ){ return def; }
}

function getNodeValue(nd,def)
{
	if (typeof(def)=="undefined") def=null;
	try {
		var q = isIE?nd.text:nd.childNodes.length==0?nd.nodeValue:nd.firstChild.nodeValue;
		return q ;
	} catch ( e ){ return def; }
}

function processXML(d, TCObject){
	try {
		var q, i;
		var x = getSingleNode(d,"tabbox");
		TCObject.xmlObj = x;
		TCObject.hasSource = true;
		TCObject.labelNode = getSingleNode(x, 'label');
		TCObject.tabsNode = getSingleNode(x, 'tabs');
		TCObject.h = retAttrib(x, 'contentheight',0);
		TCObject.w = retAttrib(x, 'width',0);
		TCObject.caption = getNodeValue(TCObject.labelNode,TCObject.caption);
		TCObject.loopDelay = retAttrib(x, 'autotabdelay',0) * 1000; 
		TCObject.allTabs = TCObject.tabsNode.getElementsByTagName('tab');
		TCObject.Create();
	}catch(e){ outError(v)}
}

function getDocument(szfile, isXML, TCObject){
	
	
	var xhttp = getFileObj();
	if ( xhttp == null ) 
	{
		alert("Could not create XML object");
		return null;
	}
	
	try
	{
		xhttp.open("GET", szfile, false);
		xhttp.send(null);
	}
	catch (e)
	{
		outError(e);
	}
	
	var x;
	
	
	if ( isMoz ){
		x = document.implementation.createDocument("", "", null);
		x.ignorewhitespace;
		x.load(szfile);
		x.onload=function()
		{ 
			if ( isXML ) processXML(x, TCObject); 
		}
	}
	else if ( isIE ){
		
		try { 
			x = new ActiveXObject("Msxml2.DOMDocument");
		} 
		catch (e) {
			try { 
				x = new ActiveXObject("Microsoft.DOMDocument");
			} catch (e) {outError(v) }  
		}

		if ( isXML ){
			x.loadXML(xhttp.responseText);
			processXML(x, TCObject);
		}
	}
	
	return xhttp.responseText;

}

function enumObject(x,lev)
{
	if ( lev > 10 || lns>1000 ) 
	var st = "",xn,w,wv,wn;
	for (xn in x)
	{
		w=null;
		wv=null;
		try
		{
			wv=x[xn];
			w=typeof(wv);
		}
		catch(e)
		{
			wv=wv==null?"<font color=#FF0000>?</font>":wv;
			w=w==null?"<font color=#FF0000>?</font>":w;
		}
		st=st+repChar(":",lev)+"["+xn+"]{"+w+"}"+wv+"<hr>";
		if (w!="function" && wv!="?" && !isNaN(wv)) st=st+enumObject(xn,lev+1);
		lns++;
		if ( lev > 10 || lns>1000 ) break;
  }
  if (lev==0) lns=0;
	return st;
}

function enumNode(x, lev)
{
	if ( lev > 10 || lns>1000 ) return "";
	var st = "";
	for (var n=0;n<x.childNodes.length;n++)
	{
		var xn=x.childNodes[n];
		st=st+repChar(":",lev)+"["+xn.nodeType+"]"+(xn.tagName==null?xn.nodeName:xn.tagName)+"="+(xn.nodeValue==null?"":xn.nodeValue)+","+(xn.nodeText==null?"":xn.nodeText)+"\n";
		st=st+enumNode(xn,lev+1);
		lns++;
		if ( lev > 10 || lns>1000 ) break;
  }
  if (lev==0) lns=0;
	return st;
}

function repChar(c, n)
{
	var st = "";
	for ( var ct = 0; ct < n; ct++ ) st += c;
	return st;
}

function showContentsById(i1,i2){showContents(TC[i1],i2);}
function showContents(tb, intTID){
		//hide content of all tabs
		try{
			if ( typeof(tb)=="undefined" || intTID < 0 )return;
			var e, p;
			
			//if (e = document.getElementById("TC"+tb.TCID+"_CONTENT"))e.innerHTML = "&nbsp;";
			
			if (e = document.getElementById("scroll-pane-window"))e.innerHTML = "&nbsp;";

			for ( var i = 0; i < tb.allTabs.length; i++ ){
				if ( p = document.getElementById("TC"+tb.TCID+"_"+i+"L") )p.className = "TabLeft";
				if ( p = document.getElementById("TC"+tb.TCID+"_"+i+"C") )p.className = "Tab";
				if ( p = document.getElementById("TC"+tb.TCID+"_"+i+"R") )p.className = "TabRight";
			}
	
			//hilite this tab and show its contents
			//alert(enumObject(tb.tabs[intTID]));
			
			//e.style.overflow = "hidden";
			e.innerHTML = tb.tabs[intTID].contents;
			
			if ( p = document.getElementById("TC"+tb.TCID+"_"+intTID+"L") )p.className = "TabLeftSel";
			if ( p = document.getElementById("TC"+tb.TCID+"_"+intTID+"C") )p.className = "TabSel";
			if ( p = document.getElementById("TC"+tb.TCID+"_"+intTID+"R") )p.className = "TabRightSel";
			tb.isReady = true;
			tb.tabCounter = intTID;
			updateWindow = true;
			//Start scroll feature

			ScrollPane(0);
			initStartPoint();
			if ( spStartHandle != 0 ) clearTimeout(spStartHandle);
			spStartHandle = setTimeout('ScrollPane(-1)', spStartTimeout);
			
		} catch ( v ) {outError(v); }
}
function outError(z)
{
  var m="e line: " + z.lineNumber + "<br>";
  m=m + "e.number is: " + (z.number & 0xFFFF) + "<br>";
  m=m + "e.description is: " + z.description + "<br>";
  m=m + "e.name is: " + z.name + "<br>";
  m=m + "e.message is: " + z.message + "<br>";
  alert(m);
}
function alertz(d)
{
	var s=document.getElementById("edisplay").innerHTML;
	document.getElementById("edisplay").innerHTML=s+"<br>"+d;
}
function Tab(){
	var tself = this;
	this.tparent;
	this.label = "[tab]";	
	this.tabID = 0;
	this.tabControlID = 0;
	this.contents = null;

// // // // // // // // // // // // // // // // // // // //
// // // // // // // // // // // // // // // // // // // //
	this.Data = function(){
		var s =  "<TD id=TC"+tself.tabControlID+"_"+tself.tabID+"L class=\'TabLeft\'>&nbsp;</TD>";
				s += "<TD onclick='showContentsById("+tself.tabControlID+", "+tself.tabID+")' id=TC"+tself.tabControlID+"_"+tself.tabID+"C class=Tab>"+tself.label+"</TD>";
				s += "<TD id=TC"+tself.tabControlID+"_"+tself.tabID+"R class=TabRight>&nbsp;</TD>";
		return s;
	}
// // // // // // // // // // // // // // // // // // // //
// // // // // // // // // // // // // // // // // // // //
}

function TabControl (tabFile,par)
{
	//TCxxx_FRAME (ID for tabbox, where xxx is a number)
	if (typeof(par)=="string") par=document.getElementById(par);
	
	if (par==undefined) 
	{
		alert("Tabcontrol with " + tabFile + " could not be created due to parent object non existant.");
		
		return null;
	}
	var tcself = this, q;
	this.containerObj=par;
	this.TCID = TC_CREATED;
  this.tabfile=tabFile;
	this.xmlObj = null;
	this.hasSource = false;
	this.allTabs = [];
	this.tabsNode = null;
	this.labelNode = null;
	this.caption = "[untitled]";
	this.isDrawn = false; //specifies whether tbox frame is drawn on document
	this.isStatic = true; //ignores x/y positioning by default
	this.isReady = false; //is true when tc is ready for another autotab session
	this.loopID = 0;

	//3 sec default. loopDelay is in seconds
	this.loopDelay = 0;
	this.tabs = []; //object collection of all tabs
	this.loopState = PLAY;
	this.tabCounter = 0;
	//set dimensions/positioning in pixels
	this.h = 0;
	this.w =  0;
	//this.x = 0;
	//this.y = 0;
	//this.z = 0;
  
	this.xmlSource =tabFile;

// // // // // // // // // // // // // // // // // // // //
// // // // // // // // // // // // // // // // // // // //
	this.Process = function(){
		if ( !tcself.hasSource )return;
		//this function should be recursive
		if ( tcself.isReady ){ 
			clearTimeout(tcself.loopID); 
			tcself.isReady = false; 
			var hToggle = tcself.Toggle;
			tcself.loopID = setTimeout(hToggle, tcself.loopDelay); 
		}
	};
// // // // // // // // // // // // // // // // // // // //
// // // // // // // // // // // // // // // // // // // //
	function initTabs(){
		var htmlFile, useThisPage = [];

		for ( q = 0; q < tcself.allTabs.length; q++ ){

			var tPageObj = tcself.allTabs[q];
			tPageObj = tPageObj.getElementsByTagName('page');
	
			var tPages = [], tpDays = [], tpHours = [];

			for ( var i = 0; i < tPageObj.length; i++ ){
				tPages[i] = tPageObj[i];
				tpDays[i] = retAttrib(tPageObj[i], 'dow');
				tpHours[i] = retAttrib(tPageObj[i], 'hours');

				var begin, end;

				var hasCorrectHour = false;
				var hasCorrectDay = false;

				// // // // // // // // // // // // // // // // // // // // // // // // //
				//scan through hours 
				// // // // // // // // // // // // // // // // // // // // // // // // //
				begin = 0;
				end = 1;
				var szDays = "", szHours = "";
				
				if ( tpHours[i] != null ){
					var h = tpHours[i].split(",");
					for ( var ct = 0; ct < h.length; ct++ ){
						var hal = h[ct].split("-");
						end = hal[1];
						if ( end == undefined )end = hal[0];
						for ( begin = hal[0]; begin <= end; begin++ ){
							if ( begin == hour ){ 
								hasCorrectHour = true;
								
							} szHours += begin + ",";
						}
					}
				}
				else {
					//this page is available at all hours because 'hours' were not specified
					hasCorrectHour = true;
				}

				// // // // // // // // // // // // // // // // // // // // // // // // //
				//scan through days
				// // // // // // // // // // // // // // // // // // // // // // // // //
				begin = 0;
				end = 1;
				if ( tpDays[i] != null ){
					var h = tpDays[i].split(",");
					for ( var ct = 0; ct < h.length; ct++ ){
						var hal = h[ct].split("-");
						end = hal[1];
						if ( end == undefined )end = hal[0];
						for ( begin = hal[0]; begin <= end; begin++ ){
							if ( begin == day ){ 
								hasCorrectDay = true;
							} szDays += begin + ",";
						}
					}
				}
				else {
					//this page is available on all days because 'days' were not specified
					hasCorrectDay = true;
				}

				var report = "Page Report #"+i+"\nFile: "+getNodeValue(tPages[i])+"\n--------------------------------\n";
				report += "correct day? ("+hasCorrectDay+")\ncorrect hour? ("+hasCorrectHour+")\n";
				report += "Page Days: ("+szDays+")\nPage Hours: ("+szHours+")\n";
				report += "Today\'s Day: "+day+"\nToday\'s Hour: "+hour;
				//alertz(report);
				if ( hasCorrectHour && hasCorrectDay )
				{ 
					useThisPage[q] = getNodeValue(tPages[i]); 
					break;
				}
			}//for loop finding pages

			if ( typeof(useThisPage[q]) == "undefined" )htmlFile = "<center>There is nothing to display at this time.</center>";
			else htmlFile								= useThisPage[q];

			tcself.tabs[q] 							= new Tab();
			tcself.tabs[q].label 				= retAttrib(tcself.allTabs[q], 'label'); 
			tcself.tabs[q].tabID 				= q;
			tcself.tabs[q].tabControlID = tcself.TCID;
			tcself.tabs[q].tparent			= tcself;

			if ( useThisPage[q] == "undefined" )tcself.tabs[q].contents = htmlFile;
			else {
				try { tcself.tabs[q].contents = getDocument(htmlFile); } 
				catch (w){ 
					if ( typeof(useThisPage[q]) != "undefined" ){ 
						tcself.tabs[q].contents = "<span style='width: 100%; font-weight: bold; color: 0000FF'><center><h2 style='color: gray'>Oops!</h2>";
						tcself.tabs[q].contents += "File not found: ";
						tcself.tabs[q].contents += htmlFile;
						tcself.tabs[q].contents += "</center></span>"; 
					}
					else tcself.tabs[q].contents = htmlFile;
				}
			}
		}
	} 
// // // // // // // // // // // // // // // // // // // //
// // // // // // // // // // // // // // // // // // // //
	this.Toggle = function(){
		if ( !tcself.isDrawn )return false; //no autotabbing if frame not drawn
		tcself.loopID = setTimeout(tcself.Toggle, tcself.loopDelay);

		if ( tcself.loopState != DISABLE )tcself.isReady = true;
		if ( tcself.loopState != PLAY )return false;

		if ( tcself.tabCounter >= tcself.allTabs.length )tcself.tabCounter = 0;
		showContents(tcself, tcself.tabCounter);
		tcself.tabCounter++;
		return true;
	};
// // // // // // // // // // // // // // // // // // // //
// // // // // // // // // // // // // // // // // // // //
	this.Create = function(){		
		//draw the framework of the tab box
		if ( !tcself.hasSource ) return;
		initTabs();
		if ( tcself.isDrawn ) return; //no redrawing...
		tcself.isDrawn = true;

		var t = 'onclick="TC['+tcself.TCID+'].loopState = DISABLE;" onmouseover="if (TC['+tcself.TCID+'].loopState != DISABLE) TC['+tcself.TCID+'].loopState = PAUSE" onmouseout="if (TC['+tcself.TCID+'].loopState != DISABLE) TC['+tcself.TCID+'].loopState = PLAY"';
		var s =  '<TABLE '+t+' border=0 id=TC'+tcself.TCID+'_FRAME style="width: '+tcself.w+'" class=TabBox cellspacing=0 cellpadding=0>';
				s += '<TR><TD colspan=3></TD></TR>';
  			s += '<TR class=TabLabelRow>';
				s += '<TD class=TabLabelLeft>&nbsp;</TD>';
				s += '<TD id=TC'+tcself.TCID+'_LABEL class=TabLabel style="WIDTH: '+retAttrib(tcself.labelNode, "width",0)+'px; VERTICAL-ALIGN: middle; TEXT-ALIGN: left;"><h3>'+tcself.caption+'</h3></TD>';
				s += '<TD class=TabLabel style="WIDTH: '+ (tcself.w-retAttrib(tcself.labelNode, "width",0)-10) +'px; VERTICAL-ALIGN: bottom; TEXT-ALIGN: right;"><div class=TabContentDiv align="right">';
				s += '<TABLE class=TabItem cellspacing=0 cellpadding=0><TR>';

				// tabs

				for ( q = 0; q < tcself.allTabs.length; q++ )
					s += tcself.tabs[q].Data();

				s += '</TR></TABLE></div></TD><TD class=TabLabelRight>&nbsp;</TD></TR>';
				s += '<TR><TD style="HEIGHT: 10px;FONT-SIZE: 6px;" class=TabContent colspan=4>&nbsp;</TD></TR>';
				s += '<TR><TD id=TC'+tcself.TCID+'_CONTENT valign="top" style="HEIGHT: '+tcself.h+'px; BORDER-TOP:0px;" class=TabContent colspan=4>';
				s += '<div id="scroll-pane-mask" style="HEIGHT: '+tcself.h+'px; OVERFLOW: hidden;" onmouseover="ScrollPane(0)" onmouseout="ScrollPane(-1)">';
				s += '<span id="scroll-pane-window">';
				
				// tab contents

				s += '</span></div>';
				s += '</TD></TR></TABLE>';

		tcself.containerObj.innerHTML=s;
		tcself.isReady = true;

		if ( tcself.allTabs.length > -1 )
		{
			showContents(tcself, 0);
			if (tcself.loopDelay!=0) tcself.loopID = setTimeout(tcself.Toggle, tcself.loopDelay); 
		}
		tcself.tabCounter++;
	};
// // // // // // // // // // // // // // // // // // // //
// // // // // // // // // // // // // // // // // // // //
	try 
	{
			//getDocument(tabFile, true, tcself);
		
			if ( getDocument(tabFile, true, tcself) == null )throw v;
	}
	catch (v){ alert("File could not be read: '"+tabFile+"'\nThe file may be corrupt or missing."); tcself.xmlObj = null; return; }
	TC_CREATED++;
}	

