Ultimizing this JS gem....

A  little gnat's bothering me.

What would it take for below form to proclaim all values in Euros when this currency is selected? I find it a bit discomforting to switch to Euro and see those - apparantly - inflated amounts for the add-ons. It may discourage potential buyers when they're expecting approximately half those sums.

The data:

<html>
 <head>
<SCRIPT LANGUAGE="JavaScript"><!-- // cloak
// Variables to be changed by webmaster:

function Item( Code, Price ) {
 this.Code  = Code;
 this.Price = Price;
}

var prices    = new Array( new Item( "basic",  "1450" ),
                           new Item( "geen",   "0.00" ),
                           new Item( "hd6_4",  "40.00" ),
                           new Item( "hd9_1",  "58.00" ),
                           new Item( "hd13_6", "88.00" ),
                           new Item( "hd19_2", "148.00" ),
                           new Item( "m64",    "100.00" ),
                           new Item( "m128",   "248.00" ),
                           new Item( "m192",   "496.00" ),
                           new Item( "C400",   "80.00" ),
                           new Item( "C466",   "88.00" ),
                           new Item( "C500",   "128.00" ),
                           new Item( "P450",   "358.00" ),
                           new Item( "m105s",  "288.00" ),
                           new Item( "m105mb", "388.00" ),
                           new Item( "m107s",  "488.00" ),
                           new Item( "m107mp", "688.00" ),
                           new Item( "s7w",    "28.00" ),
                           new Item( "s9w",    "38.00" ),
                           new Item( "s18w",   "48.00" ),
                           new Item( "hs1",    "28.00" ) );

var BTW = 0.175;

function pricefrom( s ) {
 var i=0, code = s.options[s.selectedIndex].value;
 while( i < prices.length ) {
  if( prices[i].Code == code ) return prices[i].Price;
  i++;
 }
 return "0.00";
}

// e.g. 5% discount for 11 or more units
var pieces_for_discount = 11;
var pieces_discount     = 0.05;
var discount_offered    = false;

function calculate_totals( f ) {

 f.basic.value = prices[0].Price;
 f.hard_disk.value = pricefrom( f.s_hard_disk );
 f.memory.value    = pricefrom( f.s_memory );
 f.processor.value = pricefrom( f.s_processor );
 f.screen.value    = pricefrom( f.s_screen );
 f.speakers.value  = pricefrom( f.s_speakers );
 f.headset.value   = pricefrom( f.s_headset );

 var unitprice = parseInt( f.basic.value, 10) +
                 parseInt( f.hard_disk.value, 10) +
                 parseInt( f.memory.value, 10) +
                 parseInt( f.processor.value, 10) +
                 parseInt( f.screen.value, 10) +
                 parseInt( f.speakers.value, 10) +
                 parseInt( f.headset.value, 10);


 if( isNaN(f.pieces.value) ) {
  f.pieces.value = "1";
 }

 if( discount_offered ) {
   unitprice -= (unitprice * pieces_discount);
 }

 subtotal = unitprice * f.pieces.value;
 tax      = subtotal * BTW;
 total    = subtotal + tax;

 f.unitprice.value = round( unitprice, 2 );
 f.subtotal.value  = round( subtotal, 2 );
 f.tax.value       = round( tax, 2 );
 if (f.currency.selectedIndex == 0)
   f.total.value     = round( total, 2 );
else if (f.currency.selectedIndex == 1)
   f.total.value     = round( (total/2.20371 ), 2 );
 
 
}

function checkPieces( o ) {
 var v = parseInt( o.value, 10);
 if(isNaN(v)) v=1;
 if(v<1) v=1;

 var msg1 = "You have ordered "+v+" units.\n";
 var msg2 = "Our volume discount of "+(pieces_discount*100)+"% starts at "+pieces_for_discount+" units.\n";

 if( v<pieces_for_discount && discount_offered) {
  alert( msg1 + msg2 + "The volume discount no longer applies to the prices." );
  discount_offered = false;
 }
 else if( v>=pieces_for_discount && !discount_offered) {
  alert( msg1 + msg2 + "The volume discount now applies to the prices." );
  discount_offered = true;
 }

 o.value = v;
}

function round(number,X) {
 X = (!X ? 2 : X);
 rc = ''+(Math.round(number*Math.pow(10,X))/Math.pow(10,X));
 if ( rc.indexOf(".") == -1 ) rc += '.00';
 else if ( rc.indexOf(".") == rc.length-2 ) rc += '0';
 if (rc.indexOf(".") == 0) rc = ''+0+rc
 return(rc);
}

