Various JS improvements

- avoid double variable declaration
- reorder functions to be defined before used
Various javascript fixes

- replaced some (.. ? .. : .. ) with if for more readability
- missed escapin in some regexps
Various js improvements

- move variable declarations to top
- fixed linebreaks around +
- removed extra space
Better label name and fix indentation
Fix line breaks before +
Various javascript fixes

- line breaking around operators
- wrong scope of variables