Attaching the handlers to the window. In the previous implementation, when the page was re-drawn after an ajax call all the handlers was being missed.

Also reloaded the checkboxes from the selected_items dict after an ajax call
Refactor, deleted duplicated code.