function isInteger(cantidad){
    lon = cantidad.length;
	for (i=0; i<lon; i++)
	{
		if ((cantidad.charCodeAt(i) < 48) || (cantidad.charCodeAt(i) > 57))
		{
			return false;
		}
	}
	return true;
}


// función que coge cuántos días tien un mes
function getdaymonth(year, month){
  if ( month==2){
    if (( year%4==0 &&  year%100!=0) ||  year%400==0)
	  return 29;
	else return 28;
  } else if (( month==1) || ( month==3) || ( month==5) || ( month==7) || ( month==8) || ( month==10) || ( month==12))
    return 31;
  else return 30;
}


// función que valida el formato de la fecha
function fechas(fecha){
  if (fecha.value.length<4 || fecha.value.length>10){
    return "El formato de la fecha es incorrecto\n";
	fecha.value = "";
  } else {
	if (fecha.value.length==10){
	  if ((fecha.value.substring(2,3)=="-") && (fecha.value.substring(5,6)=="-")) {
        dia = fecha.value.substring(0,2);
        mes =fecha.value.substring(3,5);
        anyo = fecha.value.substring(6,10);
		if (isInteger(dia) && isInteger(mes) && isInteger(anyo)){
	      mes2 = parseFloat(mes);
	      anyo2 = parseFloat(anyo);
	      dia2 = parseFloat(dia);
	      dia1 = getdaymonth(anyo2,mes2);
          if (dia2<=0 || !isInteger(dia) || dia2>dia1){
            return "El formato del día es incorrecto\n";
          }
          if (mes2<=0 || !isInteger(mes) || mes2>12){
            return "El formato del mes es incorrecto\n";
          } 
          if (anyo2<=0 || !isInteger(anyo)){
            return "El formato del año es incorrecto\n";
          }
		  fecha.value = dia + "-" + mes + "-" + anyo;
		  return "";
		} else return "El formato de la fecha es incorrecto\n";
	  } else return "El formato de la fecha es incorrecto\n";
	} else return "El formato de la fecha es incorrecto\n";
 }//else
  return "";
}//function
