clean up javascript code

- Move the template selection into the options hash
- Extract some more html code from the javascript
- Use named functions for ajax callbacks
- use a json object instead of an array for the server response
- fix autocompletion when entering multiple commands