function daysInFebruary (year) {   	
    // Febrero tiene 29 dias en cualquier año divisible por 4,
    // EXCEPTO los múltiplos de 100 no divisibles por 400.
    return (  ((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0) ) ) ? 29 : 28 );
}

function makeArray(n) {
    for (var i = 1; i <= n; i++) {
        this[i] = 0
    } 
    return this
}

function checkFecha(dia,mes,anio) {
    // compruebo que el dia sea correcto
    c = new String(parseInt(dia.value,10));
    if(c == "NaN") {
        alert("El día no es correcto.");
        return false;
    } else {
        dia.value = c;
        if(dia.value < 1 || dia.value > 31) {
            alert("El día no es correcto.");
            return false;
        }
    }

    c = new String(parseInt(mes.value,10));
    if(c == "NaN") {
        alert("El mes no es correcto.");
        return false;
    } else {
        mes.value = c;
        if(mes.value < 1 || mes.value > 12) {
            alert("El mes no es correcto.");
            return false;
        }
    }
    c = new String(parseInt(anio.value,10));
    if(c == "NaN") {
        alert("El año no es correcto.");
        return false;
    } else {
        anio.value = c;
        if(anio.value < 1850 || anio.value > 2020) {
            alert("El año no es correcto.");
            return false;
        }
    }
		
    // compruebo que la fecha en conjunto sea correcta.
    var daysInMonth = makeArray(12);
    daysInMonth[1] = 31;
    daysInMonth[2] = 29;   // esto no es siempre cierto por lo qu habrá que hacer comprobación extra.
    daysInMonth[3] = 31;
    daysInMonth[4] = 30;
    daysInMonth[5] = 31;
    daysInMonth[6] = 30;
    daysInMonth[7] = 31;
    daysInMonth[8] = 31;
    daysInMonth[9] = 30;
    daysInMonth[10] = 31;
    daysInMonth[11] = 30;
    daysInMonth[12] = 31;

    if((dia.value != "") && (mes.value != "") && (anio.value != "")) {
        var intYear = parseInt(anio.value,10);
        var intMonth = parseInt(mes.value,10);
        var intDay = parseInt(dia.value,10);

        // capturo los días inválidos, excepto para Febrero
        if (intDay > daysInMonth[intMonth]) {
            alert("La fecha no es correcta");
            return false; 
        }

        if ((intMonth == 2) && (intDay > daysInFebruary(intYear))) {
            alert("La fecha no es correcta");
            return false;
        }
    }

    return true;
}