///// INIT /////
	$(document).ready(function() {
		$.post("includes/cart.php", { }, function(data){
			$("#cart_container").html(data);
			setListners();
		});
		
		setListnersBig();
	});
///// VOEG EEN PRODUCT TOE AAN DE WINKELWAGEN /////
function toCart( prod_id )
{
	$keuze = $("#keuze").val();
	$.post("includes/cart.php?action=adtocart", { productid: prod_id, keuze: $keuze }, function(data){
		$("#cart_container").html(data);
		setListners();
	});
}
///// LEEG DE WINKELWAGEN /////
function clearCart()
{
	$.post("includes/cart.php?action=clearcart", {}, function(data){	
		$("#cart_container").html(data);
	});
}
///// VERWIJDER EEN PRODUCT UIT DE WINKELWAGEN /////
function clearFromCart( prod_id )
{
	$.post("includes/cart.php?action=clearfromcart", { productid: prod_id }, function(data){
		$("#cart_container").html(data);
		setListners();
	});
}

function setListners()
{
	if ( $(".winkelwagen").length == 0 )
	{
		$(".field").each(function (i) {
			$(this).bind('blur', function(i) {	
										  
				id = $(this).attr('rel');
				nr = $(this).val();
				if (isInt(nr))
				{
					$("#cart_error").hide();
					$.post("includes/cart.php?action=changeamount", { productid: id, aantal: nr }, function(data){
						$("#cart_container").html(data);
						setListners();
					});
				}
				else
				{
					$("#cart_error").html('Ingevulde waarden is geen nummer');
					$("#cart_error").show();
				}
			});
		});
	}
}
///// HANG EVENT LISTNERS AAN DE AANTAL VELDEN IN DE WINKELWAGEN /////
function removeBig(id)
{
	nr = 0;
	$.post("includes/cart.php?action=removebig", { productid: id, aantal: nr }, function(data){
		$("#wagen_container").html(data);
		setListnersBig();
	});
}
function setListnersBig()
{
	if ( $(".winkelwagen").length > 0 )
	{
		$(".field").each(function (i) {
			$(this).bind('blur', function(i) {	
										  
				id = $(this).attr('rel');
				nr = $(this).val();
				if (isInt(nr))
				{
					$("#cart_error").hide();
					$.post("includes/cart.php?action=updatelist", { productid: id, aantal: nr }, function(data){
						$("#wagen_container").html(data);
						setListnersBig();
					});
				}
				else
				{
					//$("#cart_error").html('Ingevulde waarden is geen nummer');
					//$("#cart_error").show();
				}
			});
		});
	}
}

function isInt(x)
{
	var y = parseInt( x );
	if ( isNaN( y ) ) return false;
	return x==y && x.toString()==y.toString();
} 