﻿// The value returned by window.setInterval() that can be passed to window.clearInterval()
// to cancel the periodic execution of the function.
var stopMe = null;

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.
updateSessionTimeoutDisplayByMinutes();

// Update the timeout display once a minute, unless it is already being updated every second.
if (stopMe == null)
  stopMe = window.setInterval(updateSessionTimeoutDisplayByMinutes, 60 * milliseconds_per_second);

// Display the timeout message and the Extend button.
document.getElementById('Timeout_Button').style.display = '';

function updateSessionTimeoutDisplayByMinutes()
{
  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
  {
    // Reduce the time interval between updates from minutes to seconds.
    window.clearInterval(stopMe);
    // Convert the remaining time to seconds.
    time_remaining = time_remaining * 60;
    // To keep the timer from running out too late, slightly shorten the remaining seconds.
    var adjustment = 5;  // Subtract 5 seconds from the remaining time interval.
    // The new definition of a second (in actual milliseconds).
    var duration_of_a_second = (time_remaining - adjustment) / time_remaining * milliseconds_per_second;
    stopMe = window.setInterval(updateSessionTimeoutDisplayBySeconds, duration_of_a_second);
  }
}  // updateSessionTimeoutDisplayByMinutes()

    
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(updateSessionTimeoutDisplayByMinutes, 60 * milliseconds_per_second);
  // Load a page to restart the session timer on the server.
  var request = new XMLHttpRequest();
  request.open("GET", '/Guest/Restart_Session.aspx', false);
  request.send(null);
  // Display the initial message and reinitialize the button.
  updateSessionTimeoutDisplayByMinutes();
  document.getElementById('Timeout_Button').style.display = '';
  // Restore the document's original title.
  document.title = original_document_title;
}  // Extend_Session()

// Extend the session when the Extend button is clicked.
document.getElementById('Timeout_Button').onclick = Extend_Session;

