var decimalPointDelimiter = "."
var cadpassword="1234567890abcdefghijklmnñopqrstuvwxyzABCDEFGHIJKLMNÑOPQRSTUVWXYZ"
var DiasEnMes = new Array(12);
DiasEnMes[1] = 31;
DiasEnMes[2] = 29;   // Esto debe comprobarse por programa
DiasEnMes[3] = 31;
DiasEnMes[4] = 30;
DiasEnMes[5] = 31;
DiasEnMes[6] = 30;
DiasEnMes[7] = 31;
DiasEnMes[8] = 31;
DiasEnMes[9] = 30;
DiasEnMes[10] = 31;
DiasEnMes[11] = 30;
DiasEnMes[12] = 31;

function Validar(f)
{
if (!Comprueba(f.Nombre,"Nombre","req","")) return false
if (!Comprueba(f.Apellidos,"Apellidos","req")) return false
if (!Comprueba(f.DNI,"D.N.I.","req")) return false
if (!Comprueba(f.DNI,"D.N.I.","entero")) return false
if (!Comprueba(f.DNI,"D.N.I.","longmax",9)) return false
if (!Comprueba(f.Direccion,"Direccion","req")) return false
if (!Comprueba(f.Poblacion,"Poblacion","req")) return false
if (!Comprueba(f.Provincia,"Provincia","req")) return false
if (!Comprueba(f.CP,"Código Postal","req")) return false
if (!Comprueba(f.CP,"Código Postal","entero")) return false
if (!Comprueba(f.CP,"Código Postal","longmax",5)) return false
if (f.Dedicacion.value=="0") return MuestraError(f.Dedicacion,"Dedicación","txt","tiene que tener un valor seleccionado")
if (!Comprueba(f.Telefono,"Teléfono","entero")) return false
if (!Comprueba(f.Telefono,"Teléfono","longmax",10)) return false
if (!Comprueba(f.Email,"E-mail","email")) return false
return true
}

function MuestraError(obj,nombre,i,valor)
{
	txt="El campo '" + nombre+"' "
	if (i=="txt") txt+=valor
	if (i=="req") txt+="no puede estar vacío."
	if (i=="entero") txt+="debe ser un número entero."
	if (i=="numero") txt+="debe ser un número. El separador decimal tiene que ser el punto (.)."
	if (i=="password") txt+="sólo puede contener letras (mayúsculas o minúsculas) y números, sin acentos."
	if (i=="fecha") txt+="debe ser una fecha válida. El formato es dd/mm/aa o dd/mm/aaaa"
	if (i=="email") txt+="no es una dirección de correo válida."
	if (i=="noval") txt+="contiene caracteres que no son válidos."
	if (i=="min") txt+="no puede ser menor de "+valor+"."
	if (i=="max") txt+="no puede ser mayor de "+valor+"."
	if (i=="longmax") txt+="no puede tener más de "+valor+" caracteres de longitud."
	if (i=="longmin") txt+="no puede tener menos de "+valor+" caracteres de longitud."
	if (i=="igual") txt+="no coincide con el campo '" + valor + "'. Ambos deben ser iguales"
	if (i=="excepto") txt+="contiene caracteres no permitidos."
	alert(txt)
	obj.focus()
	return false
}

function Comprueba(obj,nombre,i,valor)
{
	if (i=="req") if (isEmpty(obj.value)) return MuestraError(obj,nombre,i,valor)
	if (i=="entero") if (!EsEntero(obj.value)) return MuestraError(obj,nombre,i,valor)
	if (i=="numero") if (!EsNumero(obj.value)) return MuestraError(obj,nombre,i,valor)
	if (i=="password") if (!EstaEnCadena(cadpassword,obj.value)) return MuestraError(obj,nombre,i,valor)
	if (i=="fecha") if (!EsFecha(obj)) return MuestraError(obj,nombre,i,valor)
	if (i=="email") if (!EsEmail(obj.value)) return MuestraError(obj,nombre,i,valor)
	if(i=="min") 
		{
		if (EsNumero(obj.value)) { if (eval(obj.value) < eval(valor)) return MuestraError(obj,nombre,i,valor);}
		}
	if(i=="max") 
		{
		if (EsNumero(obj.value)) { if (eval(obj.value) > eval(valor)) return MuestraError(obj,nombre,i,valor);}
		}
	if(i=="longmax") 
		{
		if (obj.value.length>0 & obj.value.length>eval(valor)) return MuestraError(obj,nombre,i,valor);
		}
	if(i=="longmin") 
		{
		if (obj.value.length>0 & obj.value.length<eval(valor)) return MuestraError(obj,nombre,i,valor);
		}
	if(i=="igual")
		{
		if (obj.value!=valor.value) return MuestraError(obj,nombre,i,valor.name);
		}
	if(i=="excepto")
		{
		if (EstaEnCadena(obj.value,valor)) return MuestraError(obj,nombre,i,valor);
		}
	return true;
}

