﻿function rollInMenu()
{
    var menuDiv = top.frames['frameBody'].document.getElementById('menuDiv');
    
	if (menuDiv != null)
	{
	    menuDiv.style.visibility = 'hidden';
	}
	
	if (top.frames['frameBody'] != null)
        top.frames['frameBody'].eval('showSelects();');
}
function launchCustomUI(customUiSelect)
{
	var value = customUiSelect.value;	
	customUiSelect.selectedIndex=0;
	window.focus();
	childWindowAdd(windowOpen('ASPProxy.asp?ProgID=vcsXRNetUI30.CASPFrameSet&ProcName=XRSubFrameSet_Execute&Title=&SubFrameURL=' + escape(value) + '', '', true, 2, 0)); 
};

function imageOver (img)
{
	if (window.document.images[img].src.indexOf('_On.jpg') < 0) 
	{
	 imgName = window.document.images[img].src.substring(window.document.images[img].src.lastIndexOf('/')+1,window.document.images[img].src.lastIndexOf('_'));
	 window.document.images[img].src = 'images/' + imgName + '_Over.jpg';
	}
};

function imageOut (img)
{
	if (window.document.images[img].src.indexOf('_On.jpg') < 0)
	{
	 imgName = window.document.images[img].src.substring(window.document.images[img].src.lastIndexOf('/')+1,window.document.images[img].src.lastIndexOf('_'));
	 window.document.images[img].src = 'images/' + imgName + '_Off.jpg';
	}
};

function imageClick (img)
{
	if (window.document.images[img].src.indexOf('_On.jpg') < 0)
	{
	 imgName = window.document.images[img].src.substring(window.document.images[img].src.lastIndexOf('/')+1,window.document.images[img].src.lastIndexOf('_'));
	 window.document.images[img].src = 'images/' + imgName + '_On.jpg';
	}
};

function ChangeTab(newTab, url)
{
	// Turn off previous tab
	if (url != "")
	{
		if (top.frames["frameBody"] != null)
		{
			var mainDiv = top.frames["frameBody"].document.getElementById('mainDiv');
			
			if (mainDiv != null)
			{
				mainDiv.style.visibility = 'hidden';
			}
			
			top.frames["frameBody"].location.href = url;
		}
	}
	
	if (currentTab.toUpperCase() != newTab.toUpperCase() && newTab.toUpperCase() != 'COMMITTEE')
	{
		var maxTabIndex = tabsLength - 1;
		var currentTabIndex = GetTabIndex(currentTab);
		var newTabIndex = GetTabIndex(newTab);	
		
		newTab = tabs[newTabIndex];
		
		if (currentTabIndex == 0)
		{
			document.getElementById(currentTab + "_Tab").src = imgTabStartOff;
			document.getElementById(tabs[currentTabIndex + 1] + "_Tab").src = imgTabOffOff;
		}
		else if (currentTabIndex == maxTabIndex)
		{
			document.getElementById(currentTab + "_Tab").src = imgTabOffOff;
			document.getElementById("End_Tab").src = imgTabEndOff;
		}
		else
		{					
			document.getElementById(currentTab + "_Tab").src = imgTabOffOff;
			document.getElementById(tabs[currentTabIndex + 1] + "_Tab").src = imgTabOffOff;
		}
		
		document.getElementById(currentTab + "_Td").style.backgroundImage = "url('" + imgTabBackgroundOff + "')";
				
		if (newTabIndex == 0)
		{
			document.getElementById(newTab + "_Tab").src = imgTabStartOn;
			document.getElementById(tabs[newTabIndex + 1] + "_Tab").src = imgTabOnOff;
		}
		else if (newTabIndex == maxTabIndex)
		{
			document.getElementById(newTab + "_Tab").src = imgTabOffOn;
			document.getElementById("End_Tab").src = imgTabEndOn;
		}
		else
		{					
			document.getElementById(newTab + "_Tab").src = imgTabOffOn;
			document.getElementById(tabs[newTabIndex + 1] + "_Tab").src = imgTabOnOff;
			
		}
		
		document.getElementById(newTab + "_Td").style.backgroundImage = "url('" + imgTabBackgroundOn + "')";
			
		currentTab = newTab;
	}
};

