 usernameFormMessages = new FormMessages()
-passwordFormMessages = new FormMessages()
-passwordReadyForValidation = false
-passwordFieldModified = false
 usernameReadyForValidation = false
 usernameFieldModified = false
+
+passwordFormMessages = new FormMessages()
+newPasswordReadyForValidation = false
+newPasswordFieldModified = false
 
 resetUsernameForm = ->
   usernameFormMessages.resetMessages()
   usernameReadyForValidation = false
+  usernameFieldModified = false
 
 resetPasswordForm = ->
   passwordFormMessages.resetMessages()
   $('#current-password').val('')
   $('#new-password').val('')
-  passwordReadyForValidation = false
+  newPasswordReadyForValidation = false
+  newPasswordFieldModified = false
 
 # Reset forms when settings page becomes active
 Deps.autorun ->

  !passwordFormMessages.getErrorMessage field

validatePassword = (newPassword, messageField) ->
  return unless passwordReadyForValidation
  try
    User.validatePassword newPassword, messageField
    passwordFormMessages.resetMessages messageField

changePassword = (currentPassword, newPassword, callback) ->
  try
    User.validatePassword newPassword, "new-password"
  catch error
    callback error
    return

  try
    # We check this manually because changePassword error throws global 'Match failed' error if current password is empty string
    throw new FormError 400, "Incorrect password", "current-password" unless currentPassword
    Accounts.changePassword currentPassword, newPassword, (error) ->
      formError = new FormError error.error, error.reason, "current-password" if error
      callback formError
  catch error
    callback error

