// VERKKOKAUPAN PERUSFUNKTIOT

//
// Ostoskori
//

function showCart() {
	$.ajax({
		type:      "POST",
		url:       "/ostoskori/nayta/",   
        dataType:  "json",
		success:    function (data) {
			$("#ajaxCart").html(data.cart);
			
			$('table.colors tbody tr:not([th]):odd').addClass('odd');
			$('table.colors tbody tr:not([th]):even').addClass('even');
			
			$("#ajaxCart").dialog({
				title: "Ostoskori",
				modal: true,
				resizable: false,
				draggable: false,
				width: 800,
				buttons: {
					"Sulje ostoskori": function() {
						$(this).dialog('destroy');
					},
					"Kassalle": function() {
						window.location = "/kassa/"
					},
					"Tyhjennä ostoskori": function() {
						emptyCart();
					}
				},
				close: function() {
					$(this).dialog('destroy');
				}
			});
			$(".button-refresh").button({
				icons: { primary:'ui-icon-refresh' },
				text:false
			});
		},
		error: 	   function ( _response )
		{
			alert('Virhe!');
		}
	});
}

function addToCart() {
	$.ajax({
		type:      "POST",
		url:       "/ostoskori/lisaa/",   
        dataType:  "json",
		data:		$('#form-tuote').serialize(),
		success:    function (_response) {
			if(processAjaxResponse(_response)) {
				$('#webshop_header_cart').html(_response.header_cart)
			}
			else
			{
			
			}
		},
		error: 	   function ( _response )
		{
			alert('Virhe!');
		}
	});
}

function updateCart(_rowid, _qty) {
	$.ajax({
		type:      "POST",
		url:       "/ostoskori/paivita/",   
        dataType:  "json",
		data:		"rowid="+_rowid+"&qty="+_qty,
		success:    function (_response) {
			if(processAjaxResponse(_response)) {
				if(_qty == 0)
				{
					$('#cart-item-'+_rowid).remove();
				}
				
				$('#cart-subtotal-'+_rowid).html(_response.subtotal);
				$('#cart-ajax-total').html(_response.total);
				$('#webshop_header_cart').html(_response.header_cart)
				
			}
			else
			{
			
			}
		},
		error: 	   function ( _response )
		{
			alert('Virhe!');
		}
	});
}

function emptyCart() {
	$.ajax({
		type:      "POST",
		url:       "/ostoskori/tyhjenna/",   
        dataType:  "json",
		data:		$('#form-tuote').serialize(),
		success:    function (_response) {
			if(processAjaxResponse(_response)) {
				$('#cart-ajax-items').html(_response.cart);
				$('#webshop_header_cart').html(_response.header_cart)
			}
			else
			{
			
			}
		},
		error: 	   function ( _response )
		{
			alert('Virhe!');
		}
	});
}
