Hi, I’m having some trouble with a Javascript code, and I was wondering if anyone can help:
I am trying to build a price estimator that has multiple fields. I would like the first two fields to have a price value based on a quantity price, so for example 1-10 qantity equals $30, 20-30 quantity equals $40 in the “Total” field). The rest of the fields in the following code work as intended whereas they just add based on the price to the right.
http://mendirobeson.info/clean-guara...-prices-f1.htm
[HTML] <html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>CLEAN GUARANTEED PRICE ESTIMATOR
<script language="JavaScript" type="text/javascript">
function CalculateTotal(frm) {
var order_total = 0
for (var i=0; i < frm.elements.length; ++i) {
form_field = frm.elements[i]
form_name = form_field.name
if (form_name.substring(0,4) == "PROD") {
item_price = parseFloat(form_name.substring(form_name.lastIndex Of("_") + 1))
item_quantity = parseInt(form_field.value)
if (item_quantity >= 0) {
order_total += item_quantity * item_price
}
}
}
frm.TOTAL.value = round_decimals(order_total, 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) {
var value_string = rounded_value.toString()
var decimal_location = value_string.indexOf(".")
if (decimal_location == -1) {
decimal_part_length = 0
value_string += decimal_places > 0 ? "." : ""
}
else {
decimal_part_length = value_string.length - decimal_location - 1
}
var pad_total = decimal_places - decimal_part_length
if (pad_total > 0) {
for (var counter = 1; counter <= pad_total; counter++)
value_string += "0"
}
return value_string
}
</script>
</head>
<body>
<FORM>
<TABLE BORDER =3>
<TD COLSPAN=3>PRICE ESTIMATOR
<TR>Please
enter <BR>
quantity:</FONT>
<TD ALIGN="CENTER">DescriptionPrice
(each)</B>
<TR>
<TD ALIGN="CENTER">
Product 1</TD>Qty Based Price
</TR>
<TR>
Product 2</TD>Qty Based Price
</TR>
<TR>
Product 3</TD>$5.00
</TR>
<TR>
Product 4</TD>$6.00
</TR>
<TR>
Product 5</TD>$7.00
</TR>
<TR>
Product 6</TD>$8.00
</TR>
<TR>
Product 7</TD>$9.00
</TR>
<TR>
TOTAL
</TABLE>
<P>
<INPUT TYPE=RESET VALUE="CLEAR FORM">
</FORM>
</body>
</html>
[/HTML]
I had another idea that maybe I could get a set price with drop-down fields, here’s the code, can’t get that to work either, thanks!
http://mendirobeson.info/clean-guar...m-prices-f2.htm
[HTML]<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>CLEAN GUARANTEED PRICE ESTIMATOR
<script language="JavaScript" type="text/javascript">
function CalculateTotal(frm) {
var order_total = 0
for (var i=0; i < frm.elements.length; ++i) {
form_field = frm.elements[i]
form_name = form_field.name
if (form_name.substring(0,4) == "PROD") {
item_price = parseFloat(form_name.substring(form_name.lastIndex Of("_") + 1))
item_quantity = parseInt(form_field.value)
if (item_quantity >= 0) {
order_total += item_quantity * item_price
}
}
}
frm.TOTAL.value = round_decimals(order_total, 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) {
var value_string = rounded_value.toString()
var decimal_location = value_string.indexOf(".")
if (decimal_location == -1) {
decimal_part_length = 0
value_string += decimal_places > 0 ? "." : ""
}
else {
decimal_part_length = value_string.length - decimal_location - 1
}
var pad_total = decimal_places - decimal_part_length
if (pad_total > 0) {
for (var counter = 1; counter <= pad_total; counter++)
value_string += "0"
}
return value_string
}
function setNumParticipants( inputId, outputId )
{
var p = document.getElementById( inputId );
var t = document.getElementById( outputId );
if(!p ) return;
if(!t ) return;
switch (p.value)
{
case "2":
t.value = "1.00";
break;
case "3":
t.value = "2.00";
break;
case "4":
t.value = "3.00";
break;
case "5":
t.value = "4.00";
break;
}
}
function setNumParticipants2( inputId, outputId )
{
var p = document.getElementById( inputId );
var t = document.getElementById( outputId );
if(!p ) return;
if(!t ) return;
switch (p.value)
{
case "7":
t.value = "5.00";
break;
case "8":
t.value = "6.00";
break;
case "9":
t.value = "7.00";
break;
case "10":
t.value = "8.00";
break;
}
}
</script>
</head>
<body>
<FORM>
<TABLE BORDER =3>
<TD COLSPAN=3>PRICE ESTIMATOR
<TR>Please
enter <BR>
quantity:</FONT>
<TD ALIGN="CENTER">DescriptionPrice
(each)</B>
<TR>
<TD ALIGN="CENTER">
<select name="numParticipants" onchange="setNumParticipants('numParticipants','TO TAL');">
<option value="1">
<option value="2">1.00
<option value="3">2.00
<option value="4">3.00
<option value="5">4.00
</select>
</TD>
Product 1</TD>Qty Based Price
</TR>
<TR>
<select name="numParticipants2" onchange="setNumParticipants2('numParticipants2',' TOTAL');">
<option value="6">
<option value="7">5.00
<option value="8">6.00
<option value="9">7.00
<option value="10">8.00
</select>
</TD>
Product 2</TD>Qty Based Price
</TR>
<TR>
Product 3</TD>$5.00
</TR>
<TR>
Product 4</TD>$6.00
</TR>
<TR>
Product 5</TD>$7.00
</TR>
<TR>
Product 6</TD>$8.00
</TR>
<TR>
Product 7</TD>$9.00
</TR>
<TR>
TOTAL
</TABLE>
<P>
<INPUT TYPE=RESET VALUE="CLEAR FORM">
</FORM>
</body>
</html>[/HTML]