function cambia_espacio(cantidad) {
	$("#loader_espacio").show();
	$("#espacio").hide();
	$.post('inc/espacio.php',
	{
		cantidad: cantidad
	},
	function(data){
		$("#espacio").html(data).fadeIn();
		$("#loader_espacio").hide();
	});
	
}

function cambia_espacio_colores(tipo_polera_id, id) {
	$("#loader_espacio").show();
	var indice = id.replace("tipo_", "");
	$("#espacio_colores_"+indice).hide();
	$.post('inc/espacio_colores.php',
	{
		tipo_polera_id: tipo_polera_id,
		indice: indice
	},
	function(data){
		$("#espacio_colores_"+indice).html(data).fadeIn();
		$("#loader_espacio").hide();
	});
	
}

function agregar_polera(){
	if ($("#cantidad").val() != 0) {
		var cantidad = $("#cantidad").val();
		var tipos = $(".tipo_polera");
		error_tipo = true;
		$.each(tipos, function(k, obj){
			if ($(obj).val() != "") {
				error_tipo = false;
			}
		});
		if (error_tipo) {
			alert("Debes seleccionar al menos un tipo de polera");
		}
		else {
			str = "";
			$.each(tipos, function(k, obj){
				id = $(obj).attr('id').replace("tipo_", "");
				if ($(obj).val() != "") {
					if (!color_seleccionado(id)) {
						str += "Falta seleccionar el color para la Polera "+id + "\n";
					}
					if (!talla_seleccionada(id)) {
						str += "Falta seleccionar la talla para la Polera "+id + "\n";
					}
				}
			});
			if (str != "") {
				alert(str);
			}
			else {
				if (!confirm("Agregar productos al carro?")) 
					return false;
				
				var producto_id = $("#producto_id").val();

				var detalle = "";
				
				$.each(tipos, function(k, obj){
					id = $(obj).attr('id').replace("tipo_", "");
					tipo = $(obj).val();
					if (tipo != "") {
						color = get_color_seleccionado(id);
						talla = get_talla_seleccionada(id);
						detalle += tipo + "," + color + "," + talla;
						if (k+1 < tipos.length) {
							detalle += ";";
						}
					}
				});


				$.post('inc/agregar_al_carro.php',
				{
					producto_id: producto_id,
					detalle: detalle
				},
				function(data){
					document.location = "carro.php";
				});

			}
		}
	}
	else {
		alert("Debes especificar la cantidad de productos");
	}

}
function color_seleccionado(indice) {
	return $(".color_"+indice + ":checked").length;
}
function talla_seleccionada(indice) {

	return (!$("#talla_"+indice).val() == "")
}

function get_color_seleccionado(indice) {
	return $(".color_"+indice+":checked").val();
}
function get_talla_seleccionada(indice) {
	return ($("#talla_"+indice).val())
}
