	function IsNumeric(sText)

	{
	   var ValidChars = "0123456789.";
	   var IsNumber=true;
	   var Char;
	
	   for (i = 0; i < sText.length && IsNumber == true; i++) 
		  { 
		  Char = sText.charAt(i); 
		  if (ValidChars.indexOf(Char) == -1) 
			 {
			 IsNumber = false;
			 //alert('Please enter a valid decimal value here')
			 }
		  }
	   return IsNumber;
   
    }
   
	//format a number into specified number of decimal places
	Math.formatDecimals = function (num, digits) {
        //if no decimal places needed, we're done
        if (digits <= 0) {
                return Math.round(num); 
        } 
        //round the number to specified decimal places
        //e.g. 12.3456 to 3 digits (12.346) -> mult. by 1000, round, div. by 1000
        var tenToPower = Math.pow(10, digits);
        var cropped = String(Math.round(num * tenToPower) / tenToPower);

        //add decimal point if missing
        if (cropped.indexOf(".") == -1) {
                cropped += ".0";  //e.g. 5 -> 5.0 (at least one zero is needed)
        }

        //finally, force correct number of zeroes; add some if necessary
        var halves = cropped.split("."); //grab numbers to the right of the decimal
        //compare digits in right half of string to digits wanted
        var zerosNeeded = digits - halves[1].length; //number of zeros to add
        for (var i=1; i <= zerosNeeded; i++) {
                cropped += "0";
        }
        return(cropped);
	} //Robert Penner May 2001 - source@robertpenner.com

	function calculateTotal(recalcshipping) {
		//alert('calculateTotal running');
		var order_total = 0;
		var lineitem_total = 0;
		var tax_total = 0;
		var shipping_total = 0;
		var weight_total = 0;
		var promo_total = 0;
		
		// Run through all the form fields
		for (var i=0; i < document.fOrder.elements.length; ++i) {
	
			// Get the current field
			form_field = document.fOrder.elements[i]
	
			// Get the field's name
			form_name = form_field.name
	
			// Is it a "product" field?
			if (form_name.substring(0,5) == "price") {
				//alert(form_name);
				// If so, extract the price from the name
				item_price = document.getElementById(form_name).value;
				//alert(item_price);
				
				// Get the quantity
				field_index = form_name.substring(5);
				qty_field = 'qty' + field_index;
				item_quantity = document.getElementById(qty_field).value;
				if (!item_quantity || item_quantity < 0) {
				 alert('Please enter a valid order quantity');
				 document.getElementById(qty_field).value = 1;
				}
				
				// tax value
				tax_rate_field = 'taxid' + field_index;
				item_tax_rate = document.getElementById(tax_rate_field).value;
				//alert(item_tax_rate);
				
				// weight value
				weight_field = 'weight' + field_index;
				item_weight = document.getElementById(weight_field).value;
				//alert(item_tax_rate);
				
				// Update the order total
				if (item_quantity >= 0) {
					lineitem_total += item_quantity * item_price;
					//alert(lineitem_total);
					tax_total += ((item_quantity * item_price) * item_tax_rate / 100);
					weight_total += (item_quantity * item_weight);
				}
			}
      }

      // Display the total rounded to two decimal places
      formattedSubtotal = Math.formatDecimals(lineitem_total,2);
	  document.fOrder.subtotal.value = formattedSubtotal;
	
	  //call shipping rates function again, if recalcshipping == 0
	  if (recalcshipping == 1) {
	  	loadShippingRates();
	  }
	  shipping_total = document.getElementById("orderShipping").value;
	  if (!shipping_total || shipping_total < 0) { 
		alert('Please enter a valid shipping amount');
		shipping_total = 0; 
		document.getElementById("orderShipping").value = 0;
	  }
	  
	  //promo code value - call function first
	  var promo_code_text = document.getElementById("orderPromoCode").value;
	  if (promo_code_text.length >= 2) {
		  getPromoCodeValue();
		  promo_total = document.getElementById("orderpromoamt").value;
	  }
	  order_total = ((lineitem_total * 1) + (tax_total * 1) + (shipping_total * 1) - (promo_total * 1));
	  formattedTotal = Math.formatDecimals(order_total,2);
	  document.getElementById("ordertotal").value = formattedTotal;
	  formattedShipping = Math.formatDecimals(shipping_total,2);
	  document.getElementById("orderShipping").value = formattedShipping;
	  formattedTax = Math.formatDecimals(tax_total,2);
	  document.getElementById("ordertax").value = formattedTax;
	  formattedWeight = Math.formatDecimals(weight_total,2);
	  document.getElementById("weighttotal").value = formattedWeight;
	  
	}