<!--

/************************************************************************/
/* globals.js                                                           */
/*----------------------------------------------------------------------*/
/* Global variables and initialisation code.                            */
/*----------------------------------------------------------------------*/
/* File created 30Apr08 acurtis                                         */
/* 29May08 acurtis               Added previous/next message selection. */
/*----------------------------------------------------------------------*/
/* Copyright (c) 2008-2009 Avaya Inc.                                   */
/************************************************************************/

// Some strings that would otherwise escape translation. Not used here
// but kept here so that the Perl localisation script jslocalise.pl
// finds them.
var ls1 = LocaliseString("Inbox");
var ls2 = LocaliseString("Trash");

// MesssageState enums. Must correspond to values used on VM Server.
var eMS_unknown = 0;
var eMS_new = 1;
var eMS_unopened = 2;
var eMS_open = 4;
var eMS_saved = 8;
var eMS_deleted = 16;
var eMS_temporary = 32;
var eMS_archived = 64;
        
var DEFAULT_THEME = "avaya";

var gCurrentOrderBy = 'date';
var gCurrentSortDir = 'DESC';
var gCurrentFwdOrderBy = 'name';
var gCurrentFwdSortDir = 'ASC';
var gForwardingListData = '';
var gForwardingListMailboxType = 0;
var gForwardingListMsgIds = '';
var gCurrentMsg = null;
var gActiveDialogs = [];
var gNumFoldersInList = 0;
var gNumExtnsInForwardList = {};
var gKeys = '';
var gMessageList = null;
var gLastError = '';
var gBlockRedirection = false;
var gPageRefreshTimer = null;  // Timer to periodically refresh messages list
var gPageRefreshSemaphore = 0;
var gDragNDropElem = null;
var gVerboseErrors = true;
var gSessionFailed = false;
var gBusyCursorCount = 0;
var gExtnList = null;
var gHGList = null;
var gLastActivityTime = new Date().getTime();
var gActivityTimer = null; // Timer to inform server of last user activity
var gEnableDialFromExtn = true; // Enables the "Ringback" button

// Sets up combos selections and fills message list
// Fills the messages list
function Initialise()
{
  // Flag to block automatic page redirection. Used for debugging.
  if( gBrowser.GetCookie( VMPRO_block_redirection ) )
  {
    gBlockRedirection = true;
  }

  gBrowser.NoteClientArea();

  // If this element isn't present it meant page wasn't fully loaded (it is probably displaying an error message instead)
  if( document.getElementById('tblScreenArea') )
  {

    // Create folders list
    // FolderList constructor will select current folder
    gFolderList = new FolderList( gDefaultFolderId, gTrashFolderId, gFoldersCsv );

    // Render folders list
    var ajaxDisplay = document.getElementById( 'tabs' );
    if( ajaxDisplay )
    {
      ajaxDisplay.innerHTML = gFolderList.ToHtml( GetCurrentTheme() );
    }

    // Show sort direction : Date desc - this is a default when window is just opened.
    ShowElem( 'date_sort', true );

    // Show an empty messages list while we wait for the real one to load
    var ajaxDisplay = document.getElementById('divMsgs');
    if( ajaxDisplay )
    {
      var messageList = new MessageList(0,GetNumMsgsPerPage());
      ajaxDisplay.innerHTML = messageList.ToHtml( GetCurrentTheme(), GetHideToColSetting() );
    }


    // Get mailboxes now as it is slow.
    GetMailboxes();
    // Fetch and show message list
    GetMessages( true );

  }

  // Call ActivateDialog to ensure the correct elements are disabled/enabled, and that
  // they have the correct mouse pointer styles.
  ActivateDialog('none');

  // Periodically inform server of user's last activity time.
  ResetActivityTimer();

  // Save some resources now this fn not needed
  delete Initialise;
}

// Use in event handlers to stop other elements handling the event after this function is called.
function CatchEvent(e)
{
  if( !e )
  {
    e = window.event;
  }
  e.cancelBubble = true;
  if( e.stopPropagation )
  {
    e.stopPropagation();
  }
}

// Page auto refresh
// Starts the auto-refresh timer
function ResetAutoRefreshTimer(seconds)
{
  if( !seconds )
  {
    seconds = REFRESH_PERIOD;
  } 
  if( seconds )
  {
    gPageRefreshTimer = window.setTimeout( AutoRefreshTimeout, seconds * 1000 );
    var now = new Date().getTime();
  }
}

// Page auto refresh
// Refreshes the page when the timer has fired
function AutoRefreshTimeout()
{ 
	// Let server know that session is still alive (even though user may not be using the session)
  UpdateLastContact();

  if( gPageRefreshSemaphore == 0 )
  {
    // Fetch and show message list
    GetMessages( false );
  }
  else
  {
    // Keep trying, more rapidly so that refresh happens soon after semaphore released
    ResetAutoRefreshTimer(10);    
  }
}

//-->
