     if opt.length
       # Template was selected; clear that selection and allow creation.
       templates.val([]).change()
-      $('#template_name').focus()
+      $('#template_title').focus()
     else
-      name  = $('#template_name').val().trim()
-      title = $('#template_title').val().trim()
       # Wipe any pre-existing error messages.
       $('#errors').find('li').remove()
       # User submitted new template so check it out already.

    opt.data 'image',    $('#template_image option:selected').val()
    opt.data 'shortcut', $('#template_shortcut').val().trim().toUpperCase()
    opt.text $('#template_title').val().trim()
    opt.val  $('#template_name').val().trim()
  opt

# Update the selection of templates in the toolbar behaviour section to reflect
# those available in the templates section.
updateToolbarTemplates = ->
  templates                = []
  toolbarTemplates         = $ '#toolbarTemplateName'
  toolbarTemplateName      = store.get 'toolbarTemplateName'
  lastSelectedTemplate     = toolbarTemplates.find 'option:selected'
  lastSelectedTemplateName = ''
  if lastSelectedTemplate.length
    lastSelectedTemplateName = lastSelectedTemplate.val()
  toolbarTemplates.find('option').remove()
  $('#templates option').each ->
    $this    = $ this
    template =
      name:     $this.val()
      selected: no
      title:    $this.text()
    if lastSelectedTemplateName
      template.selected = yes if template.name is lastSelectedTemplateName
    else if template.name is toolbarTemplateName
      template.selected = yes
    templates.push template
  for template in templates
    option = $ '<option/>',
      text:  "#{template.title} (#{template.name})"
      value: template.name
    option.attr 'selected', 'selected' if template.selected
    toolbarTemplates.append option

# Validation functions
# --------------------

# Indicate whether or not the specified `name` is available for use by a
# template.
isNameAvailable = (name, additionalNames = []) ->
  available = yes
  $('#templates option').each ->
    available = no if $(this).val() is name
  available and name not in additionalNames

# Indicate whether or not the specified `name` is valid for use by a template.
isNameValid = (name) ->
  name.search(R_VALID_NAME) isnt -1

# Indicate whether or not the specified keyboard `shortcut` is valid for use by
# a template.
isShortcutValid = (shortcut) ->
  shortcut.search(R_VALID_SHORTCUT) isnt -1

# Indicate whether or not `template` contains the required fields of the
# correct types.

        ext.IMAGES[template.image - 1]
      else
        ''
      template.key   = ext.getKeyForName template.name
      template.usage = 0
    if validateImportedTemplate template
      if isKeyNew template.key, keys
        # Attempt to create and add the new template.
        template = addImportedTemplate template
        if template
          data.templates.push template
          keys.push template.key
      else
        # Attempt to update the previously imported template.
        for imported, i in data.templates
          if imported.key is template.key
            existing          = updateImportedTemplate template, imported
            data.templates[i] = existing
            break
        unless existing.key
          # Attempt to derive the existing template from the available options.
          existing = deriveTemplate $ "#templates
            option[value='#{template.key}']"
          # Attempt to update the derived template.
          if existing
            existing = updateImportedTemplate template, existing
            data.templates.push existing
            keys.push existing.key
  data

# Options page setup
