
function toggleLogin(basePath)
{
  $('#loginmessage').html('');
  var visible = $('#logindropdown').css('visibility');
  if(visible == 'hidden') $('#logindropdown').css('visibility', 'inherit');
  else $('#logindropdown').css('visibility', 'hidden');
};

function login(basePath)
{
  var request = {Action: 'Login'};
  request.UserName = $('#username').val();
  request.Password = $('#password').val();
  
  if(request.UserName == '' || request.Password == '')
  {
    $('#loginmessage').html('Pease enter both a user name and password.');
    return false;
  }
  
  $('#loginicon').css({'visibility': 'inherit'});
  
  jQuery.ajax({
    type: 'GET',
    url: basePath+'login.php',
    data: request,
    success: loginResponse,
    error: loginError,
    dataType: 'json',
    async: true
  });
};

function logout(basePath)
{
  $('#loginicon').css({'visibility': 'inherit'});
  var request = {Action: 'Logout'};
  
  jQuery.ajax({
    type: 'GET',
    url: basePath+'login.php',
    data: request,
    success: loginResponse,
    error: loginError,
    dataType: 'json',
    async: true
  });
};

function loginResponse(data, status, XMLHttpRequest)
{
  $('#loginicon').css({'visibility': 'hidden'});
  
  if(data.action == 'Login')
  {
    if(data.loggedIn == 'true') window.location.reload();  // Refresh the page to update the display of any restricted data or options.
    else
    {
      if(data && data.message != '') $('#loginmessage').html(data.message);
      else $('#loginmessage').html('');
    }
  }
  else window.location.reload();
};

function loginError(XMLHttpRequest, status, errorThrown)
{
  window.alert('Login failed: '+status+'; '+errorThrown);
  $('#loginicon').css({'visibility': 'hidden'});
  $('#loginmessage').html('Login failed: '+status+'; '+errorThrown);
}
