Refactor js

**Resolves issues #777 and #780.**

---

Rewriting our js to be more readable and less hacky.



`jQuery.when()` takes a `Deferred` and returns a `Promise`, which is just a more restricted `Deferred`. Calling `when()` on the `Deferred` returned by `ajax_form_submit` doesn't really serve a useful purpose here.