/****************************************************************************
 * Confirm that the user would like to log in, else redirect them
 * to the proper place.
****************************************************************************/
var loginExhib_OnLoad = function()
{
  var isPostback     = $('#isPostback').val();
  var homePage       = $('#homePage').val();
  var prevPage       = $('#prevPage').val();
  var reqLoginPage   = $('#reqLoginPage').val();

  // Only show the confirmation dialog if the user is hitting the page
  // the first time and the user was redirected from a page that
  // requires a login.
  if (isPostback != '' || reqLoginPage == '')
    return;

  if (!confirm('You must first log in to access this page.  Click OK to ' +
    'login, or click Cancel to be redirected to the previous page.'))
  {
    if (prevPage != '')
      window.location.href = prevPage;
    else
      window.location.href = homePage;
  }
}
onload = loginExhib_OnLoad;

/****************************************************************************
 * Show the email form.
****************************************************************************/
function showEmail()
{
  $('.emailBlock').show();
  alert("Enter your email address then hit send and your " +
    "information will be sent to you.");
}

/****************************************************************************
 * Check for a valid email in the database, and if found mail the user's
 * username and password.
****************************************************************************/
function findUsername(email, from, subject) 
{
  var email = $('#email').val();
  var from  = $('#from').val();
  var subj  = $('#subject').val();
  
  var cfc   = new LoginExhib();
  
  if (email == "") 
    alert("Please enter your email address.");
  else 
  {
    $('#btnFindUsername').attr('disabled', 'disabled');
    cfc.setCallbackHandler(fEmailCallback);
    cfc.checkEmail(email, from, subj);
  }
  return false;
}

/****************************************************************************
 * Callback handler for the check email function.
****************************************************************************/
fEmailCallback = function(result)
{
  if (result)
  {
    $('.emailBlock').hide();
    alert("Your username has been sent.");
  } 
  else
    alert("No valid exhibitor login was found for this email address.")

  $('#email').val('');
  $('#btnFindUsername').removeAttr('disabled');
}

/****************************************************************************
 * Check if the username/password combination is valid.  If so set all the
 * session information.
****************************************************************************/
function checkLogin()
{
  var username = $('#username');
  var password = $('#password');
  var login    = $('#login');
  var cfc      = new LoginExhib();
  
  if (username.val() == "")
  {
    alert("Please enter your username.")
    username.focus();
  }
  else if (password.val() == "")
  {
    alert("Please enter your password.");
    password.focus();
  }
  else
  {
    login.attr('disabled', 'disabled');
    cfc.setCallbackHandler(fLoginCallback);
    cfc.checkLogin(username.val(), password.val());
  }
  return false;
}

/****************************************************************************
 * Callback handler for the check login function.
****************************************************************************/
fLoginCallback = function(result)
{
  var username = $('#username');
  var password = $('#password');
  var login    = $('#login');
  var retURL   = $('#retURL');
  
  login.removeAttr('disabled');
  
  if (result)
  {
    username.val('');
    password.val('');
    window.location.href = retURL.val();
  }
  else
  {
    alert("A valid exhibitor login was not " +
      "found using this username/password.");
    username.focus();
  }
  return false;
}


