function update_cart_amount(cartid, new_amount){
 $.post("source/apis/ShopCartUpdate.php", { cartid: ""+cartid+"", new_amount: ""+new_amount+"" }, function(){ window.location = window.location; } );
}


function remove_from_cart(id){
 $.post("source/apis/ShopCartRemove.php", { id: ""+id+"" }, function(){ window.location = window.location; } );
}


function remove_coupon_from_cart(id){
 $.post("source/apis/ShopCartRemoveCoupon.php", { id: ""+id+"" }, function(){ window.location = window.location; } );
}
	
$(document).ready(function(){	
	
	// STOCK CHECK ///////////////////////////////////////////////////////////////////////////////////////////////////////
	
		$('.product_option').change( function() {
			
			$("#AddToCartImage").removeAttr("disabled");
			$("#AddToCartImage").attr("disabled", "disabled");
			
			// prepare variable for array
			var parameters = [];
		   
			// get selected values from selectbox
			$('.product_option :selected').each(function(i, selected){
				
				// store valueid in array
				parameters[i] = $(selected).val();
											
			});
			
			// currect productid (parentid on opening)
			var parentid = $('input[name="parentid"]').val();
				
			// check for available products	
			$.post("source/apis/GetData.php?what=GetQuantity", { parentid: "" + parentid + "", parameters: "" + parameters + "" }, function(data){
				
				$('#shipping_period').removeClass();
				
				if(data.checked == data.choices && data.matches >= data.choices){
					
					if(data.quantity == 0){
						
						$("#AddToCartImage").removeAttr("disabled");
						$("#AddToCartImage").attr("disabled", "disabled");
						$('#shipping_period').addClass("status_red");
						$('#shipping_period').html("<span class=status_out>Niet op voorraad</span>");
					
					} else {
					
						$('#shipping_period').addClass("status_green");
						$('#shipping_period').html("<span class=status_in>Op voorraad</span>"); // data.shipping
						$('input[name="productid"]').val( data.childid );
						$("#AddToCartImage").removeAttr("disabled");
						
						// alert("Naam:\t\t\t" + data.name + "\nID:\t\t\t\t" + data.childid + "\nParentID:\t\t" + data.parentid + "\nArtikelnummer:\t" + data.serial_number + "\nBarcode:\t\t\t" + data.barcode + "\nVoorraad:\t\t" + data.quantity + "/" + data.instock + "\n\nKeuzes:\t\t\t" + data.choices + "\nSelectie:\t\t\t" + data.checked + "\nHint:\t\t\t" + data.hint + "\n\nToken:\t\t\t" + data.token + "");
						
					}
					
				} else {
					
					if(data.quantity == 0){
						
						$("#AddToCartImage").removeAttr("disabled");
						$("#AddToCartImage").attr("disabled", "disabled");
						$('#shipping_period').addClass("status_orange");
						
						if(data.hint != null)
							$('#shipping_period').html("<span class=status_hint>" + data.hint + "</span>");
						else
							$('#shipping_period').html("Maak uw keuze compleet");
					
					} else {
					
						$('#shipping_period').addClass("status_orange");
						
						if(data.hint != null)
							$('#shipping_period').html("<span class=status_hint>" + data.hint + "</span>");
						else
							$('#shipping_period').html("Maak uw keuze compleet");
							
						$("#AddToCartImage").attr("disabled", "disabled");
					
					}
										
				}			
				
			}, "json");
		
		});
	
	// STOCK CHECK ///////////////////////////////////////////////////////////////////////////////////////////////////////
	
	
	
	$('.product_extra').change( function() {		
		
		$("#AddToCartImage").removeAttr("disabled");
		$("#AddToCartImage").attr("disabled", "disabled");
		
		var enable_button = 0;
	   
	    // get selected values from selectbox
		$('.product_extra :selected').each(function(i, selected){
						
			if($(selected).val() != 0){
				enable_button = 1;
			}
			
		});
		
		if(enable_button != 0){
			
			var parentid = $('input[name="parentid"]').val();
			
			$('input[name="productid"]').val( parentid );
			$("#AddToCartImage").removeAttr("disabled");
						
		}
		
	});
		
	
	// on click of AddToCartImage-button
	$('#AddToCartImage').click( function () { 
		
		if ($('#AddToCartImage').is(':disabled') == true) {
            alert('U dient eerst een keuze te maken');
        }
				
	});
 
});