       @render()
       
       # Setup the Collection Name autocomplete
+      split = (val) ->
+        return val.split /,\s*/
+      
+      extractLast = (term) ->
+        return split(term).pop()
+      
       opts =
-        source: root.collections
+        source: (req, res) ->
+          res $.ui.autocomplete.filter root.collections, extractLast req.term
+        focus: ->
+          return false
         select: (event, ui) ->
-          $('#selected-collection').val ui.item.id
-          $(this).val ui.item.value
+          terms = split this.value
+          terms.pop()
+          terms.push ui.item.value
+          terms.push ''
+          
+          ids = []
+          for collection in root.collections
+            for term in terms
+              if collection.value == term then ids.push collection.id
+          
+          $('#selected-collection').val ids.join ", "
+          $(this).val terms.join ", "
           return false
         change: (event, ui) ->
-          if not ui.item?
-            $('#selected-collection').val ''
+          terms = split this.value
+
+          ids = []
+          for collection in root.collections
+            for term in terms
+              if collection.value == term then ids.push collection.id
+          $('#selected-collection').val ids.join ", "           
+            
       $('#collection-selector').autocomplete opts
       return 
       

      'click': 'doHarvest'
      
    doHarvest: ->
      postBody =
        recordUrl: $('#input-url').val()
        inputFormat: $('input[name=harvestFormat]:checked').val()
        destinationCollections: [ $('#selected-collection').val() ]
      opts =
        type: 'POST'
        contentType: 'application/json'


$(document).ready ->
  
  class HarvestApplication extends Backbone.Viewl
    jade: new root.Jade '/static/templates/harvest-application.jade'
    
    responseJade: new root.Jade '/static/templates/harvest-response.jade'
