Thanks for the tip Hiko!
I do want to caution about one thing which you may want to change: I found out about a year or so ago that the embedded Rhino engine version we use has a bit of a bug when it comes to string comparisons.
The best option around that is to use '===' operator instead of '=='.
Here's the W3CSchools reference: http://www.w3schools.com/js/js_comparisons.asp
This little problem gave me a headache for about two weeks. Thanks to KulbirNijjer for pointing me in the right direction.