var SynchOffset;
var NextUpdate;
var CurrentStatus;
var xmlHttp;
var xmlHttpNotepad;
var xmlHttpLeft;
var xmlHttpRight;
var xmlSessionCheck;
var PollTime = 30000;   // Default to 30 seconds, unless we are in a blitz
var CurLinkColor = 0;
var spd=7;
var tm=10;
var LeftCollapsing = false;
var RightCollapsing = false;
var dclr = "w";         // Default to gray (sort of white)
var clr_offset=-136;
var ClockShowing = "";
var ColonShowing = "w";
var colon = "off";
var Rainbow = "ryry";
var UpdCalled = false;
var CacheSet = false;
var CacheStatus = "";
var CacheData = "";
var CacheAlerts = false;
var NowInBlitz = false;
var FirstBlink = false;
var DirtyTime = false;  // Set to true if moderator changes end time

function SetClr(NwClr)
{
    dclr = NwClr;

    if(dclr == "g") clr_offset=0;
    if(dclr == "r") clr_offset=-34;
    if(dclr == "b") clr_offset=-68;
    if(dclr == "y") clr_offset=-102;
    if(dclr == "w") clr_offset=-136;

    return false;
}

// Ajax object to get connection
function GetXmlHttpObject()
{
    var xmlHttp=null;
    try
      {
      // Firefox, Opera 8.0+, Safari
      xmlHttp=new XMLHttpRequest();
      }
    catch (e)
      {
      // Internet Explorer
      try
        {
        xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        }
      catch (e)
        {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
      }
    return xmlHttp;
}

// Check session time every polltime, and refresh if it has changed dramatically
function CheckSessionTime(GetCmd)
{
    xmlSessionCheck=GetXmlHttpObject()
    if (xmlSessionCheck==null)
    {
      alert ("Your browser does not support AJAX!");
      return;
    }
    // Note that we are calling for updates
    UpdCalled = true;

    var url = "blitztimer.php?cmd=" + GetCmd + "&ajx=" + NextUpdate;
    if(document.all.ctxt.value) url += "&ctxt=" + document.all.ctxt.value;
    xmlSessionCheck.onreadystatechange=ProcessSessionTime;
    xmlSessionCheck.open("GET",url,true);
    xmlSessionCheck.send(null);

    window.setTimeout("CheckSessionTime('Full');", PollTime);

}

// Get result of check for session time, and force refresh timer if there has been a significant change
function ProcessSessionTime()
{
    var HasChanged = false;
    var SecondsLeft = NextUpdate - GetTime();

    var StatusChanged = false;
    var NextUpdateChanged = false;
    var TimeDelta = 0;
    var OldTime = 0;


    // Wait until we get to ready state before processing
    if(xmlSessionCheck.readyState == 4)
    {
        // Commands are | delimited, with each command being Cmd~Params
        // Cycle through commands

        var Commands = xmlSessionCheck.responseText.split("|");
        var Cmd;
        var i;

        //alert(xmlSessionCheck.responseText);
        OldTime = document.all.NextUpdate.value;
        for(i=0;i<Commands.length;i++)
        {
            Cmd = Commands[i].split("~");

            //if(Cmd[0] == "Refresh") NextUpdate = Cmd[1];
            if(Cmd[0] == "NextUpdate" && document.all.NextUpdate.value != Cmd[1])
            {
                HasChanged = true;
                NextUpdateChanged = true;
            }
            if(Cmd[0] == "CurrentStatus" && document.all.CurrentStatus.value != Cmd[1])
            {
                HasChanged = true;
                StatusChanged = true;
            }
            if(Cmd[0] == "CacheUpdate" && document.all.CacheUpdate.value != Cmd[1]) CacheSet = false;
            document.all[Cmd[0]].value = Cmd[1];

            //if(Cmd[0] == "update_time" && UpdCalled) alert(Cmd[1]);
        }

        // if status stayed the same, but session time changed, then update should change time
        if(NextUpdateChanged && !StatusChanged)
        {
            TimeDelta = OldTime - document.all.NextUpdate.value;
            //alert(TimeDelta);
            DirtyTime = true;
        }
        /*
        //alert(xmlHttp.responseText);
        // Now resynch, but only if something has changed; and only once per ccall to GetUpdates()
        if(UpdCalled)
        {
            UpdCalled = false;

            if(HasChanged)
            {
                //CacheSet = false;   // We can also reset the cache now
                SynchTime();
            }
            else
                window.setTimeout("SynchTime();", 1000);
        }
//        else
//            if (document.all.CurrentStatus.value == "InSession" || SecondsLeft < 900) window.setTimeout("GetUpdates('Full');", 1000);
        */

    }
}

function ProcessUpdate()
{
    var HasChanged = false;
    var SecondsLeft = NextUpdate - GetTime();

    // Wait until we get to ready state before processing
    if(xmlHttp.readyState == 4)
    {
        // Commands are | delimited, with each command being Cmd~Params
        // Cycle through commands

        var Commands = xmlHttp.responseText.split("|");
        var Cmd;
        var i;

        for(i=0;i<Commands.length;i++)
        {
            Cmd = Commands[i].split("~");

            //if(Cmd[0] == "Refresh") NextUpdate = Cmd[1];
            if(Cmd[0] == "NextUpdate" && document.all.NextUpdate.value != Cmd[1]) HasChanged = true;
            if(Cmd[0] == "CurrentStatus" && document.all.CurrentStatus.value != Cmd[1]) HasChanged = true;
            if(Cmd[0] == "CacheUpdate" && document.all.CacheUpdate.value != Cmd[1]) CacheSet = false;
            document.all[Cmd[0]].value = Cmd[1];

            //if(Cmd[0] == "update_time" && UpdCalled) alert(Cmd[1]);
        }

        //alert(xmlHttp.responseText);
        // Now resynch, but only if something has changed; and only once per ccall to GetUpdates()
        if(UpdCalled)
        {
            UpdCalled = false;

            if(HasChanged)
            {
                //CacheSet = false;   // We can also reset the cache now
                SynchTime();
            }
            else
                window.setTimeout("SynchTime();", 1000);
        }
//        else
//            if (document.all.CurrentStatus.value == "InSession" || SecondsLeft < 900) window.setTimeout("GetUpdates('Full');", 1000);
    }
}

function UpdateCache()
{
    var url;
    var SecondsLeft = NextUpdate - GetTime();

    // Only attempt update if 1) No cache exists, and 2) we are between 5 and 30 seconds left
    if(SecondsLeft < 4 || SecondsLeft > 30) return false;

    xmlHttpRight=GetXmlHttpObject()
    if (xmlHttpRight==null)
    {
      alert ("Your browser does not support AJAX!");
      return;
    }

    if(document.all.CachePID.value < 0)
        url = "inblitz.php?cache=t&gtpn=right";
    else
    {
        url = "/inb_cache/pid" + document.all.CachePID.value + ".htm";
        document.all.CachePID.value = -1;
        //alert("Getting " + url);
    }
    xmlHttpRight.onreadystatechange=ProcessUpdateCache;
    xmlHttpRight.open("GET",url,true);
    xmlHttpRight.send(null);

    return false;
}

function ProcessUpdateCache()
{
    var ParmSplit;

    // Wait until we get to ready state before processing
    if(xmlHttpRight.readyState != 4 || RightCollapsing)
        window.setTimeout("ProcessUpdateCache();", 500);
    else
    {
        // Process cache info
        ParmSplit = xmlHttpRight.responseText.split("|");
        if(CacheAlerts && ParmSplit[0] != CacheStatus) alert("Received Cache Data: " + xmlHttpRight.responseText);
        CacheStatus = ParmSplit[0];
        CacheData = xmlHttpRight.responseText; //ParmSplit[1];
    }
}

function GetUpdates(GetCmd)
{
    xmlHttp=GetXmlHttpObject()
    if (xmlHttp==null)
    {
      alert ("Your browser does not support AJAX!");
      return;
    }
    // Note that we are calling for updates
    UpdCalled = true;

    var url = "blitztimer.php?cmd=" + GetCmd + "&ajx=" + NextUpdate;
    if(document.all.ctxt.value) url += "&ctxt=" + document.all.ctxt.value;
    xmlHttp.onreadystatechange=ProcessUpdate;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);

    //window.setTimeout("GetUpdates();", PollTime);

}

