$(document).ready(function() {	
	$('.total').change(function () {
		if (this.value >= 0) {
			var rawId = new String((this.id).replace("aantal", "").replace("", "")); // ID
			var ids = rawId.split("_"); // ID + variantID = tempID
			
			$.getJSON("/json.aspx", { id: ids[0], type: ids[1] , amount: this.value, oldtype: ids[1], tempID: ids[2]}, function(json) {
				setTimeout("location.reload(true);", 1);
			});
		}		
 	});
	
	$('.select').change(function () {
		if (this.value >= 0) {
			var rawId = new String((this.id).replace("aantal", "").replace("", "")); // ID
			var oudType = rawId.split("_")[0]; // previous variantID
			var id = rawId.split("_")[1]; // productID
			var tempID = rawId.split("_")[2]; //tempID
			//var amount = $(".select option:selected").text()
			var amount = this.options[this.selectedIndex].text
			
			$.getJSON("/json.aspx", { id: id, type: this.value , amount: amount, oldtype: oudType, tempID: tempID}, function(json) {
				setTimeout("location.reload(true);", 1);
			});
		}		
 	});
	
	$('.variant').change(function () {
		$.getJSON("/json.aspx", {variantID: this.value}, function(json) {
			$("#prijs").html(formatCurrency(json.prijs * 0.01));
		});
 	});
});

function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');
	
	if(isNaN(num))
		num = "0";
		
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	
	if(cents<10)
		cents = "0" + cents;
	
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	num = num.substring(0,num.length-(4*i+3))+'.'+
	num.substring(num.length-(4*i+3));
	
	return (((sign)?'':'-') +  '\u20AC ' + num + ',' + cents);
}
