 # Username settings
 Template.settingsUsername.events =
   'submit form#set-username': (event, template) ->
+    usernameSubmitted = true
     event.preventDefault()
     usernameFormMessages.resetMessages()
     username = $('#username').val()

    return # Make sure CoffeeScript does not return anything

  'focus input': (event, template) ->
    usernameFormMessages.resetMessages '' # Reset global messages

    return # Make sure CoffeeScript does not return anything

    return # Make sure CoffeeScript does not return anything

  'blur input#username': (event, template) ->
    # We postpone blur event handling as a workaround for a bug in Meteor.
    # See https://github.com/peerlibrary/peerlibrary/pull/520#issuecomment-52514483
    # TODO: Move function below out of setTimeout when we move to Blaze. Also, remove
    # flag usernameSubmitted and all related to it.
    Meteor.setTimeout ->
      usernameReadyForValidation = usernameFieldModified
      return if usernameSubmitted
      username = $('#username').val()
      validateUsername username, 'username'
    ,
      100 # ms

    return # Make sure CoffeeScript does not return anything

