function compruebaFormCV()
{
	var campos = $('form .req');
	if (campos)
	{
		$('form .req').removeClass('error');
		
		for( var i = 0; i < campos.length; i++ ) {
			valor = jQuery.trim($(campos[i]).val());
			if (valor == '' || ($(campos[i]).attr('name') == 'email' && !checkEmail(valor))	){
				alert("Por favor comprueba el campo marcado en rojo");
				$(campos[i]).addClass('error').get(0).focus();
				return false;
			}
		}
		
		return true;
	}
	
	return false;
}



$(document).ready(function () {
	$("#nav ol>li").hover(
      function () {
        $(".submenu", this).css('display','block'); //fadeIn('fast');
      }, 
      function () {
        $(".submenu", this).fadeOut('fast');
      }
    );
	
	$("#nav ol>li").each(function (i) { // hago que el padre enlace al primer hijo
		var href = $('.submenu a:first', this).attr('href');
		if (href != undefined)
			$('a:first',this).attr('href', href);
      });
	
	if ($('#product-details .product-description .observaciones').get(0))
	{
		var html = $('#product-details .product-description .observaciones').html();
		$('.tdobservaciones').html(html);
		$('#product-details .product-description .observaciones').remove();
	}
	
	// Catálogo BV
	if ($('#page-blog').get(0))
	{
		$('.catalogo-anterior').each (function (i) {
													var imagen = $('img:first', this).attr('src');
													
													enlacecat = $('td:eq(1) a', this).attr('href');
													$('td:first a', this).attr('href', enlacecat);
													$('#catalogo-ampliado td:first a').attr('href', enlacecat);
													enlacedes = $('td:eq(3) a', this).attr('href');
													$('td:eq(2) a', this).attr('href', enlacedes);
													$('#catalogo-ampliado td:eq(2) a').attr('href', enlacedes);
													
													$('p, img, table', this).css('display', 'none');
													var elemento = '<a href="javascript:void(0)" onclick="$(\'#catalogo-ampliado\').html($(\'#' + $(this).attr('id') + '\').html()); $(\'#catalogo-ampliado p, #catalogo-ampliado img, #catalogo-ampliado table\').css(\'display\',\'\'); $(\'#catalogo-ampliado img:first\').css(\'display\',\'none\'); return false"><img src="' + imagen + '" width="99" /></a>';
													$(this).prepend(elemento);
												});
	
		$('.lista tr').each(function(i) {
				var href = $('a:first', this).attr('href');
				$('a:last', this).attr('href', href);
			});
	}
	

	// Articulo
	if ($('#page-article').get(0))
	{
		var plantilla = $('.article-images').html();
		$('.article-images p').remove();

		$('.articlecontents img').each (function(i) {
			var href = $(this).attr('src');
			var p = $(plantilla);
			$('a', p).attr('href', href).attr('rel','shadowbox');
			var cont = $(p).html();

			$('.article-images').append('<p><a rel="shadowbox[article]" href="' + href + '"><img src="' + href + '" width="234" /></a></p><p style="text-align:right;padding:1px 5px;">' + cont + '</p>');
			$(this).remove();
		});
	}
	
	
	// Portada
	if ($('#page-index').get(0))
	{
		var portadas = $('.portada>div').get();
		var portadaActual  = 0;
		function iniciar_animacion_portada()
		{
			$('#portada-' + portadaActual).fadeOut(1000, function() {
				portadaActual = (portadaActual+1)%portadas.length;
				$('#portada-' + portadaActual).fadeIn(1000, function() { setTimeout(iniciar_animacion_portada, 3000); });
			});
		}
		setTimeout(iniciar_animacion_portada, 4000);
		
		// $('.portada').cycle();
	}
	
	
	if ($('#messages').get(0))
		setTimeout( function(){ $('#messages').fadeOut('fast'); }, 2500);
		

	$('#formcv').submit (function() {
								  return compruebaFormCV();
								  });
});









/*
 *
 * Valida el formato de una cadena que representa una dirección de email
 *
 */
function checkEmail (emailStr) 
{
		/* Verificar si el email tiene el formato usuario@dominio. */
		var emailPat=/^(.+)@(.+)$/ 

		/* Verificar la existencia de caracteres. ( ) < > @ , ; : \ " . [ ] */
		var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]" 
		
		/* Verifica los caracteres que son válidos en una dirección de email */
		var validChars="\[^\\s" + specialChars + "\]" 

		var quotedUser="(\"[^\"]*\")" 

		/* Verifica si la dirección de email está representada con una dirección IP Válida */ 
		var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/

		/* Verificar caracteres inválidos */ 
		var atom=validChars + '+'
		var word="(" + atom + "|" + quotedUser + ")"
		var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
		/* domain, as opposed to ipDomainPat, shown above. */
		var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
	
		var matchArray=emailStr.match(emailPat)
		if (matchArray==null) 
		{
			// alert("La dirección de correo electrónico introducida parece ser incorrecta")
			return false
		}
		var user=matchArray[1]
		var domain=matchArray[2]

		// Si el usuario "uuarior" es valido 
		if (user.match(userPat)==null) 
		{
			// Si no
			// alert("El nombre de usuario de la dirección de correo electrónico no es válido.")
			return false
		}

		/* Si la dirección IP es válida */
		var IPArray=domain.match(ipDomainPat)
		if (IPArray!=null) 
		{
			for (var i=1;i<=4;i++) {
				if (IPArray[i]>255) 
				{
					// alert("IP de destino del email inválida")
					return false
				}
			}
			return true
		}

		var domainArray=domain.match(domainPat)
		if (domainArray==null) 
		{
			// alert("El dominio parece no ser válido.")
			return false
		}

		var atomPat=new RegExp(atom,"g")
		var domArr=domain.match(atomPat)
		var len=domArr.length
		if (domArr[domArr.length-1].length<2 || 
			domArr[domArr.length-1].length>3) 
		{ 
			// alert("La dirección de email debe tener 3 letras si es .com o 2 si es de algún pais.")
			return false
		}

		if (len<2) 
		{
			// var errStr="La dirección de correo electrónico es errónea"
			// alert(errStr)
			return false
		}

		// La dirección de email ingresada es Válida
		return true;
}
