Dynamic Field Names (referencing fields on the fly)

Dynamic Field Names (referencing fields on the fly)

I have the following code fire OnUpdate for my input.banana Decision Box field on my order form:

if (input.banana)
{
    input.total = (input.total  +  input.banana_cost);
}
if (!input.banana)
{
    input.total = (input.total  -  input.banana_cost);
}

When a user checks the Banana decision box, the Total field gets updated with the amount in the hidden field banana_cost.
 
 
Now, I have to perform this same function (hint hint) for 3 more fruits: apples, apricots, and grapes.
 
 
I've attempted to create a function UpdateTotal with one string parameter called "fruit" and call it from input.banana's OnUpdate .  My problems arise when:

1) I try to reference input.banana from the function, as the field isn't automatically global (or I don't understand how to make it global) so my function barks.  I've attempted to correct this by running "listofallfields = getFieldNames();", but then what?

2) Even if I could access all of the field names, how would I used the passed parameter (i.e. "banana") to be variable in the code above?