Moved javascript code to js file, fixed ajax bugs