I'm trying to add numbers to an array when users click on a link.

More specifically, I have a form with an array like this:
HTML Code:
<input type="hidden" name="testarray[]" value="0">
I then have a popup-window where users can click on multiple images to select the images. I want the id of the image to be added to the array if they select that image. (And the id removed from the array if they click the image again. This way the array ends up 2,3,6 or 4,59 (or 0 if no images are selected) when the form is submitted.

Can this be done?

The script I have so far:

function select(id) {    
var theElement = window.opener.document.getElementById('testarray');
But then I'm stuck
Can anyone offer some help?