/* global Headers, fetch */
(function (w) {
  if (!w.fetch || !w.Headers || !w.FormData) return

  const d = window.document
  let form = d.getElementById('login-form')
  const url = form.getAttribute('action')
  const passphraseInput = d.getElementById('password')
  const redirectInput = d.getElementById('redirect')
  const submitButton = d.getElementById('login-submit')
  let errorPanel = form.querySelector('.errors')

  const showError = function (error) {
    error = error || 'The Cozy server is unavailable. Do you have network?'

    if (!errorPanel) {
      errorPanel = d.createElement('div')
      errorPanel.classList.add('errors')
      form.appendChild(errorPanel)
    }

    errorPanel.innerHTML = `<p>${error}</p>`
    submitButton.removeAttribute('disabled')
  }

  form.addEventListener('submit', (event) => {
    event.preventDefault()
    submitButton.setAttribute('disabled', true)

    const passphrase = passphraseInput.value
    const redirect = redirectInput.value + window.location.hash
    let headers = new Headers()
    headers.append('Content-Type', 'application/x-www-form-urlencoded')
    headers.append('Accept', 'application/json')
    fetch('/auth/login', {
      method: 'POST',
      headers: headers,
      body: `passphrase=${encodeURIComponent(passphrase)}&redirect=${encodeURIComponent(redirect)}`,
      credentials: 'same-origin'
    }).then((response) => {
      const loginSuccess = response.status < 400
      response.json().then((body) => {
        if (loginSuccess) {
          submitButton.innerHTML = '<i class="fa fa-check"></i>'
          submitButton.classList.add('btn-success')
          if (body.redirect) {
            window.location = body.redirect
          } else {
            form.submit()
          }
        } else {
          showError(body.error)
        }
      }).catch(showError)
    }).catch(showError)
  })

  passphraseInput.focus()
  submitButton.removeAttribute('disabled')

  // Preload font awesome
  try { document.fonts.load('14px FontAwesome') } catch (e) {}
})(window)

@@ -713,25 +713,30 @@
 L = 
-%60%3Cp%3E$%7B
+'%3Cp%3E' + 
 error
-%7D
+ + '
 %3C/p%3E
-%60
+'
 %0A   
@@ -1252,17 +1252,17 @@
   body: 
-%60
+'
 passphra
@@ -1264,18 +1264,20 @@
 sphrase=
-$%7B
+' + 
 encodeUR
@@ -1298,17 +1298,20 @@
 sphrase)
-%7D
+ + '
 &redirec
@@ -1316,10 +1316,12 @@
 ect=
-$%7B
+' + 
 enco
@@ -1348,10 +1348,8 @@
 ect)
-%7D%60
 ,%0A  

Fix JS for IE11