This is a fair chunk of code with little HTML.  Did you consider moving this to perl?  Although I guess it is mostly presentation and we don't have a good place for it on the perl side.