function Blink()
{
    var SecondsLeft = NextUpdate - GetTime();
    var ShowLink = "";

    xmlHttp=GetXmlHttpObject()
    if (xmlHttp==null)
    {
      alert ("Your browser does not support AJAX!");
      return;
    }

    if(document.all.InBlitz.value == "F")
    {
        // If we have a blitz going on, but are not on "InBlitz", show prominent warning / link now
        if(document.all.CurrentStatus.value == "InSession" || SecondsLeft < 120)
        {
            // Force Redirect to blitz if we haven't done that yet and it is within 30 seconds
            if(document.all.EventRedirected.value != document.all.JobID.value &&
              (document.all.CurrentStatus.value == "InSession" || SecondsLeft <= 30))
            {
                document.all.EventRedirected.value = document.all.JobID.value;
                window.parent.location.href = "/app/blitz/inblitz.php?evtrdr=" + document.all.JobID.value;
            }


            if(!FirstBlink)
            {
                FirstBlink = true;

                if(++CurLinkColor == 2) CurLinkColor = 0;

                var url = "blitztimer.php?ajxgetphonestatus=t";
                xmlHttp.onreadystatechange=AJXShowHelpLinkReturn;
                xmlHttp.open("GET",url,true);
                xmlHttp.send(null);

            }
        }
        else
            window.parent.document.all.BlitzName.innerHTML = "";
    }

}

