Comet, Comet on the Wall: Fix comets sent down via AJAX from within a comet

This fixes a few issues:
 - We centralize comet command generation into `S.jsToAppend`.
 - We fix when we look up `S.jsToAppend` during AJAX processing in comet contexts so we don't miss anything that might be added during the actual AJAX function execution.
 - We make sure not to trigger a second comet request when setting up new comets client-side from within a comet response.

Fixes issue #1645 .
Am I wrong to complain about the asymmetry of these lines? If the result of the case goes on the next line here, I feel like it should for 953 as well.