         @entities.push(entity)
     @updateData()
 
-  updateData: () ->
+  updateData: (callback) ->
     encrypted = @encrypt(JSON.stringify(@entities))
     hash = CryptoJS.SHA1(encrypted).toString()
 

      if evt.keyCode is 13
        @password = $(that).val()
        @fetchData()
        $('.hide').removeClass('hide')
        $('#passwordRequest').addClass('hide')


    bTitle = b.title.toLowerCase()
    `((aTitle < bTitle) ? -1 : ((aTitle > bTitle) ? 1 : 0))`

  showForm: (num) ->
    $('#editDialog input').val('')
    $('#editDialog textarea').val('')
    $('#editDialog .hide').removeClass('hide')
    fields = ['title', 'username', 'password', 'url', 'comment', 'tags']

    if typeof num isnt "undefined" and typeof @entities[num] isnt "undefined"
      $('#editDialog input[name="num"]').val(num)
      for elem in fields
        $("#editDialog ##{elem}").val(@entities[num][elem])
      $("#editDialog input#repeat_password").val(@entities[num]['password'])
    else
      $('#editDialog button.btn-danger').addClass('hide')

    $('#editDialog').modal({})
    $('#editDialog .btn-primary').unbind('click').click =>
      if @validateForm()
        num = $('#editDialog input[name="num"]').val()
        entity = {}
        for field in fields
          entity[field] = $("##{field}").val()
        if typeof num != "undefined" and num != ""
          @entities[num] = entity
        else
          @entities.push(entity)

        @updateData =>
          $('#formClose').click()
      return

  validateForm: () ->
    $('#editDialog .has-error').removeClass('has-error')
    success = true
    if $('#title').val() == ""
      $('#title').parent().addClass('has-error')
      success = false

    if $('#password').val() isnt "" && $('#repeat_password').val() isnt $('#password').val()
      $('#password, #repeat_password').parent().addClass('has-error')
      success = false

    return success

window.CloudKeys = new CloudKeys()
$('#importLink').click =>
  $('#importContainer').toggle(500)
