﻿        var SanityCheck1 = "";
        var SanityCheck2 = "";
        var SanityCounter = 1;
        var vDdlClasses = -3;
        var clrMsg = "false";
        var tmrCheckLastClassLap;
        var tmrChkClassDrop = "";
        var lastTmrChkClassDrop = 0;
        var classDropCounter = 0;
        var updateInterval = 15000  //frequency of polls
		var serverdate=""
		var tmrUpdateTime = ""
        
        function startUpdates()
        {
          //  check for newer results.
          tmrCheckLastClassLap = setTimeout("CheckLastClassLap()", updateInterval / 2);
           
          //This makes sure we're still updating - only checks the server time, may change this later to check the current laps updater
          setTimeout("fSanityCheck()", 30300);
          
          //This checks that class list hasn't changed due to a new race starting
          //*tmrChkClassDrop = setTimeout("chkClassDrop()", 30750);
          
          //get the server time
          CheckServerTime();
        }
        
        function setClassIndex(x)
        { vDdlClasses = x; }
        
        function fSanityCheck()
        {
        if (SanityCheck1 != SanityCheck2)
            {
            //all is well, we're still updating, make sanityCheck1 = sanityCheck2 (current Time) & wait for the next check
            SanityCheck1 = SanityCheck2;
            SanityCounter = 1;
            }
        else
            {
            //kickstart the updaters & let the cards fall where they may.
            document.getElementById("LblServerTime").innerHTML = "Connection Lost, attempting reconnect (" + SanityCounter + ")..."
            SanityCounter++;
            tmrCheckLastClassLap = setTimeout("CheckLastClassLap()", 500);
            CheckServerTime();
            }
        setTimeout("fSanityCheck()", 30300);
        }
            
        function CheckLastClassLap()
        {
          clearTimeout(tmrCheckLastClassLap);
          //call the webservice that checks the last scanned record number - pass class id to service
          if (vDdlClasses != -3) //check to see if class display is disabled
            { worcs.svcCurrentScans.chkClassLastLap(vDdlClasses, OnClassSucceeded, OnClassFailed, OnClassTimeOut); }     
          else
            { tmrCheckLastClassLap = setTimeout("CheckLastClassLap()", updateInterval); }
            //reset timer for checking laps
            //put timeout in var so we can cancel it if needed
            
            //check to make sure class drop timer is still running
            if (tmrChkClassDrop != lastTmrChkClassDrop)
            {
                classDropCounter = 0;
            }
            else
            {
                //elimiate the class drop timer and call straight from here????
                classDropCounter++
                if (classDropCounter >= 30000 / updateInterval)
                {
                    tmrChkClassDrop = setTimeout("chkClassDrop()", 1250);
                    classDropCounter = 0;
                }
            }
            lastTmrChkClassDrop = tmrChkClassDrop;
            //end class drop check
        }

        //we got a result, now do something with it
        function OnClassSucceeded(result)
        {
          var LastClassScanned = result;  //returned by webservice
          var LastClassScannedDisplay = $get('latestClassLap').value;  //get last scanned record from display
          if (LastClassScanned != LastClassScannedDisplay && LastClassScanned != "-1")
          { __doPostBack('udpClassResults', vDdlClasses + ' a'); }
          //set next poll
          tmrCheckLastClassLap = setTimeout("CheckLastClassLap()", updateInterval);
        }

        // Stub to make the webservice call happy.
        function OnClassFailed(error, userContext, methodName)
        {}

        function OnClassTimeOut(result)
        {}
        
        function CheckServerTime()
        {
          //PageMethods.chkLastLap(OnSucceeded, OnFailed, OnTimeOut);
          //Call the webservice that returns the time on the server. 
          worcs.svcCurrentScans.chkServerTime(OnServerTimeSucceeded, OnServerTimeFailed, OnServerTimeTimeOut);         
        }

        var tmrCheckServerTime;
        function OnServerTimeSucceeded(result, userContext, methodName)
        {
          var timeMsg = result; //returned by webservice
          var timeMsg_array=timeMsg.split("|");
          var svrTime = timeMsg_array[0]
          var svrMsg = timeMsg_array[1]
          
          //write out special user message
          if (svrMsg != "" || clrMsg == "true")
          { 
            document.getElementById("usrMsg").innerHTML = svrMsg; 
            //if we're here to clear a msg, set bool to false so we don't do it again
            if (clrMsg == "true" && svrMsg == "" )
            { clrMsg = "false"; }
            else
            { clrMsg = "true"; }
          }
          
          //var svrTime = result;  //returned by webservice
          clearTimeout(tmrCheckServerTime);
          //document.getElementById("LblServerTime").innerHTML = svrTime;
		  serverdate=new Date('1/1/2010 '+svrTime)
		  clearInterval(tmrUpdateTime);
		  tmrUpdateTime = setInterval("displaytime()", 1000);
          SanityCheck2 = svrTime;
          tmrCheckServerTime = setTimeout("CheckServerTime()", 15000);
        }

        // Stub to make the webservice call happy.
        function OnServerTimeFailed(error, userContext, methodName)
        { }

        function OnServerTimeTimeOut(result)
        { }
        
        function chkClassDrop()
        {
            clearTimeout(tmrChkClassDrop);
            worcs.svcCurrentScans.chkClassDrop(OnClassDropSucceeded, OnClassDropFailed, OnClassDropTimeOut);
        }
        
        function OnClassDropSucceeded(result, userContext, methodName)
        {
          var curClassList = result;  //returned by webservice
          var lastClassList = $get('classString').value;  //get last scanned record from display
          if (curClassList != lastClassList && curClassList != "-1")
          { 
          __doPostBack('udpClassDrop', '');
          setTimeout("reloadClassResults()", 5000);
          vDdlClasses = -3;
          //document.getElementById("latestClassLap").value = 0;  //probably don't need this
          }

          //*tmrChkClassDrop = setTimeout("chkClassDrop()", 30000);
        }
        
        // Stub to make the webservice call happy.
        function OnClassDropFailed(error, userContext, methodName)
        { }

        function OnClassDropTimeOut(result)
        { }
        
        function reloadClassResults()
        { __doPostBack('udpClassResults', '-3 a'); }
        
	    function openNewPopUp(url)
	    {	
	        var newwindow;
	        newwindow=window.open(url,'','height=630,width=500,scrollbars=no,resizable=yes,toolbar=no,location=no,directories=no,status=no,menubar=yes,copyhistory=no');
	        if (window.focus) 
	        {newwindow.focus()}
	    }
	    
	    function openRTPopUp(url)
	    {	
	        var newwindow;
	        newwindow=window.open(url,'rt','height=150,width=740;scrollbars=no,resizable=yes,toolbar=no,location=no,directories=no,status=no,menubar=no,copyhistory=no');
	        if (window.focus) 
	        {newwindow.focus()}
	    }

		function padlength(what){
			var output=(what.toString().length==1)? "0"+what : what
			return output
		}

		function displaytime(){
			serverdate.setSeconds(serverdate.getSeconds()+1)
			var timestring=padlength(serverdate.getHours())+":"+padlength(serverdate.getMinutes())+":"+padlength(serverdate.getSeconds())
			document.getElementById("LblServerTime").innerHTML=timestring
		}

