I have a paypal form and in part of it, I need the user to select one of two radio buttons in one category and one of two radio buttons in another. Each one has a cost associated with the selection and so I am trying to build up a total cost based on what they select and add the tax giving me a grand total. Here are the functions I have:
function newqty( what )
{
var price = document.getElementById( what + "_cost" ).innerHTML.replace( "$", "" );
var qty = document.getElementById( what + "_qty" ).value;
if( qty == "" ) qty = 0;
document.getElementById( what + "_ttl" ).value = "$" + (price * qty) + ".00";
recalc();
}
function recalc()
{
var rs = document.getElementById( "rs_ttl" ).value.replace( "$", "" );
if( rs == "" ) rs = 0;
var rl = document.getElementById( "rl_ttl" ).value.replace( "$", "" );
if( rl == "" ) rl = 0;
var ts = document.getElementById( "ts_ttl" ).value.replace( "$", "" );
if( ts == "" ) ts = 0;
var tl = document.getElementById( "tl_ttl" ).value.replace( "$", "" );
if( tl == "" ) tl = 0;
<!-- var sp = document.getElementById("shsel").options[ document.getElementById("shsel").selectedIndex ].value; -->
<!-- document.getElementById("sh_price").value = sp; -->
<!-- sh = document.getElementById("sh_price").value.replace( "$", "" ); -->
var sttl = parseFloat( rs ) + parseFloat( rl ) + parseFloat( ts ) + parseFloat( tl );
document.getElementById("amount").value = "$" + sttl + ".00";
var hst = parseFloat( sttl * 0.13 );
document.getElementById( "hst" ).value = price(hst);
var tax = 0;
if( document.getElementById("province").options[document.getElementById("province").selectedIndex].value == "ON" ) {
tax = parseFloat( sttl * 0.13 );
document.getElementById( "tax" ).value = price(tax);
} else {
tax = parseFloat( sttl * 0.05 );
document.getElementById( "tax" ).value = price(tax);
}
var ttl = sttl + tax;
document.getElementById("grandtotal").value = price(ttl);
}
and here is the html + javascript:
I have a list near the top that sets the cost:
<ul>
<li>Radio: Small Market (pop’n under 200,000): <span id="rs_cost">$50</span></li>
<li>Radio: Large Market (pop’n over 200,000): <span id="rl_cost">$100</span></li>
<li>Television: Small Market (pop’n under 400,000): <span id="ts_cost">$100</span></li>
<li>Television: Large Market (pop’n over 400,000): <span id="tl_cost">$100</span></li>
</ul>
I then have the following which is part of the code from a form:
<li><div id="radiogroup-left">
<label for="radio_market">radio market:</label>
<label><input name="rs_qty" type="hidden" value="" />
<input type="radio" name="radio_market" value="under 200,000 persons 12+" onClick="newqty('rs');" />
under 200,000 persons 12+</label>
<label>
<input type="radio" name="radio_market" value="over 200,000 persons 12+" onblur="newqty('rl');" />
over 200,000 persons 12+</label>
<input name="rl_qty" type="hidden" value="1" />
</div><div id="radiogroup-right">
<label for="tv_market">tv market</label>
<label>
<input type="radio" name="tv_market" value="under 400,000 persons 12+" onblur="newqty('ts');" />
under 400,000 persons 12+</label>
<label>
<input name="ts_qty" type="hidden" value="1" />
<input type="radio" name="tv_market" value="over 400,000 persons 12+" onblur="newqty('tl');" />
over 400,000 persons 12+</label>
<input name="tl_qty" type="hidden" value="1" />
<p class="align-right note">* market size based on BBM</p>
</div>
<input type="hidden" id="rs_ttl" name="rs_ttl" size="7" maxlength="7" value="">
<input type="hidden" id="rl_ttl" name="rl_ttl" size="7" maxlength="7" value="">
<input type="hidden" id="ts_ttl" name="ts_ttl" size="7" maxlength="7" value="">
<input type="hidden" id="tl_ttl" name="tl_ttl" size="7" maxlength="7" value="">
</li>
<li>
<table width="450px" border="0" cellspacing="2" cellpadding="1">
<tr>
<td>Total cost for all entry selections:</td>
<td><input size="7" maxlength="7" readonly="true" name="amount" id="amount" value=""/></td>
</tr>
<tr>
<td>HST/GST</td>
<td><input size="7" maxlength="7" readonly="true" name="hst" id="hst" value=""/></td>
</tr>
<tr>
<td>Grand Total</td>
<td><input size="7" maxlength="7" readonly="true" name="grandtotal" id="grandtotal" value=""/></td>
</tr>
</table>
</li>
What I need to have happen is that if the user clicks say on “radio market under 200,000” that the var rs_qty be set to 1 and the function new_qty(what) be called and thus have the “amount” field be updated as well as the hst field and finally also the grand total field. The user can also, in conjunction, make a selection from the television checkboxes and that value also needs to be calculated and added to the total. The other consideration is that if a person selects the wrong checkbox and then selects a different checkbox in that group, we need to adjust the totals as one of the categories ( “radio market under 200,000” ) is a 50.00 fee while the others are 100.00 fees.
I have been stuck on this for a while now and seem to be going in circles. I would sincerely appreciate some help on this.
Cheers
Dave