<!-- Declaración de variables Globales -->

var tipo_validacion = '0';
var nombre = "";
var apellidos = "";
var telefono = "";
var email = "";
var matricula = "";
var fecha = "";
var acepta_politica = false;
var numero = /[0-9]/;
var tipo_mensaje = 1;
var length_telefono = 0;
var mensaje = "";
var destinatario = "";


function comprobar_datos(num){
   var resultado_validacion	= false;
   this.nombre = get_value_input('nombre');
   this.tipo_validacion = num;
  
  switch(num)
  {
	
	case '1':   this.telefono = get_value_input('telefono');
				if(this.telefono!=''){
					this.length_telefono =  this.telefono.length;
				}
				
				this.email = get_value_input('email');
				//this.acepta_politica = get_is_checked('acepta_pol');				
				this.acepta_politica = true;
				if(!validaciones_genericas()){
					break;
				}				
				
			    resultado_validacion = true;
				break;
				
	case '2':   this.apellidos = get_value_input('apellidos');
				this.telefono = get_value_input('telefono');
				if(this.telefono!=''){
					this.length_telefono =  this.telefono.length;
				}
				this.email = get_value_input('email');
				
				
				this.matricula = get_value_input('matricula');
				this.fecha = get_value_input('fecha');
				this.acepta_politica = get_is_checked('acepta_pol');	
				
				if(!validaciones_genericas()){
					break;
				}
				
				if(this.apellidos == '')
				{			
					muestra_mensaje('Debe indicar sus apellidos.');
					break;								
				}
				
				if(this.fecha == '')
				{
					muestra_mensaje('Debe seleccionar una fecha para la cita.');
					break;							
				}
				
				if((!get_is_checked('rev60000')) && (!get_is_checked('rev15000')) && (!get_is_checked('rev90000')) && (!get_is_checked('rev30000')) && (!get_is_checked('rev120000')) && (!get_is_checked('rev45000')) && (!get_is_checked('revaire')) && (!get_is_checked('revaceite')) && (!get_is_checked('otroscheck')))
				{
					muestra_mensaje('Debe seleccionar el motivo de la solicitud.');
					break;			
				}
	
				resultado_validacion = true;
			    break;
				
	case '3':   this.destinatario = get_value_input('email_destino');
				this.telefono = get_value_input('telefono');
				if(this.telefono!=''){
					this.length_telefono =  this.telefono.length;
				}
				
				this.email = get_value_input('email');
				this.acepta_politica = get_is_checked('acepta_pol');				
				
				if(!validaciones_genericas()){
					break;
				}				
				
				if(this.destinatario == '')
				{			
					muestra_mensaje('Debe seleccionar un destinatario.');
					break;								
				}
				
			    resultado_validacion = true;
				break;
				
	case '4' : 	this.email = get_value_input('email');
				this.acepta_politica = get_is_checked('acepta_pol');		
				if(!validaciones_genericas()){
					break;
				}				
				resultado_validacion = true;
				break;
  }
  
  return resultado_validacion;
}
  

function get_value_input(id_input){
	var res = '';
	if(document.getElementById(id_input).value != null){	
		return document.getElementById(id_input).value;
	}else{
		return res;
	}
}

function get_is_checked(id_check)
{
	if(document.getElementById(id_check).checked==true){	
		return true;
	}
	return false;
}

function muestra_mensaje(mensaje)
{
	
	switch(this.tipo_mensaje)
	{
		case 1: alert(mensaje);
		
		case 2: window.scroll(40,20);	
				document.getElementById('titulo_msg').innerHTML = '&iexcl;ATENCI&Oacute;N!';	
				document.getElementById('texto_msg').innerHTML = ''+mensaje+'';
	}
}
	
