Fix javascript to handle FileList with zero length

In Chrome this can happen if the user cancels the file dialog.  The browser
will then clear the FileList, causing an error in the previous code.

Also in this commit the event handlers are separated from the HTML content.