window.onload=Page_Load;


function calcage(secs, num1, num2) {
  s = ((Math.floor(secs/num1))%num2).toString();
  if (s.length < 2)
  {  s = "0" + s; }
  return "<b>" + s + "</b>";
}
function CountBack(secs) {
  CountStepper=-1;
  var SetTimeOutPeriod = (Math.abs(CountStepper)-1)*1000 + 990;
  DisplayFormat = "%%H%% : %%M%% : %%S%% ";
  Level_Format = "Level %%L%%";   

  if (secs <= 15 && secs%2==0) {   /* on the even numbers blink red and black*/
      document.getElementById('cntdwn').style.color = 'red';
      document.getElementById('cntdwn').style.backgroundColor = 'black';
      }
  else if (secs <= 15 && secs%2==1) {   /* on the odd number blink black and red*/
      document.getElementById('cntdwn').style.color = 'black';
      document.getElementById('cntdwn').style.backgroundColor = 'red';
      }
  else { /* greater than 10 seconds do normal color */
      document.getElementById('cntdwn').style.color = 'black';
      document.getElementById('cntdwn').style.backgroundColor = 'lightblue';  
  }
  
  if (secs < 0) {
    /*document.getElementById("cntdwn").innerHTML = FinishMessage;*/
    Level = Level+1;
    DisplayStr = Level_Format.replace(/%%L%%/g, Level);
    document.getElementById("blndlvl").innerHTML = DisplayStr;
    
    /* play the level end sound if the user has checked the box*/
    if (document.getElementById('Check_Play_Sound').checked==true) 
    { 
       document.getElementById("sound_element").innerHTML= "<embed src='"+sound_file_url+"' hidden=true autostart=true loop=false>";
    } 
    
    secs=gsecs-1;
    save_time=0;
  }  
  
  Update_Display(secs);
  if (timer_is_on) {
    t=setTimeout("CountBack(" + (secs+CountStepper) + ")", SetTimeOutPeriod);
    save_time=save_time+1;
  }  
}


function doTimer()
{
   Level_Time()
     
   if (!timer_is_on)
     {
     timer_is_on=1;
     old_secs = gsecs - save_time;
     CountBack(old_secs);
  }
}


function stopCount()
{
   
   clearTimeout(t);
   timer_is_on=0;
}


function Level_Time()
{
   Count_Seconds = Math.max(parseFloat(document.getElementById("Seconds_Per_Level").value),0);
   Count_Minutes = Math.max(parseFloat(document.getElementById("Minutes_Per_Level").value),0);
   Count_Hours = Math.max(parseFloat(document.getElementById("Hours_Per_Level").value),0);
   if (isNaN(Count_Seconds)){
      Count_Seconds=0;
   }
   if (isNaN(Count_Minutes)){
      Count_Minutes=0;
   }
   if (isNaN(Count_Hours)){
      Count_Hours=0;
   }   
   gsecs = Count_Hours * 3600 + Count_Minutes * 60 + Count_Seconds;
   if (gsecs==0){
      gsecs=600;
   }

   Update_Display(gsecs);   

   Level_Time_on=1;
}



/* This function resets the clock back to the starting time */
function Reset()
{
   clearTimeout(t);
   timer_is_on=0;
   t=0;
   save_time=0;
   
   Update_Display(gsecs); 

   Level = 1;     /* On Reset, go back to blind level 1 */
   DisplayStr = Level_Format.replace(/%%L%%/g, Level);
   document.getElementById("blndlvl").innerHTML = DisplayStr;    /* update the blind level display */
   
   document.getElementById('cntdwn').style.color = 'black';
   document.getElementById('cntdwn').style.backgroundColor = 'lightblue';  /* make sure it is set to a neutral color */
}


/*  This function updates the clock with the latest seconds.   It calls calcage */
/*  in order to convert a number in seconds to hours, minutes, and seconds */
function Update_Display(display_secs)  
{
   DisplayFormat = "%%H%% : %%M%% : %%S%% ";
   Level_Format = "Level %%L%%";   
   DisplayStr = DisplayFormat.replace(/%%D%%/g, calcage(display_secs,86400,100000));
   DisplayStr = DisplayStr.replace(/%%H%%/g, calcage(display_secs,3600,24));
   DisplayStr = DisplayStr.replace(/%%M%%/g, calcage(display_secs,60,60));
   DisplayStr = DisplayStr.replace(/%%S%%/g, calcage(display_secs,1,60)); 
   document.getElementById("cntdwn").innerHTML = DisplayStr;
}

/* This function gets done when the page is loaded */
function Page_Load(){
   t=0;   /* initialize the time variable */
   timer_is_on=0;
   Level=1;
   save_time=0;
   sound_file_url="http://www.easypokerchips.com/scripts/DingLing.wav";
}
