Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-11-2012, 12:21 PM   PM User | #1
Limey10
New Coder

 
Join Date: Sep 2009
Location: Malaysia
Posts: 43
Thanks: 6
Thanked 0 Times in 0 Posts
Limey10 is an unknown quantity at this point
Price Quote Generator - re-re-visited

Hi all. I hope that everyone is fine and doing well.

This is something of an annual visit for me. You guys helped me enormously with the code below. It is a price quote generator that takes a number (entered by the client) and depending on the package selected, produces a price quote.

As I said, it works really well, but it's time for me to expand my business overseas.

At the moment, I display the price as a single currency, but I would like for clients to be able to select (maybe from a drop down list in the answer field) any one of 4 different currencies (i.e., GBP, USD, EURO, MYR). The different exchange rates don't need to be up to the minute accurate. I'm quite happy to go in to the code and update them weekly or as required.

I have attempted to research different ways of doing this, but as my needs are quite unusual and my brain really can't wrap around the ins and outs of java script, i'm here again begging with my cap in hand for help.

To see this code in action as it stands, please go to www.grammarproofing.com

Code:
<head>
<!--quote generator -->
<script type="text/javascript">
function update(){
var price = document.getElementById("Package").value;
var words = document.getElementById("words").value;
if (words != "") {
words = parseInt(words);
if (isNaN (words)) {
alert ("Please enter the number of words in figures");
document.getElementById("words").value = "";
return false;
}
}
var wordsOver2000 = words-2000;
if (wordsOver2000 <0) {wordsOver2000 = 0}
words = words - wordsOver2000;
var Tprice = ((price * words) + (price * .95 * wordsOver2000)).toFixed(2) ;document.getElementById("Totprice").value = Tprice;
document.getElementById("Totprice2").innerHTML = "Your Quote Total Is: MYR" + Tprice
}
</script>
<!--end quote generator -->
</head>
Code:
<body>
<!--start quote generator -->
<div class="quote" id="quote">
  <select name="Package" id = "Package" onchange = "update()">
    <option value="0.052">STUDENTS THE 11th HOUR PACKAGE</option>
    <option value="0.041">STUDENTS EXPRESS PACKAGE</option>
    <option value="0.035" selected="selected">STUDENTS WORK in PROGRESS PACKAGE</option>
  </select> 
   x   
   <input type="text" id="words" onchange="update()" value="" />
  WORDS<br />
= MYR
<input type = "text" id = "Totprice" readonly />
</div>
<!--end quote generator -->
</body>
Thanks in advance for any help offered.

Regards as always, Phil
__________________
"I'm still smiling because they haven't found the bodies yet.....muhahaha (evil laugh)"
GrammarProofing
Students Section
Limey10 is offline   Reply With Quote
Old 01-11-2012, 12:39 PM   PM User | #2
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
This is a real easy one

1. Define a variable for the exchange rates (indexed 0 to x)
2. Provide a select drop down for the currencies (values 0 to x)
3. add an onchange event listener to the new select and point to the same function as used for your current select
4. Change the calculation to take the exchange rate into consideration

Code:
var exchangeRates = [1, 0.889, 1.234];
function update(){
   var er = exchangeRates[document.getElementById("Currency").value];// selected exchange rate
   var price = document.getElementById("Package").value;
   var words = document.getElementById("words").value;
   if (words != "") {
      words = parseInt(words);
      if (isNaN (words)) {
         alert ("Please enter the number of words in figures");
         document.getElementById("words").value = "";
         return false;
      }
   }
   var wordsOver2000 = words-2000;
   if (wordsOver2000 <0) {wordsOver2000 = 0}
   words = words - wordsOver2000;
   var Tprice = (((price * words) + (price * .95 * wordsOver2000))*er).toFixed(2);
   document.getElementById("Totprice").value = Tprice;
   document.getElementById("Totprice2").innerHTML = "Your Quote Total Is: " + Tprice
}
Code:
<body>
<!--start quote generator -->
<div class="quote" id="quote">
  <select name="Package" id = "Package" onchange = "update()">
    <option value="0.052">STUDENTS THE 11th HOUR PACKAGE</option>
    <option value="0.041">STUDENTS EXPRESS PACKAGE</option>
    <option value="0.035" selected="selected">STUDENTS WORK in PROGRESS PACKAGE</option>
  </select> 
  <select name="Currency" id = "Currency" onchange = "update()">
    <option value="0" selected="selected">USD</option>
    <option value="1">EUR</option>
    <option value="2">CND</option>
  </select> 
   x   
   <input type="text" id="words" onchange="update()" value="" />
  WORDS<br />
= MYR
<input type = "text" id = "Totprice" readonly />
</div>
<!--end quote generator -->
</body>
devnull69 is offline   Reply With Quote
Users who have thanked devnull69 for this post:
Limey10 (01-11-2012)
Old 01-11-2012, 01:19 PM   PM User | #3
Limey10
New Coder

 
Join Date: Sep 2009
Location: Malaysia
Posts: 43
Thanks: 6
Thanked 0 Times in 0 Posts
Limey10 is an unknown quantity at this point
Spectacular devnull69, it works like a charm.

I love the way you say "This is a real easy one"

Thanks again, Phil
__________________
"I'm still smiling because they haven't found the bodies yet.....muhahaha (evil laugh)"
GrammarProofing
Students Section
Limey10 is offline   Reply With Quote
Reply

Bookmarks

Tags
currencies, generator, price, script

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:03 AM.


Advertisement
Log in to turn off these ads.