function AJXShowHelpLinkReturn()
{
    var UsrOnBlitzLine;

    // Wait until we get to ready state before processing
    if(xmlHttp.readyState != 4)
        window.setTimeout("AJXShowHelpLinkReturn();", 500);
    else
    {
        // Get status & new html
        UsrOnBlitzLine = xmlHttp.responseText;

        var LinkColor = "#6D1018";

        var BlitzLink = "<b><span class=title><span style=\"font-size: 18px\"><a target=_top href=/app/blitz/inblitz.php?jid=" + document.all.JobID.value + "><font color=" + LinkColor + ">*** Go To Event Now ***</a></span></span></b></font>";

        // JDURSO 1/30/9 - If user not logged in, give link to list alternate lines
        if(UsrOnBlitzLine == 'F')
            ShowLink = "<b><span style=\"font-size: 21px\"><font color=" + LinkColor + ">*** Trouble Dialing in? <a href=# onClick=\"return ShowAltLines('" + document.all.AltBlitzLines.value + "', true);\">Click Here</a></font></span>";
        else
            ShowLink = BlitzLink;

        window.parent.document.all.BlitzName.innerHTML = ShowLink;
    }
    return false;
}

function ChangeLeft(NewStatus)
{
    // Only process if this is a change in status:
    if(NewStatus != window.parent.document.all.left_status.value)
    {
        xmlHttpLeft=GetXmlHttpObject()
        if (xmlHttpLeft==null)
        {
          alert ("Your browser does not support AJAX!");
          return;
        }
        var url = "inblitz.php?gtpn=left&" + NewStatus;
        xmlHttpLeft.onreadystatechange=ProcessChangeLeft;
        xmlHttpLeft.open("GET",url,true);
        xmlHttpLeft.send(null);
    }

    return false;
}

function RefreshLeft()
{
    xmlHttpLeft=GetXmlHttpObject()
    if (xmlHttpLeft==null)
    {
      alert ("Your browser does not support AJAX!");
      return;
    }
    var url = "inblitz.php?gtpn=left&" + window.parent.document.all.left_status.value;
    xmlHttpLeft.onreadystatechange=ProcessChangeLeft;
    xmlHttpLeft.open("GET",url,true);
    xmlHttpLeft.send(null);
}

function ProcessChangeLeft()
{
    var ParmSplit;

    // Wait until we get to ready state before processing
    if(xmlHttpLeft.readyState != 4)
        window.setTimeout("ProcessChangeLeft();", 500);
    else
    {
        // Get status & new html
        ParmSplit = xmlHttpLeft.responseText.split("|");

        window.parent.document.all.left_status.value = ParmSplit[0];

        window.parent.document.all.inblitz_left.style.display = "";
        window.parent.document.all.inblitz_left.innerHTML = ParmSplit[1];
    }
}

function RefreshRight()
{
    xmlHttpRight=GetXmlHttpObject()
    if (xmlHttpRight==null)
    {
      alert ("Your browser does not support AJAX!");
      return;
    }
    var url = "inblitz.php?gtpn=right&" + window.parent.document.all.right_status.value;
    xmlHttpRight.onreadystatechange=ProcessChangeRight;
    xmlHttpRight.open("GET",url,true);
    xmlHttpRight.send(null);

    return false;
}

