var prohibitedChars="'\""; // characters prohibited to ensure security
var prohibitedCharstoInform="',\""; // characters not allowed to print out them to inform the user
var longMaxLogin=40
var longMinLogin=4
var longMaxPassword=20
var longMinPassword=5

var a, mes, dia, anyo, febrero;
    
function anyoBisiesto(anyo)
{
        if (anyo < 100)
            var fin = anyo + 1900;
        else
            var fin = anyo ;

        /*
        * primera condicion: si el resto de dividir el año entre 4 no es cero > el año no es bisiesto
        * es decir, obtenemos año modulo 4, teniendo que cumplirse anyo mod(4)=0 para bisiesto
        */
        if (fin % 4 != 0)
            return false;
        else
        {
            if (fin % 100 == 0)
            {
                /**
                * si el año es divisible por 4 y por 100 y divisible por 400 > es bisiesto
                */
                if (fin % 400 == 0)
                {
                    return true;
                }
                /**
                * si es divisible por 4 y por 100 pero no lo es por 400 > no es bisiesto
                */
                else
                {
                    return false;
                }
            }
            /**
            * si es divisible por 4 y no es divisible por 100 > el año es bisiesto
            */
            else
            {
                return true;
            }
        }
    }
    
    /**
    * funcion principal de validacion de la fecha
    * argumento fecha > cadena de texto de la fecha introducida por el usuario
    */
    function fechaCorrecta( )
    {
       /**
       * obtenemos la fecha introducida y la separamos en dia, mes y año
       */
       a=document.forms[0].fecha.value;
       dia=a.split("/")[0];
       mes=a.split("/")[1];
       anyo=a.split("/")[2];

    
       if ((dia==null)||(mes==null)||(anyo==null)){
       	alert ("La fecha introducida es incorrecta. Por favor introduzca una en formato dd/mm/aaaa");
        document.forms[0].fecha.focus();
        document.forms[0].fecha.select();       	
        return false;
	}
       
       if(anyoBisiesto(anyo))
           febrero=29;
       else
           febrero=28;
       /**
       * si el mes introducido es negativo, 0 o mayor que 12 > alertamos y detenemos ejecucion
       */
       if ((mes<1) || (mes>12))
       {
           alert("El mes introducido no es válido. Por favor, introduzca un mes correcto formato dd/mm/aaaa");
           document.forms[0].fecha.focus();
           document.forms[0].fecha.select();
           return false;
       }
       /**
       * si el mes introducido es febrero y el dia es mayor que el correspondiente 
       * al año introducido > alertamos y detenemos ejecucion
       */
       if ((mes==2) && ((dia<1) || (dia>febrero)))
       {
           alert("El día introducido no es válido. Por favor, introduzca un día correcto formato dd/mm/aaaa");
           document.forms[0].fecha.focus();
           document.forms[0].fecha.select();
           return false;
       }
       /**
       * si el mes introducido es de 31 dias y el dia introducido es mayor de 31 > alertamos y detenemos ejecucion
       */
       if (((mes==1) || (mes==3) || (mes==5) || (mes==7) || (mes==8) || (mes==10) || (mes==12)) && ((dia<1) || (dia>31)))
       {
           alert("El día introducido no es válido. Por favor, introduzca un día correcto formato dd/mm/aaaa");
           document.forms[0].fecha.focus();
           document.forms[0].fecha.select();
           return false;
       }
       /**
       * si el mes introducido es de 30 dias y el dia introducido es mayor de 301 > alertamos y detenemos ejecucion
       */
       if (((mes==4) || (mes==6) || (mes==9) || (mes==11)) && ((dia<1) || (dia>30)))
       {
           alert("El día introducido no es válido. Por favor, introduzca un dia correcto formato dd/mm/aaaa");
           document.forms[0].fecha.focus();
           document.forms[0].fecha.select();
           return false;
       }
       /**
       * si el mes año introducido es menor que 1990 o mayor que 2010 > alertamos y detenemos ejecucion
       * NOTA: estos valores son a eleccion vuestra, y no constituyen por si solos fecha erronea
       */
       if ((anyo<1990) || (anyo>2100))
       {
           alert("El año introducido no es válido. Por favor, introduzca un año entre 1990 y 2100");
           document.forms[0].fecha.focus();
           document.forms[0].fecha.select();
	   return false;
       } 
      else
	  return true;
    }    


