﻿    var timerRuns;
    var timeStamp;
    var currentTime;
    var countdownRuns;
    var countdownValue;
    var disableSpaceBar;
    
    var timer;
    
    function initialize()
    {
       if (document.addEventListener){
            document.addEventListener('keyup', timerFunction, false); 
       } else if (document.attachEvent){
            document.attachEvent('onkeyup', timerFunction);
       }   
       
             
        timerRuns = false;
        countdownRuns=false;
        disableSpaceBar = false;
        timeStamp = 0;
        currentTime=0;
        timeSpan = 0;
       
    }
    
    
    //Receive data back from server for listBox
    function ReceiveServerData(arg,context)
    {
    
       
        
        var timeListBox = document.getElementById('timeListBox');
        
        var labelBestTime = document.getElementById('lblBestTimeVal');
        
        var command = arg.split('|');
       
        if(command[0] == "SCRAMBLE")
        {
            document.getElementById('divScramble').innerHTML = "Scramble: " + command[1] +  "&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"javascript:discard_Click();\">REQUEST SCRAMBLE</a>";
        }
        else
        {

            //Reset the ListBox
            for (j=0; j<timeListBox.options.length; j++)
            {
                   timeListBox.options[j]=null;
            }
           
            timeListBox.options.length=0;
            
            StatsIndex = 0;
            scrambleIndex = 0;
            progressIndex = 0;
           
                   
            //Enters new times
            for(j = 1; j<command.length;j++)
            {
            
              if(command[j]!="STATS")
              {
                  var newOption = window.document.createElement('OPTION');
                  newOption.text = command[j];
                  newOption.value = command[j];

                   timeListBox.options.add(newOption);
               }
               else if(command[j]=="STATS") 
               {
                   StatsIndex=j;
                   j=command.length;
               }
               
            }
            
            for (j = StatsIndex+1; j<command.length;j+=2) 
            {
                if(command[j] != "PROGRESS")
                    setLabelValue(command[j],command[j+1]);
                else
                {   
                    progressIndex = j;
                    j=command.length;
                }
            }
            
            setProgress(command[progressIndex+1],command[progressIndex+2]);

             for (j = StatsIndex+1; j<command.length;j++) 
            {
                if(command[j] == "SCRAMBLE")
                {
                    document.getElementById('divScramble').innerHTML = "Scramble: " + command[j+1] + "&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"javascript:discard_Click();\">REQUEST SCRAMBLE</a>";
                    j = command.length;
                }
                    
            }
        }
    }
    
    function setProgress(ID,value)
    {
        label = document.getElementById(ID);
        
        if(value == "True")
            label.style.color = "#FF0000";
        else
            label.style.color = "#00FF00";
    }
    
    function setLabelValue(ID, value)
    {
        label = document.getElementById(ID);
        label.innerHTML = value;
    }
    
    //For DropDown
    function ReceiveServerDataDrp(arg,context)
    {
        
    }
    
    //Update timer time after 100 mills
    function updateTime()
    {
        timer = setTimeout("updateTime()",1);
        
        currentTime = new Date();
        
        var lblTime = document.getElementById('lblTime');
        timeSpan = currentTime.getTime()-timeStamp.getTime();
        
        var timeString = constructTimeString(timeSpan);
        
        lblTime.innerHTML = timeString;
    }
    
    function updateCountdownTime()
    {
        timer = setTimeout("updateCountdownTime()",1000);
        
        currentTime = new Date();
        
        var lblTime = document.getElementById('lblTime');
        timeSpan = currentTime.getTime()-timeStamp.getTime();
        
        if(countdownIsOver(timeSpan,countdownValue))
        {
            startTimer();
        }
        else
        {
                     
            var timeString = constructTimeStringCountdown(timeSpan,countdownValue);
                
            lblTime.innerHTML = timeString;      
        }   
            
    }
    
    function showConfirmation(){
    
    	document.getElementById('divConfirmation').style.display = 'block';
    	hideScramble();
    }
    
    function hideConfirmation(){
    
        document.getElementById('divConfirmation').style.display = 'none';
    }
    
    function showScramble(){
        document.getElementById('divScramble').style.display = 'block';
        hideConfirmation();
    }
    
    function hideScramble(){
        document.getElementById('divScramble').style.display = 'none';
    }
    
    function accept_Click(){
    
        var lblTime = document.getElementById('lblTime');
        var arg = "addTime";
        arg = arg + "|" + lblTime.innerHTML;
        
        timeList = document.getElementById('timeListBox');
        
        CallServer(arg);
            
        hideConfirmation();
        showScramble();
        disableSpaceBar = false;
                    
    }
    
    function discard_Click(){
    
        arg = "scrambleRequest";
        CallServer(arg);
        hideConfirmation();
        showScramble();
        disableSpaceBar = false;
    }
    
    
    function pop_Click(){
    
        accept_Click();
    }
    
    //Construct a time String from interger in mills
    function constructTimeString(time)
    {
               
        var one_min = 1000*60;
        var one_sec = 1000;
        
        mills = Math.floor(time % 1000);
        secs = Math.floor((time/1000) % 60);
        mins = Math.floor((time/60000) % 60);
        
        if (mins < 10) {
            mins = "0" + mins;
        }

        if (secs < 10) {
            secs = "0" + secs;
        }
        
        if (mills<10) {
            mills = "0" + (mills.toString()).substring(0,1);
        }
        else{
            mills = (mills.toString()).substring(0,2);
        }
  
        return mins + ":" + secs + ":" + mills;

    }  
    
    function constructTimeStringCountdown(time,countdownLimit)
    {
               
        var one_min = 1000*60;
        var one_sec = 1000;
        
        var mins = Math.floor(time/one_min);
        var secs = Math.floor( (time/one_sec)-(mins*60) );
        
        var timeString = countdownLimit - secs;
          
        return timeString;

    }  
    
    function countdownIsOver(time,targetValue)
    {
                     
        var one_min = 1000*60;
        var one_sec = 1000;
        
        var mins = Math.floor(time/one_min);
        var secs = Math.floor( (time/one_sec)-(mins*60) );
        
        if( Math.floor((secs-targetValue)) == 0)
            return true;
        else
            return false;
    }
    
    //timer start functions. Detect space key
    function timerFunction(e)
    {
        var key = window.event ? e.keyCode : e.which;
                
        if( (key==32) && (disableSpaceBar == false))
        {
            if(countdownRuns)
            {
                countdownRuns=false;
                clearTimeout(timer);
                
            }
            else if(timerRuns)
            {
                timerRuns = false;
                clearTimeout(timer);  
                
                var lblTime = document.getElementById('lblTime');
                disableSpaceBar = true;
                showConfirmation();
              
            }
            else
            {
                countdownValue = document.getElementById('drpCountdown').value;
                
                if(countdownValue != 0){
                    countdownRuns=true
                    timeStamp= new Date();
                    currentTime = new Date();
                    lblTime = document.getElementById('lblTime');
                    
                                   
                    lblTime.innerHTML = countdownValue;
                    
                    timer = setTimeout("updateCountdownTime()",1000);
                }
                else{
                
                    startTimer();

               }
                
            }
        }
        else
        {
             
        }
       
    }
    
    function startTimer(){
        
        countdownRuns=false;
        clearTimeout(timer);
        timerRuns=true;
        timeStamp= new Date();
        currentTime = new Date();
        timer = setTimeout("updateTime()",100);
    
    }
    
    function timeExist(timeList, time)
    {
                
        for (j=0; j<timeList.options.length; j++)
        {
            if ((timeList.options[j].value)==time)       
                return true;
        }
        
        return false;
    }
    
    function clearAll()
    {
        arg = "clearAll";
        CallServer(arg);
    }
    
    function clearSelected()
    {
        arg="clearSelected";
        selectedIndex = (document.getElementById('timeListBox')).selectedIndex;
        
        if(selectedIndex != -1)
        {
            arg += "|" + selectedIndex;
            CallServer(arg);
        }
    
    }