// JavaScript Document
$(document).ready(function(){
	
// remove link background images since we're re-doing the hover interaction below 
// (doing it this way retains the CSS default hover states for non-javascript-enabled browsers)
// we also want to only remove the image on non-selected nav items, so this is a bit more complicated
$(".nav").children("li").each(function() {
	var current = "nav current-" + ($(this).attr("class"));
	var parentClass = $(".nav").attr("class");
	if (parentClass != current) {
		$(this).children("a").css({backgroundImage:"none"});
	}
});	


// create events for each nav item
attachNavEvents(".nav", "btnInicio");
attachNavEvents(".nav", "btnJuanma");
attachNavEvents(".nav", "btnReto");
attachNavEvents(".nav", "btnGaleria");
attachNavEvents(".nav", "btnNoticias");
attachNavEvents(".nav", "btnLibro");
attachNavEvents(".nav", "btnContacto");


function attachNavEvents(parent, myClass) {
	$(parent + " ." + myClass).mouseover(function() {
		$(this).append('<div class="nav-' + myClass + '"></div>');
		$("div.nav-" + myClass).css({display:"none"}).fadeIn(100);
	}).mouseout(function() {
		// fade out & destroy pseudo-link
		$("div.nav-" + myClass).fadeOut(100, function() {
			$(this).remove();
		});
	});
}
});	

function calculaCiudad(){
	switch (document.getElementById("ciudadkm").options[document.getElementById("ciudadkm").selectedIndex].value){
		case "1":
		case "2":
		case "4":
		case "5":
			document.getElementById("valor").value = "500,00 " + String.fromCharCode(8364);
			document.getElementById("km").value = "0";
			break;
		case "6":
			document.getElementById("valor").value = "1.000,00 " + String.fromCharCode(8364);
			document.getElementById("km").value = "0";
			break;		
	}
}

function calculaCiudadE(){
	switch (document.getElementById("ciudadkmE").options[document.getElementById("ciudadkmE").selectedIndex].value){
		case "1":
		case "2":
		case "4":
		case "5":
			document.getElementById("valorE").value = "500,00 " + String.fromCharCode(8364);
			document.getElementById("kmE").value = "0";
			break;
		case "6":
			document.getElementById("valorE").value = "1.000,00 " + String.fromCharCode(8364);
			document.getElementById("kmE").value = "0";
			break;		
	}
}

function calculaKM(){
	var kms;
	var valor;
	kms = document.getElementById("km").value;
	//Compruebo si es un valor numérico
	if (isNaN(kms)) {
		//no es un numero
		Sexy.alert('<h1>Error al calcular el importe</h1><p></p><p>Debe introducir el n&uacute;mero de kilometros que desee comprar en forma n&uacute;merica.</p>');
	}else{
		//si es un numero
		valor = parseInt(kms) * 10;
		document.getElementById("valor").value = valor + ",00 " + String.fromCharCode(8364);
	} 
}

function calculaKME(){
	var kms;
	var valor;
	kms = document.getElementById("kmE").value;
	//Compruebo si es un valor numérico
	if (isNaN(kms)) {
		//no es un numero
		Sexy.alert('<h1>Error al calcular el importe</h1><p></p><p>Debe introducir el n&uacute;mero de kilometros que desee comprar en forma n&uacute;merica.</p>');
	}else{
		//si es un numero
		valor = parseInt(kms) * 10;
		document.getElementById("valorE").value = valor + ",00 " + String.fromCharCode(8364);
	} 
}

function validarEmail(valor) {
  if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(valor)){
   return (true)
  } else {
   return (false);
  }
 }


function valida(){
	if ((isNaN(document.getElementById("km").value)) || (document.getElementById("email").value == '') || (document.getElementById("nombre").value == '') || (document.getElementById("apellidos").value == '')) {
		Sexy.alert('<h1>ATENCI&Oacute;N</h1><p></p><p>Debe introducir los kilometros a comprar o una ciudad, su nombre y apellidos, y un email para enviarle notificaci&oacute;n de la operaci&oacute;n.</p>');
		return false;
	}else{
		if(validarEmail(document.getElementById("email").value)) {
			return true;
		}else{
			Sexy.alert('<h1>Email incorrecto</h1><p></p><p>La dirrecci&oacute;n email que ha introducido no es correcta.</p>');
			return false;
		};
	} 
}
function validaE(){
	if ((isNaN(document.getElementById("kmE").value)) || (document.getElementById("emailE").value == '') || (document.getElementById("nombreE").value == '') || (document.getElementById("cif").value == '')) {
		Sexy.alert('<h1>ATENCI&Oacute;N</h1><p></p><p>Debe introducir los kilometros a comprar o una ciudad, nombre de la empresa y CIF, y un email para enviarle notificaci&oacute;n de la operaci&oacute;n.</p>');
		return false;
	}else{
		if(validarEmail(document.getElementById("emailE").value)) {
			return true;
		}else{
			Sexy.alert('<h1>Email incorrecto</h1><p></p><p>La dirrecci&oacute;n email que ha introducido no es correcta.</p>');
			return false;
		};
	} 
}
