﻿    function isEmail(theElement) {
        var s = theElement.value;
        var filter=/^[A-Za-z][-A-Za-z0-9_.]*@[A-Za-z0-9_]+.[-A-Za-z0-9_.]+[A-za-z]$/;
        
        if (s.length == 0 ) return true;
        
        if (filter.test(s))
            return true;
        else
            return false;
    }


  	function SetFocusAndAlert(tInput,message){
    	tInput.focus();
    	parent.jAlert(message);
  	}
  	
    checkFormUsuario = function(f) {
        
        if(f.txt_nombre.value=="") {
            SetFocusAndAlert(f.txt_nombre, 'El campo "Nombre" es obligatorio.');
            return false;
        }
        
        if(f.txt_apellidos.value=="") {
            SetFocusAndAlert(f.txt_apellidos, 'El campo "Apellidos" es obligatorio.');
            return false;
        }
        
        if(f.txt_email.value=="") {
            SetFocusAndAlert(f.txt_email, 'El campo "E-mail" es obligatorio.');
            return false;
        }
        
        if(!isEmail(f.txt_email)) {
            SetFocusAndAlert(f.txt_email, 'Teclee una dirección de correo válida.');
            return false;
        }
        
        if(f.txt_email_control.value!=f.txt_email.value){
            SetFocusAndAlert(f.txt_email_control, 'El campo "Repite E-mail" debe de coincidir con el campo "E-mail".');
            return false;
        }
        
        if(!validarFecha(f.sel_dia.value, f.sel_mes.value, f.sel_anio.value)) {
            SetFocusAndAlert(f.sel_dia, 'La "Fecha de nacimiento" introducida no es válida.');
            return false;
        }
        
        
        
        if(f.txt_municipio.value=="") {
            SetFocusAndAlert(f.txt_municipio, 'El campo "Localidad" es obligatorio.');
            return false;
        }
        
        if(f.txt_cp.value=="") {
            SetFocusAndAlert(f.txt_cp, 'El campo "Código Postal" es obligatorio.');
            return false;
        }
        
        var ControlCP = new Number(f.txt_cp.value);
        
        if(isNaN(ControlCP)) {
            SetFocusAndAlert(f.txt_cp, 'El "Código Postal" introducido no es válido.');
            return false;            
        }

        if(f.sel_provincia.value=="0") {
            SetFocusAndAlert(f.sel_provincia, 'El campo "Provincia" es obligatorio.');
            return false;
        }

        if(f.txt_direccion.value=="") {
            SetFocusAndAlert(f.txt_direccion, 'El campo "Dirección" es obligatorio.');
            return false;
        }
        
        
         strDia=f.sel_dia.value
         strMes=f.sel_mes.value
         strAno=f.sel_anio.value
           if(strDia.length <2)
                strDia="0"+strDia
        
            if(strMes.length <2)
                strMes="0"+strMes
     
        
        
            strFecha=new String(strMes+ "/" + strDia + "/" + strAno);
            var strEdad=getAge(strFecha)
            
            if(strEdad<15)
            {
                if (f.txt_dnitutor.value=="" )
                {
                    SetFocusAndAlert(f.txt_dnitutor, 'El campo "DNI Tutor" es obligatorio.');
                    return false;
                }
                
                 if (f.txt_ntutor.value=="" )
                {
                    SetFocusAndAlert(f.txt_ntutor, 'El campo "Nombre Tutor" es obligatorio.');
                    return false
                }
            }
    
    
    
            
        if(f.txt_telefono.value=="") {
            SetFocusAndAlert(f.txt_telefono, 'El campo "Teléfono" es obligatorio.');
            return false;
        }
        
        var ControlTelefono = new Number(f.txt_telefono.value);
        
        if(isNaN(ControlTelefono)) {
            SetFocusAndAlert(f.txt_telefono, 'El "Teléfono" introducido no es válido.');
            return false;
        }

        if(new String(f.txt_telefono.value).charAt(0)!=9 && new String(f.txt_telefono.value).charAt(0)!=6) {
            SetFocusAndAlert(f.txt_telefono, 'El "Teléfono" introducido no es válido.');
            return false;            
        }
        
        if(!f.chk_acepto.checked) {
            SetFocusAndAlert(f.chk_acepto, 'Debe de aceptar los términos y condiciones.');
            return false;
        }        
        
        return true;
    }

  checkFormUsuariolandingPage = function(f) {
        if(f.txt_nombre.value=="") {
            SetFocusAndAlert(f.txt_nombre, 'El campo "Nombre" es obligatorio.');
            return false;
        }
        
        if(f.txt_apellidos.value=="") {
            SetFocusAndAlert(f.txt_apellidos, 'El campo "Apellidos" es obligatorio.');
            return false;
        }
             
        if(f.txt_telefono.value=="") {
            SetFocusAndAlert(f.txt_telefono, 'El campo "Teléfono" es obligatorio.');
            return false;
        }
        
        var ControlTelefono = new Number(f.txt_telefono.value);
        
        if(isNaN(ControlTelefono)) {
            SetFocusAndAlert(f.txt_telefono, 'El "Teléfono" introducido no es válido.');
            return false;
        }

        if(new String(f.txt_telefono.value).charAt(0)!=9 && new String(f.txt_telefono.value).charAt(0)!=6) {
            SetFocusAndAlert(f.txt_telefono, 'El "Teléfono" introducido no es válido.');
            return false;            
        }
        if(f.txt_email.value=="") {
            SetFocusAndAlert(f.txt_email, 'El campo "E-mail" es obligatorio.');
            return false;
        }
        
        if(!isEmail(f.txt_email)) {
            SetFocusAndAlert(f.txt_email, 'Teclee una dirección de correo válida.');
            return false;
        }
        
        if(f.txt_email_control.value!=f.txt_email.value){
            SetFocusAndAlert(f.txt_email_control, 'El campo "Repite E-mail" debe de coincidir con el campo "E-mail".');
            return false;
        }
        
        
        if(f.txt_cp.value=="") {
            SetFocusAndAlert(f.txt_cp, 'El campo "Código Postal" es obligatorio.');
            return false;
        }
        
        var ControlCP = new Number(f.txt_cp.value);
        
        if(f.txt_cp.value.length < 5)
        {
               SetFocusAndAlert(f.txt_cp, 'El "Código Postal" introducido no es válido.');
            return false;            
        }
        if(isNaN(ControlCP)) {
            SetFocusAndAlert(f.txt_cp, 'El "Código Postal" introducido no es válido.');
            return false;            
        }

        
    
       
        
        if(!f.chk_acepto.checked) {
            SetFocusAndAlert(f.chk_acepto, 'Debe de aceptar los términos y condiciones.');
            return false;
        }        
        
        return true;
    }


