A simple Javascript finite state machine.