/* Funciones para la validación de formularios */
function texto_vacio(formulario, campo, texto)
    {
    if (eval('document.' + formulario + '.' + campo + '.' + 'value') == "")
        {
        alert("El campo " + texto + " no puede quedar vac\u00edo");
        eval('document.' + formulario + '.' + campo + '.' + 'style.border' + '="2px solid #E40000"');
        eval('document.' + formulario + '.' + campo + '.' + 'focus()');
        return false;
        }

    else
        {
        return true;
        }
    }

function validarEmail(formulario, campo)
     {
     valor = eval('document.' + formulario + '.' + campo + '.' + 'value');

     if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(valor))
         {
         return true;
         }

     else
         {
         alert("La direcci\u00f3n de correo electr\u00f3nico no es correcta");
         eval('document.' + formulario + '.' + campo + '.' + 'style.border' + '="2px solid #E40000"');
         eval('document.' + formulario + '.' + campo + '.' + 'focus()');
         return false;
         }
     }

function validarNIF(formulario, campo)
    {
    abc = eval('document.' + formulario + '.' + campo + '.' + 'value');
    dni = abc.substring(0, abc.length - 1);
    let = abc.charAt(abc.length - 1);

    if (!isNaN(let))
        {
        alert('Falta la letra del NIF');
        eval('document.' + formulario + '.' + campo + '.' + 'style.border' + '="2px solid #E40000"');
        eval('document.' + formulario + '.' + campo + '.' + 'focus()');
        return false;
        }

    else
        {
        cadena="TRWAGMYFPDXBNJZSQVHLCKET";
        posicion = dni % 23;
        letra = cadena.substring(posicion, posicion + 1);

        if (letra != let.toUpperCase())
            {
            alert("NIF no v\u00e1lido");
            eval('document.' + formulario + '.' + campo + '.' + 'style.border' + '="2px solid #E40000"');
            eval('document.' + formulario + '.' + campo + '.' + 'focus()');
            return false;
            }
        }
    return true;
    }

function esDigito(sChr)
    {
    var sCod = sChr.charCodeAt(0);
    return((sCod > 47) && (sCod < 58));
    }

function valSep(oTxt)
    {
    //oTxt = rutaCampoFecha;
    var bOk = false;
    var sep1 = oTxt.value.charAt(2);
    var sep2 = oTxt.value.charAt(5);
    bOk = bOk || ((sep1 == "-") && (sep2 == "-"));
    bOk = bOk || ((sep1 == "/") && (sep2 == "/"));
    return bOk;
    }

function finMes(oTxt)
    {
    var nMes = parseInt(oTxt.value.substr(3, 2), 10);
    var nAno = parseInt(oTxt.value.substr(6), 10);
    var nRes = 0;

    switch (nMes)
        {
        case 1:
            nRes = 31;
            break;

        case 2:
            nRes = 28;
            break;

        case 3:
            nRes = 31;
            break;

        case 4:
            nRes = 30;
            break;

        case 5:
            nRes = 31;
            break;

        case 6:
            nRes = 30;
            break;

        case 7:
            nRes = 31;
            break;

        case 8:
            nRes = 31;
            break;

        case 9:
            nRes = 30;
            break;

        case 10:
            nRes = 31;
            break;

        case 11:
            nRes = 30;
            break;

        case 12:
            nRes = 31;
            break;
        }
    return nRes + (((nMes == 2) && (nAno % 4) == 0) ? 1 : 0);
    }

function valDia(oTxt)
    {
    var bOk = false;
    var nDia = parseInt(oTxt.value.substr(0, 2), 10);
    bOk = bOk || ((nDia >= 1) && (nDia <= finMes(oTxt)));
    return bOk;
    }

function valMes(oTxt)
    {
    var bOk = false;
    var nMes = parseInt(oTxt.value.substr(3, 2), 10);
    bOk = bOk || ((nMes >= 1) && (nMes <= 12));
    return bOk;
    }

function valAno(oTxt)
    {
    var bOk = true;
    var nAno = oTxt.value.substr(6);
    bOk = bOk && ((nAno.length == 2) || (nAno.length == 4));

    if (bOk)
        {
        for (var i = 0; i < nAno.length; i++)
            {
            bOk = bOk && esDigito(nAno.charAt(i));
            }
        }
    return bOk;
    }

function valFecha(oTxt)
    {
    var sw = 0;
    var bOk = true;

    if (oTxt.value != "")
        {
        bOk = bOk && (valAno(oTxt));
        bOk = bOk && (valMes(oTxt));
        bOk = bOk && (valDia(oTxt));
        bOk = bOk && (valSep(oTxt));

        if (!bOk)
            {
		    sw = 1;
            oTxt.value="";
            oTxt.focus();
		//	return false;
            }
        }

    if (sw == 1){
		return false;
    }else{
        return true;
    }
}

function Fecha(formulario, campo)
    {
    var rutaCampoFecha = eval('document.' + formulario + '.' + campo);
    var oTxt = rutaCampoFecha;

    if (eval('document.' + formulario + '.' + campo + '.value') == "")
        {
		eval('document.' + formulario + '.' + campo + '.' + 'style.border' + '="2px solid #E40000"');
		eval('document.' + formulario + '.' + campo + '.' + 'focus()');
		alert("El campo fecha no puede estar vac\u00edo.");
        return false; 
        }

    else
        {

		if(valFecha(rutaCampoFecha)){
			return true;
		}else{
			eval('document.' + formulario + '.' + campo + '.' + 'style.border' + '="2px solid #E40000"');
			eval('document.' + formulario + '.' + campo + '.' + 'focus()');
			alert("El formato de la fecha no es correcto.");
			return false;
        } //fin else
    }
}

function numerico(formulario, campo)
    {
    var valor = eval('document.' + formulario + '.' + campo + '.value');
    valor = parseInt(valor);

    if (isNaN(valor))
        {
        return false; 
        }

    else
        return true;
    }

function decimal(formulario, campo)
    {
    var valor = eval('document.' + formulario + '.' + campo + '.value');
    valor = parseFloat(valor.replace(',', '.'));

    if (valor % 1 == 0)
        {
        return false; // el valor no es decimal
        }

    else
        return true;
    }


	
function isDigit (c)
{   return ((c >= "0") && (c <= "9"))
}

function isLetterOrDigit (c)
{   return (isLetter(c) || isDigit(c))
}

function isInteger (s)
{   var i;
    if (isEmpty(s)) 
       if (isInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isInteger.arguments[1] == true);
    
    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if( i != 0 ) {
            if (!isDigit(c)) return false;
        } else { 
            if (!isDigit(c) && (c != "-") || (c == "+")) return false;
        }
    }
    return true;
}

function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}


function isNumber (s)
{   var i;
    var dotAppeared;
    dotAppeared = false;
    if (isEmpty(s)) 
       if (isNumber.arguments.length == 1) return defaultEmptyOK;
       else return (isNumber.arguments[1] == true);
    
    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if( i != 0 ) {
            if ( c == ","  || c == "." ) {
                if( !dotAppeared )
                    dotAppeared = true;
                else
                    return false;
            } else     
                if (!isDigit(c)) return false;
        } else { 
            if ( c == "," || c == "." ) {
                if( !dotAppeared )
                    dotAppeared = true;
                else
                    return false;
            } else     
                if (!isDigit(c) && (c != "-") || (c == "+")) return false;
        }
    }
    return true;
}