By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,712 Members | 2,063 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,712 IT Pros & Developers. It's quick & easy.

Form problem - Price break

P: n/a
Ed
Hoping someone an assist me urgently. I would truly appreciate and help.

I have a form and prices are based on 'price break'.

1.The price break fields work fine, but I cannot for the life of me add more
than one to a page.
2. Also need to add the two results to a 'total' field. (See code attached).

Many thanks in advance.

Sample code ...

<HEAD>

</HEAD>

<BODY onLoad="findPrice();">

<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
var max_hsunits1 = 1000; // quantities in excess of max_hsunits1 all have
the same unit price
var currency = "$"; // currency sign used in 'formatMessage()'
// Edit this function to reflect discount prices!
function getDiscountPrice(hsunits1) {
// Note: Work way down from max to min amounts!
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.Hypersnap.hsunits1.value;
return (hsunits1 == "") ? 0 : hsunits1;
}
function showResult(result) {
// adjust the following line if result must popup somewhere else
document.Hypersnap.hsresult1.value = result;
}
function formatMessage(hsunits1, unit_price) {
return hsunits1 + " * " + currency + formatPrice(unit_price) + " = " +
currency + formatPrice(hsunits1 * unit_price);
}
// AltUnits (alternate hsunits1): add extra hsunits1 to reach minimum for
next discount 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[i]) != -1) result += chars[i];
}
if (field.value != result) field.value = result;
}
// End -->
</script>
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
var max_hsunits2 = 1000; // quantities in excess of max_hsunits2 all have
the same unit price
var currency = "$"; // currency sign used in 'formatMessage2()'
// Edit this function to reflect your discount prices!
function getDiscountPrice2(hsunits2) {
// Note: It is important to work your way down from max to min amounts!
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 55;
if (hsunits2 <= 0) return 0;
}
function getNumberOfUnits2() {
var hsunits2 = document.Hypersnap.hsunits2.value2;
return (hsunits2 == "") ? 0 : hsunits2;
}
function showResult2(result2) {
// adjust the following line if result2 must popup somewhere else
document.Hypersnap.hsresult22.value2 = 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 * (value-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[i]) != -1) result2 += chars[i];
}
if (field.value2 != result2) field.value = result2;
}
// End -->
</script>
<form name="Hypersnap" method="post">

&nbsp;
<p><font face="Arial" size="2"><b>Number of Users Required :</b></font>
<input type=text value="1" 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>Number of Users Required :</b></font>
<input type=text value="1" name="hsunits2" onkeydown="findPrice()"
onKeyUp="filterNonNumeric(this); findPrice()" onkeypress="findPrice()"
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;</form>
<form name="form1">
<p align="left"><font
color="black"><strong>Total Sales
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&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;"
language="JavaScript"></strong></font></p>
</form>
</html>


Jul 20 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Ed wrote on 14 Dec 2003 at Sun, 14 Dec 2003 15:35:44 GMT:
Hoping someone an assist me urgently. I would truly appreciate
and help.

I have a form and prices are based on 'price break'.

1.The price break fields work fine, but I cannot for the life of
me add more than one to a page.
2. Also need to add the two results to a 'total' field. (See
code attached).
Rather than hard-code the form elements that the script checks, pass
the form elements as objects to the functions. For example:

<FORM name="someForm" id="someForm">
<INPUT name="someTextBox" onkeypress="checkKey()">

function checkKey() {
var txtBox = document.forms['someForm'].elements['someTextBox'];

// Do something with the text box
txtBox.value
}

What I've presented above is similar to how you access the form in
your code below. It would work fine, but it can only access the hard-
coded forms and controls. One could change it to this:

<FORM name="someForm" id="someForm">
<INPUT name="someTextBox" onkeypress="checkKey(this)">

function checkKey(control) {
var txtBox = control;

// Do something with the text box
txtBox.value
}

Notice the function call and how the control is referenced. The
snippet above is effectively the same: the variable 'txtBox' refers
to the same control, someTextBox. When the keyword, this, is used in
an intrinsic event, it creates an object reference that represents
the control. You can then use that reference in the same way as you
would a fully-qualified one. This way, you can use the same function
on multiple controls.

That will stop you from having to duplicate the same script, and
allow you to use as many price breaks (whatever they are) as you
like. What follows are some comments on your HTML.
<HEAD>
You are using intrinsic events, but you have not specified the
default scripting language. Include:

<META http-equiv="Content-Script-Type" content="text/javascript">

here. Also, HTML documents required a TITLE element (and a DOCTYPE).
</HEAD>

<BODY onLoad="findPrice();">

