 $ = jQuery
 
 supportSelectionEnd = 'selectionEnd' of document.createElement('input')
+
+# Timeout to work around cursor bugs in Android Firefox
+withTimeout = (fn) -> setTimeout(fn, 50)
 
 formatForPhone_ = (phone, defaultPrefix = null) ->
   if phone.indexOf('+') != 0 and defaultPrefix

    e.preventDefault()
    @val(phone)
    if !selectionAtEnd
      @caret(selection)

formattedPhone_ = (phone, lastChar) ->
  if phone.indexOf('+') != 0 && @data('defaultPrefix')

  value = value.substring(0, @caret()) +
          String.fromCharCode(e.which) +
          value.substring(caretEnd, value.length)
  format_.call(@, value, e)
  withTimeout => @caret(@val().length)

formatUp_ = (e) ->
  checkForCountryChange_.call(@)
