refactor js into separate named files, prepare for more OO

I did make one substantive change to how the messages are looked up. They are now generated only when needed, and the same function is called (with a different parameter to generate them).