JS: cleaned up by removing global variables.

The javascript is a somewhat mixed bag of stuff
still. Some further refactoring is needed.