function ChangeRight(NewStatus, Clpse)
{
    // Only collapse if right is changing
    if(NewStatus != window.parent.document.all.right_status.value)
    {
        // Start collapsing right side, unless we just loaded the page
        if(Clpse) Collapse("right");
    }

    // If we have the profile cached, handle that now
    if(CacheStatus == NewStatus)
    {
        if(CacheAlerts) alert("Using Cached version for " + CacheStatus);
        window.setTimeout("DoProcessChangeRight(CacheData);", 1000);
    }
    else
    {
        if(CacheAlerts) alert("Getting new version for " + NewStatus);

        xmlHttpRight=GetXmlHttpObject()
        if (xmlHttpRight==null)
        {
          alert ("Your browser does not support AJAX!");
          return;
        }
        var url = "inblitz.php?gtpn=right&" + NewStatus;
        xmlHttpRight.onreadystatechange=ProcessChangeRight;
        xmlHttpRight.open("GET",url,true);
        xmlHttpRight.send(null);
    }
    return false;
}

function ProcessChangeRight()
{
    // Wait until we get to ready state before processing
    if(xmlHttpRight.readyState != 4 || RightCollapsing)
        window.setTimeout("ProcessChangeRight();", 500);
    else
        DoProcessChangeRight(xmlHttpRight.responseText);
}

function DoProcessChangeRight(RightStuff)
{
    var ParmSplit;
    var Msg = "";

    // Get status & new html
    ParmSplit = RightStuff.split("|");

//    if(ParmSplit[0] == CacheStatus) Msg = "CACHED<br>";

    window.parent.document.all.right_status.value = ParmSplit[0];

    window.parent.document.all.inblitz_right.style.display = "";
    window.parent.document.all.inblitz_right.style.visibility = "";
    window.parent.document.all.inblitz_right.style.height = "auto";

    //Expand("right", ParmSplit[1]);
    window.parent.document.all.inblitz_right.innerHTML = Msg + ParmSplit[1];

    //alert(ParmSplit[1]);
}

var s=3;
var t=3;

function Collapse(Side)
{
    var dv = window.parent.document.getElementById("inblitz_" + Side);

    dv.s = s;
    dv.t = t;
    dv.maxh =sh(dv);
    //dv.style.display='none';
    dv.style.overflow='hidden';

    dv.t=setInterval('ct("inblitz_'+Side+'")',t);

    // Set Collapsing flag
    if(Side == "right")
        RightCollapsing = true;
    if(Side == "left")
        LeftCollapsing = true;

    return false;
}

function Expand(Side, SideHTML)
{
    var dv = window.parent.document.getElementById("inblitz_" + Side);

    dv.style.display = "";
    dv.style.visibility = "hidden";
    dv.style.height = "auto";
    dv.innerHTML = SideHTML;
    var MaxHeight = dv.offsetHeight;
    dv.style.visibility = "";
    dv.style.height = "5";
    //alert(MaxHeight);

    ExpandTimer(Side, 5, MaxHeight);
    //alert (dv.offsetHeight);

    return false;
}

function ExpandTimer(Side, Height, MaxHeight)
{
    var dv = window.parent.document.getElementById("inblitz_" + Side);

    dv.style.height = Height;
    if(Height < MaxHeight)
    {
        NewHeight = Height + 20;
        if(NewHeight > MaxHeight) NewHeight = MaxHeight;
        window.setTimeout("ExpandTimer('" + Side + "', " + NewHeight + ", " + MaxHeight + ");", 10);
    }
}

//**********************************************************************************************************************
//**********************************************************************************************************************
//**********************************************************************************************************************

// Prototype Method to get the element based on ID
function $(d){
	return window.parent.document.getElementById(d);
}

// set or get the current display style of the div
function dsp(d,v){
	if(v==undefined){
		return d.style.display;
	}else{
		d.style.display=v;
	}
}

// set or get the height of a div.
function sh(d,v){
	// if you are getting the height then display must be block to return the absolute height
	if(v==undefined){
		if(dsp(d)!='none'&& dsp(d)!=''){
			return d.offsetHeight;
		}
		viz = d.style.visibility;
		d.style.visibility = 'hidden';
		o = dsp(d);
		dsp(d,'block');
		r = parseInt(d.offsetHeight);
		dsp(d,o);
		d.style.visibility = viz;
        return r;
	}else{
		d.style.height=v;
	}
}

