since your using a template, you cant add script behaviors to the body onload event.
add the behaivors for set selected and populate form an array to the onblur event of a div or the form tag, then copy the code to a script tag near the bottom of the page so it will be executed after the page loads.