Refactoring plan editing JavaScript

Moved functions out of $(document).ready(), removed some duplication