Please help Javascript Calculation Forms

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • theflyingminstrel
    New Member
    • Jul 2007
    • 5

    Please help Javascript Calculation Forms

    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.




    [HTML] <html>

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

    <script language="JavaS cript" type="text/javascript">

    function CalculateTotal( frm) {
    var order_total = 0

    for (var i=0; i < frm.elements.le ngth; ++i) {

    form_field = frm.elements[i]

    form_name = form_field.name

    if (form_name.subs tring(0,4) == "PROD") {

    item_price = parseFloat(form _name.substring (form_name.last IndexOf("_") + 1))

    item_quantity = parseInt(form_f ield.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(resu lt1)
    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.t oString()

    var decimal_locatio n = value_string.in dexOf(".")

    if (decimal_locati on == -1) {

    decimal_part_le ngth = 0

    value_string += decimal_places > 0 ? "." : ""
    }
    else {

    decimal_part_le ngth = value_string.le ngth - decimal_locatio n - 1
    }

    var pad_total = decimal_places - decimal_part_le ngth

    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"> DescriptionPric e

    (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!



    [HTML]<html>

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

    <script language="JavaS cript" type="text/javascript">

    function CalculateTotal( frm) {
    var order_total = 0

    for (var i=0; i < frm.elements.le ngth; ++i) {

    form_field = frm.elements[i]

    form_name = form_field.name

    if (form_name.subs tring(0,4) == "PROD") {

    item_price = parseFloat(form _name.substring (form_name.last IndexOf("_") + 1))

    item_quantity = parseInt(form_f ield.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(resu lt1)
    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.t oString()

    var decimal_locatio n = value_string.in dexOf(".")

    if (decimal_locati on == -1) {

    decimal_part_le ngth = 0

    value_string += decimal_places > 0 ? "." : ""
    }
    else {

    decimal_part_le ngth = value_string.le ngth - decimal_locatio n - 1
    }

    var pad_total = decimal_places - decimal_part_le ngth

    if (pad_total > 0) {

    for (var counter = 1; counter <= pad_total; counter++)
    value_string += "0"
    }
    return value_string
    }



    function setNumParticipa nts( inputId, outputId )
    {
    var p = document.getEle mentById( inputId );
    var t = document.getEle mentById( 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 setNumParticipa nts2( inputId, outputId )
    {
    var p = document.getEle mentById( inputId );
    var t = document.getEle mentById( 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"> DescriptionPric e

    (each)</B>
    <TR>
    <TD ALIGN="CENTER">
    <select name="numPartic ipants" onchange="setNu mParticipants(' numParticipants ','TOTAL');">
    <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="numPartic ipants2" onchange="setNu mParticipants2( 'numParticipant s2','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]
    Last edited by gits; Jul 21 '07, 09:36 AM. Reason: fix CODE tags
  • mplungjan
    New Member
    • Dec 2007
    • 13

    #2
    I tried to help but the link do not work and the html you pasted also not

    Comment

    Working...