
function esMayordEdad(dia,mes,anyo,edad){
	hoy=new Date();
	difA=hoy.getFullYear() - anyo;
	difM=mes - (1+hoy.getMonth());
	difD=dia - hoy.getDate();
	if (difA > 18){
		return true;
	}
	if (difA == 18){
		if (difM >=0){
			if (difD >=0){
				return true;
			}
		}
	}
	return false;
}

function LTrim(String)
{
	var i = 0;
	var j = String.length - 1;

	if (String == null)
		return (false);

	for (i = 0; i < String.length; i++)
	{
		if (String.substr(i, 1) != ' ' &&
		    String.substr(i, 1) != '\t')
			break;
	}

	if (i <= j)
		return (String.substr(i, (j+1)-i));
	else
		return ('');
}


function RTrim(String)
{
	var i = 0;
	var j = String.length - 1;

	if (String == null)
		return (false);

	for(j = String.length - 1; j >= 0; j--)
	{
		if (String.substr(j, 1) != ' ' &&
			String.substr(j, 1) != '\t')
		break;
	}

	if (i <= j)
		return (String.substr(i, (j+1)-i));
	else
		return ('');
}


function Trim(String)
{
	if (String == null)
		return (false);

	return RTrim(LTrim(String));
}

function IsNumber(Expression)
{
	Expression = Expression.toLowerCase();
	RefString = "0123456789.-";

	if (Expression.length < 1) 
		return (false);

	for (var i = 0; i < Expression.length; i++) 
	{
		var ch = Expression.substr(i, 1)
		var a = RefString.indexOf(ch, 0)
		if (a == -1)
			return (false);
	}
	return(true);
}

function IsEmail(Expression)
{
	if (Expression == null)
		return (false);

	var supported = 0;
	if (window.RegExp)
	{
		var tempStr = "a";
		var tempReg = new RegExp(tempStr);
		if (tempReg.test(tempStr)) supported = 1;
	}
	if (!supported) 
		return (Expression.indexOf(".") > 2) && (Expression.indexOf("@") > 0);
	var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
	var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
	return (!r1.test(Expression) && r2.test(Expression));
}


function IsDate(dateStr)
{
	// Checks for the following valid date formats:
	// DD/MM/YYYY   DD-MM-YYYY

	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;

	var matchArray = dateStr.match(datePat)
	if (matchArray == null)
		return false

	month = matchArray[3]
	day = matchArray[1]
	year = matchArray[4]
	if (month < 1 || month > 12)
		return false

	if (day < 1 || day > 31)
		return false

	if ((month==4 || month==6 || month==9 || month==11) && day==31)
		return false

	if (month == 2)
	{
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0))
		if (day>29 || (day==29 && !isleap))
			return false;
	}
	return true;
}

function IsChar(Expression)
{
	Expression = Expression.toLowerCase();
	var RefString = "0123456789";

	if (Expression.length < 1) 
		return (false);

	for (var i = 0; i < Expression.length; i++) 
	{
		var ch = Expression.substr(i, 1)
		var a = RefString.indexOf(ch, 0)
		if (a != -1)
			return (false);
	}
	return(true);
}


/**************************************************************
	isNif
 **************************************************************/
// funcion que comprueba la letra que corresponde a un NumDNI de un individuo
function letraNIF( nif1, error)
{
   var cadena  = "";
   var letras='TRWAGMYFPDXBNJZSQVHLCKET'
   var NumDNI=0;
   var Indice=0;

   nif1 = nif1.toUpperCase();
   if( nif1.charAt(0) != 'X') { // NIF PARA ESPAÑOLES
      cadena += nif1;
   } else { // NIF PARA EXTRANJEROS RESIDENTES
      cadena += nif1.substring(1,11);
   }
   correcto = cadena.charAt(cadena.length-1);
   correcto = correcto.toUpperCase();
   NumDNI = parseInt(cadena,10);
   Indice = NumDNI - (23 * parseInt(NumDNI/23,10));
   if( (Indice >= 0) && (Indice < 24) ) {
      if( letras.charAt(Indice) == correcto) {
         // valor correcto devuelve la letra directamente
         return true;
      } else {
         //valor incorrecto
         if( nif1.charAt(0) != 'X') {
            alert("el " + error + " introducido es incorrecto"); 
         } else {
            alert("el " + error + " introducido es incorrecto"); 
         }
         return false;
      }
   } else {
      return false;
   }
}