function GetTabIndex(tab)
{
	for (i=0;i<=tabsLength-1;i++)
	{
		if (tabs[i].toUpperCase() == tab.toUpperCase())
		{
			return i;
		}
	}
};

function TimeKeeperLogout(link)
{
	StopTimeKeeping();
	var timeKeeperClock = document.getElementById('timeKeeperClock');
	var tempValue = timeKeeperClock.value;
	var timeKeeperArray = tempValue.split(":");
	var restartHours = parseInt(timeKeeperArray[0]);
	var restartMinutes = parseInt(timeKeeperArray[1]);
	var restartSeconds = parseInt(timeKeeperArray[2]);
	var secondsLoggedAtPause = (restartHours * 3600) + (restartMinutes*60) + restartSeconds;
	
	link = link + "&TimeKeeperValue=" + secondsLoggedAtPause;
	window.parent.top.location.href = link;
};

function LoadTimeKeeper()
{
	if (!timerRunning)
	{
		timerRunning = true;
		//document.getElementById('hidTkAction').value = 'play';
		StartTimeKeeping();
	}
};

function PlayClick(url)
{	 
	document.getElementById('hidTkAction').value = 'play';
	if (window.document.images['playbutton'].src.indexOf('play_onstate') > 0)
	{
		//Do nothing if play button is already active. 
	}
	else if (window.document.images['pausebutton'].src.indexOf('pause_offstate') < 0)
	{
		if (isWhite)
		{
			window.document.images['playbutton'].src = url + 'images/play_onstate_white.gif';
			window.document.images['pausebutton'].src = url + 'images/pause_offstate_white.gif';
			window.document.images['stopbutton'].src = url + 'images/stop_offstate_white.gif';
		}
		else
		{
			window.document.images['playbutton'].src = url + 'images/play_onstate.gif';
			window.document.images['pausebutton'].src = url + 'images/pause_offstate.gif';
			window.document.images['stopbutton'].src = url + 'images/stop_offstate.gif';
		}

		timerRunning = true;
		document.getElementById('hidTkAction').value = 'unpause';
		//RestartTimeKeeping();
		handleAction('unpause');
	}
	else
	{
		if (isWhite)
		{
			window.document.images['playbutton'].src = url + 'images/play_onstate_white.gif';
			window.document.images['pausebutton'].src = url + 'images/pause_offstate_white.gif';
			window.document.images['stopbutton'].src = url + 'images/stop_offstate_white.gif';
		}
		else
		{
			window.document.images['playbutton'].src = url + 'images/play_onstate.gif';
			window.document.images['pausebutton'].src = url + 'images/pause_offstate.gif';
			window.document.images['stopbutton'].src = url + 'images/stop_offstate.gif';
		}

		timerRunning = true;
		document.getElementById('hidTkAction').value = 'play';
		handleAction('play');
		//StartTimeKeeping();
	}
};

function StopClick(url, startError)
{
	 if (window.document.images['stopbutton'].src.indexOf('stop_onstate') > 0)
	 {
		//Do nothing if stop is already active
	 }
	 else if (!((window.document.images['pausebutton'].src.indexOf('pause_onstate') > 0) || (window.document.images['playbutton'].src.indexOf('play_onstate') > 0) || (window.document.images['stopbutton'].src.indexOf('stop_onstate') > 0)))
	 {
		//If none of the buttons are active, do nothing
		document.getElementById('hidTkAction').value = 'stop';

		if (isWhite)
		{
			window.document.images['playbutton'].src = url + 'images/play_offstate_white.gif';
			window.document.images['pausebutton'].src = url + 'images/pause_offstate_white.gif';
			window.document.images['stopbutton'].src = url + 'images/stop_offstate_white.gif';
		}
		else
		{
			window.document.images['playbutton'].src = url + 'images/play_offstate.gif';
			window.document.images['pausebutton'].src = url + 'images/pause_offstate.gif';
			window.document.images['stopbutton'].src = url + 'images/stop_offstate.gif';
		}

		alert(startError);
	 }
	 else
	 {
		document.getElementById('hidTkAction').value = 'stop';

		if (isWhite)
		{
			window.document.images['playbutton'].src = url + 'images/play_offstate_white.gif';
			window.document.images['pausebutton'].src = url + 'images/pause_offstate_white.gif';
			window.document.images['stopbutton'].src = url + 'images/stop_onstate_white.gif';
		}
		else
		{
			window.document.images['playbutton'].src = url + 'images/play_offstate.gif';
			window.document.images['pausebutton'].src = url + 'images/pause_offstate.gif';
			window.document.images['stopbutton'].src = url + 'images/stop_onstate.gif';
		}

		StopTimeKeeping();
		handleAction('stop');
	 }
};

