function setCookie(c_name,value,expiredays) {
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toUTCString());
}

function Cart() {
	this.subTotal = 0;
	this.shipping = 0;
	this.total = 0;

	this.getSubTotal = function() {
		var subTotal = 0;

		$("span.price").each(function() {
			var amount = $(this).parents("tr").find("input.amount");
			amount = parseInt(amount.val());

			var price = parseFloat($(this).text());

			subTotal += (amount * price);
		});

		this.subTotal = subTotal;

		return this.subTotal.toFixed(2);
	};

	this.getShipping = function() {
		if ($("#deliveryMethod").val() == "shipping") {
			var amount = this.subTotal;

			if (amount < 250  && amount > 0) {
				this.shipping = 15.00;
			}
		}

		return this.shipping.toFixed(2);
	};
	
	this.getBTW = function() {
		this.BTW = 0.19 * (this.subTotal + this.shipping);
		return this.BTW.toFixed(2);
	};

	this.getTotal = function() {
		this.total = this.subTotal + this.shipping + this.BTW;
		return this.total.toFixed(2);
	};
}

$(document).ready(function() {
	$("input.amount").keyup(function() {
		var value = $(this).val(); 
		var amount = parseInt(value);
		
		if (value == "") {
			return;
		}
		
		if (value.search(/[^\d]+/) > -1) {
			$(this).val(1).keyup();
			return;
		}
		
		if (amount == 0) {
			$(this).parents("tr").find("a.removeProduct").click();
			return;
		}

		//Store order info
		$.post("/order/store/", $(this.form).serialize());

		var price = $(this).parents("tr").find("span.price");
		price = parseFloat(price.text());

		var newPrice = new Number(price * amount);
		newPrice = newPrice.toFixed(2);
		newPrice = newPrice.replace(".", ",");

		$(this).parents().find("span").eq(0).text(newPrice);

		var cart = new Cart();

		$("#subTotal").text(cart.getSubTotal().replace(".", ","));
		$("#shipping").text(cart.getShipping().replace(".", ","));
		$("#btw").text(cart.getBTW().replace(".", ","));
		$("#total").text(cart.getTotal().replace(".", ","));
	}).keyup();
	
	$("#deliveryMethod").change(function() {
		$("input.amount").keyup();
	})

	$("#emptyButton").click(function() {
		window.location = "/order/delete";
	});

	$("#furtherButton").click(function() {
		window.location = "/webshop/";
	});

	$("input[name=isShipping]:radio").each(function() {
		if ($(this).val() == 1 && $(this).is(":checked")) {
			$("div#shipping").slideDown("normal");
		}
	});

	$("input[name=isShipping]:radio").click(function() {
		if ($(this).val() == 1 && $(this).is(":checked")) {
			$("div#shipping").slideDown("normal");
		} else {
			$("div#shipping").slideUp("normal");
		}
	});

	$("input#customerNumber").blur(function() {
		var number = parseInt($(this).val());
		if (isNaN(number) || (number <= 0)) number = 0;

		$.getJSON("/order/lookup/" + number, function(data) {
			$.each(data, function(key, value) {
				if (key == "isShipping") {
					if (value == 1) {
						$("#yesShipping").attr("checked", "checked").click();
					} else if (value == 0) {
						$("#noShipping").attr("checked", "checked").click();
					}
				} else {
					$("input[name=" + key + "]").val(value);
				}
			});
		});
	});

	$("a.removeProduct").click(function(e) {
		e.preventDefault();

		var tr = $(this).parents("tr");

		$.get("/order/delete/" + tr.attr("id"), function() {
			tr.slideUp("normal", function() {
				$(this).remove();

				var inputElements = $("input.amount");

				if (inputElements.length > 0) {
					inputElements.keyup();
				} else {
					window.location = "/order/cart/";
				}
			})
		});
	});
		
	$("div#banner").cycle({timeout: 5000});
});