Cleaned it up and separated Javascript. Added comments and may need to do a little bit more. I think it's ok for now.