function PauseClick(url, startError, revertError)
{
	 if (window.document.images['pausebutton'].src.indexOf('pause_onstate') > 0)
	 {
		//Do nothing if stop is already active
	 }
	 else if (!((window.document.images['pausebutton'].src.indexOf('pause_onstate') > 0) || (window.document.images['playbutton'].src.indexOf('play_onstate') > 0) || (window.document.images['stopbutton'].src.indexOf('stop_onstate') > 0)))
	 {
		//If none of the buttons are active, do nothing
		document.getElementById('hidTkAction').value = 'stop';

		if (isWhite)
		{
			window.document.images['playbutton'].src = url + 'images/play_offstate_white.gif';
			window.document.images['pausebutton'].src = url + 'images/pause_offstate_white.gif';
			window.document.images['stopbutton'].src = url + 'images/stop_offstate_white.gif';
		}
		else
		{
			window.document.images['playbutton'].src = url + 'images/play_offstate.gif';
			window.document.images['pausebutton'].src = url + 'images/pause_offstate.gif';
			window.document.images['stopbutton'].src = url + 'images/stop_offstate.gif';
		}

		alert(startError);
	 }
	 else if (window.document.images['stopbutton'].src.indexOf('stop_onstate') > 0)
	 {
		//If TimeKeeper is stopped, and user attempts to "pause", alert them and do nothing more. 
		document.getElementById('hidTkAction').value = 'stop';

		if (isWhite)
		{
			window.document.images['playbutton'].src = url + 'images/play_offstate_white.gif';
			window.document.images['pausebutton'].src = url + 'images/pause_offstate_white.gif';
			window.document.images['stopbutton'].src = url + 'images/stop_onstate_white.gif';
		}
		else
		{
			window.document.images['playbutton'].src = url + 'images/play_offstate.gif';
			window.document.images['pausebutton'].src = url + 'images/pause_offstate.gif';
			window.document.images['stopbutton'].src = url + 'images/stop_onstate.gif';
		}

		alert(revertError);
	 }
	 else
	 {
		document.getElementById('hidTkAction').value = 'pause';

		if (isWhite)
		{
			window.document.images['playbutton'].src = url + 'images/play_offstate_white.gif';
			window.document.images['pausebutton'].src = url + 'images/pause_onstate_white.gif';
			window.document.images['stopbutton'].src = url + 'images/stop_offstate_white.gif';
		}
		else
		{
			window.document.images['playbutton'].src = url + 'images/play_offstate.gif';
			window.document.images['pausebutton'].src = url + 'images/pause_onstate.gif';
			window.document.images['stopbutton'].src = url + 'images/stop_offstate.gif';
		}

		//PauseTimeKeeping()
		handleAction('pause');
	 }
};

function handleAction(strAction) 
{
		document.getElementById('hidTkAction').value = strAction;
		//alert(document.getElementById('hidTkAction').value);
		document.headerForm.submit();
};

var	timerID = null;
var	timerRunning = false;
var	startDate;
var	startSecs;
var tempValue = '';
var restart = false;

function StopTimeKeeping()
{
	//Save enddate and reset timer
	if(timerRunning)
	{
		var timeKeeperClock = document.getElementById('timeKeeperClock');
		var tempValue = timeKeeperClock.value;
		timerRunning = false;
		startDate = new Date();
		timeKeeperClock.value = tempValue;
	}
};

function StartTimeKeeping()
{
	startDate = new Date();
	startSecs = (startDate.getHours()*60*60) + (startDate.getMinutes()*60) + startDate.getSeconds();
	ShowTime();
};

function PauseTimeKeeping()
{
	//Save pausedate; on start, recalc
	if(timerRunning)
	{
		var timeKeeperClock = document.getElementById('timeKeeperClock');
		tempValue = timeKeeperClock.value;
		timerRunning = false;
		startDate = new Date();
		timeKeeperClock.value = tempValue;
	}
};

