Slighty improve javascript.

- use only one call of sql_select
- use only one call of sql_format
- use jQuery selector instead of getElementById to avoid js errors when
not found.