Javascript fails to generate correct html for doubly nested forms

If you have a doubly nested creation form, the javascript will incorrectly strip out the name of the second association and replace it with an integer, breaking the form.