<SCRIPT LANGUAGE="JavaScript">
The type attribute is /required/ in HTML. When used, it renders the
language attribute (which is deprecated, anyway) redundant.

<SCRIPT type="text/javascript">
<!-- Begin
Don't use SGML comments to hide SCRIPT contents - there's no need as
the archaic reasons for doing it are no longer valid.

<snip>
function getNumberOfUnits() {
var hsunits1 = document.Hypersnap.hsunits1.value;
return (hsunits1 == "") ? 0 : hsunits1;
}
To make your script work across more browsers, you should use the
'collections syntax' when accessing form elements:

var hsunits1 =
document.forms['Hypersnap'].elements['hsunits1'].value;

<snip>
// End -->
This closing comment should be removed.
</script>
<snipped duplicate script>
<form name="Hypersnap" method="post">

&nbsp;
<p><font face="Arial" size="2"><b>Number of Users Required
:</b></font>
Why use style sheets, but also include deprecated FONT, B, and bad
layout techniques (non-breaking spaces)?

<snip>
<input type="text" name="hstotal" value="0.00" size="16" <snipped other attributes> language="JavaScript">


INPUT elements do not have a language attribute.

Hope that helps,

Mike

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk")
Jul 20 '05 #2

P: n/a
Ed
Thank you so much Michael. I will go away and digest your advice.

regards.

"Michael Winter" <M.******@blueyonder.co.invalid> wrote in message
news:Xn*******************************@193.38.113. 46...
Ed wrote on 14 Dec 2003 at Sun, 14 Dec 2003 15:35:44 GMT:
Hoping someone an assist me urgently. I would truly appreciate
and help.

I have a form and prices are based on 'price break'.

1.The price break fields work fine, but I cannot for the life of
me add more than one to a page.
2. Also need to add the two results to a 'total' field. (See
code attached).


Rather than hard-code the form elements that the script checks, pass
the form elements as objects to the functions. For example:

<FORM name="someForm" id="someForm">
<INPUT name="someTextBox" onkeypress="checkKey()">

function checkKey() {
var txtBox = document.forms['someForm'].elements['someTextBox'];

// Do something with the text box
txtBox.value
}

What I've presented above is similar to how you access the form in
your code below. It would work fine, but it can only access the hard-
coded forms and controls. One could change it to this:

<FORM name="someForm" id="someForm">
<INPUT name="someTextBox" onkeypress="checkKey(this)">

function checkKey(control) {
var txtBox = control;

// Do something with the text box
txtBox.value
}

Notice the function call and how the control is referenced. The
snippet above is effectively the same: the variable 'txtBox' refers
to the same control, someTextBox. When the keyword, this, is used in
an intrinsic event, it creates an object reference that represents
the control. You can then use that reference in the same way as you
would a fully-qualified one. This way, you can use the same function
on multiple controls.

That will stop you from having to duplicate the same script, and
allow you to use as many price breaks (whatever they are) as you
like. What follows are some comments on your HTML.
<HEAD>


You are using intrinsic events, but you have not specified the
default scripting language. Include:

<META http-equiv="Content-Script-Type" content="text/javascript">

here. Also, HTML documents required a TITLE element (and a DOCTYPE).
</HEAD>

<BODY onLoad="findPrice();">

<SCRIPT LANGUAGE="JavaScript">


The type attribute is /required/ in HTML. When used, it renders the
language attribute (which is deprecated, anyway) redundant.

<SCRIPT type="text/javascript">
<!-- Begin


Don't use SGML comments to hide SCRIPT contents - there's no need as
the archaic reasons for doing it are no longer valid.

<snip>
function getNumberOfUnits() {
var hsunits1 = document.Hypersnap.hsunits1.value;
return (hsunits1 == "") ? 0 : hsunits1;
}


To make your script work across more browsers, you should use the
'collections syntax' when accessing form elements:

var hsunits1 =
document.forms['Hypersnap'].elements['hsunits1'].value;

<snip>
// End -->


This closing comment should be removed.
</script>


<snipped duplicate script>
<form name="Hypersnap" method="post">

&nbsp;
<p><font face="Arial" size="2"><b>Number of Users Required
:</b></font>


Why use style sheets, but also include deprecated FONT, B, and bad
layout techniques (non-breaking spaces)?

<snip>
<input type="text" name="hstotal" value="0.00" size="16"

<snipped other attributes>
language="JavaScript">


INPUT elements do not have a language attribute.

Hope that helps,

Mike

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk")

Jul 20 '05 #3