function daysInFebruary (year){ return (  ((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0) ) ) ? 29 : 28 );}

function EsAnno (s){ if (isEmpty(s)) return false;  if (!EsNumero(s)) return false;  return ((s.length == 2) || (s.length == 4));}

function EsMes (s){ if (isEmpty(s)) return false; return isIntegerInRange (s, 1, 12);}

function EsDia (s){ if (isEmpty(s)) return false; return isIntegerInRange (s, 1, 31);}

function isEmpty(s){ return ((s == null) || (s.length == 0)) }

function isDigit (c){ return ((c >= "0") && (c <= "9")) }

function isIntegerInRange (s, a, b)
{   if (isEmpty(s)) return false
    if (!EsEntero(s))  return false;
    var num = eval(s);
    return ((num >= a) && (num <= b));
}

function EsEntero(num)
{
if (isEmpty(num)) return true;
var i;
for (i = 0; i < num.length; i++)
    {   
        var c = num.charAt(i);
        if (!isDigit(c)) return false;
    }
    return true;
}
function EsNumero (s)

{   var i;
    var seenDecimalPoint = false;
    if (isEmpty(s)) return true
    if (s == decimalPointDelimiter) return false;
    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if ((c == decimalPointDelimiter) && !seenDecimalPoint) seenDecimalPoint = true;
        else if (!isDigit(c)) return false;
    }
    return true;
}
function EstaEnCadena(patron,cad)
{
	var n;
	if (isEmpty(cad)) return true;
	for (n=0; n<cad.length;n++)
		{
        if (patron.indexOf(cad.charAt(n)) == -1) return false;
		}
	return true
}
function EsFecha (obj)
{
	var cad,n,fd,fm,year,mont,day;
	cad=obj.value
	fd=cad.indexOf("/");
	if(fd==-1) return false;
	fm=cad.indexOf("/",fd+1);
	if(fm==-1) return false;
	if(cad.indexOf("/",fm+1)!= -1) return false
	day=cad.substring(0,fd);
	month=cad.substring(fd+1,fm);
	year=cad.substring(fm+1,cad.length)
	if(day.length<2) day="0"+day;
	if(month.length<2) month="0"+month;
    if (! (EsAnno(year) && EsMes(month) && EsDia(day))) return false;
    var intYear = eval(year);
    var intMonth = eval(month);
    var intDay = eval(day);
    if (intDay > DiasEnMes[intMonth]) return false; 
    if ((intMonth == 2) && (intDay > daysInFebruary(intYear))) return false;
    if (year.length<4) if(intYear>50 & intYear!=0) year="19"+year; else year="20"+year;
    obj.value=day + "/" + month + "/" + year
    return true;
}

function EsEmail (s)
{
	if (isEmpty(s)) return true;
   if (EstaEnCadena(s," ")) return false 
    var i = 1;
    var sLength = s.length;
    while ((i < sLength) && (s.charAt(i) != "@")){ i++ }
    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;
    while ((i < sLength) && (s.charAt(i) != ".")) { i++ }
    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
    else return true;
}

function CampoIgual(cad,campo,form)
{
	var n;
	for (n=0; n<form.length;n++)
		{
		if(form.elements[n].name == campo) if (cad==form.elements[n].value) return true
		}
	return false
}


