var bPaused = false ;
var timerID = 0;
var total_time_left = 0 ;
var total_time_gone = 0 ;
var bDeciSECS = true ;
var time_slice = 1000 ;
var timeCOEFF = 1 ;
var h = 0, m = 0 , s = 0, centSECS ;

function checkCENTIsecs()
{
      bDeciSECS = document.theTimer.deciCHECK.checked ;
      document.theTimer.TIMEleft.value = FormatTimeFromSeconds( 0 );
      document.theTimer.TIMEgone.value = FormatTimeFromSeconds( 0 );
}

function UpdateTimer()
{
   if ( !bPaused )
   {
       document.theTimer.TIMEleft.value = FormatTimeFromSeconds( total_time_left );
       document.theTimer.TIMEgone.value = FormatTimeFromSeconds( total_time_gone );
       document.title = "TecnoLinuxRadio - " + document.theTimer.TIMEgone.value ;
       total_time_left-- ;
       total_time_gone++ ;
   }
   if ( total_time_left >= 0 ) timerID = setTimeout("UpdateTimer()", time_slice );
   else Stop();
}

function FormatTimeFromSeconds( time )
{
    document.theTimer.totalSecs.value = time ;
        h = parseInt( time / ( 3600 * timeCOEFF ) ) ; h = h.toFixed( 0 );
        time = time - ( ( 3600 * timeCOEFF ) * h ) ;
        m = parseInt( time / ( 60 * timeCOEFF ) ) ; m = m.toFixed( 0 );
        time = time - ( ( 60 * timeCOEFF ) * m ) ;
    if ( bDeciSECS )
    {
        s = parseInt( time / ( timeCOEFF ) ) ;
        time = time - ( ( timeCOEFF ) * s ) ;
        centSECS = time ;
    }
    else s = time ;
    h = ( h < 10 ) ? "0" + h : h ;
    m = ( m < 10 ) ? "0" + m : m ;
    s = ( s < 10 ) ? "0" + s : s ;
    centSECS = centSECS + "0" ;
    return "TxR :"+ h + ":" + m + ":" + s + ( ( bDeciSECS ) ? ":"+centSECS : "" );
}

function Start()
{
   h = 24;
   m = parseInt( document.theTimer.startTimeMins.value ) ;
   s = parseInt( document.theTimer.startTimeSecs.value ) ;
   if ( isNaN( h ) ) document.theTimer.startTimeHours.value = h = 0 ;
   if ( isNaN( m ) ) document.theTimer.startTimeMins.value = m = 0 ;
   if ( isNaN( s ) ) document.theTimer.startTimeSecs.value = s = 0 ;
   if ( bDeciSECS )
   {   
      total_time_left *= 10 ;
      time_slice = 100 ;
      timeCOEFF = 10 ;
   }
   else
   {   
      time_slice = 1000 ;
      timeCOEFF = 1 ;
   }
   
   total_time_left = h * 3600 * timeCOEFF ;
   total_time_left += m * 60 * timeCOEFF ;
   total_time_left += s * timeCOEFF ;

   ///////////////////////////////////////////////////////////////
   // Reformat time and display it again inside the edit boxes
   // in order to assure the correct input syntax
   
   time = total_time_left ;
   
        var hDISPLAY = parseInt( time / ( 3600 * timeCOEFF ) ) ; hDISPLAY = hDISPLAY.toFixed( 0 );
        time = time - ( ( 3600 * timeCOEFF ) * hDISPLAY ) ;
        
        mDISPLAY = parseInt( time / ( 60 * timeCOEFF ) ) ; mDISPLAY = mDISPLAY.toFixed( 0 );
        time = time - ( ( 60 * timeCOEFF ) * mDISPLAY ) ;

        sDISPLAY = time ;

   hDISPLAY = ( hDISPLAY < 10 ) ? "0" + hDISPLAY : hDISPLAY ;
   mDISPLAY = ( mDISPLAY < 10 ) ? "0" + mDISPLAY : mDISPLAY ;
   sDISPLAY = ( sDISPLAY < 10 ) ? "0" + sDISPLAY : sDISPLAY ;

   document.theTimer.startTimeHours.value = hDISPLAY ;
   document.theTimer.startTimeMins.value = mDISPLAY  ;
   document.theTimer.startTimeSecs.value = sDISPLAY ;
   ///////////////////////////////////////////////////////////////

   total_time_gone = 0 ;

   if ( total_time_left > 0 )
   {   
       timerID  = setTimeout( "UpdateTimer()", time_slice );
    
       document.theTimer.start.disabled = true ;
       document.theTimer.stop.disabled = false ;
       document.theTimer.pause.disabled = false ;
       document.theTimer.reset.disabled = true ;

       document.theTimer.deciCHECK.disabled = true ;
   }
}

function Stop()
{
   if( timerID ) {
      clearTimeout( timerID );
      timerID  = 0;
   }

   document.theTimer.start.disabled = false ;
   document.theTimer.stop.disabled = true ;
   document.theTimer.pause.disabled = true ;
   document.theTimer.reset.disabled = false ;

   document.theTimer.deciCHECK.disabled = false ;
}

function Pause()
{
   bPaused = !bPaused ;

   document.theTimer.start.disabled = true ;
   document.theTimer.stop.disabled = ( bPaused ) ? true : false ;
   document.theTimer.pause.disabled = false ;
   document.theTimer.reset.disabled = ( bPaused ) ? false : true ;
}

function Reset()
{
   bDeciSECS = false ;
   bPaused = false ;
   timerID = 0;
   total_time_left = 0 ;
   total_time_gone = 0 ;
   
   h = m = s = centSECS = 0 ;
   
   document.theTimer.TIMEleft.value = FormatTimeFromSeconds( total_time_left );
   document.theTimer.TIMEgone.value = FormatTimeFromSeconds( total_time_gone );

   document.title = "" ;

   document.theTimer.startTimeHours.value = "00" ;
   document.theTimer.startTimeMins.value = "00" ;
   document.theTimer.startTimeSecs.value = "00" ;

   document.theTimer.deciCHECK.disabled = false ;
   document.theTimer.deciCHECK.checked = bDeciSECS ;

   document.theTimer.start.disabled = false ;
   document.theTimer.stop.disabled = true ;
   document.theTimer.pause.disabled = true ;
   document.theTimer.reset.disabled = true ;
}

