// JavaScript Document
function Ajax(){
 var xmlhttp=false;
 try{
  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 }catch(e){
  try {
   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  }catch(E){
    xmlhttp = false;
  }
 }
 if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
  xmlhttp = new XMLHttpRequest();
 }
 return xmlhttp;
}
/*-------------*/

function validarEmail(valor) {
  if (/^w+([.-]?w+)*@w+([.-]?w+)*(.w{2,3})+$/.test(valor)){
	return (true);
  } else {
	return (false);
  }
}
/*-------------*/
function mostrar_email(){
	ocultar_foto();
	ocultar_codigo();
	ocultar_amigos();
	ocultar_foto_form();
	//var mivar = document.getElementById("cont_email");
	//mivar.style.display = "block";
}
function ocultar_email(){
	var mivar = document.getElementById("cont_email");
	mivar.style.display = "none";
}
/*---*/
function mostrar_email_exito(){
	var mivar = document.getElementById("cont_email_exito");
	mivar.style.display = "block";
}
function ocultar_email_exito(){
	var mivar = document.getElementById("cont_email_exito");
	mivar.style.display = "none";
}
/*--------------------------------*/
function mostrar_foto(){
	ocultar_email();
	ocultar_codigo();
	ocultar_amigos();
	ocultar_foto_form();
	var mivar = document.getElementById("cont_mostrar_foto");
	mivar.style.display = "block";
}
function ocultar_foto(){
	var mivar = document.getElementById("cont_mostrar_foto");
	mivar.style.display = "none";
}
/*---*/
function mostrar_foto_form(){
	var mivar = document.getElementById("cont_form_cargar_foto");
	mivar.style.display = "block";
}
function ocultar_foto_form(){
	var mivar = document.getElementById("cont_form_cargar_foto");
	mivar.style.display = "none";
}
/*---*/
function mostrar_foto_exito(){
	var mivar = document.getElementById("cont_cargar_foto_exito");
	mivar.style.display = "block";
}
function ocultar_foto_exito(){
	var mivar = document.getElementById("cont_cargar_foto_exito");
	mivar.style.display = "none";
}
/*--------------------------------*/
function mostrar_codigo(){
	ocultar_foto();
	ocultar_email();
	ocultar_amigos();
	ocultar_foto_form();
	var mivar = document.getElementById("cont_mostrar_codigo");
	mivar.style.display = "block";
}
function ocultar_codigo(){
	var mivar = document.getElementById("cont_mostrar_codigo");
	mivar.style.display = "none";
}
/*---*/
function mostrar_registro(){
	var mivar = document.getElementById("cont_registro");
	mivar.style.display = "block";
}
function ocultar_registro(){
	var mivar = document.getElementById("cont_registro");
	mivar.style.display = "none";
}
/*---*/
function mostrar_gracias_registro(){
	var mivar = document.getElementById("cont_registro_gracias");
	mivar.style.display = "block";
}
function ocultar_gracias_registro(){
	var mivar = document.getElementById("cont_registro_gracias");
	mivar.style.display = "none";
}
/*--------------------------------*/
function mostrar_amigos(){
	ocultar_codigo();
	ocultar_email();
	ocultar_foto_form();
	var mivar = document.getElementById("cont_engancha_a_tus_amigos");
	mivar.style.display = "block";
}
function ocultar_amigos(){
	var mivar = document.getElementById("cont_engancha_a_tus_amigos");
	mivar.style.display = "none";
}
/*---*/
function mostrar_amigos_exito(){
	var mivar = document.getElementById("cont_amigos_exito");
	mivar.style.display = "block";
}
function ocultar_amigos_exito(){
	var mivar = document.getElementById("cont_amigos_exito");
	mivar.style.display = "none";
}
/*-----------------------------------------------------------------------------------------------------*/
function mensaje_ya_suscrito(){
	alert('Este email ya está suscrito\n Gracias');
}

