Refactored JavaScripts and put common functions in main.html.scala.