Order 'total' problem.

Discussion in 'Javascript' started by Ed, Dec 15, 2003.

  1. Ed

    Ed Guest

    Thank you Michael, you helped me to add more than one product to the order
    form. Will also take your advice when I get past the basics.

    I now need to collect the results from the two products and put them into a
    'Total' field.
    Also need to add 'freight'.
    Would normally be ok with this, but the 'result' from each product field is
    a little different to most. (Have a look at the HTML result).
    Again, any assistance would be appreciated.

    PS: Please let me know if there is somewhere else I should be posting these
    questions. Seems I am very much a beginner.

    Have attached the code below for the example.

    Thank you.


    </head>
    <META http-equiv="Content-Script-Type" content="text/javascript">
    <BODY onLoad="findPrice();">
    <form name="Hypersnap" method="post">
    <SCRIPT type="text/javascript">
    var max_hsunits1 = 1000;
    var currency = "$"; // currency sign used in 'formatMessage()'
    function getDiscountPrice(hsunits1) {
    if (hsunits1 >= max_hsunits1) return 18;
    if (hsunits1 >= 750) return 20;
    if (hsunits1 >= 500) return 25;
    if (hsunits1 >= 250) return 28;
    if (hsunits1 >= 150) return 30;
    if (hsunits1 >= 75) return 31;
    if (hsunits1 >= 50) return 40;
    if (hsunits1 >= 25) return 45;
    if (hsunits1 >= 10) return 47;
    if (hsunits1 >= 5) return 50;
    if (hsunits1 >= 1) return 55;
    if (hsunits1 <= 0) return 0;
    }
    function getNumberOfUnits() {
    var hsunits1 = document.forms['Hypersnap'].elements['hsunits1'].value;
    return (hsunits1 == "") ? 0 : hsunits1;
    }
    function showResult(result) {
    document.Hypersnap.hsresult1.value = result;
    }
    function formatMessage(hsunits1, unit_price) {
    return hsunits1 + " * " + currency + formatPrice(unit_price) + " = " +
    currency + formatPrice(hsunits1 * unit_price);
    }
    function getAltUnits(hsunits1) {
    var discount_price = getDiscountPrice(hsunits1);
    if (hsunits1 < max_hsunits1) do { hsunits1++ } while (discount_price ==
    getDiscountPrice(hsunits1));
    return hsunits1;
    }
    function findPrice() {
    var hsunits1 = getNumberOfUnits();
    var unit_price = getDiscountPrice(hsunits1);
    var alt_hsunits1 = getAltUnits(hsunits1);
    var alt_unit_price = getDiscountPrice(alt_hsunits1);
    var result;
    if ((hsunits1 * unit_price) < (alt_hsunits1 * alt_unit_price))
    result = formatMessage(hsunits1, unit_price);
    else
    result = formatMessage(alt_hsunits1, alt_unit_price);
    showResult(result);
    }
    function formatPrice(value) {
    var result= Math.floor(value) + ".";
    var cents = 100 * (value-Math.floor(value)) + 0.5;
    result += Math.floor(cents / 10);
    result += Math.floor(cents % 10);
    return result;
    }
    function filterNonNumeric(field) {
    var result = new String();
    var numbers = "0123456789";
    var chars = field.value.split(""); // create array
    for (i = 0; i < chars.length; i++) {
    if (numbers.indexOf(chars) != -1) result += chars;
    }
    if (field.value != result) field.value = result;
    }
    </script>
    <SCRIPT type="text/javascript">
    var max_hsunits2 = 1000;
    var currency = "$"; // currency sign used in 'formatMessage2()'
    function getDiscountPrice2(hsunits2) {
    if (hsunits2 >= max_hsunits2) return 18;
    if (hsunits2 >= 750) return 20;
    if (hsunits2 >= 500) return 25;
    if (hsunits2 >= 250) return 28;
    if (hsunits2 >= 150) return 30;
    if (hsunits2 >= 75) return 31;
    if (hsunits2 >= 50) return 40;
    if (hsunits2 >= 25) return 45;
    if (hsunits2 >= 10) return 47;
    if (hsunits2 >= 5) return 50;
    if (hsunits2 >= 1) return 47;
    if (hsunits2 <= 0) return 0;
    }
    function getNumberOfUnits2() {
    var hsunits2 = document.forms['Hypersnap'].elements['hsunits2'].value;
    return (hsunits2 == "") ? 0 : hsunits2;
    }
    function showResult2(result2) {
    // adjust the following line if result2 must popup somewhere else
    document.Hypersnap.hsresult2.value = result2;
    }
    function formatMessage2(hsunits2, unit_price2) {
    return hsunits2 + " * " + currency + formatPrice2(unit_price2) + " = " +
    currency + formatPrice2(hsunits2 * unit_price2);
    }
    // AltUnits2 (alternate hsunits2): add extra hsunits2 to reach minimum for
    next discount price
    function getAltUnits2(hsunits2) {
    var discount_price2 = getDiscountPrice2(hsunits2);
    if (hsunits2 < max_hsunits2) do { hsunits2++ } while (discount_price2 ==
    getDiscountPrice2(hsunits2));
    return hsunits2;
    }
    function findPrice2() {
    var hsunits2 = getNumberOfUnits2();
    var unit_price2 = getDiscountPrice2(hsunits2);
    var alt_hsunits2 = getAltUnits2(hsunits2);
    var alt_unit_price2 = getDiscountPrice2(alt_hsunits2);
    var result2;
    if ((hsunits2 * unit_price2) < (alt_hsunits2 * alt_unit_price2))
    result2 = formatMessage2(hsunits2, unit_price2);
    else
    result2 = formatMessage2(alt_hsunits2, alt_unit_price2);
    showResult2(result2);
    }
    function formatPrice2(value2) {
    var result2= Math.floor(value2) + ".";
    var cents = 100 * (value2-Math.floor(value2)) + 0.5;
    result2 += Math.floor(cents / 10);
    result2 += Math.floor(cents % 10);
    return result2;
    }
    function filterNonNumeric(field) {
    var result2 = new String();
    var numbers = "0123456789";
    var chars = field.value.split(""); // create array
    for (i = 0; i < chars.length; i++) {
    if (numbers.indexOf(chars) != -1) result2 += chars;
    }
    if (field.value2 != result2) field.value = result2;
    }
    </script>
    <p>&nbsp;
    <p><font face="Arial" size="2"><b>Product 1 - Number of
    Users:</b></font>
    <input type=text value="0" name="hsunits1" onkeydown="findPrice()"
    onKeyUp="filterNonNumeric(this); findPrice()" onkeypress="findPrice()"
    size="4" style="font-weight:bold; text-align:center;">
    <input type=text onfocus="this.blur()" name="hsresult1" size="24"
    style="border:0; font-weight:bold;">
    <br><font face="Arial" size="2"><b>Product 2 - Number of
    Users:</b></font>
    <input type=text value="0" name="hsunits2" onkeydown="findPrice2()"
    onKeyUp="filterNonNumeric(this); findPrice2()" onkeypress="findPrice2()"
    size="4" style="font-weight:bold; text-align:center;">
    <input type=text onfocus="this.blur()" name="hsresult2" size="24"
    style="border:0; font-weight:bold;">
    &nbsp;<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs
    p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n
    bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="black"><select
    name="hsfreight" tabindex="46" onchange="ComputeForm()"
    language="JavaScript" size="1">
    <option value="-/0" selected>Please select
    your choice of freight</option>
    <option value="AUD/700">Australia - Standard
    $7.00</option>
    <option value="AUD/1500">Outside Australia
    $15.00</option>
    <option value="Nil/000">No Freight - (Software
    Downloads)</option>
    <option value="-/0"></option>
    </select></font>&nbsp;<p align="left"><font color="black"
    face="Arial"><strong>Total Sales</strong></font><font
    color="black"><strong>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs
    p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input
    type="text" name="hstotal" value="0.00" size="16"
    onchange="ComputeForm()"
    style="font-family:Arial; font-style:normal;
    font-weight:bolder; font-size:11pt; color:white;
    background-color:black;"></strong></font></p>
    </form>
    </html>
    Ed, Dec 15, 2003
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Sko=?=

    Total Field Problem

    =?Utf-8?B?Sko=?=, Jan 10, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    310
    =?Utf-8?B?Sko=?=
    Jan 11, 2005
  2. Soren Kuula
    Replies:
    2
    Views:
    481
    Soren Kuula
    Feb 1, 2004
  3. cspoh
    Replies:
    0
    Views:
    233
    cspoh
    Jul 31, 2003
  4. Stephan Kämper
    Replies:
    2
    Views:
    227
    Stephan Kämper
    Jan 18, 2004
  5. Replies:
    1
    Views:
    83
    Dr John Stockton
    Jan 22, 2006
Loading...

Share This Page