/**
 * Handles OnComplete Event for the account dialog
 * 
 * @param xhr XMLHttpRequest object provided by RequestContextAPI
 * @param status Status code of the request provided by RequestContextAPI
 * @param args Optional arguments provided by the RequestContextAPI
 */
function handleAccountComplete(xhr, status, args) {
  if (args && !args.validationFailed) {
    if (args.emailChanged) {
      PF('confirmEmailChangeDlg').show();
    } else {
      PF('accountDlg').hide();
    }
  }
}

/**
 * Handles OnComplete Event for the email change confirmation
 * 
 * @param xhr XMLHttpRequest object provided by RequestContextAPI
 * @param status Status code of the request provided by RequestContextAPI
 * @param args Optional arguments provided by the RequestContextAPI
 */
function handleEmailChangeComplete(xhr, status, args) {
  if (args && !args.validationFailed) {
    PF('confirmEmailChangeDlg').hide();
    PF('accountDlg').hide();
    setAccountDlgSaved();
  } else {
    PF('confirmEmailChangeDlg').hide();
  }
}

/**
 * Handles OnClick to close the Account Dialog
 */
function handleAccountCancel() {
  if (isAccountDlgSaved()) {
    PF('confirmExit').show();
  } else {
    PF('accountDlg').hide();
  }
}

/**
 * Bind all (current and future) 'form :input' onChange event to set 'saved =
 * false' on the account form
 */
$('#accountForm').on('change', ':input', function() {
  $('#accountForm').data('saved', false);
});

/**
 * Checks to see if the account dialog has been saved
 * @returns {Boolean}
 */
function isAccountDlgSaved() {
  return ($('#accountForm').data('saved') === false);
}

/**
 * Sets the Account Dialog as saved
 */
function setAccountDlgSaved() {
  $('#accountForm').data('saved', true);
}