// JavaScript Document
var producto;

function agregarCarrito(productoId, cantidad, talla){
	producto = productoId;
	var add = 1;
	//var cantidad = parseInt($('#cantidad').val());
	$.ajax({
		type: "POST",
		url: "carrito-ajax.php",
		data: "productoId="+productoId+"&cantidad="+cantidad+"&talla="+talla+"&accion=1",
		success: function(datos){
			switch(datos){
				case '1':
					
					break;
				case '2':
					//$('#dialog-cart-mod').dialog('open');
					add=0;
					break;
				case '3':
					$('#dialog-preorder-list').dialog('open');
					add=0;
					break;
			}
			var cant = parseInt($('#cant-cart').html());
			cant = cant + add;
			$('#cant-cart').html(cant.toString());
			if(datos!='3')
				location.href='carrito-de-compras.php';
		},
		error: function(objeto, quepaso, otroobj){
			alert("Ha ocurrido un error intentelo nuevamente");
		}
	});
}

function eliminarCarrito(productoId){
	$.ajax({
		type: "POST",
		url: "carrito-ajax.php",
		data: "productoId="+productoId+"&accion=2",
		success: function(datos){
			if(datos==1){
				jqRow = $("#producto-"+productoId);	
				jqRow.fadeOut("slow", function()
				{
					actualizaPrecio();
					jqRow.remove();
					var cant = parseInt($('#cant-cart').html());
					cant = cant - 1;
					$('#cant-cart').html(cant.toString());
				});
			}
		},
		error: function(objeto, quepaso, otroobj){
			alert("Ha ocurrido un error intentelo nuevamente");
		}
	});
}

function actualizaPrecio(){
	$.ajax({
		type: "POST",
		url: "carrito-ajax.php",
		data: "accion=3",
		success: function(datos){
			$("#subtotal").html("&cent;"+datos);
		},
		error: function(objeto, quepaso, otroobj){
			alert("Ha ocurrido un error intentelo nuevamente");
		}
	});
	
	$.ajax({
		type: "POST",
		url: "carrito-ajax.php",
		data: "accion=4",
		success: function(datos){
			$("#descuento").html("&cent;"+datos);
		},
		error: function(objeto, quepaso, otroobj){
			alert("Ha ocurrido un error intentelo nuevamente");
		}
	});
	
	$.ajax({
		type: "POST",
		url: "carrito-ajax.php",
		data: "accion=5",
		success: function(datos){
			$("#total_label").html(datos);
			$("#total").val(datos);
		},
		error: function(objeto, quepaso, otroobj){
			alert("Ha ocurrido un error intentelo nuevamente");
		}
	});
	
}
