/*
webshop azzuri

*/

function AddToCart(id)
{
	var id = arguments[0];
	
	var update = arguments[2];
	if(update === undefined)
	{
		update = "nieuw";
		jAlert('Artikel is in de winkelwagen geplaatst.', 'Alert Box');
		var aantal = $("#prod-"+id).val();
	}else
	{
		var aantal = arguments[1];
	}
	if(aantal === undefined)
	{
		aantal = "1";
	}
	
	if(isInt(aantal))
	{
		if(id != '')
		{
			var product_id = id;
			$.post("/webshop/cart.php", { productid: product_id, aantal: aantal, update: update}, function(data) {
				$(".cartBox").replaceWith(data);
				
				$.post("/webshop/cartteaser.php", { productid: product_id, aantal: aantal, update: update}, function(data) {
					$(".cartTeaser").replaceWith(data);
				});
			});
			
			
			
		}
	}else
	{
		alert(aantal+' is geen getal, voer alleen een getal in');
	}
}

function clearCart()
{
	$.post("webshop/cart.php", { clearcart: 1 }, function(data) {
		$(".cartBox").replaceWith(data);
	});
}

function deleteFromCart(id, minaantal)
{
	var id = arguments[0];
	var remove = arguments[1];
	var aantal  = $("#update-" + id).val();
	if (aantal <= minaantal)
	{
		//console.log('deleted');
		$.post("webshop/cart.php", { productid: id, remove: 'dodelete' }, function(data) {
			$(".cartBox").replaceWith(data);
		});
	}
	else
	{
		if(remove === undefined)
		{
			remove = "1";
		}
		$.post("webshop/cart.php", { productid: id, remove: 'remove' }, function(data) {
			$(".cartBox").replaceWith(data);
		});
	}
}
//deze functie is bedoelt een artikel helemaal uit de cart te halen dus ook als er meerdere van in de cart zitten
function deleteThisAllFromCart(id)
{
	var id = arguments[0];
	
	$.post("webshop/cart.php", { productid: id, remove: 'dodelete' }, function(data) {
			$(".cartBox").replaceWith(data);
	});
	
	
}

function rebuildProductListing(priceFrom, priceTo, cat)
{
	$.post("/webshop/products.php", { pricef: priceFrom, pricet: priceTo, category: cat }, function(data) {
			$(".boxmiddenstuk").replaceWith(data);
	});
	
}

function rebuildProductListingKeyword(priceFrom, priceTo, keyword)
{
	$.post("/webshop/products.php", { pricef: priceFrom, pricet: priceTo, keyWord: keyword }, function(data) {
			$(".boxmiddenstuk").replaceWith(data);
	});	
}


function updateCart(id)
{
	
	aantal = $("#update-"+id).val();
	if(aantal == '0')
	{
		result = deleteFromCart(id,"2");
	}else
	{
		result = AddToCart(id,aantal,'update');
	}
}


function plaatsBestelling()
{
	$.post("webshop/sendOrder.php", { productid:'hoi' }, function(data) {
		$(".cartBox").replaceWith(data);
	});
}
/* utils */
function isInt(x)
{
	var y = parseInt( x );
	if ( isNaN( y ) ) return false;
	return x==y && x.toString()==y.toString();
}

