Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Additional help for math.floor insurance calculation 1

Status
Not open for further replies.

doubleimpact

Technical User
Oct 19, 2004
4
US
Chessbot helped out wonderfully before and offered the following code. I am just now getting back on this and can't seem to get this to work. So I'll leave it to the experts here.
Code:
<script>
<!-- Calculate shipping insurance rate, given by chessbot on tek-tips.com -->
var inc = 100; // increment
var fee = 2; // fee per increment
function getInsurance(amount)
{
  var total = parseInt(amount);
  total += Math.floor(amount/inc) * fee;
  return total;
}
</script>

The rest of the story is I have a drop down menu that a customer should choose if they want shipping insurance or not. The default is no. But when they change it I want to be sure that the javascript changes the value shipins to the amount to add for shipping insurance.

You will see in the code below, I have added the shipins as a input field, I will hide this in the final version. I just like to see it while I am testing to make sure the values are calculated correctly.

Code:
		<tr>
			<td><b>Shipping Insurance (Optional): </b><br><font size=1>$2.00 for every $100</font></td>
			<td colspan=2>
			<select name="shipinschoice" onchange="getInsurance()">
            <option>Add Shipping Insurance (+$2 per $100)</option>
            <option value="0" selected>No Shipping Insurance (Free)</option>
			</select>&nbsp;&nbsp;&nbsp;&nbsp;<input type="text" name="shipins" size="10">
		</tr>

I have tried a couple of variations and changes, but my disgraceful understanding of Javascript fails me everytime.

Could you please tell me what I am doing wrong?

P.S. Is there any way to have one javascript run several javascripts in a specified order?
 
Something like this?
Code:
<select name="shipinschoice" onchange="[red]this.form.elements['shipins'].value = [/red]getInsurance()">
            <option>Add Shipping Insurance (+$2 per $100)</option>
            <option value="0" selected>No Shipping Insurance (Free)</option>
            </select>

P.S. Yes, separate them with semicolons (";")
P.P.S. You don't need to credit me.

--Chessbot

"Violence is the last refuge of the incompetent." -- Asimov, Foundation
 
Thanks Chessbot,

That really helps, but I forgot to mention that one detail:

If shipinschoice is selected, then the shipins amount is based off of winbidamount (all are form field names). I can't seem to get the getInsurance function to base its calculation off the amount stored in winbidamount when the shipping insurance option is selected. Currently, it just says NaN when I select the option.

2) If the customer chooses the option, then changes their mind, it should return the shipins value back to zero.

Thanks for all the great help. You're awesome at this.
 
Here you go...
Code:
var inc = 100;
var fee = 2;
function getInsurance(amount)
{
  var total = parseInt(amount);
  total += Math.floor(amount/inc) * fee;
  return total;
}

function doChange(sel)
{
  var f = sel.form;
  var target = f.elements['shipins'];
  var amt = f.elements['winbidamount'];
  if (sel.options[sel.selectedIndex].value != 0)
    target.value = getInsurance(amt.value);
  else
    target.value = "0";
}
// ...
<select name="shipinschoice" onchange="doChange(this);">
 <option value="1">Add Shipping Insurance (+$2 per $100)</option>
 <option value="0" selected>No Shipping Insurance (Free)</option>
</select>

--Chessbot

"Violence is the last refuge of the incompetent." -- Asimov, Foundation
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top