Auto Compute Shipping Total

[Total: 0    Average: 0/5]

This example will auto compute the total cost including shipping.

Body Code:

<form name=f1>
Box 1<input onblur=addTotals() type=text name=one value=4>
<BR />
Box 2<input onblur=addTotals() type=text name=two value=7>
<BR />
Sub-Total:
<input type=text name=sub_total value="?" readonly onfocus="this.blur()">
<BR />

Shipping Cost:<input type=text name=ship value="10" readonly>
<BR />
Total:
<input type=text name=total value="?" readonly onfocus="this.blur()">
</form>
<script language="javascript"> 
var RATE=10;/* percent rate for shipping */
function addTotals() { 
  with (document.forms["f1"])
  {
    var totalResult = Number( one.value ) + Number( two.value ); 
    var _total = roundTo(totalResult,2);
    sub_total.value = _total;
    var _ship = roundTo((totalResult / RATE),2);
    ship.value = _ship;
    total.value = roundTo(parseFloat(_total) + parseFloat(_ship),2);
  }
} 


function roundTo(num,pow){ 
  if( isNaN( num ) )
  { 
    num = 0; 
  } 

  num *= Math.pow(10,pow); 
  num = (Math.round(num)/Math.pow(10,pow))+ "" ; 
  if(num.indexOf(".") == -1) 
    num += "." ; 
  while(num.length - num.indexOf(".") - 1 < pow) 
    num += "0" ; 

  return num; 
} 

</script>

Times Viewed: 0

Leave a Reply

Your email address will not be published. Required fields are marked *