function f_email(){
  var email = document.getElementById('el_email').value;
  
  var contenedor = document.getElementById('cont_email');
  
  if(email != ""){
	  if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(email)){
		  valores ="email="+email;
		  ajax=Ajax();
		  ajax.open("POST", "acciones/email.php", true);
		  ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		  ajax.onreadystatechange=function() {
			if (ajax.readyState==4) {
			  
			  //contenedor.innerHTML = ajax.responseText;
			  
			  //target="popup";
			  var salida = ajax.responseText;
			  if(salida == "repe"){
				  mensaje_ya_suscrito();
			  }
			  if(salida == "ok"){
				mostrar_email_exito();
			  }
			}
		  }
		  ajax.send(valores);
	  }else{
		  alert('Debe introducir un email válido');
	  }
  }else{
	alert('Debe introducir el email');	
  }
}
/*-----------------------------------------------------------------------------------------------------*/
function f_amigos(){
  var de_nombre = document.getElementById('b_de_nombre').value;
  var de_mail = document.getElementById('b_de_mail').value;
  var para_nombre = document.getElementById('b_para_nombre').value;
  var para_mail = document.getElementById('b_para_mail').value;
  
  var contenedor = document.getElementById('cont_engancha_a_tus_amigos');
  
  if(de_nombre != ""){
	  if(de_mail != ""){
		  if(para_nombre != ""){
			  if(para_mail != ""){
				  if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(de_mail)){
					  if(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(para_mail)){
						  valores ="de_nombre="+de_nombre+"&de_mail="+de_mail+"&para_nombre="+para_nombre+"&para_mail="+para_mail;
						  ajax=Ajax();
						  ajax.open("POST", "acciones/amigos.php", true);
						  ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
						  ajax.onreadystatechange=function() {
							if (ajax.readyState==4) {
							  
							  //contenedor.innerHTML = ajax.responseText;
							  
							  //target="popup";
							  var salida = ajax.responseText;
							  
							  if(salida == "ok"){
								ocultar_amigos();
								mostrar_amigos_exito();
							  }
							}
						  }
						  ajax.send(valores);
					  }else{
						  alert('El email de tu amigo es incorrecto');
					  }
				  }else{
					  alert('Tu email es incorrecto');
				  }
			  }else{
				  alert('Falta el email de tu amigo');
			  }
		  }else{
			  alert('Falta el nombre de tu amigo');
		  }
	  }else{
		  alert('Falta tu email');
	  }
  }else{
	alert('Falta tu nombre');	
  }
}
/*-----------------------------------------------------------------------------------------------------*/
function f_codigo(){
  var el_codigo = document.getElementById('el_codigo').value;
  var contenedor = document.getElementById('cont_mostrar_codigo');
  if (el_codigo != ""){
	  valores ="el_codigo="+el_codigo;
	  ajax=Ajax();
	  ajax.open("POST", "acciones/codigo.php", true);
	  ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	  ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
		  
		  //contenedor.innerHTML = ajax.responseText;
		  
		  //target="popup";
		  var salida = ajax.responseText;
		  
		  if(salida == "ok"){
			mostrar_registro();
		  }
		  if(salida == "no"){
			  alert('El código no es válido');
		  }
		}
	  }
	  ajax.send(valores);
  }else{
	  alert('Por favor, introduzca su código');
  }
}
/*-----------------------------------------------------------------------------------------------------*/
function mostrar_err_fnac(){
	alert('La fecha de nacimiento es incorrecta');
}
function mostrar_ya_3_hoy(){
	alert('Ya has hecho 3 registros hoy, debes esperar a mañana');
}
function mostrar_menor(){
	alert('Para poder participar debes ser mayor de 18 años');
}

