PDA

View Full Version : Form Calculations



hans_cellc
10-04-2011, 08:00 PM
This code does not want to add the text box to my calculations. I also tried to parseFloat() the tDelivery but then it gives a NaN, I also tried parseInt() same Nan then With the exact code below it encapsulates the two totals printing eg: R150 R35.
All the other boxes works perfectly just the last one to add the delivery.


// Function to calculate order value
function calculateValue(orders) {
var orderValue = 0;

// Run through all the product fields
for(var i = 0; i < orders.elements.length; ++i) {

// Get the current field
var formField = orders.elements[i];

// Get the fields name
var formName = formField.name;

// Is it a "product" field?
if (formName.substring(0,4) == "prod") {

// Items price extracted from name
var itemPrice = parseFloat(formName.substring(formName.lastIndexOf("_") + 1));

// Get the Quantity
var itemQuantity = parseInt(formField.value);

// Update the OrderValue
if(itemQuantity >= 0) {
orderValue += itemQuantity * itemPrice;
}
}
}

// Display the totals
orders.totalExcl.value = "R " + orderValue.toLocaleString();

// function to calculate VAT at 15% as required and total inclusive.
function calcTotals(oValue) {
var vat = oValue * .15;
var totalIncluding = oValue + vat;
var tDelivery = orders.delivery.value;
var theTotalOrder = totalIncluding + tDelivery;
orders.vat.value = "R " + vat.toLocaleString();
orders.totalIncl.value = "R " + totalIncluding.toLocaleString();
orders.totalOrder.value = "R " + theTotalOrder.toLocaleString();
}
return calcTotals(orderValue);
}


<!-- Row 10, Col 1 Order Value exluding Vat -->
<tr><td colspan="2" align="right">Order Value Excluding Vat:</td>

<!-- Row 10, Col 2 Order Value exluding Vat Box-->
<td colspan="1" align="left">
<input name="totalExcl" type="text" style="margin-left: 60px" size="10" onfocus="this.form.elements[0].focus()" />
</td></tr>

<!-- Row 11, Col 1 Vat of 15% -->
<tr><td colspan="2" align="right">Vat calculated at 15%:</td>

<!-- Row 11, Col 2 Vat Value Box-->
<td colspan="1" align="left"><input name="vat" style="margin-left: 60px" type="text" size="10" readonly="readonly" />
</td></tr>

<!-- Row 12, Col 1 Total Order Value including Vat -->
<tr><td colspan="2" align="right">Order Value Including Vat:</td>

<!-- Row 12, Col 2 otal Order Value including Vat Box-->
<td colspan="1" align="left"><input name="totalIncl" style="margin-left: 60px" type="text" size="10" readonly="readonly" />
</td></tr>

<!-- Row 13, Col 1 Order Value -->
<tr><td colspan="2" align="right">Delivery Options:
<input type="radio" name="sapo" value="35" onclick="delivery.value='R ' + this.value" /> R35 - SA Post Office
<input type="radio" name="sapo" value="80" onclick="delivery.value='R ' + this.value" /> R80 - Speed Services
<input type="radio" name="sapo" value="150" onclick="delivery.value='R ' + this.value" /> R150 - Courier Services
</td>

<!-- Row 13, Col 2 Order Value Box-->
<td colspan="1" align="left"><input name="delivery" style="margin-left: 60px" type="text" size="10" readonly="readonly" />
</td></tr>

<!-- Row 14, Col 1 Total Order Value including Vat -->
<tr><td colspan="2" align="right"><strong>Total Order Value:</strong></td>

<!-- Row 14, Col 2 otal Order Value including Vat Box-->
<td colspan="1" align="left"><input name="totalOrder" style="margin-left: 60px" type="text" size="10" readonly="readonly" />
</td></tr>

Old Pedant
10-05-2011, 12:26 AM
*WHICH* "text box"?????

If you mean "delivery", then of course it won't work right.

delivery.value is going to be "R 35" or something like that, and you can't convert that to a number.

Well, you can, but you have to strip off the "R" and the space first.

Possibly like this:


var tDelivery = Number( orders.delivery.value.substring(2) );

A better solution, I think, would be to ignore delivery.value and go get the number from the set of radio buttons, directly. But either could work.