/*****
 *
 * Checks if the email is correct
 * 
 * @param: src: the string to be checked
 * @return: true or false
 *
 *****/
 
function emailOK(src) {

   var regex = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
   return regex.test(src);
}	

function  formatApostrofe(t) {		
	
	var strTemp;
	strTemp="";
	for (var i=0; i<t.length; i++){
		if (t.substring(i, i+1)=="'") strTemp=strTemp+'’';
			else strTemp=strTemp+t.substring(i, i+1);
		}
//		alert (strTemp);
		return strTemp;	
}


/*****
 *
 * to check if the telephone number contains only numbers or +
 * 
 * @param: the string of the telephone number
 * @return: true or false depending on the result
 *
 *****/

function  telNumOK(t) {		

	var valsOK="+0123456789";		
	var thisChar;
	var counter=0;
	for (var i=0; i<t.length; i++){
		thisChar=t.substring(i, i+1);
		if (valsOK.indexOf(thisChar)!=-1) counter++;
		}
		if (counter==t.length) return true;	else	return false;	
}

/*****
 *
 * returns if the passed string is a valid number
 *
 * @param t: string to be checked
 * @return: true or false depending on the result
 *
 *****/

function  numOK2(t) {		

var valsOK="0123456789";		
var thisChar;
var counter=0;
for (var i=0; i<t.length; i++) 
	{
	thisChar=t.substring(i, i+1);
	if (valsOK.indexOf(thisChar)!=-1) counter++;
	}
	if (counter==t.length) return true;	else	return false;	
}

function  numOK(t) {		

   var regex = /^-?\d+?$/
   return regex.test(t);
   
}

function  isRealOk(t) {		

   var regex = /^-?\d+(\,\d+)?$/
   return regex.test(t);
   
}

/*****
 *
 * to check if the passed string contains 'illegal' characters defined as global variable
 *
 * @param t: string to be checked
 * @return: true or false depending on the result
 *
 *****/

function  charsOK(t) {		

var valWrong=prohibitedChars;		
var thisChar;
var counter=0;
for (var i=0; i<t.length; i++){
	thisChar=t.substring(i, i+1);
	if (valWrong.indexOf(thisChar)!=-1) counter++;
	}
	if (counter>0) return false;	else	return true;	
}

/*****
 *
 * Checks if the passed value is empty or not
 *
 * @param dt: string to be checked
 * @return: true or false depending on the result
 *
 *****/

function isEmpty (dt) {
for (var i=0; i<dt.length; i++){
	if (dt.substring(i, i+1)!=" ")	return false;
	}
return true;
}

function comprobarConsulta() {

// Para el formulario de solicitud de información

if (isEmpty(document.formConsulta.nombre.value)) {
           alert("Por favor introduzca su nombre en la consulta.");
           document.formConsulta.nombre.focus();
           document.formConsulta.nombre.select();
	   return false;
	}

if (isEmpty(document.formConsulta.asunto.value)) {
           alert("Por favor especifique el asunto de la consulta (Ayuda, sugerencia, queja u otros).");
           document.formConsulta.asunto.focus();
           document.formConsulta.asunto.select();
	   return false;
	}  
  
if (isEmpty(document.formConsulta.email.value)==false) {
	if (emailOK(document.formConsulta.email.value)==false){
           alert("La dirección de E-Mail introducida no es correcta.");
           document.formConsulta.email.focus();
           document.formConsulta.email.select();
	   return false;
	 }
}

if (isEmpty(document.formConsulta.telefono.value)==false) {
	if (telNumOK(document.formConsulta.telefono.value)==false){
           alert("El número de teléfono introducido no es correcto. Sólo puede contener números y '+'. No son válidos espacios en blanco ni letras.");
           document.formConsulta.telefono.focus();
           document.formConsulta.telefono.select();
	   return false;
	 }
}

if (isEmpty(document.formConsulta.consulta.value)) {
           alert("No ha introducido ninguna consulta o comentario.");
           document.formConsulta.consulta.focus();
           document.formConsulta.consulta.select();
	   return false;
	}

return true;
} 
