Added comments to javascript. Fixed 'selected' bug.
Please feel free to comment on the way I organized the code...