var now = new Date();
var expiry = new Date(now.getTime() + 1 * 24 * 60 * 60 * 1000); // plus 1 days
var thepath = "/";
var thedomain = "www.ballen.be";

// This function checks if the given var is a valid number between 0 and 999)
function checknbr(nbr) {
	if(nbr.length>0) {
		if(isNaN(nbr) || nbr<0 || nbr>999 || nbr.indexOf('.')>0) {
			alert("U hebt een ongeldige waarde als aantal opgegeven.");
			return false;
		}
		return true;
	}
	else {
		alert("U moet nog het aantal dat u wenst te bestellen invullen.");
		return false;
	}
}

// This function removes all leading and trailing spaces.
function stripSpaces(x) {
	while (x.substring(0,1) == ' ') x = x.substring(1);
	while (x.substring(x.length-1,x.length) == ' ') x = x.substring(0,x.length-1);
	return x;
}

// add a product to the cart
function addToCart(code,value) {
	if(checknbr(value)) {
		Set_Cookie(code,value,expiry,thepath,thedomain);
	}
}

// remove all items from the cart
function resetCart() {
	if(confirm("Dit verwijderd alle producten uit je winkel wagentje.\nBen je zeker dat je dit wil ?")) {
		var cookie = document.cookie;
		var endstr = -1;
		
		// split cookie-string into seperate cookies
		while(cookie.length>0) {
			endstr = cookie.indexOf(";");
			if(endstr==-1) {
				endstr=cookie.length;
			}
			
			var fullcookie = cookie.substring(0,endstr);
			cookie = cookie.substring(endstr+1,cookie.length);
		
			endstr = fullcookie.indexOf("=");
			if(endstr>=0) {
				var cname = fullcookie.substring(0,endstr);
				Delete_Cookie(stripSpaces(cname),thepath,thedomain);	
			}
		}

		alert("Your cart has been emptied.");
		window.location.href = window.location.href;
        }
        else {
        	//alert("Nothing deleted.");
        }
}

// remove a single item from the cart
function deleteFromCart(code) {
	Delete_Cookie(code,thepath,thedomain);
}
