Reformatted all templates, externalised most javascript

Made minor changes and corrections in the markup, but most changes are whitespace.