473,395 Members | 1,676 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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.

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]
Jul 21 '07 #1
1 1799
I tried to help but the link do not work and the html you pasted also not
Dec 26 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

53
by: Cardman | last post by:
Greetings, I am trying to solve a problem that has been inflicting my self created Order Forms for a long time, where the problem is that as I cannot reproduce this error myself, then it is...
11
by: milkyway | last post by:
Hello, I have an HTML page that I am trying to import 2 .js file (I created) into. These files are: row_functions.js and data_check_functions.js. Whenever I bring the contents of the files into...
9
by: chris vettese | last post by:
On my subform I have a field in the footer that totals the value of a field. On the main form I have referenced this field. I'm using this field in a calculation on my main form. The problem...
5
by: Aravind | last post by:
Hi folks. I have a form, frmHistory, that has the following code for its On Open event. =-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-= Private Sub Form_Open(Cancel As Integer) ...
2
by: Del | last post by:
Thanks in advance for any help. I have a database that was created in Access 2000. Several users have been upgraded to Access 2003. Since upgrading to 2003 we have noticed that some of the...
4
by: Michiel Alsters | last post by:
Hello everybody, I hope anybody can help me. I'll try to give a brief overview of my problem. I have running a program that performs a heavy calculation. To give the user feedback what the...
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
14
by: ap.sakala | last post by:
Hello, How the heck should I make this simple summering of a data without a submit button? Like in an excel sheet I would like to have a couple of cells in a column and as soon the visitor...
2
by: semutmerah | last post by:
hi.. my english maybe wrong.. but hope somebody can understand me :) I did created 3 forms. "MainForm" , "CalForm" , "WaitForm". On "MainForm", I did put a command button called "Run". ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.