function RestartTimeKeeping()
{
	var timeKeeperArray = tempValue.split(":");
	var restartHours = parseInt(timeKeeperArray[0]);
	var restartMinutes = parseInt(timeKeeperArray[1]);
	var restartSeconds = parseInt(timeKeeperArray[2]);
	
	newStartDate = new Date();
	secondsLoggedAtPause = (restartHours * 3600) + (restartMinutes*60) + restartSeconds;
	var now = new Date();
	startSecs = (now.getHours()*60*60) + (now.getMinutes()*60) + now.getSeconds() - secondsLoggedAtPause;
	timerRunning = true;
	ShowTime();
};

function RestartTimeKeeping(lastReadout)
{
	var timeKeeperArray = lastReadout.split(":");
	var restartHours = parseInt(timeKeeperArray[0]);
	var restartMinutes = parseInt(timeKeeperArray[1]);
	var restartSeconds = parseInt(timeKeeperArray[2]);
	
	newStartDate = new Date();
	secondsLoggedAtPause = (restartHours * 3600) + (restartMinutes*60) + restartSeconds;
	var now = new Date();
	//Subtract 2 extra seconds to make up for the pause in ShowTime().
	startSecs = (now.getHours()*60*60) + (now.getMinutes()*60) + now.getSeconds() - secondsLoggedAtPause;
	timerRunning = true;
	firstLoad = false;
	secondLoad = false;
	restart = true;
	ShowTime();
};

function ToggleTimerRunning(isRunning)
{
	timerRunning = isRunning;
};
function delay(gap)
{ /* gap is in millisecs */
	var then,now; 
	then=new Date().getTime();
	now=then;
	while((now-then)<gap)
	{now=new Date().getTime();}
};

var firstLoad = true;
var secondLoad = true;
function ShowTime()
{
	if (restart)
	{
		setTimeout("ShowTime();",1000);
		restart = false;
		return;
	}
	else if (firstLoad)
    {
		//To give the page time to load...
		setTimeout("ShowTime();",3000);
		firstLoad = false;
		return;
    }
    else if (secondLoad)
    {
		//To reset the clock appropriately...
		setTimeout("ShowTime();",1000);
		secondLoad = false;
		startDate = new Date();
		startSecs = (startDate.getHours()*60*60) + (startDate.getMinutes()*60) + startDate.getSeconds();
		return;
    }
	else if (timerRunning)
	{
		var fiveMinuteUpdate = false;
		var now = new Date();
		var nowSecs = (now.getHours()*60*60) + (now.getMinutes()*60) + now.getSeconds();
		
		var elapsedSecs = nowSecs - startSecs;

		var hours = Math.floor( elapsedSecs / 3600 );
		elapsedSecs = elapsedSecs - (hours*3600);

		var minutes = 	Math.floor( elapsedSecs / 60 );
		
		elapsedSecs = elapsedSecs - (minutes*60);

		var seconds = elapsedSecs;

		var timeValue = "" + hours;
		timeValue  += ((minutes < 10) ? ":0" : ":") + minutes;
		
		switch(minutes)
		{
			case 5:
			case 10:
			case 15:
			case 20:
			case 25:
			case 30:
			case 35:
			case 40:
			case 45:
			case 50:
			case 55:
				if (seconds == 0)
				{
					fiveMinuteUpdate = true;
				}
				else
				{
					fiveMinuteUpdate = false;
				}
				break;
			default:
				fiveMinuteUpdate = false;
				break;
		}
		
		timeValue  += ((seconds < 10) ? ":0" : ":") + seconds;

		if(document.getElementById('timeKeeperClock'))
		{
			var timeKeeperClock = document.getElementById('timeKeeperClock');
			timeKeeperClock.value = timeValue;
			
			if (fiveMinuteUpdate)
			{
				document.getElementById('timeKeeperIFrame').contentWindow.document.getElementById('hidFiveMinUpdate').value = "true";
				document.getElementById('timeKeeperIFrame').contentWindow.document.timeKeeperUpdate.submit();
				fiveMinuteUpdate = false;
			}
			else
			{
				document.getElementById('timeKeeperIFrame').contentWindow.document.getElementById('hidFiveMinUpdate').value = 'false';				
			}
		}
		
		timerID = setTimeout("ShowTime()",1000);
		timerRunning = true;
	}
};