Changed the javascript coding style

Changed to the "sieve" style, where we first sieve out invalid inputs and then assume that the input is correct.

The advantages are:
1) We create less nested ifs (improves readability)
2) It is extremely easy to add a new check on input

Apart from this, minor changes: the code used a mix of space and tab for identation, changed to use only tabs.