I had a working ticket purchase page and I have done something to break it but cannot find what I did to do so. I have the following javascript functions:

<script type="text/javascript">
function newqty( what )
{ 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 price( number )
{
        if( (number > 8191) && (number < 10485) ) {
                number -= 5000;
                var nnumber = Math.round(number*Math.pow(10,2))/Math
.pow(10,2);
                nnumber = String( nnumber + 5000 );
        } else {
                var nnumber = String( Math.round(number*Math.pow(10,
2))/Math.pow(10,2) );        }

        if( nnumber.indexOf( "." ) != -1 ) {
                var num = nnumber.split( "." );
                if( num[1].length < 2 )
                        return "$" + num[0] + "." + num[1] + "0";
                else
                        return "$" + nnumber;
        } else
                return "$" + nnumber;
}
function addValues(){
    var ppname = document.getElementById("contact").value;
    var ppemail = document.getElementById("email").value;
    var ppcompany = document.getElementById("company").value;
    var ppadd1 = document.getElementById("address1").value;
    if(document.getElementById("address1").value != ""){
        var ppsuite = document.getElementById("suite").value;
    }
    var ppadd2 = document.getElementById("address2").value;
    var ppcity = document.getElementById("city").value;
    var ppprov =document.getElementById("province").options[document.getElementById("province").selectedIndex].value;
    var pppostal = document.getElementById("postal").value;
    var ppphone = document.getElementById("phone").value;
    var ppext = document.getElementById("extn").value;
    var ppfax = document.getElementById("fax").value;
    var pporddate = document.getElementById("order_date").value;
    var ppqty = document.getElementById("bv_qty").value;
    var ppttl = document.getElementById("bv_ttl").value;
    var ppship = document.getElementById("shsel").options[document.getElementById("shsel").selectedIndex].value;
    var pphowpay = document.getElementById("ptype").options[document.getElementById("ptype").selectedIndex].value;
    var ppsub = document.getElementById("subtotal").value;
    var ppgst = document.getElementById("gst").value;
    var pppst = document.getElementById("pst").value;
    var ppgrndttl = document.getElementById("grandtotal").value;
     
    var stripvals=ppname+"|"+ppemail+"|"+ppcompany+"|"+ppadd1+"|"+ppsuite+"|"+ppadd2+"|"+ppcity+"|"+ppprov+"|"+pppostal+"|"+ppphone+"|"+ppext+"|"+ppfax+"|"+pporddate+"|"+ppqty+"|"+ppttl+"|"+ppship+"|"+pphowpay+"|"+ppsub+"|"+ppgst+"|"+pppst+"|"+ppgrndttl;
    stripvals=stripvals.replace(/\\$/g,'');
    document.getElementById('custom').value=stripvals;
       

        // Uncomment the next line to test to see what IPN Values are being sent:
        //document.getElementById('custom').value+="|test=true";
        //return true;
}

function checkexpire()
{
	var m = document.getElementById("expmonth").options[document.getElementById("expmonth").selectedIndex].value;


	var y = document.getElementById("expyear").options[document.getElementById("expyear").selectedIndex].value;
	var p = document.getElementById("ptype").options[document.getElementById("ptype").selectedIndex].value;



	if( (y == "2010") && (m < 4) && (p == "VISA") ) {
		alert( "This credit card has expired." );
		return false;
	}
        addValues();
	return true;
}
function recalc()
{
	var bv = document.getElementById( "bv_ttl" ).value.replace( "$", "" );
	if( bv == "" ) bv = 0;
	

	var mc = document.getElementById( "mc_ttl" ).value.replace( "$", "" );
	if( mc == "" ) mc = 0;

	var aw = document.getElementById( "aw_ttl" ).value.replace( "$", "" );
	if( aw == "" ) aw = 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( bv ) + parseFloat( mc ) + parseFloat( aw ) + parseFloat( sh ); 
	document.getElementById("subtotal").value = "$" + sttl + ".00";

	var gst = parseFloat( sttl * 0.05 );
	
	document.getElementById("gst").value = price(gst);
	
	var pst = 0;
	if( document.getElementById("province").options[document.getElementById("province").selectedIndex].value == "ON" ) {
		pst = parseFloat( sttl * 0.08 );
		
		document.getElementById( "pst" ).value = price(pst);
	} else {
		document.getElementById( "pst" ).value = "";
	}

	var ttl = sttl + gst + pst;
	document.getElementById("grandtotal").value = price(ttl);
	
	document.getElementById("amount").value = price(ttl);
	
	addValues();
	
} 

</script>

and then this bit of code, which is part of some code that is built dynamically, (not the whole form but you should get the idea):

<tr>'
	  .'	<td style="border-top: 1px dotted #990c00;">May 12th Cocktail Party, Exhibit and Awards Show</td>'
	  .'	<td align="center" valign="top" id="bv_cost" name="bv_cost" style="border-top: 1px dotted #990c00;">$45.00</td>'
	  ."	<td align=\"center\" valign=\"top\" style=\"border-top: 1px dotted #990c00;\"><input id=\"bv_qty\" name=\"bv_qty\" size=\"4\" maxlength=\"4\" onblur=\"newqty('bv');\" onkeyup=\"newqty('bv');\"></td>"
	  .'	<td align="center" valign="top" style="border-top: 1px dotted #990c00;"><input id="bv_ttl" name="bv_ttl" size="7" maxlength="7" readonly="true"></td>'
	  .'</tr>
<tr>'
	  .'    <td colspan="3">Shipping: <select name="shsel" id="shsel" '
	  .'onClick="recalc();"/>'
	  .'<option value="$10.00">Send my tickets via courier ($10.00 fee)</option>'
	  .'<option value="$0.00">Send my tickets via regular mail</option>'
	  .'<option value="$0.00">I will pick my tickets up at Adcam</option>'
	  .'</select></td>'
	  .'    <td><input name="sh_price" id="sh_price" size="7" readonly="true" value="$10.00"/></td>'
	  .'</tr><tr>'
	  .'    <td>Payment Method: <select name="ptype" id="ptype">'
	  .'      <option value="PayPal">PayPal</option>'
	  .'      <option value="Cheque">Cheque</option>'
	  .'      <option value="Cash">Cash</option>'
	  .'    </select></td>'
	  .'	<td align="right" colspan="2"><b>Subtotal</b></td>'
	  .'	<td align="center" valign="top">'
	  .'		<input size="7" maxlength="7" readonly="true" name="subtotal" id="subtotal" value=""/>'
	  .'	</td>'
	  .'</tr><tr>'
	  .'	<td align="right" colspan="3"># 85764 5881 RT0001 5% GST</td>'
	  .'	<td align="center" valign="top">'
	  .'		<input size="7" maxlength="7" readonly="true" name="gst" id="gst" value=""/>'
	  .'	</td>'
	  .'</tr><tr>'
	  .'	<td align="right" colspan="3">Ontario Residents: 8% PST</td>'
	  .'	<td align="center" valign="top">'
	  .'		<input size="7" maxlength="7" readonly="true" name="pst" id="pst" value=""/>'
	  .'	</td>'
	  .'</tr><tr>'
	  .'	<td align="right" colspan="3"><b>GRAND TOTAL</b></td>'
	  .'	<td align="center" valign="top">'
	  .'		<input size="7" maxlength="7" readonly="true" name="grandtotal" id="grandtotal" value=""/>'
.'	</td>'
	  .'</tr>'

So this should be simple; when a user selects the quantity and moves to the next field, the function newqty() is called which does a simple calculation for the total cost of the number of tickets required. This function then calls recalc() which is supposed to calculate all the costs including shipping if chosen, GST and PST (if applicable) and the final GrandTotal, and then write these calculations to the form fields. This was working find but now, it does calculate up to the Sub-total but does not calculate the GST + PST and the GrandTotal. I have stuck alert calls in and it does seem to be calculating the GST ( which is at 5%) but seems to be getting stopped when it calls the price() function to turn the number into currency format.

I apologize in advance for the length of this post but I figured it was necessary for you to get the whole picture (hopefully you have I have not simply confused the issue). Anyone see what stupidity I have done to screw this up?

Dave

I had a working ticket purchase page and I have done something to break it

I don't know what else may be wrong, but you have a variable and a function with the same name: price.

I don't know what else may be wrong, but you have a variable and a function with the same name: price.

Hummm .. well I don't think this interferes as price in the first case is not a global var and is destroyed once the function it is part of completes. Maybe not the best practice but I don't think this is it. Besides, I have not changed this for sure and it was working earlier.

Cheers for responding though.

Dabve

Well I'll be damned if you weren't correct. I really thought that that name duplication would not cause the script to not work as I know it used to work with things named that way. Well, I am happy it is working but I am just a bit puzzled.

Anyway ... thanks for your help.

Dave

function price( num )

Assuming that calculations are done in pennies [a Good Thing] this

function price( num )    
{
	var n = new String(num);
	return "$" + n.substring(0,n.length-2) + "." + (n<10?'0':'') + n.substring(n.length-2);
}

is simpler.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.