Update to JavaScript and HTML to make sure it suits MAS coding standards

This JavaScript is still not perfect but at least maintainable from our point of view.
I found it easier to heavily refactor it rather than wait for more updates.

After merging please retest if all functionality still works as it should.