//Collapse Timer is triggered as a setInterval to reduce the height of the div exponentially.
function ct(dv){
    d = $(dv);
	if(sh(d)>0){
		v = Math.round(sh(d)/d.s);
		v = (v<1) ? 1 :v ;
		v = (sh(d)-v);
		sh(d,v+'px');
		//d.style.opacity = (v/d.maxh);
		//d.style.filter= 'alpha(opacity='+(v*100/d.maxh)+');';
        if(v < 15)
        {
    		sh(d,0);
    		dsp(d,'none');
    		clearInterval(d.t);

            // Clear Collapsing flag
            if(dv == "inblitz_right") RightCollapsing = false;
            if(dv == "inblitz_left") LeftCollapsing = true;
        }
	}else{
		sh(d,0);
		dsp(d,'none');
		clearInterval(d.t);
	}
}
//**********************************************************************************************************************
//**********************************************************************************************************************
//**********************************************************************************************************************


function ChangeNotepad(NewStatus)
{
    // Only process if this is a change in status:
    if(NewStatus != window.parent.document.all.notepad_status.value)
    {
        xmlHttpNotepad=GetXmlHttpObject()
        if (xmlHttpNotepad==null)
        {
          alert ("Your browser does not support AJAX!");
          return;
        }
        var url = "inblitz.php?gtpn=notepad&" + NewStatus;
        xmlHttpNotepad.onreadystatechange=ProcessChangeNotepad;
        xmlHttpNotepad.open("GET",url,true);
        xmlHttpNotepad.send(null);
    }

    return false;
}

function ProcessChangeNotepad()
{
    var ParmSplit;

    // Wait until we get to ready state before processing
    if(xmlHttpNotepad.readyState != 4)
        window.setTimeout("ProcessChangeNotepad();", 500);
    else
    {
        // Get status & new html
        ParmSplit = xmlHttpNotepad.responseText.split("|");

        window.parent.document.all.notepad_status.value = ParmSplit[0];

        if(ParmSplit[1] == "0")
            window.parent.document.all.inblitz_notepad.style.display = "none";
        else
        {
            window.parent.document.all.inblitz_notepad.style.display = "";
            window.parent.document.all.inblitz_notepad.innerHTML = ParmSplit[1];
        }
    }
}

function SetupTimers()
{
    window.setTimeout("CheckSessionTime('Full');", PollTime);

    return SynchTime();
}

function SynchTime()
{
    var PaneVal;

    // Synchronize time between client and server
	var tm = Math.round(new Date().getTime()/1000.0);

    SynchOffset = tm - document.all.SvrTime.value;
    NextUpdate = document.all.NextUpdate.value;
    CurrentStatus = document.all.CurrentStatus.value;

    // If we are setting a Cache Update request, do that now
    if(!CacheSet && document.all.CacheUpdate.value > -1)
    {
        window.setTimeout("UpdateCache();", (document.all.CacheUpdate.value * 1000));
        document.all.CacheUpdate.value = -1;
        CacheSet = true;
    }

    // If we are in session, update polling time to 10 seconds
    if(document.all.CurrentStatus.value == "InSession")
        PollTime = 10000;

    // If we are InBlitz, initialize panes now:
    if(document.all.InBlitz.value == "T")
    {
        NowInBlitz = true;

        // Init notepad if requested (try parent, then local)
        if(window.parent.document.all.notepad_init.value != "")
        {
            PaneVal = window.parent.document.all.notepad_init.value;
            window.parent.document.all.notepad_init.value = "";
            ChangeNotepad(PaneVal);
        }
        else if(document.all.notepad_init.value != "")
        {
            PaneVal = document.all.notepad_init.value;
            document.all.notepad_init.value = "";
            ChangeNotepad(PaneVal);
        }

        // Init left if requesed
        if(window.parent.document.all.left_init.value != "")
        {
            PaneVal = window.parent.document.all.left_init.value;
            window.parent.document.all.left_init.value = "";
            ChangeLeft(PaneVal);
        }
        else if(document.all.left_init.value != "")
        {
            PaneVal = document.all.left_init.value;
            document.all.left_init.value = "";
            ChangeLeft(PaneVal);
        }

        // Init right if requesed
        if(window.parent.document.all.right_init.value != "")
        {
            PaneVal = window.parent.document.all.right_init.value;
            window.parent.document.all.right_init.value = "";
            ChangeRight(PaneVal, false);
        }
        else if(document.all.right_init.value != "")
        {
            PaneVal = document.all.right_init.value;
            document.all.right_init.value = "";
            ChangeRight(PaneVal, true);
        }
    }
    else
    {
    }

    //window.setTimeout("GetUpdates();", PollTime);

    // Now update time every second
    UpdateBlitzTime();
}

