$$.extend({
  User : {
    FormSubmission : false,
  
    Ready : function()
    {
      $('input#hUserFormSubmit').click(
        function($e) {
          if ($('input#hUserEmail').val() && !$$.User.FormSubmission && !$('input#hUserSecurityAnswer').nextAll('p.hFormError').length) {
            $e.preventDefault();
            $$.User.Validate();
          }
        }
      );
    },

    Validate : function()
    {
      $.post(
        $$.Path(
          '/plugins/hUser/hUserLogin/getSecurityQuestion'
        ), {
          hUserEmail: $('input#hUserEmail').val()
        },
        function(json) {
          var $response = parseInt(json);
    
          if (isNaN($response)) {
            $$.User.FormSubmission = true;

            $('label[for="hUserSecurityAnswer"]').text(json + ':');
            $('fieldset#hUserSecurityQuestionFieldset').slideDown('slow');
          } else {
            switch ($response) {
              case -5: {
                alert('Unable to query the security question; required information was missing from the request.');
                break;
              };
              case -24: {
                alert('The email address you provided is not associated with a valid account.');
                break;
              };
              case -25: {
                // No security question is defined... 
                $('input#hUserFormSubmit').parents('form').submit();
                break;
              };
            }
          }
        },
        'json'
      );
    }
  }
});

$(document).ready(
  function() {
    $$.User.Ready();
  }
);