function validaciones_genericas(){

  if(this.nombre!='' && (this.telefono!='' || this.email!=''))
  {
	if(comprobar_mail(this.email)==false && this.email!='')
	{		
		muestra_mensaje('Revisa la direccion de correo, no es correcta'); 		
		return false;		
	}  	  
	
	else if((this.tipo_validacion != '4') && (isNaN(this.telefono)  && this.telefono!=''))
	{	
		muestra_mensaje('El numero de telefono dado no es valido!');
		return false;
	}
	else
	{
		if(this.length_telefono<9  && this.telefono!='')
		{

			muestra_mensaje('El numero de telefono dado es demasiado corto!');
			return false;
		}
	}
	
	if(this.acepta_politica == false){
			muestra_mensaje('Debe aceptar nuestra politica de privacidad.');
			return false;
	}


  }
   else
   {	
	
	   if(this.telefono=='' && this.email=='' && this.nombre=='')
	   {
		   muestra_mensaje('Faltan datos necesarios por rellenar!');
	   }
	   else
	   {
			if(this.nombre!='')
			{
				muestra_mensaje('Indique un telefono o una direccion de correo!');

			}
			else
			{
				muestra_mensaje('Indique su nombre, por favor.');
			}			
	   }
	   return false;
	}
	
	return true;
}



function comprobar_mail(email){
	 
	var plant = /[^\w^@^\.^-]+/gi
	
	if (plant.test(email))
	{
		
		 return false;
	}
	else
	{
		plant = /^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$/i
    <!--	plant =/(^\w+)(@{1})([\w\.-]+$)/i  -->
     	if (plant.test(email))
		{		
        	return true;
     	}
		else
        {
			if (email!='')
			{				
				return false;
			}
		}
	}
	  
 }
 
 
function comprueba_fecha()
{
  //posicion 0 = dia		posicion 1 = mes		posicion 2 = ano
  var fecha_introducida = document.getElementById('fecha').value
  var fecha = fecha_introducida.split('/');
  dia_actual = document.getElementById('dia_actual').value;
  mes_actual = document.getElementById('mes_actual').value;
  ano_actual = document.getElementById('ano_actual').value;
 
  if (fecha[0]!='0'&& fecha[1]!='0' && fecha[2]!='0')
  {
	  if (!comparar_fechas(dia_actual,mes_actual,ano_actual,fecha[0],fecha[1],fecha[2]))
	  {
  	      window.scroll(40,20);
		  //document.getElementById('nombre').innerHTML='ERROR';
		  document.getElementById('texto_alerta').innerHTML='La fecha no es correcta, por favor rev&iacute;sela';
		  return false; 
	  }else{
			return true;
			}
  }else{
 	      window.scroll(40,20);
		  //document.getElementById('nombre').innerHTML='ERROR';
		  document.getElementById('texto_alerta').innerHTML='Por favor, rellene la fecha!';
  		  return false;
		}
}

 
function comparar_fechas(dia_ac,mes_ac,ano_ac,dia,mes,ano){
	
	if(ano_ac<ano){ 
		return true; 
	} else 
		{ 
			if((ano_ac==ano)&&(mes_ac<mes)){
				return true;
			}else{
				if((ano_ac==ano)&&(mes_ac==mes)&&(dia_ac<=dia)){	
				     return true;		
				}
				else
				{
					return false;
				}	
			}
	   }
}

function validarFecha(fecha_introducida)
	{
		//posicion 0 = dia		posicion 1 = mes		posicion 2 = ano
		var fecha=fecha_introducida.split('/');
		//// alert(ano+' '+mes+' '+dia);
		var bisiesto = false;
		if (fecha[2] % 4 == 0 && fecha[2] % 100 != 0) bisiesto = true;
		if (fecha[2] % 100 == 0 && fecha[2] % 400 == 0) bisiesto = true;
		
		if (fecha[1]==4 || fecha[1]==6 || fecha[1]==9 || fecha[1]==11 || fecha[1]==2) 
		{  
			if(bisiesto == true)
			{
				if ((fecha[1]==2 && fecha[0] > 29) || fecha[0]>30) 
				{  
					return false;  
					//// alert('mes de no 31, con ano bisiesto y mes 2, con dia mayor que 29 o 30');
				} else {
					//// alert('mes de no 31, con ano bisiesto y mes 2, con dia correcto');
					return true;	
				}
			} else {
				if (fecha[1]==2 && fecha[0] > 29 || fecha[0]>30) 
				{  
					//// alert('mes de no 31, con ano no bisiesto y mes 2, con dia mayor que 29 o 30');
					return false;  
				} else {
					//// alert('mes de no 31, con ano no bisiesto y mes 2, con dia correcto');
					return true;	
				}
			}
			
        } else {
			//// alert('mes de 31, lo demas da igual');
			return true;	
		}
	}