// uncloak -->
</SCRIPT>
 </head>
 <body onload="calculate_totals( document.forms[0] );">
  <DIV ALIGN="CENTER">
   <FORM ACTION="stuff to fill out later" METHOD="POST" onsubmit="calculate_totals(this);return true;">
    <TABLE STYLE="font-size: 12px" BORDERCOLOR="#000000" BORDERCOLORDARK="#ffffff" CELLSPACING="2" CELLPADDING="0" BORDER="1">
     <TR><Th COLSPAN="3"><FONT SIZE="+1" COLOR="#000080">Basispakket ASUS COBRA Profile</FONT></Th></TR>
     <TR><TD COLSPAN="3">&nbsp;</TD></TR>
     <TR>
      <TD><FONT COLOR="#000080"><B>PC onderdeel</B></FONT></TD>
      <TD><FONT COLOR="#000080"><B>Maak hier uw keuze voor een meer uitgebreid pakket</B></FONT></TD>
      <TD><FONT COLOR="#000080"><B>Prijs</B></FONT></TD>
     </TR>

     <TR><TD COLSPAN="3">&nbsp;</TD></TR>

<TR>
      <TD><B>Basispakket</B></TD>
      <TD><B>Zie specificaties</B>

</TD>
     <TD><INPUT NAME="basic" VALUE="1450" SIZE="8" MAXLENGTH="16" TYPE="Text" READONLY onchange="calculate_totals(this.form)"></TD>
     </TR>

    <TR>
      <TD>Harde Schijf (GB)</TD>
      <TD>&nbsp;&nbsp;
       <SELECT NAME="s_hard_disk" onchange="calculate_totals(this.form);">
        <OPTION VALUE="hd6_4">6,4
        <OPTION VALUE="hd9_1">9,1
        <OPTION VALUE="hd13_6">13,6
        <OPTION VALUE="hd19_2">19,2
       </SELECT>
      </TD>
      <TD><INPUT NAME="hard_disk" VALUE="40.00" SIZE="8" MAXLENGTH="16" TYPE="Text" READONLY onchange="calculate_totals(this.form)"></TD>
     </TR>
     <TR>
      <TD>Intern geheugen (MB)</TD>
      <TD>&nbsp;&nbsp;
       <SELECT NAME="s_memory" onchange="calculate_totals(this.form);">
        <OPTION VALUE="m64">64
        <OPTION VALUE="m128">128
        <OPTION VALUE="m192">192
       </SELECT>
      </TD>
      <TD><INPUT NAME="memory" VALUE="100.00" SIZE="8" MAXLENGTH="16" TYPE="Text" READONLY onchange="calculate_totals(this.form)"></TD>
     </TR>
     <TR>
      <TD>Processor (MHz)</TD>
      <TD>&nbsp;&nbsp;
       <SELECT NAME="s_processor" onchange="calculate_totals(this.form);">
        <OPTION VALUE="C400">Intel Celeron 400
        <OPTION VALUE="C466">Intel Celeron 466
        <OPTION VALUE="C500">Intel Celeron 500
        <OPTION VALUE="P450">Intel Pentium III 450
       </SELECT>
      </TD>
      <TD><INPUT NAME="processor" VALUE="80.00" SIZE="8" MAXLENGTH="16" TYPE="Text" READONLY onchange="calculate_totals(this.form)"></TD>
     </TR>
     <TR>
      <TD>Monitor (Philips)</TD>
      <TD>&nbsp;&nbsp;
       <SELECT NAME="s_screen" onchange="calculate_totals(this.form);">
        <OPTION VALUE="geen">Geen
        <OPTION VALUE="m105s">105s 15 &quot;
        <OPTION VALUE="m105mb">105mb 15 &quot;
        <OPTION VALUE="m107s">107s 17&quot;
        <OPTION VALUE="m107mp">107mp 17 &quot;
       </SELECT>
      </TD>
      <TD><INPUT NAME="screen" VALUE="0.00" SIZE="8" MAXLENGTH="16" TYPE="Text" READONLY onchange="calculate_totals(this.form)"></TD>
     </TR>
     <TR>
      <TD>Geluidsboxen</TD>
      <TD>&nbsp;&nbsp;
       <SELECT NAME="s_speakers" onchange="calculate_totals(this.form);">
        <OPTION VALUE="geen">Geen
        <OPTION VALUE="s7w">2 x 7w RMS 4 &quot;, 120W PMPO, 200Hz - 20kHz
        <OPTION VALUE="s9w">2 x 9w RMS 3 &quot;, 180W PMPO, 40Hz - 20kHz
        <OPTION VALUE="s18w">2 x 18w RMS 1 &quot; &amp; 4 &quot;, 350W PMPO, 20Hz - 20kHz
       </SELECT>
       &nbsp;&nbsp;
      </TD>
      <TD><INPUT NAME="speakers" VALUE="0.00" SIZE="8" MAXLENGTH="16" TYPE="Text" READONLY onchange="calculate_totals(this.form)"></TD>
     </TR>
     <TR>
      <TD>Hoofdtelefoon (Philips SBCL300)&nbsp;&nbsp;</TD>
      <TD>&nbsp;&nbsp;
       <SELECT NAME="s_headset" onchange="calculate_totals(this.form);">
        <OPTION VALUE="geen">Geen
        <OPTION VALUE="hs1">Ja
       </SELECT>
      </TD>
      <TD><INPUT NAME="headset" VALUE="0.00" SIZE="8" MAXLENGTH="16" TYPE="Text" READONLY onchange="calculate_totals(this.form)"></TD>
     </TR>
     <TR>
      <TD>Totaal aantal PC's</TD>
      <TD>&nbsp;&nbsp;
       <INPUT Type="text" name="pieces" value="1" SIZE="4" MAXLENGTH="16" onchange="checkPieces(this);calculate_totals(this.form);">
      </TD>
      <TD><INPUT NAME="unitprice" sIZE="8" VALUE="1670.00" MAXLENGTH="16" TYPE="Text" READONLY onchange="calculate_totals(this.form)"></TD>
     </TR>
     <TR>
      <TD><B>Subtotaal:&nbsp;</B></TD>
      <Td ALIGN="RIGHT" COLSPAN="2">
       <INPUT NAME="subtotal" VALUE="1670.00" SIZE="8" MAXLENGTH="16" TYPE="Text" READONLY onchange="calculate_totals(this.form)">
      </Td>
     </TR>
     <TR>
      <TD><B>BTW (17,5%):&nbsp;</B></TD>
      <Td ALIGN="RIGHT" COLSPAN="2">
       <INPUT NAME="tax" VALUE="292.25" SIZE="8" MAXLENGTH="16" TYPE="Text" READONLY onblur="calculate_totals(this.form)">
      </TD>
     </TR>
     <TR>
      <Td><INPUT type="button" value="Totaal" onClick="calculate_totals(this.form)">&nbsp;&nbsp;in&nbsp;


 <SELECT NAME="currency" onchange="calculate_totals(this.form);">
        <OPTION SELECTED VALUE="guilders">fl.
        <OPTION VALUE="euro">euro
         </SELECT></TD>
