Hopefully, this is just something for users to play with. You should certainly not trust what it produces if you're calculating real order prices. That invites tampering from an unscrupulous user, so only the server should be involved in such things.
You don't say how the Tax Group control is structured. What are the values of each option element? From your description, I'd guess that they are numbers between 0 and 1. For instance, VAT (value-added tax) in the UK is 17.5%, so the value I'd expect is "0.175".
HTML Code:
<form action="">
<label>Item price: <input type="text" name="price"></label>
<label>Shipping charge: <input type="text" name="shipping"></label>
<label>Tax group:
<select name="tax" size="1">
<!-- VAT: <option value="0.175">...</option> -->
</select></label>
<label>Total: <input type="text" name="total"></label>
<input type="button" value="Calculate" onclick="calculate(this.form);">
</form>
Code:
function calculate(f) {
var e = f.elements,
p = e.price.value,
s = e.shipping.value,
t = e.tax,
m = ['The following fields are invalid:\n'];
if(!Number.isReal(p)) {m.push('Item price');}
if(!Number.isReal(s)) {m.push('Shipping charge');}
if(-1 == t.selectedIndex) {m.push('Tax group');}
if(1 != m.length) {
alert(m.join('\n'));
return false;
}
e.total.value = ((+p + (+s)) * (1 + (+t.options[t.selectedIndex].value))
* 100).toCurrency('$');
}
if('function' != typeof Array.prototype.push) {
Array.prototype.push = function(v) {
var i = this.length >>> 0,
j = 0,
n = arguments.length;
while(n > j) {this[i++] = arguments[j++];}
return (this.length = i);
};
}
Number.prototype.toCurrency = function(c, t, d) {
var n = +this,
s = (0 > n) ? '-' : '',
m = String(Math.round(Math.abs(n))),
i = '',
j, f;
c = c || '';
t = t || '';
d = d || '.';
while (m.length < 3) {m = '0' + m;}
f = m.substring((j = m.length - 2));
while (j > 3) {i = t + m.substring(j - 3, j) + i; j -= 3;}
i = m.substring(0, j) + i;
return s + c + i + d + f;
};
Number.isReal = function(s) {
return /^[+-]?(0|[1-9][0-9]*)(\.[0-9]+)?$/.test(s);
};
Hope that helps,
Mike
Damn you, Twey!
I started writing this ages ago, and then you beat me to it. 
Typical.
Bookmarks