Dried some javascript out into a function