Hi

Have been programming an javascript for a number of years now, and just started to get involved with jQuery, and I've stumbled across a problem which has really got me stumped.

Code:
function calc_calculate ()
{
	// empty all quotation areas
	$("#quotation-aggregates").empty();
	$("#quotation-stones").empty()
	$("#quotation-total-discount").empty()
	$("#quotation-total-collect").empty()
	extras = calc_get_extras ();
	aggstotal = 0;
	agglist = $("<ul />");
	if(extras['cement']['retail'])
	{
		aggstotal+=extras['cement']['retail'];
		agglist.append($("<li />").append("Bags of cement: "+extras['cement']['bags']+" (&pound;"+extras['cement']['retail']+")"));
	}
	if(extras['sharpSand']['retail'])
	{
		aggstotal+=extras['sharpSand']['retail'];
		aggstotal+=extras['buildingSand']['retail'];
		agglist.append($("<li />").append("Bags of sharp sand: "+extras['sharpSand']['bags']+" (&pound;"+extras['sharpSand']['retail']+")"));
		agglist.append($("<li />").append("Bags of building sand: "+extras['buildingSand']['bags']+" (&pound;"+extras['buildingSand']['retail']+")"));
	}
	stoneslist = $("<ul />");
	var stonestotal=0;
	$.post("/legacy/costCalculatorResults.php", {
				colour: $("#calc-colour input:checked").attr("value"),
				patio_size: $("#calc-size-input").attr("value"),
				pattern: $("#calc-pattern input:checked").attr("value"),
				paving_variety: $("#calc-variety-select").val()},
			function(data){
				stones = data.split ("\n");
				for(i=0;i<stones.length;i++)
				{
					if (stones[i].length)
					{
						details = stones[i].split(",");
						stoneslist.append($("<li />").append(details[0] + ": " + details[1] + " (&pound;"+details[2]+")"));
						stonestotal+=Number(details[2]);
					}
				}
			});

	stonestotal=(Math.round(100*stonestotal)/100);
        // alert (stonestotal)
	$("#quotation-stones").append(stoneslist);
	$("#quotation-aggregates").append(agglist);
	$("#quotation-total").append(stonestotal);
}
The problem variable is stonestotal. for some reason, it's value is 0, UNLESS i uncomment out the alert 4 lines from the bottom, in which case the alert says 0, then the correct value is appended to the document.

Any ideas on why this might be happening? Seems very weird to me that the scope of the variable changes after it gets used in the alert function.

Any suggestions of how else to get this variable out of the anonymous function would also be useful!