This isn't an easy function to clean up all by itself, Paul. For instance, we don't know what the DebrX variables are when this is called -- whether they are field names or field objects.
In any case, JavaScript doesn't have "variable variables", so this part can't work:
if ( "Type"+ n != "" || parseFloat("Type" + n) > 0 )
The expression "Type" + n will always evaluate to a string, so when n is 1, you've got the equivalent of:
if ( "Type1" != "" || parseFloat("Type1") > 0 )
Obviously, that's not what you want. You have a choice here. One is to use eval() to turn the string into the code it represents, but eval() is almost never the right option unless you are programming in a functional style (returning a function from a function -- explaining what that means is beyond the scope of this forum, but if you have three or four hours to kill, you can watch the first four lectures of
Structure and Interpretation of Computer Programs and have your mind blown). The simpler option is to set up your "Type" variable as an array:
var Type = new Array();
Type[1] = Debr1.value;
Type[2] = Debr2.value;
//and so on
That means your test for the if() loop becomes:
if ( Type[n] != "" || parseFloat( Type[n] ) > 0 )
Now, I have to say that the "n" is really bothering me. It's perfectly valid, but it's unconventional. One should always try to keep to the
Principle of Least Astonishment when coding so the person maintaining the code has to think as little as possible. There's a convention for iterator variables (the counters in a for-loop) going back to the dawn of time that says they should be named "i", "j" or "k"; "i" being used for the outer loop, "j" for a loop nested inside the "i" loop, and "k" for a loop nested inside the "j" loop (let's hope you don't see many "k" loops). I see that you're sending an "i" to this function, but I don't see it used anywhere.
You're also making the "n" a global variable because you haven't used the
var keyword in the declaration in the for loop. When you use
var, you make the variable a local to the loop, so it won't interact with the rest of your code:
for (
var i = 0; i < someValue; i++) {...}
Finally, there's the little matter of the test for your for loop:
for(n = 1; n == 5; n++);
The loop can only run while the test (the second term in the parentheses) is true. When n is one, it's not 5, so the loop can't start. It should be:
for (
var i = 0; i < 5; i++) {...}
(Feel free to go back to "n" if you absolutely have to -- you'll hurt my feelings, but you won't really hurt the code.)