Actually, the loop Shawn provided is working (with Richard's mod)
Each line totals up fine. I think its the next step (totaling all the lines)
that's tripping me up:
var total=0;
var form = document.forms["calculations"];
for (var i=1; i<=<%= RS("myTotal")%>; i++) {
total += (form["Line_Item_Subtotal"+i].value-0);
}
form.subtotal.value = money(total);
On page load I get 'undefined' is null or not an object.
Once I hit the calc button, I get 'value' is null or not an object.
It always returns the wrong line # so I dont know to to trace it.
Complete code:
<SCRIPT LANGUAGE="JavaScript"><!--
// INVOICE CALCULATIONS
function calc(form) {
var sum = 0;
var rowsum;
var quantity = 1;
var lineNumber = 1;
var qty;
var prc;
var subt;
qty = form['Line_Qty' + lineNumber];
prc = form['Line_Unit_Price' + lineNumber];
subt = form['Line_Item_Subtotal' + lineNumber];
while (qty.value){
if ( parseFloat(qty.value) && parseFloat(prc.value) ) {
quantity += parseInt(qty.value);
qty.value = parseInt(qty.value);
prc.value = parseFloat(prc.value);
rowsum = qty.value * prc.value;
sum += rowsum;
prc.value = money(prc.value);
subt.value = money(rowsum)
}
lineNumber++;
qty = form['Line_Qty' + lineNumber];
prc = form['Line_Unit_Price' + lineNumber];
subt = form['Line_Item_Subtotal' + lineNumber];
}
var total=0;
var form = document.forms["calculations"];
for (var i=1; i<=<%= RS("myTotal")%>; i++) {
total += (form["Line_Item_Subtotal"+i].value-0);
}
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>
"Targa" <targa1_removethistoreply_@alltel.net> wrote in message
news:IhHvc.384$783.64@fe39.usenetserver.com...[color=blue]
> Hey! Looks like youve got me on the right track!
>
> But now I get 'undefined' is null or not an object.
>
> How can I track this buggar down?
>
>
>
>
> "Richard Cornford" <Richard@litotes.demon.co.uk> wrote in message
> news:c9nbf6$3ta$1$830fa17d@news.demon.co.uk...[color=green]
> > Shawn Milo wrote:
> > <snip>[color=darkred]
> > > Try something like this (untested).
> > > Some people would say that using
> > > eval() is a bad thing, so maybe
> > > one of them will take the time to post an
> > > alternative.[/color]
> >
> > OK:-
> >
> > <snip>[color=darkred]
> > > qty = eval('form.Line_Qty' + lineNum);[/color]
> >
> > qty = form['Line_Qty' + lineNum];
> >[color=darkred]
> > > prc = eval('form.Line_Unit_Price' + lineNum);[/color]
> >
> > prc = form['Line_Unit_Price' + lineNum];
> >[color=darkred]
> > > subt = eval('form.Line_Subtotal' + lineNum);[/color]
> >
> > subt = form['Line_Subtotal' + lineNum];
> >
> > <snip>[color=darkred]
> > > qty = eval('form.Line_Qty' + lineNum);[/color]
> >
> > qty = form['Line_Qty' + lineNum];
> >[color=darkred]
> > > prc = eval('form.Line_Unit_Price' + lineNum);[/color]
> >
> > prc = form['Line_Unit_Price' + lineNum];
> >[color=darkred]
> > > subt = eval('form.Line_Subtotal' + lineNum);[/color]
> >
> > subt = form['Line_Subtotal' + lineNum];
> >
> > <URL:
http://jibbering.com/faq/#FAQ4_39 >
> >
> > In this context the alternative to - eval - is the standard bracket
> > notation property accessor syntax, supported in every javascript
> > version, shorter, simpler and between two and twenty times faster
> > depending on the browser (and unlike - eval - is not optional in ECMA
> > 327 "Compact Profile" implementations so it will also work in more
> > browsers).
> >
> > The reason that - eval - is considered a bad thing is that it really is
> > the _worst_ way of doing 99.99% of the things that are done with it.
> >
> > Richard.
> >
> >[/color]
>
>
>[/color]