﻿// The value returned by window.setInterval() that can be passed to window.clearInterval()
// to cancel the periodic execution of the function.
var stopMe = null;

// Create a copy of the session timeout, in minutes.
var time_remaining = sessionTimeout;

// Store the document's original title.
var original_document_title = document.title;

// Conversion factor.
var milliseconds_per_second = 1000;

// Display the initial message.
updateSessionTimeoutDisplay();

// Update the timeout display once a minute, unless it is already being updated every second.
if (stopMe == null)
  stopMe = window.setInterval(updateSessionTimeoutDisplay, 60 * milliseconds_per_second);

// Display the timeout message and the Extend button.
document.getElementById('Timeout_Button').style.display = '';

function updateSessionTimeoutDisplay()
{
  var message;
  if (time_remaining > 2)
  {
    message = 'Session time remaining: ';
    if (time_remaining > 59)
    {
      var hours = Math.floor(time_remaining / 60);
      var minutes = time_remaining - (hours * 60);
      if (minutes < 10)
        minutes = '0' + minutes;
      message += hours + ':' + minutes + ':00';
    }
    else
      message += time_remaining + ':00';
    time_remaining -= 1;  // Subtract one minute.
    document.getElementById('Timeout_Message').innerHTML = message + ' &nbsp;';
  } 
  else  // time_remaining <= 2, so change the timer.
  {
    // Reduce the time interval between updates from minutes to seconds.
    window.clearInterval(stopMe);
    time_remaining = time_remaining * 60  // Convert remaining time to seconds.
    stopMe = window.setInterval(updateSessionTimeoutDisplayBySeconds, milliseconds_per_second);
  }
}  // updateSessionTimeoutDisplay()

    
function updateSessionTimeoutDisplayBySeconds()
{
  var message_prefix, message_time;
  if (time_remaining == 0)
  {
    message_prefix = 'Your session has expired';
    message_time = '';
    document.getElementById('Timeout_Button').style.display = 'none';
    window.clearInterval(stopMe);
    document.title = '***Time expired***';
  }
  else  // time_remaining > 0
  {
    message_prefix = 'Session time remaining: ';
    if (time_remaining < 60)
    {
      var seconds = time_remaining;
      if (seconds < 10)
        seconds = '0' + seconds;
      message_time = '0:00:' + seconds;        
    }
    else  // time_remaining >= 60
    {
      var minutes = Math.floor(time_remaining / 60);
      var seconds = time_remaining - (minutes * 60);
      if (seconds < 10)
        seconds = '0' + seconds;
      message_time = '0:0' + minutes + ':' + seconds;      
    }
    document.title = '***Time: ' + message_time + '***';
    time_remaining -= 1;
  }

  document.getElementById('Timeout_Message').innerHTML = message_prefix + message_time + ' &nbsp;';
}  // updateSessionTimeoutDisplayBySeconds()


// Extend the session
function Extend_Session()
{
  // Stop and then reset the session timer in the browser, updating the timeout display once a minute.
  window.clearInterval(stopMe);
  time_remaining = sessionTimeout;
  stopMe = window.setInterval(updateSessionTimeoutDisplay, 60 * milliseconds_per_second);
  // Load an empty page as if it were an image to restart the session timer on the server.
  var img = new Image(1,1);
  img.src = '/Guest/Restart_Session.aspx';
  // Display the initial message and reinitialize the button.
  updateSessionTimeoutDisplay();
  document.getElementById('Timeout_Button').style.display = '';
  // Restore the document's original title.
  document.title = original_document_title;
}

// Extend the session when the Extend button is clicked.
document.getElementById('Timeout_Button').onclick = Extend_Session;