function UpdateBlitzTime()
{
    // First get time
    var SvTime = GetTime() * 1000;
//MDS //   var OffsetTime = SvTime + (parseInt(document.all.TimeOffset.value) * 1000);
    var OffsetTime = SvTime; // + (parseInt(document.all.TimeOffset.value) * 1000);
    var Dt = new Date(SvTime);
    var DtOffset = new Date(OffsetTime);
    var SecondsLeft;
    var TimeLeft;
    var UpdatePoll = 500;

    Blink();

    //Dt.getHours() + ":" + Dt.getMinutes() + ":" + Dt.getSeconds();
    //document.all.CurTime.innerHTML = ShowTime;

    // How long until next update?
    SecondsLeft = NextUpdate - GetTime();
    TimeLeft = GetTimeString(SecondsLeft);

    // If right refresh requested, do that now
    if(NowInBlitz)
    {
        if(window.parent.document.all.ForceRightRefresh.value == 'T')
        {
            window.parent.document.all.ForceRightRefresh.value = 'F';
            RefreshRight();
        }
    }

    // Only show standard clock if we are not in blitz and no blitz is coming up within 10 minutes
    if(SecondsLeft > 600 && document.all.CurrentStatus.value != "InSession")
    {
        dclr = "w";
        SetClr(dclr);
        // Now translate to standard format
        var ShowTime = ConvTime(DtOffset.getHours(), DtOffset.getMinutes(), DtOffset.getSeconds());

        ShowClock(ShowTime, true);

        SetCurMsg("<b>Your Local Time<!-- (" + document.all.ZoneCode.value + ")-->:</b>");
    }
    else
    {
        // If not in session show that countdown
        if(document.all.CurrentStatus.value != "InSession")
        {
            dclr = "b";
            SetClr(dclr);

            // Set current message
            SetCurMsg("<font color=#6D1018><b>Your Next Session:</b></font>");
            //alert(TimeLeft);
            ShowClock(TimeLeft, true);
        }
        else
        {
            // Show appropriate clock color based on session time left
            SetCurMsg("<font color=#6D1018><b>Time Remaining:</b></font>");
            if(SecondsLeft > 60)
                dclr = "g";
            else
            {
                if(SecondsLeft > 30)
                    dclr = "y";
                else
                {
                    if(SecondsLeft > 15)
                        dclr = "r";
                    else
                    {
                        if(dclr == "r")
                            dclr = "y";
                        else
                            dclr = "r";
                    }
                }
            }
            SetClr(dclr);
            if(SecondsLeft > 15)
                ShowClock(TimeLeft, true);
            else
            {
                ShowClock(TimeLeft, false);
                UpdatePoll = 250;
            }
        }

        // Make funky background color changes happen
        //SetColors(document.all.CurrentStatus.value, SecondsLeft);

        // Note: if we are at 0 or below, force refresh now
        if(SecondsLeft < 1)
        {
            //window.location.reload();
            GetUpdates("Full");
            return false;
        }

        // If we are in a countdown, show that also
        if(CurrentStatus != "None")
        {
            // Only show countdown if we are less than
            if(document.all.CurrentStatus != "InSession")
            {
            }
            //document.all.Countdown.innerHTML = TimeLeft;
        }

    }

    if(!DirtyTime)
        window.setTimeout("UpdateBlitzTime();", UpdatePoll);
    else
    {
        DirtyTime = false;
        SynchTime();
    }
}

// SetCurMsg - Sets the message as appropriate, unless it is already set
function SetCurMsg(NewMsg)
{
    if(document.all.CurMsg.innerHTML == NewMsg)
        return false;

    document.all.CurMsg.innerHTML = NewMsg;
}