function f_registrar(){
  var el_codigo = document.getElementById('el_codigo').value;
  var nombre = document.getElementById('reg_nombre').value;
  var apellidos = document.getElementById('reg_apellidos').value;
  var fnac_d = document.getElementById('reg_fnac_d').value;
  var fnac_m = document.getElementById('reg_fnac_m').value;
  var fnac_y = document.getElementById('reg_fnac_y').value;
  var direccion = document.getElementById('reg_direccion').value;
  var telefono = document.getElementById('reg_telefono').value;
  var cp = document.getElementById('reg_cp').value;
  var dni = document.getElementById('reg_dni').value;
  var provincia = document.getElementById('reg_provincia').value;
  var email = document.getElementById('reg_email').value;
  
  
  var fnac = fnac_d+"-"+fnac_m+"-"+fnac_y;
  var err_nac = 0;
  var contenedor = document.getElementById('cont_registro');
  
  if(document.getElementById('reg_bases_legales').checked){
	  if((nombre == "") || (apellidos == "" ) || (fnac == "")  || (direccion == "") || (telefono == "") || (cp == "") || (provincia == "") || (email == "")){
		  alert('Debe introducir todos los datos');
	  }else{
		  if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(email)){
				valores ="el_codigo="+el_codigo+"&nombre="+nombre+"&apellidos="+apellidos+"&fnac="+fnac+"&direccion="+direccion+"&telefono="+telefono+"&cp="+cp+"&dni="+dni+"&provincia="+provincia+"&email="+email;
				ajax=Ajax();
				ajax.open("POST", "acciones/registro.php", true);
				ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
				ajax.onreadystatechange=function() {
				  if (ajax.readyState==4) {
					//contenedor.innerHTML = ajax.responseText;
					
					//target="popup";
					var salida = ajax.responseText;
					
					if(salida == 'err_fnac'){
						mostrar_err_fnac();
					}
					if(salida == 'menor'){
						mostrar_menor();
					}
					if(salida == 'demasiados'){
						mostrar_ya_3_hoy();
					}
					if(salida == 'ok'){
						ocultar_registro();
						mostrar_gracias_registro();
					}
				  }
				}
				
				ajax.send(valores);
		  }else{
			  alert('Por favor, introduzca un email válido');
		  }
	  }
  }else{
	  alert('Debe aceptar las bases legales');
  }
  
}
/*-----------------------------------------------------------------------------------------------------*/
function f_foto(){
  var nombre = document.getElementById('a_nombre').value;
  var telefono = document.getElementById('a_telefono').value;
  var email = document.getElementById('a_email').value;
  var a_ruta = document.getElementById('a_ruta').value;
  
  
  var contenedor = document.getElementById('cont_form_cargar_foto');
  
  
  if((nombre != "") || (telefono != "" ) || (email != "")){
	  if(a_ruta != ""){
		  if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(email)){
			  valores ="nombre="+nombre+"&telefono="+telefono+"&email="+email+"&a_ruta="+a_ruta;
			  ajax=Ajax();
			  ajax.open("POST", "acciones/foto.php", true);
			  ajax.setRequestHeader('Content-Type', 'multipart/form-data');
			  ajax.onreadystatechange=function() {
				if (ajax.readyState==4) {
				  
				  //contenedor.innerHTML = ajax.responseText;
				  
				  //target="popup";
				  var salida = ajax.responseText;
				  
				  if(salida == 'ok'){
					  ocultar_codigo();
					  ocultar_foto_form();
					  mostrar_foto_exito();
				  }
				}
			  }
			  ajax.send(valores);
		  }else{
			  alert('Por favor, introduzca un email válido');
		  }
	  }else{
		  alert('Falta la fotografía');
	  }
  }else{
	  alert('Debe introducir todos los datos');
  }
}
// multipart/form-data
/**********************************************************/
function ok_foto(){
	ocultar_foto_form();
	mostrar_foto_exito();
}
/**********************************************************/
function f2_foto(){
  var nombre = document.getElementById('a_nombre').value;
  var telefono = document.getElementById('a_telefono').value;
  var email = document.getElementById('a_email').value;
  var a_ruta = document.getElementById('a_ruta').value;
  
  if(nombre != ""){
	  if(telefono != ""){
		  if(email != ""){
			  if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(email)){
				  if(a_ruta != ""){
					  // ACCIONES TODO OK
					  
					  document.getElementById('form_foto_por_iframe').submit();
					  
					  ok_foto();
				  }else{
					  alert('Falta la fotografía');
				  }
			  }else{
				  alert('El email es incorrecto');
			  }
		  }else{
			  alert('Debe introducir su email');
		  }
	  }else{
		  alert('Debe introducir su teléfono');
	  }
  }else{
	  alert('Debe introducir su nombre');
  }
}
/*********************************************************/
function cerrar_todo(){
	ocultar_email();
	ocultar_foto();
	ocultar_codigo();
	ocultar_amigos();
	ocultar_foto_form();
}

