PDA

View Full Version : Please help Javascript Calculation Forms



theflyingminstrel
07-21-2007, 08:07 AM
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-guaranteed/test/auto-sum-prices-f1.htm


<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>CLEAN GUARANTEED PRICE ESTIMATOR</title>

<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.lastIndexOf("_") + 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><b>PRICE ESTIMATOR</b></TD>

<TR><TD ALIGN="CENTER"><B>Please <BR>

enter <BR>
quantity:</FONT></TD>
<TD ALIGN="CENTER"><B>Description</TD><TD ALIGN="CENTER"><B>Price<BR>
(each)</B></TD></TR>
<TR>
<TD ALIGN="CENTER"><INPUT TYPE=TEXT NAME="PROD_SP_0.00" SIZE=3 MAXLENGTH=3 onChange="CalculateTotal(this.form)"></TD><TD>
Product 1</TD><TD ALIGN="RIGHT">Qty Based Price</TD>
</TR>
<TR><TD ALIGN="CENTER"><INPUT TYPE=TEXT NAME="PROD_SPMG_0.00" SIZE=3 MAXLENGTH=3 onChange="CalculateTotal(this.form)"></TD><TD>
Product 2</TD><TD ALIGN="RIGHT">Qty Based Price</TD>

</TR>
<TR><TD ALIGN="CENTER"><INPUT TYPE=TEXT NAME="PROD_SPCR_5.00" SIZE=3 MAXLENGTH=3 onChange="CalculateTotal(this.form)"></TD><TD>
Product 3</TD><TD ALIGN="RIGHT">$5.00</TD>
</TR>
<TR><TD ALIGN="CENTER"><INPUT TYPE=TEXT NAME="PROD_SW_6.00" SIZE=3 MAXLENGTH=3 onChange="CalculateTotal(this.form)"></TD><TD>
Product 4</TD><TD ALIGN="RIGHT">$6.00</TD>
</TR>
<TR><TD ALIGN="CENTER"><INPUT TYPE=TEXT NAME="PROD_SWMG_7.00" SIZE=3 MAXLENGTH=3 onChange="CalculateTotal(this.form)"></TD><TD>
Product 5</TD><TD ALIGN="RIGHT">$7.00</TD>
</TR>
<TR><TD ALIGN="CENTER"><INPUT TYPE=TEXT NAME="PROD_RY_8.00" SIZE=3 MAXLENGTH=3 onChange="CalculateTotal(this.form)"></TD><TD>
Product 6</TD><TD ALIGN="RIGHT">$8.00</TD>
</TR>

<TR><TD ALIGN="CENTER"><INPUT TYPE=TEXT NAME="PROD_KT_9.00" SIZE=3 MAXLENGTH=3 onChange="CalculateTotal(this.form)"></TD><TD>
Product 7</TD><TD ALIGN="RIGHT">$9.00</TD>
</TR>
<TR><TD><BR></TD><TD>TOTAL</TD><TD ALIGN="RIGHT"><INPUT TYPE=TEXT NAME=TOTAL SIZE=10 onFocus="this.form.elements[0].focus()"></TD>
</TABLE>
<P>

<INPUT TYPE=RESET VALUE="CLEAR FORM">

</FORM>


</body>

</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-guaranteed/test/auto-sum-prices-f2.htm


<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>CLEAN GUARANTEED PRICE ESTIMATOR</title>

<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.lastIndexOf("_") + 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><b>PRICE ESTIMATOR</b></TD>

<TR><TD ALIGN="CENTER"><B>Please <BR>

enter <BR>
quantity:</FONT></TD>
<TD ALIGN="CENTER"><B>Description</TD><TD ALIGN="CENTER"><B>Price<BR>
(each)</B></TD></TR>
<TR>
<TD ALIGN="CENTER">
<select name="numParticipants" onchange="setNumParticipants('numParticipants','TOTAL');">
<option value="1"></option>
<option value="2">1.00</option>
<option value="3">2.00</option>
<option value="4">3.00</option>
<option value="5">4.00</option>
</select>
</TD><TD>
Product 1</TD><TD ALIGN="RIGHT">Qty Based Price</TD>
</TR>
<TR><TD ALIGN="CENTER">
<select name="numParticipants2" onchange="setNumParticipants2('numParticipants2','TOTAL');">
<option value="6"></option>
<option value="7">5.00</option>
<option value="8">6.00</option>
<option value="9">7.00</option>
<option value="10">8.00</option>
</select>
</TD><TD>
Product 2</TD><TD ALIGN="RIGHT">Qty Based Price</TD>

</TR>
<TR><TD ALIGN="CENTER"><INPUT TYPE=TEXT NAME="PROD_SPCR_5.00" SIZE=3 MAXLENGTH=3 onChange="CalculateTotal(this.form)"></TD><TD>
Product 3</TD><TD ALIGN="RIGHT">$5.00</TD>
</TR>
<TR><TD ALIGN="CENTER"><INPUT TYPE=TEXT NAME="PROD_SW_5.00" SIZE=3 MAXLENGTH=3 onChange="CalculateTotal(this.form)"></TD><TD>
Product 4</TD><TD ALIGN="RIGHT">$6.00</TD>
</TR>
<TR><TD ALIGN="CENTER"><INPUT TYPE=TEXT NAME="PROD_SWMG_5.00" SIZE=3 MAXLENGTH=3 onChange="CalculateTotal(this.form)"></TD><TD>
Product 5</TD><TD ALIGN="RIGHT">$7.00</TD>
</TR>
<TR><TD ALIGN="CENTER"><INPUT TYPE=TEXT NAME="PROD_RY_5.00" SIZE=3 MAXLENGTH=3 onChange="CalculateTotal(this.form)"></TD><TD>
Product 6</TD><TD ALIGN="RIGHT">$8.00</TD>
</TR>

<TR><TD ALIGN="CENTER"><INPUT TYPE=TEXT NAME="PROD_KT_5.00" SIZE=3 MAXLENGTH=3 onChange="CalculateTotal(this.form)"></TD><TD>
Product 7</TD><TD ALIGN="RIGHT">$9.00</TD>
</TR>
<TR><TD><BR></TD><TD>TOTAL</TD><TD ALIGN="RIGHT"><INPUT TYPE=TEXT NAME=TOTAL SIZE=10 onFocus="this.form.elements[0].focus()"></TD>
</TABLE>
<P>

<INPUT TYPE=RESET VALUE="CLEAR FORM">

</FORM>


</body>

</html>