Well, it seems I require more help with this script.
I have been asked to add an option where if a customer so chooses, they can have a hard copy of their purchase at an extra cost. The simplest way on the actual form was to add a checkbox option at the bottom of the form, however I am having trouble getting the form to calculate the correct totals.
Thanks in advance for any assistance that can be offered.
http://www.gotbusch.com/dev/dqp/orderinfo/order2.html
Code:
/* This script is Copyright (c) Paul McFedries and
Logophilia Limited (http://www.mcfedries.com/).
Permission is granted to use this script as long as
this Copyright notice remains in place.*/
//Note: script was modified with help from member: MWinter at http://www.dynamicdrive.com/forums/
function CalculateTotal(form) {
var subTotal = 0,
discount, field, fieldName, price, quantity, hardcopy, total;
for(var i = 0, n = form.elements.length; i < n; ++i) {
field = form.elements[i];
fieldName = field.name;
if('PROD' == fieldName.substring(0, 4)) {
price = parseFloat(fieldName.substring(fieldName.lastIndexOf("_") + 1));
quantity = parseInt(field.value, 10);
if(quantity) {
subTotal += quantity * price;
}
}
}
form.elements.TOTAL.value = round_decimals(subTotal, 2);
if(30 <= subTotal) {
discount = 0.1;
} else if(100 <= subTotal) {
discount = 0.2;
} else {
discount = 0;
}
discount *= subTotal;
form.elements.discount.value = round_decimals(discount, 2);
form.elements.hardcopy.value = round_decimals(hardcopy, 2);
if(quantity) {
hardcopy += (quantity * price) + (quantity * 7);
}
if(30 <= subTotal) {
discount = 0.1;
} else if(100 <= subTotal) {
discount = 0.2;
} else {
discount = 0;
}
discount *= subTotal;
form.elements.FTOTAL.value = round_decimals(subTotal - discount + hardcopy, 2);
}
function round_decimals(original_number, decimals) {
var result1 = original_number * Math.pow(10, decimals)
var result2 = Math.round(result1)
var result3 = result2 / Math.pow(10, decimals)
return pad_with_zeros(result3, decimals)
}
function pad_with_zeros(rounded_value, decimal_places) {
// Convert the number to a string
var value_string = rounded_value.toString()
// Locate the decimal point
var decimal_location = value_string.indexOf(".")
// Is there a decimal point?
if (decimal_location == -1) {
// If no, then all decimal places will be padded with 0s
decimal_part_length = 0
// If decimal_places is greater than zero, tack on a decimal point
value_string += decimal_places > 0 ? "." : ""
}
else {
// If yes, then only the extra decimal places will be padded with 0s
decimal_part_length = value_string.length - decimal_location - 1
}
// Calculate the number of decimal places that need to be padded with 0s
var pad_total = decimal_places - decimal_part_length
if (pad_total > 0) {
// Pad the string with 0s
for (var counter = 1; counter <= pad_total; counter++)
value_string += "0"
}
return value_string
}
Bookmarks