// funcion que devuelve la letra que corresponde a un NumDNI de una persona juridica o un organismo 
function ValidarCIF( cif1, error)
{
   var Retorno = true;
   var Entidad;
   var StEntidad;
   var Provincia;
   var NumeroSec;
   var CaracterControl, CaracterControlEntrada;
   var TablaControl = 'JABCDEFGHI';
   var otras = 'ABCDEFGHSPQX';
   var numero = '0123456789';
   var Suma = 0;
   var Suma1 = 0;
   var Suma2 = 0;
   var nAux = 0;
   var tmp = '';

   tmp = "" + cif1;
   Entidad = tmp.charAt(0);
   Entidad = Entidad.toUpperCase();
   if( otras.indexOf( Entidad) == -1) {
      alert (' la letra del ' + error + ' no corresponde a ninguna válida');
      return false;
   }
   Provincia = tmp.substring(1,3);
   NumeroSec = tmp.substring(3,8);
   NumeroSec2 = tmp.substring(1,8);
   CaracterControlEntrada = tmp.substring( 8);
   CaracterControlEntrada = CaracterControlEntrada.toUpperCase();
   if( (Entidad >= "A") && (Entidad <= "H") ) {
      if( numero.indexOf( CaracterControlEntrada) != -1) {
         StEntidad = "JURIDICA";
      } else {
         alert('el Digito de Control no es correcto en el ' + error);
         return false;
      }
   }
   else if( (Entidad == "S") || (Entidad == "Q") ) {
      StEntidad="ORGANISMO";
   }
   else if( Entidad == "X") //  Controlamos si es Extranjero residente
   {
      return letraNIF( tmp, error);
   }
   if( Retorno != false) {           // Entidad correcta
      for (var i = 0; i < NumeroSec2.length; i++) {
         if( numero.indexOf( NumeroSec2.charAt( i)) == -1) {
            alert('ha introducido una letra en lugar de un Numero en el ' + error);
            return false;
         }
      }
      if( parseInt( Provincia, 10) == 0)
      { 
         Retorno = false;
      } //Provincia correcta
      else if( parseInt( NumeroSec, 10) == 0)
      {
         Retorno = false;
      }
//Numero secuencial correcto
      else if( (StEntidad == "JURIDICA") || (StEntidad == "ORGANISMO"))
      {
         Suma2 = 0;
         Suma1 = parseInt( NumeroSec2.charAt( 1), 10) + parseInt( NumeroSec2.charAt( 3), 10) + parseInt( NumeroSec2.charAt( 5), 10 );
         for( var i = 0; i < 7; i += 2) {
            nAux = parseInt( NumeroSec2.charAt( i), 10) * 2;
            if (nAux > 9) {
               Suma2 = Suma2 + parseInt( nAux / 10, 10) + nAux % 10;
            } else {
               Suma2 = Suma2 + nAux;
            }
         }
         Suma = Suma1 + Suma2;
         i = 10 - Suma % 10;
         if( i > 9) { 
            i = 0;
         };
         if( StEntidad == "JURIDICA") {
            CaracterControl = i;
         } else {
            CaracterControl = TablaControl.charAt( i);
         }
         if (CaracterControl != CaracterControlEntrada) {
            Retorno = false;                         
         };
      }
   }
   if( !Retorno) {
      alert('El ' + error + ' es incorrecto');
   };

   return Retorno;
}

// funcion que captura el NIF-CIF y distingue si NIF o CIF
function ChequeaCif( campo, error) {

   var numero;
   var vuelve;
   var sinceros;
   var i;

   numero = '0123456789';
   vuelve = false;

   for( i = 0; i < campo.length; i++)
      if( campo.charAt(i) != "0")
         break;
   sinceros = campo.substring( i);
   if( sinceros != "") {
      if( numero.indexOf( sinceros.charAt(0)) != -1) {
         vuelve = letraNIF( sinceros, error);
      } else {
         vuelve = ValidarCIF( sinceros, error);
      };
   } else {
       alert("el " + error + " no es correcto.");
      vuelve = false;
   };

   return( vuelve);
}

function IsNIF(nif,campo) {
	return ChequeaCif(nif,campo);
}

function IsPositiveNumber(Expression)
{
	Expression = Expression.toLowerCase();
	RefString = "0123456789.";

	if (Expression.length < 1) 
		return (false);

	for (var i = 0; i < Expression.length; i++) 
	{
		var ch = Expression.substr(i, 1)
		var a = RefString.indexOf(ch, 0)
		if (a == -1)
			return (false);
	}
	return(true);
}

function abrir(id){
	width=315;
	height=450;
	abierta = window.open ('login_concurso.phtm?id='+id, 'concurso_nueva','toolbar=no,location=no,directories=no,menubar=no,scrollbars=no,status=no,resizable=no,width='+width+',height='+height);
	abierta.focus();
	return;
}

