Calculations in form with missing vars

T

Targa

Trying to total some price fields in a form but doesnt work when all the
referenced form fields dont exisit.
This is for an invoice - pulled prom a database and the form doesnt always
contain the same amount of Line Items.
If I have all 20 Line Items, it works great.

var sub1 = form.Line_Item_Subtotal1.value
var sub2 = form.Line_Item_Subtotal2.value
var sub3 = form.Line_Item_Subtotal3.value
var sub4 = form.Line_Item_Subtotal4.value
var sub5 = form.Line_Item_Subtotal........>

var sub21 = (sub1-0) + (sub2-0) + (sub3-0) + (sub4-0) .......>
form.subtotal.value = money(sub21)

How can this be written to work with any number of Line_Item_Subtotal(n)
fields?

Thanks!
 
M

Matt Kruse

Targa said:
var sub1 = form.Line_Item_Subtotal1.value
var sub21 = (sub1-0) + (sub2-0) + (sub3-0) + (sub4-0) .......>
form.subtotal.value = money(sub21)
How can this be written to work with any number of
Line_Item_Subtotal(n) fields?

something like:

var total=0;
var theform = document.form["formname"];
for (var i=1; i<=n; i++) {
total += (theform["Line_Item_Subtotal"+i].value-0);
}
theform.subtotal.value = money(total);
 
T

Targa

Thanks for the reply!
Now Im getting 'document.form.calculations' is null or not an object.
Any ideas?

Here is the entire script:
<SCRIPT LANGUAGE="JavaScript"><!--
// INVOICE CALCULATIONS
function calc(form) {
var sum = 0;
var rowsum;
var quantity = 1
// Add Lines (Line_Item_Subtotalx)

if ( parseFloat(form.Line_Qty1.value) &&
parseFloat(form.Line_Unit_Price1.value) ) {
quantity += parseInt(form.Line_Qty1.value);
form.Line_Qty1.value = parseInt(form.Line_Qty1.value);
form.Line_Unit_Price1.value = parseFloat(form.Line_Unit_Price1.value);
rowsum = form.Line_Qty1.value * form.Line_Unit_Price1.value;
sum += rowsum;
form.Line_Unit_Price1.value = money(form.Line_Unit_Price1.value);
form.Line_Item_Subtotal1.value = money(rowsum)
}
if ( parseFloat(form.Line_Qty2.value) &&
parseFloat(form.Line_Unit_Price2.value) ) {
quantity += parseInt(form.Line_Qty2.value);
form.Line_Qty2.value = parseInt(form.Line_Qty2.value);
form.Line_Unit_Price2.value = parseFloat(form.Line_Unit_Price2.value);
rowsum = form.Line_Qty2.value * form.Line_Unit_Price2.value;
sum += rowsum;
form.Line_Unit_Price2.value = money(form.Line_Unit_Price2.value);
form.Line_Item_Subtotal2.value = money(rowsum)
}
if ( parseFloat(form.Line_Qty3.value) &&
parseFloat(form.Line_Unit_Price3.value) ) {
quantity += parseInt(form.Line_Qty3.value);
form.Line_Qty3.value = parseInt(form.Line_Qty3.value);
form.Line_Unit_Price3.value = parseFloat(form.Line_Unit_Price3.value);
rowsum = form.Line_Qty3.value * form.Line_Unit_Price3.value;
sum += rowsum;
form.Line_Unit_Price3.value = money(form.Line_Unit_Price3.value);
form.Line_Item_Subtotal3.value = money(rowsum)
}
if ( parseFloat(form.Line_Qty4.value) &&
parseFloat(form.Line_Unit_Price4.value) ) {
quantity += parseInt(form.Line_Qty4.value);
form.Line_Qty4.value = parseInt(form.Line_Qty4.value);
form.Line_Unit_Price4.value = parseFloat(form.Line_Unit_Price4.value);
rowsum = form.Line_Qty4.value * form.Line_Unit_Price4.value;
sum += rowsum;
form.Line_Unit_Price4.value = money(form.Line_Unit_Price4.value);
form.Line_Item_Subtotal4.value = money(rowsum)
}
//GET SUBTOTAL
var total=0;
var theform = document.form["calculations"];
for (var i=1; i<=n; i++) {
total += (theform["Line_Item_Subtotal"+i].value-0);
}
theform.subtotal.value = money(total);

}
function money(num) // converts from floating point to money format
{
var amount = Math.abs(num);
var pounds = Math.floor(amount);
var pence = Math.round( 100*(amount-pounds) );
if(pence>99) pence=0, pounds++;
pence += ""
while (pence.length < 2) pence = "0" + pence;
amount = pounds + "." + pence;
if (num < 0) return "[" + amount + "]";
return amount;
}

//-->
</SCRIPT>




Matt Kruse said:
Targa said:
var sub1 = form.Line_Item_Subtotal1.value
var sub21 = (sub1-0) + (sub2-0) + (sub3-0) + (sub4-0) .......>
form.subtotal.value = money(sub21)
How can this be written to work with any number of
Line_Item_Subtotal(n) fields?

something like:

var total=0;
var theform = document.form["formname"];
for (var i=1; i<=n; i++) {
total += (theform["Line_Item_Subtotal"+i].value-0);
}
theform.subtotal.value = money(total);
 
R

Richard Cornford

Targa said:
Thanks for the reply!
Now Im getting 'document.form.calculations' is null or not an object.
Any ideas?
<snip>

It is - document.forms - (forms is plural).

Richard.
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in
quantity += parseInt(form.Line_Qty1.value);

Can you be sure that .value will never be entered as, for example, '08'
or '09'? See FAQ.

function money(num) // converts from floating point to money format
{
var amount = Math.abs(num);
var pounds = Math.floor(amount);
var pence = Math.round( 100*(amount-pounds) );
if(pence>99) pence=0, pounds++;
pence += ""
while (pence.length < 2) pence = "0" + pence;
amount = pounds + "." + pence;
if (num < 0) return "[" + amount + "]";
return amount;
}

Note - numbers entered as #.##5 round in a different manner in different
methods. ISTM that your "while" could be an "if", and
amount = pounds + "." + LZ(pence)
could replace 3 lines above, if LZ is known. The method gives an
improper format for vast numbers (GBP 10^21 and up).

We should consider whether the result from, say, -0.002 should show as
negative zero. Likewise for +0.002 in a method where + signs are shown.

"Matt Kruse" <[email protected]> wrote in message

Responses should go after trimmed quotes; see FAQ.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top