// Lägg till produkt i kundvagnen
function addToShoppingcart($product_id, $product_quantity, $article_number){
	$.post("/shoppingcart/increase_product_count", { product_id: $product_id, count: $product_quantity, article_number: $article_number }, function(data) {
		refresh_widgets( { shoppingcart_product_update_id: $product_id } );
		update_order_form_state();
	});
}

// Ta bort X antal produkter av en typ ur kundvagnen
function removeFromShoppingcart($product_id, $product_quantity){
	$.post("/shoppingcart/decrease_product_count", { product_id: $product_id, count: $product_quantity }, function(data) {
		refresh_widgets( { shoppingcart_product_update_id: $product_id } );
		update_order_form_state();
	});
}

// Ta bort en produkt ur kundvagnen
function removeProductFromShoppingcart($product_id){
	$.post("/shoppingcart/remove_product", { product_id: $product_id }, function(data) {
		refresh_widgets( {} );
		update_order_form_state();
	});
}

// Välj betalsätt
function selectPaymentOption($payment_type_id, $payment_class){
	$.post("/shoppingcart/select_payment_type", { payment_type_id: $payment_type_id, payment_class : $payment_class }, function(data) {
		refresh_widgets( {} );
		update_order_form_state();
	});
}

// Välj leveranssätt
function selectDeliveryOption($delivery_type_id){
	$.post("/shoppingcart/select_delivery_type", { delivery_type_id: $delivery_type_id }, function(data) {
		refresh_widgets( {} );
		update_order_form_state();
	});
}

function openKreditorPopUp(){
	window.open("https://online.kreditor.se/account_se.yaws?eid=2","Information",
	"menubar=no,width=640,height=400,toolbar=no");
}

// Ta emot information från produktformuläret och lägg till en produkt i kundvagnen
$(document).ready(function() {
	// Ladda alla widgets
	refresh_widgets();
	
	// Bind "Lägg till produkt"
	$(".product_form").submit(function(event) {
		event.preventDefault();
		event.stopPropagation();
		
		var $product_id = $(this).find( ".product_id" ).val();
		var $product_quantity = $(this).find( ".quantity" ).val();
		var $article_number = $(this).find( ".article_number" ).val();
		
		addToShoppingcart($product_id, $product_quantity, $article_number);
	});
	
	$("A#tell_a_friend").click(function(event) {
		event.preventDefault();
		
		if ( $("#tell_a_friend_form").css('display') == 'none' ) {
			$("#tell_a_friend_form").show('slow');
		} else {
			$("#tell_a_friend_form").hide('slow');
		}
	});
	
	$("#tell_a_friend_form").submit(function(event) {
		event.preventDefault();
		
		$.post('/form/tell_a_friend/', { product_id: $('.product_id:first').val(), teller_name: $('.teller_name:first').val(), teller_email: $('.teller_email:first').val(), friend_email: $('.friend_email:first').val() }, function(data) {
			if ( data.substring(0,2) == 'E:' ) {
				alert(data.substring(2));
			} else {
				 $('.teller_name:first').val('');
				 $('.teller_email:first').val('');
				 $('.friend_email:first').val('');
				 alert(data);
			}
		});
	});
});