// SetColors - Set screen background color based on status and seconds left
function SetColors(Stat, Sec)
{
    // If in a blitz session:
    if(Stat == "InSession")
    {
        if(Sec == 15 || Sec == 10 || Sec == 5 || Sec == 1) document.bgColor = "#ff0000";
        if(Sec == 14 || Sec == 9 || Sec == 4) document.bgColor= "#ffffff";
    }

    // If upcoming blitz session
    if(Stat == "UpcomingSession" || Stat == "UpcomingBlitz")
    {
        if(Sec == 10 || Sec == 5 || Sec == 1) document.bgColor = "#00ff00";
        if(Sec == 9 || Sec == 4) document.bgColor= "#ffffff";
    }
}

function ConvTime(THours, TMins, TSecs)
{
    var ampm = ((THours >= 12) ? " PM" : " AM");
    var hours = ((THours == 0) ? "12" : (THours > 12) ? THours - 12 : THours);
    var minutes = ((TMins < 10) ? "0" + TMins : TMins);
    var seconds = ((TSecs < 10) ? "0" + TSecs : TSecs);
    var cvtm;

    hours = ((hours < 10) ? "0" + hours : hours);

    if(THours > 0)
        cvtm = hours + ":" + minutes;
    else
        cvtm = minutes + ":" + seconds;

    //return (hours + ":" + minutes + ":" + seconds + ampm + " <i>(" + document.all.ZoneCode.value + ")</i>");
    return cvtm.replace(":", "");

}

function ShowClock(Digits, ChangeColon)
{
    var i, j;

    if( (dclr + Digits) == ClockShowing)
        return false;

    //alert("Here");

    ClockShowing = dclr + Digits;

    // Now show each digit
    for(i=0;i<4;i++)
    {
        j = parseInt(Digits.substr(i,1));
        //alert(j);
        document.getElementById("dg" + (i+1)).style.backgroundPosition = ((-240*i) + (-24 * j)) + "px " + clr_offset + "px";
    }

    if(!ChangeColon) return false;

    // Update colon if applicable
    if(ColonShowing != dclr)
    {
        ColonShowing = dclr;
        document.getElementById("dg_c").setAttribute("class", "bt_" + ColonShowing + "colon");
        document.getElementById("dg_c").setAttribute("className", "bt_" + ColonShowing + "colon");
    }
}

function ShowClockRainbow(Digits, ChangeColon)
{
    var i, j, k;

    ClockShowing = "";

    // Now show each digit
    for(i=0;i<4;i++)
    {
        j = parseInt(Digits.substr(i,1));
        dclr = Rainbow.substr(i,1);
        SetClr(dclr);

        document.getElementById("dg" + (i+1)).style.backgroundPosition = ((-240*i) + (-24 * j)) + "px " + clr_offset + "px";
    }
    k = Rainbow.substr(0,1);
    Rainbow = Rainbow.substr(1,3) + k;
}


function GetTime()
{
    // First calculate client time
    var tm = Math.round(new Date().getTime()/1000.0);

    return tm - SynchOffset;
}

// Convert seconds into days, hours, minutes, seconds
function GetTimeString(Tm)
{
    TmString = "";
    CurTm = Tm;
    var minutes, seconds;

    // First determine if it is days
    if(CurTm >= 86400)
    {
        TmString += parseInt(CurTm / 86400) + ":";
        CurTm -= (parseInt(CurTm / 86400) * 86400);
    }

    // Do we have hours?
    //if(CurTm >= 3600)
    if(CurTm >= 6000)
    {
        if(TmString != "" && CurTm < 36000) TmString += "0";
        TmString += parseInt(CurTm / 3600) + ":";
        CurTm -= (parseInt(CurTm / 3600) * 3600);
    }
    else
        if(TmString != "") TmString += "00:";

    // Do we have minutes?
    if(CurTm >= 60)
    {
        if(TmString != "" && CurTm < 600) TmString += "0";
        TmString += parseInt(CurTm / 60) + ":";

        minutes = parseInt(CurTm / 60);
        if(minutes < 10) minutes = "0" + parseInt(CurTm / 60);

        CurTm -= (parseInt(CurTm / 60) * 60);
    }
    else
    {
        minutes = "00";
        if(TmString != "") TmString += "00:";
    }

    // Now add seconds to end
    seconds = CurTm;
    if(seconds < 10) seconds = "0" + CurTm;
    //if(TmString != "" && CurTm < 10) TmString += "0";
    //TmString += CurTm;

    //return TmString;
    TmString = minutes + ":" + seconds;

    //if(Tm < 60) alert(TmString);

    return TmString.replace(":", "");


}

