Revamped JavaScript code

The old code 4 years ago was coded in procedural manner. It is bloated
and clustered. These updates change the entire JS code into more object
oriented.