The "Custom JavaScript" page is definitely not the way to go for such a complex modification.
Instead you should create a custom module that loads your JavaScript file on the product page:
http://kb.x-cart.com/display/XDD/Adding+CSS+and+JS+files
The tutorial explains how to load a custom JS file on every site page, but in your case you should inject it into one of the widget classes that are displayed on the product page only. Perhaps, the quantity box is the widget that you should extend from your module.