// Global Variables
var sessionID = "";
var userID = "";

function showLogin( )
{
   if ( document.getElementById("LoginPrompt") != null )
   {
      // Initialise the login window
      centerWindow( "LoginPrompt" );
      clearLoginDialog( );
      
      // Display the login prompt
      document.getElementById("LoginPrompt").style.visibility = "visible";
   }
}


function showAbout( )
{
   if ( document.getElementById("AboutWindow") != null )
   {
      // Initialise the login window
      centerWindow( "AboutWindow" );
      
      // Display the login prompt
      document.getElementById("AboutWindow").style.visibility = "visible";
   }
}


function closeAbout( )
{
   if ( document.getElementById("AboutWindow") != null )
   {
      // Display the login prompt
      document.getElementById("AboutWindow").style.visibility = "hidden";
   }
}


function doLogin( )
{
   var elUserName = document.getElementById( "login_UserName" );
   var elPassword = document.getElementById( "login_Password" );

   if ( ( elUserName != null ) && ( elPassword != null ) )
   {
      // Get the values of the login and password as entered by the user
      var LoginName = elUserName.value;
      var Password = elPassword.value;
      
      // Store the users login name for later use
      userID = LoginName.toLowerCase( );
      
      // Use a regex to replace any special characters with their encoded
      // versions
      Password = Password.replace( /&/g, "&amp;" );
      Password = Password.replace( /</g, "&lt;" );
      Password = Password.replace( />/g, "&gt;" );

      if ( ( LoginName != null ) && ( Password != null ) )
      {
         // Begin construction of an AJAX request object
         var request = buildRequest( );
      
         var callback = function( )
         {
            // Local Variables
            var login = document.getElementById( "LoginPrompt" );
            var nav = document.getElementById( "Nav" );
            var logout = document.getElementById( "Header_Logout" );
            var sep = document.getElementById( "Header_Sep" );
         
            if ( request.readyState == 4 )
            {
               if( request.status == 200 )
               { 
                  var response = parseDocument( request.responseText );
               
                  // Attempt to read the session id from the return string
                  var elResponse = response.getElementsByTagName("LoginSuccessful");
               
                  // Verify that we've recieved a login successful message in response
                  if ( elResponse.length == 1 )
                  {
                     // Read the session id from the response stream
                     sessionID = nodeValue( elResponse[0], "SessionID" );

                     if ( login != null )
                     {
                        // The user has successfully logged into the system
                        // update the interface accordingly
                        login.style.visibility = "hidden";
                        nav.style.visibility = "visible";
                        
                        // Display the logout navigation item
                        sep.style.display = "inline";
                        logout.style.display = "inline";
                        
                        // Clear the username and password from the login window
                        clearLoginDialog( );
                        
                        // Check the users admin status
                        isAdmin( );
                     }
                  }
                  else
                  {
                     // Clear the password element
                     var elPassword = document.getElementById( "login_Password" );
                     elPassword.value = "";
   
                     // Inform the user of the login
                     setLoginMessage( "Login Failure" );
                  }
               }
            }
         }
         
         // Build the Parameter List
         var params = new Array(2);
         params[0] = new Parameter( "UserName", LoginName );
         params[1] = new Parameter( "Password", Password );
         
         // Perform the AJAX Call
         ajaxCall( request, "Login", params, callback );
      }
   }
}


function doLogout( )
{
   // Local Variables
   var login = document.getElementById( "LoginPrompt" );
   var nav = document.getElementById( "Nav" );
   var logout = document.getElementById( "Header_Logout" );
   var sep = document.getElementById( "Header_Sep" );

   if ( login != null )
   {
      // Create a new AJAX method to log us out on the server
      var request = buildRequest( );
      var param = new Parameter( "SessionID", getSessionID( ) );
      ajaxCall( request, "Logout", param, null );

      // Don't wait for a response since the session will be 
      // destroyed anyway if it times out

      // Close any currently active application windows
      closeFindDevices( );
      closeReports( );
      closeOptions( );
   
      // Hide any absent device warnings
      var warning = document.getElementById( "DeviceWarning" );
      warning.style.visibility = "hidden";
   
      // The user has successfully logged into the system
      // update the interface accordingly
      login.style.visibility = "visible";
      nav.style.visibility = "hidden";
                        
      // Display the logout navigation item
      sep.style.display = "none";
      logout.style.display = "none";
   }
}


function setLoginMessage( message )
{
   // Local Variables
   var elMessage = document.getElementById( "login_Message" );
   
   if ( elMessage != null )
   {
      elMessage.innerText = message;
   }
}


function clearLoginDialog( )
{
   // Local Variables
   var elUserName = document.getElementById( "login_UserName" );
   var elPassword = document.getElementById( "login_Password" );

   if ( ( elUserName != null ) && ( elPassword != null ) )
   {
      // Empty the contents of the input boxes
      elUserName.value = "";
      elPassword.value = "";
      setLoginMessage( "" );
      
      // Surrender focus
      elPassword.blur( );
      elUserName.blur( );
   }
}


function loginHandleKeypress( event )
{
   var eventKey;

   if ( browser.engine != "MSIE" ) 
   {
      eventKey = event.which;
   }
   else
   {
      eventKey = window.event.keyCode;
   }

   if ( eventKey == 13 )
   {
      doLogin( );
   }
}


function getSessionID( )
{
   return sessionID;
}


function isBetaTester( )
{
   // Local Variables
   var retVal = true;
   
   if ( ( userID == "blackbox admin" ) || ( userID == "blackbox" ) || ( userID == "tech road" ) )
   {
      retVal = true;
   }
  
   return retVal;
}
