function ordena(pagina,formulario,accion)//envia el formulario pasandole un parametros para ordenar la pagina
{
	formulario.action=accion;
	formulario.pagina.value=pagina;
	formulario.submit();
}

function esEntero(valor){
      //intento convertir a entero.
     //si era un entero no le afecta, si no lo era lo intenta convertir
     valor = parseInt(valor)
      //Compruebo si es un valor numérico
      if (isNaN(valor)) {
            //entonces (no es numero) devuelvo false
			//alert("El campo no es numérico");
            return false
      }else{
            //En caso contrario (Si era un número) devuelvo el true
            return true
      }
}

//campos de fecha
function esFecha(dateStr) {
	// Comprueba los siguientes formatos de fecha:
	// MM/DD/YY MM/DD/YYYY MM-DD-YY MM-DD-YYYY
	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;

	var matchArray = dateStr.match(datePat); // is the format ok?
	if (dateStr==""){
		return true
	}
	if (matchArray == null) {
		alert("El formato de la Fecha debe ser dd/mm/aaaa")
		return false;
	}
	month = matchArray[3]; // parse date into variables
	day = matchArray[1];
	year = matchArray[4];
	if (month < 1 || month > 12) { // check month range
		alert("El mes debe estar entre 1 y 12.");
		return false;
	}
	if (day < 1 || day > 31) {
		alert("Día debe estar entre 1 y 31.");
		return false;
	}
	if ((month==4 || month==6 || month==9 || month==11) && day==31) {
		alert("Este mes no tiene 31 días!")
		return false
	}
	if (month == 2) { // check for february 29th
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day>29 || (day==29 && !isleap)) {
			alert("Febrero " + year + " no tiene " + day + " dias!");
			return false;
		}
	}

	return true; // date is valid
}

function CompruebaCIF(elCIF){
    var resul = false;
    var temp = elCIF.toUpperCase(); // pasar a mayúsculas

    if (!/^[A-Za-z0-9]{9}$/.test(temp)){ // Son 9 dígitos?
//      alert ("Longitud incorrecta, un CIF consta de 9 dígitos");
      return false;
    }
    else if (!/^[ABCDEFGHKLMNPQS]/.test(temp)){ // Es una letra de las admitidas ?
//      alert("El primer dígito es incorrecto, debe ser una letra de las siguientes: A,B,C,D,E,F,G,H,K,L,M,N,P,Q,S ");
      return false;
    }

    return ValidaCIF(elCIF);
}


  // La función recibe el CIF completo: A58818501
 function ValidaCIF(elCIF)
 {

    var v1 = new Array(0,2,4,6,8,1,3,5,7,9);
    var temp = 0;


    for( i = 2; i <= 6; i += 2 )
    {
      temp = temp + v1[ parseInt(elCIF.substr(i-1,1)) ];
      temp = temp + parseInt(elCIF.substr(i,1));
    };

    temp = temp + v1[ parseInt(elCIF.substr(7,1)) ];

    temp = (10 - ( temp % 10));

    if( temp == 10 ){
        if(((elCIF.substr(elCIF.length-1,1))!='J')&&
           ((elCIF.substr(elCIF.length-1,1))!='0')){
//                alert( "El dígito de control "+elCIF.substr(elCIF.length-1,1)+" != J y O");
                return false;
        }
    }
    else{
        if((elCIF.substr(elCIF.length-1,1))!=temp){
//                alert( "El dígito de control "+elCIF.substr(elCIF.length-1,1)+" != "+temp );
                return false;
        }
    }

    return true;
}

function esDni(sNif)
{
        var strLetras = 'TRWAGMYFPDXBNJZSQVHLCKE';
        var Letra;
        sNif=sNif.toUpperCase();
        if(sNif.length<9) {
                return(false);
        }
        else {
             if(sNif.charAt(0)=="X")
                sNif=sNif.substr(1,sNif.length);
                Letra=sNif.charAt(sNif.length-1);
                if(Letra<'A' || Letra>'Z') {
                        return(false);
                }
                else
                        Letra=strLetras.charAt((sNif.substr(0,sNif.length-1))%23);
                if(sNif.charAt(sNif.length-1)==Letra)
                        return(true);
                else {
                        return(false);
                }
        }
}
