/*---------------------------*/
/* Member Login Panel (Ajax) */
/*---------------------------*/

jQuery(document).ready(function($) {
  
  // this script will establish the ajax
  // login capabilities of the login panel
  // create shortcut references to elements
  var p = $('.rpr-member-login-panel');  
  var b = p.find('#rpr-member-login-button');
  
  // initialize a flag to
  // prevent multiple clicks
  b.data('enabled', true);
  
  // establish click 
  // functionality for
  // the login button
  b.click(function(e) {
    e.preventDefault();
    e.stopPropagation();   
    var b = $(this);
    
    // ensure that only one click
    // is processed at a time here
    if (!b.data('enabled')) return;    
    b.data('enabled', false);
    
    // hide all messages currently being displayed
    var m = $('#rpr-member-login-panel-message');
    m.find('span').hide();
    
    // show the activity indicator by referencing the element
    $('#rpr-member-login-panel-activity-indicator').show();
    
    // compose an execute the ajax request
    $.ajax({ url: "member_login_panel.php",
             type: "POST",
             cache: false,
             data: { action: 'member_login',
                     username: p.find('input[name=username]').val(),
                     password: p.find('input[name=password]').val() },
             dataType: 'json',
             success: b.data('success'),
             error: b.data('error') });
  });
  
  // simulate a login button click on return key
  p.find('input.rpr-text').keypress(function(e) {
    if (e.which != 13) return;
    e.preventDefault();
    e.stopPropagation();
    $('#rpr-member-login-button').click();
  });
  
  // combine both of jquery's ajax status 
  // function into a single call with data
  b.data('success', function(data, status) {
    var b = $('#rpr-member-login-button');
    b.data('complete')(data, status);
  });
  b.data('error', function(request, status, error) {
    var b = $('#rpr-member-login-button');
    b.data('complete')(null, status);
  });
  
  // define our custom complete function 
  // which receives response and any status
  b.data('complete', function(data, status) {
    
    // obtain reference to message section and 
    // initially hide all other message elements
    var m = $('#rpr-member-login-panel-message');
    m.find('span').hide();
    
    // obtain references to errors
    var se = m.find('.system-error');
    var de = m.find('.dynamic-error');
    
    // when request fails, system error
    if (status != 'success') se.show();
    else
    {
      // ensure that the data return is correct type 
      if (!(data instanceof Object)) data = new Object();
      
      // when login successful simply
      // redirect as session established
      if (data.success)
      {
        // do not perform any further processing
        window.location.href = 'members/landing.php';
        return;
      }
      else
      {
        // for errors received parse and display
        if (data.error == 'SYSTEM') se.show();
        else de.text(data.error).show();
      }
    }
    
    // at this point we will be showing
    // a message so show the message section
    // remove activity indicator and allow
    // the login button to be used again
    m.show();
    $('#rpr-member-login-panel-activity-indicator').hide();
    $('#rpr-member-login-button').data('enabled', true);
  });

});