Resolved mixing of js and php; Removed code duplication; Fixed indentation