A JavaScript based finite-state machine