﻿    var timerRuns;
    var timeStamp;
    var currentTime;
    var countdownRuns;
    var countdownValue;
    var disableSpaceBar;
    var countdownEnabled;
    var hideTime;
    var time;
    var stopKeyup;
    
    var timer;
    
    function initialize()
    {
        

        window.onkeydown=function(e){ 
          if(e.keyCode==32){ 
           return false; 
          } 
        };
        
              
                 
       if (document.addEventListener){
            document.addEventListener('keyup', timerFunctionStart, false); 
       } else if (document.attachEvent){
            document.attachEvent('onkeyup', timerFunctionStart);
       }
       
        if (document.addEventListener){
            document.addEventListener('keydown', timerFunctionStop, false); 
       } else if (document.attachEvent){
            document.attachEvent('onkeydown', timerFunctionStop);
       }
       
       if (document.addEventListener){
            document.addEventListener('keydown', stopSpacebar, false); 
       } else if (document.attachEvent){
            document.attachEvent('onkeydown', stopSpacebar);
       }            
       
             
        timerRuns = false;
        countdownRuns=false;
        disableSpaceBar = false;
        countdownEnabled = false;
        hideTime = false;
        stopKeyup=false;
        time = "";
        hideTime = false;
        timeStamp = 0;
        currentTime=0;
        timeSpan = 0;
       
    }
    
    function stopSpacebar(e)
    {
        var key = window.event ? e.keyCode : e.which;
                
        if(key==32)       
            return false;
     
    }
    
    
    function setLabelValue(ID, value)
    {
        label = document.getElementById(ID);
        label.innerHTML = value;
    }
    
   
    
    //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);
        if(!hideTime)
            lblTime.innerHTML = timeString;
        else
            time = 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;      
        }   
            
    }
    
   
    //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 timerFunctionStart(e)
    {
        var key = window.event ? e.keyCode : e.which;
                
        if( (key==32) && (disableSpaceBar == false))
        {
            if(stopKeyup)
                stopKeyup=false;
            
            else if(countdownEnabled)
            {
                countdownValue = 15;
                
                if(countdownValue != 0){
                    countdownRuns=true
                    timeStamp= new Date();
                    currentTime = new Date();
                    var lblTime = document.getElementById('lblTime');
                    
                                   
                    lblTime.innerHTML = countdownValue;
                    
                    timer = setTimeout("updateCountdownTime()",1000);
                }
                else{
                    if(hideTime)
                        document.getElementById('lblTime').innerHTML = "Solve!";
                        
                    startTimer();

               }
                
            }
            else
            {
                
                startTimer();
            }
        }
                      
    }
    
    //timer start functions. Detect space key
    function timerFunctionStop(e)
    {
        var key = window.event ? e.keyCode : e.which;
                
        if(countdownRuns)
        {
            countdownRuns=false;
            clearTimeout(timer);
            stopKeyup=true;
            
        }
        else if(timerRuns)
        {
            timerRuns = false;
            clearTimeout(timer);  
            stopKeyup=true;
            
            var lblTime = document.getElementById('lblTime');
            
            if (hideTime)
                lblTime.innerHTML = time;
            document.getElementById('txtTime').value=lblTime.innerHTML;
            disableSpaceBar = true;
            raiseConfirmation();
            //raiseAsyncPostback();
            //disableSpaceBar = true;
            
          
        }
                      
    }

    
    function startTimer(){
        
        countdownRuns=false;
        clearTimeout(timer);
        timerRuns=true;
        timeStamp= new Date();
        currentTime = new Date();
        if(hideTime)
            document.getElementById('lblTime').innerHTML = "Solve!";
                        
        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 raiseAsyncPostback()
    {
        __doPostBack('asyncLinkBtn','');
        return false;
    }
    
    function raiseConfirmation()
    {
        var button = document.getElementById("btnConfirmation");
        if (button.click)
        {
            // IE
            button.click();
        }
        else
        {
            // FireFox
            var e = document.createEvent("MouseEvents");
            e.initEvent("click", true, true);
            button.dispatchEvent(e);
        }
    
    
    }
    
    function enableSpacebar()
    {
        disableSpaceBar = false;
        stopKeyup=false;
    }
    
   