
	function select_precio(precio_id, cantidad, precio, referencia)
	{
		document.getElementById('precio_id').value = precio_id;
		document.getElementById('precio_selected').value = precio_id;
		document.getElementById('subtotal').innerHTML = precio;
		document.getElementById('referencia_selected').innerHTML = referencia;
		
		if(document.getElementById('cantidad_selected').type == 'text')
		{
			document.getElementById('cantidad_selected').value = cantidad;
		}
		else
		{
			document.getElementById('cantidad_selected').innerHTML = cantidad;
		}

	}
	
	var ventana;
	
	function popup_add_cart(href, target, name, options)
	{
		if( document.getElementById('precio_id') )
		{
			var precio = document.getElementById('precio_id').value;
		
			_href = href + "&precio=" + precio;
				
			if(document.getElementById('cantidad_selected').type == 'text')
			{
				var cantidad = document.getElementById('cantidad_selected').value;
				_href = _href + "&cantidad=" + cantidad;
			}
			else
			{
//				var cantidad = document.getElementById('cantidad_selected').innerHTML;
			}

			
		}
		else		
		{
			_href = href;
		}
		
		if( ventana !== undefined ) ventana.close();
		
		ventana = popup(_href, target, name, options);
		
		return false;
	}
	
	
	function isNumeric(value) 
	{ 
	    return value.match(/^\d+$/);
	}

	
	$(document).ready(function(){
		
	// en caso de pasar el raton por encima de una cantidad
	
		$('.cantitats_select').mouseover(function(){
				
		// cambiamos el estilo
		
			$(this).css('color', '#FFFFFF');
			$(this).css('background-color', '#00416b');
			
		// cambiamos el puntero del raton
		
			$(this).css('cursor', 'pointer');
				
		});
			
	// en caso de desplazar el raton fuera de una cantidad
		
		$('.cantitats_select').mouseout(function(){
			
		// cambiamos el estilo si no está seleccionado
		
			if( $(this).attr('select') != 'true' )
			{
				$(this).css('color', '#000000');
				$(this).css('background-color', '#DEE5EC');
			}
			
		// cambiamos el puntero del raton
		
			$(this).css('cursor', 'default');
			
		});
		
	// en caso de seleccionar una cantidad
	
		$('.cantitats_select').click(function(){
			
		// deseleccionamos todas las cantidades
		
			$('.cantitats_select').each(function(){
				$(this).attr('select', 'false');
				
			// cambiamos al estilo mouseout
			
				$(this).trigger('mouseout');
			});
			
		// seleccionamos la cantidad seleccionada y cambiamos su estilo
		
			$(this).attr('select', 'true');
			$(this).trigger('mouseover');
		});
		
	// al cargar la pagina, marcamos la primera cantidad
	
		$('.cantitats_select:first').trigger('mouseover');
		
		
	// controlamos que solo se introduzcan numeros en el input de cantidad
	
		$('#cantidad_selected').keydown(function(event){
			
		// sólo aceptamos numéricas
		
			if( event.keyCode == 8 			// backspace
			 || event.keyCode == 46			// delete
			 || event.keyCode == 37			// left arrow
			 || event.keyCode == 39			// right arrow
			 || (event.keyCode >= 48 && event.keyCode <= 57)	// numeric codes
			 || (event.keyCode >= 96 && event.keyCode <= 105) ) // numeric codes
			{
				result = true;
			}
			else			
			{
				result = false;
			}
			
			return result;
			
		});

		
	// llamada
	
		$('#cantidad_selected').keyup(function(event){
		
		// comprobamos que el contenido sea un numerico

			if(isNumeric(this.value) && this.value > 0)
			{
			// substituimos el contenido del subtotal por una imagen de cargando
			
//				$('#subtotal').html('.');
				
			// llamada ajax para calcular el subtotal
			
				$.get( $('#precio_ajax_url').attr('value'),
					   { producto : $('#producto_id').attr('value'),
					     precio : $('#precio_selected').attr('value'),
					     cantidad : this.value 
					   } ,
					   function(data){
					   	$('#subtotal').html(data);
					   }
					);
					
			}
			
		});
		
		
	});