<Td ALIGN="RIGHT" COLSPAN="2"><INPUT NAME="total" VALUE="1962.25" SIZE="8" MAXLENGTH="16" TYPE="Text" READONLY onchange="calculate_totals(this.form)"></Td>
     </TR>
    </TABLE> <BR>
    <INPUT type="submit" value="Verzenden">&nbsp;&nbsp;<INPUT type="reset" value="Wissen">
   </FORM>
  </DIV>
 </body>
</html>

shihoAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Michel PlungjanConnect With a Mentor IT ExpertCommented:
var currSel= 1; // deafult is guilders

function pricefrom( s ) {
   var i=0, code = s.options[s.selectedIndex].value;
   while( i < prices.length ) {
      if( prices[i].Code == code ) return prices[i].Price*currSel;
      i++;
   }
   return "0.00";
}

function calculate_totals( f ) {
   if (f.currency.selectedIndex == 0) currSel = 1;
   else if (f.currency.selectedIndex == 1) currSel = 2.20371;

   f.basic.value = prices[0].Price*currSel;

..
..
..
..

   f.total.value     = round( (total/currSel ), 2 );
                               
0
 
oubelkasCommented:
mplungjan>> var i=0; or just i=0;

;)

Joseph
0
 
Michel PlungjanIT ExpertCommented:
Not funny Joseph... Shiho, ignore him... We have had an off-ee discussion of whether or not to have var in front of variables defined outside functions...

Michel
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
oubelkasCommented:
Chill, I was just kidding, aight? I won't do it again if you don't like it.
0
 
shihoAuthor Commented:
Are you two married?

Michel, I'm getting an 'item not defined' and 'object missing' error when I insert your sample. I've tried narrowing down the cause - to no avail.

Any clues?

S. "Clueless" K.
0
 
Michel PlungjanIT ExpertCommented:
Missing the rest of the script (from function checkPieces( o )  and on)

I multiplied instead of dividing and forgot a round:

   if( prices[i].Code == code ) return round(prices[i].Price/currSel);

Michel
0
 
shihoAuthor Commented:
Tag. Dag.
0
All Courses

From novice to tech pro — start learning today.