P: n/a
JRS: In article <ds******************@news-server.bigpond.net.au>, seen
in news:comp.lang.javascript, Ed <ed********@hotmail.com> posted at Sun,
14 Dec 2003 17:53:13 :-
Lines: 131 Thank you so much Michael. I will go away and digest your advice.

regards.

"Michael Winter" <M.******@blueyonder.co.invalid> wrote in message
news:Xn*******************************@193.38.113 .46...


Please read, understand, and honour c.l.j FAQ 2.3 para 5 sentence 6, etc.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> Jsc maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/Jsc/&c, FAQ topics, links.
Jul 20 '05 #4

P: n/a

"Ed" <ed********@hotmail.com> wrote in message
news:kr****************@news-server.bigpond.net.au...
Hoping someone an assist me urgently. I would truly appreciate and help.

I have a form and prices are based on 'price break'.

1.The price break fields work fine, but I cannot for the life of me add more than one to a page.
2. Also need to add the two results to a 'total' field. (See code attached).
Many thanks in advance.

... snip ...


Ed. your code was very difficult to follow, so I'm not sure exactly what it
is supposed to do.
From your text above I think the following example is likely to provide you
with a reasonable starting point.
I've tested it on IE and suspect it would need quite some changes to support
Netscape.
Security Note. Prices calculated on a client should never be used by a
server as this would expose you to huge security risks.

Regards,
Chris.

<HTML>

<table>
<tr>
<td></td>
<td>Quantity</td>
<td>Unit Price</td>
<td>Total Price</td>
</tr>
<tr>
<td>Number of Widgets: </td>
<td><input value = 0

onKeyUp="findPrice(this.value,1,Product1Bands,Prod uct1Price,Product1Total)">
</input>
</td>
<td id="Product1Price"></td>
<td id="Product1Total"></td>
</tr>
<tr>
<td>Number of Wodgers: </td>
<td><input value = 0

onKeyUp="findPrice(this.value,2,Product2Bands,Prod uct2Price,Product2Total)">
</input>
</td>
<td id="Product2Price"></td>
<td id="Product2Total"></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td id="TotalPrice" style="font-weight:bold"></td>
</tr>
</table>
<script>

var Product1Bands = [ // Array of Thresholds and Price Bands for
Product 1.
"1000:18",
"500:25",
"250:28",
"1:30",
"0:0"
];

var Product2Bands = [ // Array of Thresholds and Price Bands for
Product 1.
"1000:9",
"500:15",
"1:20",
"0:0"
];

// Initialise the calculated fields
findPrice(0,1,Product1Bands,Product1Price,Product1 Total);
findPrice(0,2,Product2Bands,Product2Price,Product2 Total);

function
findPrice(Quantity,ProductNumber,ProductBands,Prod uctPriceField,ProductTotal
Field) {
for (i=0;i<ProductBands.length;i++) {
Thresholds = ProductBands[i].split(":");
if (1*Quantity >= 1*Thresholds[0]) { // Force Numeric
comparison
ProductPriceField.innerText = Thresholds[1];
ProductTotalField.innerText = Thresholds[1]*Quantity;
TotalPrice.innerText = 1*Product1Total.innerText +
1*Product2Total.innerText;
return;
}
}
}
</script>
</HTML>


Jul 20 '05 #5

P: n/a
Ed
The good Doctor,
Please read, understand, and honour c.l.j FAQ 2.3 para 5 sentence 6,
Really Doc, you should play more golf.

Still, my gratitude to Mike and Chris for their helpful and sensible
responses.

"Dr John Stockton" <sp**@merlyn.demon.co.uk> wrote in message
news:jk**************@merlyn.demon.co.uk... JRS: In article <ds******************@news-server.bigpond.net.au>, seen
in news:comp.lang.javascript, Ed <ed********@hotmail.com> posted at Sun,
14 Dec 2003 17:53:13 :-
Lines: 131
Thank you so much Michael. I will go away and digest your advice.

regards.

"Michael Winter" <M.******@blueyonder.co.invalid> wrote in message
news:Xn*******************************@193.38.113 .46...


Please read, understand, and honour c.l.j FAQ 2.3 para 5 sentence 6, etc.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE

4 <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript <URL:http://www.merlyn.demon.co.uk/js-index.htm> Jsc maths, dates, sources. <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/Jsc/&c, FAQ topics,

links.
Jul 20 '05 #6

P: n/a
Lee
Ed said:

The good Doctor,
Please read, understand, and honour c.l.j FAQ 2.3 para 5 sentence 6,


Really Doc, you should play more golf.


And really, you should learn to post properly.

Jul 20 '05 #7