function dateExists(str){
str=String(str);
var re=new RegExp("^(\\d{2})/(\\d{2})/(\\d{4})$");
if(!re.test(str))return false;
var matches=str.match(re);
var m=Number(matches[1]);
var d=Number(matches[2]);
var y=Number(matches[3]);
var isLeapYear=(y%4==0);
if(m>12)return false;
if(d>31 || d<1)return false;
m--;//01 means jan so it's element 0 in the daysInMonth array
var daysInMonth=[31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];if(isLeapYear)daysInMonth[1]++;
if(!(m in daysInMonth))return false;
if(d > daysInMonth[m])return false;
return true;
}

function getAge(str){
str=str||"";if(!dateExists(str))return -1;
var strSegs=str.split("/"), m=Number(strSegs[0]), d=Number(strSegs[1]), y=Number(strSegs[2]);m--;
var then=new Date(0);
then.setFullYear(y);
then.setMonth(m);
then.setDate(d);
var now=new Date();
var birthdateThisYear=new Date(then.getTime());birthdateThisYear.setFullYear(now.getFullYear());
var hadBirthdateThisYear=(birthdateThisYear<=now);
var yrs=now.getFullYear()-then.getFullYear();
if(!hadBirthdateThisYear)yrs--;
return yrs;
}

//***********************************************************************************************
// validarFecha(dia,mes, año)
//
// Valida que el día y el mes introducidos sean correctos. Además valida que el año introducido
// sea o no bisiesto
//
//***********************************************************************************************
function validarFecha(dia,mes,anio)
{
var elMes = parseInt(mes);

if(elMes>12)
return false;
// MES FEBRERO
if(elMes == 2){
if(esBisiesto(anio)){
if(parseInt(dia) > 29){
return false;
}
else
return true;
}
else{
if(parseInt(dia) > 28){
return false;
}
else
return true;
}
}
//RESTO DE MESES

if(elMes== 4 || elMes==6 || elMes==9 || elMes==11){
if(parseInt(dia) > 30){
return false;
}
}
return true;

}
//*****************************************************************************************
// esBisiesto(anio)
//
// Determina si el año pasado com parámetro es o no bisiesto
//*****************************************************************************************
function esBisiesto(anio)
{
var BISIESTO;
if(parseInt(anio)%4==0){
if(parseInt(anio)%100==0){
if(parseInt(anio)%400==0){
BISIESTO=true;
}
else{
BISIESTO=false;
}
}
else{
BISIESTO=true;
}
}
else
BISIESTO=false;

return BISIESTO;
}

