$(document).ready(function(){
	lastBlock = $("#vinos_producto");
	maxWidth = 740;
	minWidth = 0;	

	$("#vinos").click(
	  function(){ 			  	  
		$(lastBlock).animate({width: minWidth+"px"}, { queue:false, duration:1000 });				
		$(("#vinos_producto")).animate({width: maxWidth+"px"}, { queue:false, duration:1000});
		$(".producto_vinos").show();
		$(".producto_aceites").hide('slow');
		$(".producto_conservas").hide('slow');
		$(".producto_packs").hide('slow');				
		lastBlock = $("#vinos_producto");
	  }
	);
	
	$("#aceites").click(
	  function(){
		$(lastBlock).animate({width: minWidth+"px"}, { queue:false, duration:1000 });
		$(("#aceites_producto")).animate({width: maxWidth+"px"}, { queue:false, duration:1000});
		$(".producto_aceites").show();
		$(".producto_vinos").hide('slow');
		$(".producto_conservas").hide('slow');
		$(".producto_packs").hide('slow');
		lastBlock = $("#aceites_producto");
	  }
	);
	
	$("#conservas").click(
	  function(){
		$(lastBlock).animate({width: minWidth+"px"}, { queue:false, duration:1000 });
		$(("#conservas_producto")).animate({width: maxWidth+"px"}, { queue:false, duration:1000});
		$(".producto_conservas").show();
		$(".producto_vinos").hide('slow');
		$(".producto_aceites").hide('slow');
		$(".producto_packs").hide('slow');

		lastBlock = $("#conservas_producto");
	  }
	);
	
	$("#packs").click(
	  function(){
		$(lastBlock).animate({width: minWidth+"px"}, { queue:false, duration:1000 });
		$(("#packs_producto")).animate({width: maxWidth+"px"}, { queue:false, duration:1000});
		$(".producto_packs").show();
		$(".producto_vinos").hide('slow');
		$(".producto_aceites").hide('slow');
		$(".producto_conservas").hide('slow');
		
		lastBlock = $("#packs_producto");
	  }
	);
});

function abreFicha(idProducto){
	var parametros = "?idProducto="+idProducto;
	$.ajax({
		cache: false,
		url: "./includes/tienda/ficha.php"+parametros,
		beforeSend: function() {
			var loading = '<img src="images/loading.gif" border="0" width="96" height="12" alt="action in progress, please wait" />';
			$('#ficha').html(loading);
		},	
		success: function(msg){
			$('#ficha').html(msg);
		}
	});	
}

function anyadirEliminarProducto(accion, idProducto)
{
	var parametros = "?idProducto="+idProducto+"&accion="+accion;
	var Ids = $('#arrayProductos').val();
	if (Ids != "")
	{
		var arrayIds = $('#arrayProductos').val().split("-");
		parametros += "&ids="+$('#arrayProductos').val();
		for (id in arrayIds)
		{
			var cantidad = $('#cantidad_'+arrayIds[id]).val();
			parametros += "&cantidad_"+arrayIds[id]+"="+cantidad;		
		}
	}
	$.ajax({
		cache: false,
		url: "./includes/tienda/carrito.php"+parametros,
		beforeSend: function() {
			var loading = '<img src="images/loading.gif" border="0" width="96" height="12" alt="action in progress, please wait" />';
			$('#formulario').html(loading);
		},	
		success: function(msg){
			$('#formulario').html(msg);
		}
	});
	document.location = "#formulario";
}

function recalculaPrecio(tecla, idProducto)
{
	var arrayIds = $('#arrayProductos').val().split("-");
	if ( ((95 > tecla) || (tecla > 105)) && ((48 > tecla) || (tecla > 57)) && ((37 > tecla) || (tecla > 40)) && tecla != 8)
	{
		$('#cantidad_'+idProducto).val("1");
		$('#cantidadPaypal'+idProducto).val("1")
	}
	else if ( ( (tecla == 8) && ($('#cantidad_'+idProducto).val() == "") ) || ($('#cantidad_'+idProducto).val()== 0) ) 
	{
		$('#cantidad_'+idProducto).val("1");
		$('#cantidad_'+idProducto).select();
		$('#cantidadPaypal'+idProducto).val("1");
	}
	else
	{
			$('#cantidadPaypal'+idProducto).val($('#cantidad_'+idProducto).val())
	}

	
	var precioTotal = 0;
	var ivaTotal = 0;
	for (id in arrayIds)
	{
		porcentaje = $('#iva'+arrayIds[id]).val();
		unidadesCaja = $('#unidadesCaja'+arrayIds[id]).val();
		precioProducto = parseInt($('#cantidad_'+arrayIds[id]).val())*parseFloat($('#precioValor_'+arrayIds[id]).val())*parseInt(unidadesCaja);
		ivaTotal += parseFloat(precioProducto)*parseInt(porcentaje)/100;
		precioTotal += parseFloat(precioProducto);
	}
	
	var precioTotalIva = precioTotal+ ivaTotal;
	
	$('#price_1').html(redondea(precioTotal,2)+' &euro;');
	$('#ivaTotal').html(redondea(ivaTotal,2)+' &euro;');
	$('#ivaPaypal').val(redondea(ivaTotal,2));
	$('#precioFinal').html(redondea(precioTotalIva,2)+' &euro;');
	
	if (precioTotalIva == 0)
		$('#gastos').hide();
	else
		$('#gastos').show();
		
	$('#precioFinal').effect("highlight", {}, 1500);
}

   function redondea(sVal, nDec){
    var n = parseFloat(sVal);
    var s = "0.00";
    if (!isNaN(n)){
     n = Math.round(n * Math.pow(10, nDec)) / Math.pow(10, nDec);
     s = String(n);
     s += (s.indexOf(".") == -1? ".": "") + String(Math.pow(10, nDec)).substr(1);
     s = s.substr(0, s.indexOf(".") + nDec + 1);
    }
    return s;
   }
   
function enviaPedido(){
	
	if(document.formCarrito.destinoEnvio[0].checked )//dentro de peninsula
	{
		document.formPaypal.submit();	
	}
	else//Fuera de peninsula
	{
		document.formCarrito.submit();
	}
 }
 
 function compruebaCampos()
 {
	 var error = "Por favor, rellene correctamente los siguientes campos:\n";
	 var continuar = true;
	if ($('#nombreCliente').val() == "")
	{
		error += "Nombre\n"
		continuar = false;
	}
	if ($('#apellidosCliente').val() == "")
	{
		error += "Apellidos\n"
		continuar = false;
	}
	if ($('#paisCliente').val() == "")
	{
		error += "Pais\n"
		continuar = false;
	}
	if (!validarEmail($('#emailCliente').val()))
	{
		error += "Email\n"
		continuar = false;
	}
	if ($('#telefonoCliente').val() == "")
	{
		error += "Telefono\n"
		continuar = false;
	}
	
	if (continuar)
		document.datosCliente.submit();
	else
	{
		alert (error);
	}
 }
 
function validarEmail(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    return false
		 }

 		 return true					
	}