P: n/a
JRS: In article <S7******************@news-server.bigpond.net.au>, seen
in news:comp.lang.javascript, Ed <ed********@hotmail.com> posted at Tue,
16 Dec 2003 00:14:42 :-
Please read, understand, and honour c.l.j FAQ 2.3 para 5 sentence 6,


Really Doc, you should play more golf.

Still, my gratitude to Mike and Chris for their helpful and sensible
responses.

If you cannot learn to behave yourself in public, you will find yourself
ostracised by those who consider such things significant. Perhaps you
do not expect to need further informed assistance here; though, from
your original question, that seems unlikely.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME
Web <URL:http://www.uwasa.fi/~ts/http/tsfaq.html> -> Timo Salmi: Usenet Q&A.
Web <URL:http://www.merlyn.demon.co.uk/news-use.htm> : about usage of News.
No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.
Jul 20 '05 #8

P: n/a
Ed
Dr Stockton,
You should read the reply to 'Lee'.
Yes, I do need assistance - and it's a shame I have to feed your ego to get
that help.
Behave myself? Could you please show me one instance where I have not simply
asked a question as clearly as my (very) limited JS knowledge allows?
Again, where are your contributions Doc? Apart from being the groups 'How To
Ask a Question Policeman' and your impolite links that border on Spam.

Ed
"Ed" <ed********@hotmail.com> wrote in message
news:S7******************@news-server.bigpond.net.au...
The good Doctor,
Please read, understand, and honour c.l.j FAQ 2.3 para 5 sentence 6,
Really Doc, you should play more golf.

Still, my gratitude to Mike and Chris for their helpful and sensible
responses.

"Dr John Stockton" <sp**@merlyn.demon.co.uk> wrote in message
news:jk**************@merlyn.demon.co.uk...
JRS: In article <ds******************@news-server.bigpond.net.au>, seen
in news:comp.lang.javascript, Ed <ed********@hotmail.com> posted at Sun,
14 Dec 2003 17:53:13 :-
Lines: 131

Thank you so much Michael. I will go away and digest your advice.

regards.

"Michael Winter" <M.******@blueyonder.co.invalid> wrote in message
news:Xn*******************************@193.38.113 .46...


Please read, understand, and honour c.l.j FAQ 2.3 para 5 sentence 6, etc.
--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00

IE 4
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for

news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> Jsc maths, dates,

sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/Jsc/&c, FAQ topics,

links.

Jul 20 '05 #9

P: n/a
"Ed" <ed********@hotmail.com> writes:
Dr Stockton,
You should read the reply to 'Lee'.
Yes, I do need assistance - and it's a shame I have to feed your ego to get
that help.


Don't think for a second that Dr. Stockton is the only person not
liking top posting. He pointed you to the *FAQ* for *this* newsgroup,
in particular section 2.3, paragraph 5, sentence 6. It tells you how
you should quote in this newsgroup. You have given no evidence that
you have even read it.

While Dr. Stockton might be the one to tell you that you shoudn't top
post, he's far from being the only one of that opinion. The hint, to
go Matrix on you, was for your protection. And since you repeatedly
ignore it, I too will ignore you until you change your mind. You can
assume that other people have already made the same decission without
even bothering to inform you ... after all, you won't bother to please
them, why should they care about you.

Good luck.
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #10

P: n/a
On Wed, 17 Dec 2003 20:18:33 +0100, Lasse Reichstein Nielsen wrote:
"Ed" <ed********@hotmail.com> writes:
Dr Stockton,
You should read the reply to 'Lee'.
Yes, I do need assistance - and it's a shame I have to feed your ego to
get that help.


Don't think for a second that Dr. Stockton is the only person not liking
top posting. He pointed you to the *FAQ* for *this* newsgroup, in
particular section 2.3, paragraph 5, sentence 6. It tells you how you
should quote in this newsgroup. You have given no evidence that you have
even read it.

While Dr. Stockton might be the one to tell you that you shoudn't top
post, he's far from being the only one of that opinion. The hint, to go
Matrix on you, was for your protection. And since you repeatedly ignore
it, I too will ignore you until you change your mind. You can assume
that other people have already made the same decission without even
bothering to inform you ... after all, you won't bother to please them,
why should they care about you.


Indeed.

Follow the LONG established and (almost) globally followed rules of usenet
and you will get many more responses to your requests.

The rules are there for a VERY specific reason. The majority of news
readers in existance depend on these rules to properly format threads in
messages.

If someone has to work to figure out what's been said, or who said it, in
a thread they are much less likely to want to be part of it.

--
i.m.
The USA Patriot Act is the most unpatriotic act in American history.

Jul 20 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.