var g_fecha_nacimiento=0, g_fecha_exploracion=0, g_fecha_exploracion_previa=0;
function validar_fecha_nacimiento()
	{
	var fecha=Evaluador_conversor_fecha_form_texto(document.forms.datos_deseados.fecha_nacimiento.value);
	if(!fecha)
		{
		document.forms.datos_deseados.fecha_nacimiento.value='';
		g_fecha_nacimiento=0;
		}
	else
		{
		document.forms.datos_deseados.fecha_nacimiento.value=fecha;
		g_fecha_nacimiento=1;
		}
	}
function validar_fecha_exploracion()
	{
	var fecha=Evaluador_conversor_fecha_form_texto(document.forms.datos_deseados.fecha_exploracion.value);
	if(!fecha)
		{
		document.forms.datos_deseados.fecha_exploracion.value='';
		g_fecha_exploracion=0;
		}
	else	
		{
		document.forms.datos_deseados.fecha_exploracion.value=fecha;
		g_fecha_exploracion=1;
		}
	}
function validar_fecha_exploracion_previa()
	{
	var fecha=Evaluador_conversor_fecha_form_texto(document.forms.datos_deseados.fecha_exploracion_previa.value);
	if(!fecha)
		{
		document.forms.datos_deseados.fecha_exploracion_previa.value='';
		g_fecha_exploracion_previa=0;
		}
	else	
		{
		document.forms.datos_deseados.fecha_exploracion_previa.value=fecha;
		g_fecha_exploracion_previa=1;
		}
	}
function calculo_fecha()
	{
	var edad;
	if(g_fecha_nacimiento==1 && g_fecha_exploracion==1)
		{
		with(document.forms.datos_deseados)
			{
			edad=calculo_edad(conversor_fecha_Date(fecha_nacimiento.value),conversor_fecha_Date(fecha_exploracion.value));
			if(edad<0){alert("Error: La fecha de nacimiento no debe ser superior a la exploración");g_fecha_nacimiento=0;edad_actual.value=" ";fecha_nacimiento.value="";return;}
			else edad_actual.value="Edad: "+edad_desmenuzada(edad);
			}
		}
	else document.forms.datos_deseados.edad_actual.value=" ";
	}
function calculo_edad(fecha_nac, fecha_post)
{
	return (fecha_post-fecha_nac)/31557600000;
}
function conversor_fecha_Date(fecha)
{
	var n, dia, mes, ano;
	var set_fecha= new Date();
	dia=parseInt(fecha.substr(0,2),10);
	mes=parseInt(fecha.substr(3,2),10);
	ano=parseInt(fecha.substr(6,4),10);
	set_fecha.setDate(dia);
	set_fecha.setMonth(mes-1);
	set_fecha.setFullYear(ano);
	return set_fecha;
}
function edad_desmenuzada(edad)
{
	var anos=parseInt(edad,10);
	var edad_meses=(edad-anos)*12;
	var meses=parseInt(edad_meses,10);
	var dias=parseInt((edad_meses-meses)*31,10);
	var texto_anos=" años";
	var texto_meses=" meses";
	var texto_dias=" días";
	if(anos==1) texto_anos=" año";
	if(meses==1) texto_meses=" mes";
	if(dias==1) texto_dias=" día";
	if(edad<1)
		{
	if((edad*12)>=1) return(meses+texto_meses+" y "+dias+texto_dias);
		else return(dias+texto_dias);
		}
	else return(anos+texto_anos+" y "+meses+texto_meses);
}
function hoy()
	{
	var hoy=new Date();
	var dia, mes, ano;
	dia=hoy.getDate();
	mes=1+hoy.getMonth();
	ano=hoy.getFullYear();
	if(dia<10) dia='0'+dia;
	if(mes<10) mes='0'+mes;
	document.forms.datos_deseados.fecha_exploracion.value=dia+'/'+mes+'/'+ano;
	}
function Evaluador_conversor_fecha_form_texto(fecha)
	{
	var dia, mes, ano, error="Debe introducir una fecha con el formato ddmmaa, ddmmaaaa, dd/mm/aa ó dd/mm/aaaa.";
	if(fecha.length==0)
		{
		alert(error);
		return 0;
		}
	if(fecha.length!=6 && fecha.length!=8 && fecha.length!=10)
		{
		alert(error);
		return 0;
		}
	if(fecha.length==6)
		{
		for(i=0;i<6;i++)
			{
			if(fecha.charCodeAt(i)>57 || fecha.charCodeAt(i)<48)
				{
				alert(error);
				return 0;
				}
			}
		dia=parseInt(fecha.substr(0,2),10);
		mes=parseInt(fecha.substr(2,2),10);
		ano=parseInt(fecha.substr(4,2),10);
		if(dia<1 || dia>31)
			{
			alert("Error: el dia (dd) debe estar comprendido entre 01 y 31");
			return 0;
			}
		if(mes<1 || mes>12)
			{
			alert("Error: el mes (mm) debe estar comprendido entre 01 y 12");
			return 0;	
			}
		if(ano>=70) ano+=1900;
		if(ano<70) ano+=2000;
		if(dia<10) dia='0'+dia;
		if(mes<10) mes='0'+mes;
		return(dia+'/'+mes+'/'+ano);
		}
	if(fecha.length==10)
		{
		if(fecha.charCodeAt(2)==47 && fecha.charCodeAt(5)==47)
			{
			for(i=0;i<10;i++)
				{
				if(i!=2 && i!=5)
					{
					if(fecha.charCodeAt(i)>57 || fecha.charCodeAt(i)<48)
						{
						alert(error);
						return 0;
						}
					}
				}

			}
		else {alert(error); return 0;}
		dia=parseInt(fecha.substr(0,2),10);
		mes=parseInt(fecha.substr(3,2),10);
		ano=parseInt(fecha.substr(6,4),10);
		if(dia<1 || dia>31)
			{
			alert("Error: el dia (dd) debe estar comprendido entre 01 y 31");
			return 0;
			}
		if(mes<1 || mes>12)
			{
			alert("Error: el mes (mm) debe estar comprendido entre 01 y 12");
			return 0;	
			}
		if(ano<1970){ alert("Error: el año debe ser mayor de 1970"); return 0;}
		if(ano>2100){ alert("Error: el año no debe ser mayor de 2100"); return 0;}
		if(dia<10)dia='0'+dia;
		if(mes<10)mes='0'+mes;
		return(dia+'/'+mes+'/'+ano);
		}
	if(fecha.charCodeAt(2)==47 && fecha.charCodeAt(5)==47)
		{
		for(i=0;i<8;i++)
				{
				if(i!=2 && i!=5)
					{
					if(fecha.charCodeAt(i)>57 || fecha.charCodeAt(i)<48)
						{
						alert(error);
						return 0;
						}
					}
				}
		dia=parseInt(fecha.substr(0,2),10);
		mes=parseInt(fecha.substr(3,2),10);
		ano=parseInt(fecha.substr(6,2),10);
		if(dia<1 || dia>31)
			{
			alert("Error: el dia (dd) debe estar comprendido entre 01 y 31");
			return 0;
			}
		if(mes<1 || mes>12)
			{
			alert("Error: el mes (mm) debe estar comprendido entre 01 y 12");
			return 0;	
			}
		if(ano>=70) ano+=1900;
		if(ano<70) ano+=2000;
		if(dia<10)dia='0'+dia;
		if(mes<10)mes='0'+mes;
		return(dia+'/'+mes+'/'+ano);
		}
	else 
		{
		if(fecha.charCodeAt(2)!=47 && fecha.charCodeAt(5)!=47)
			{
				for(i=0;i<8;i++)
					{
					if(fecha.charCodeAt(i)>57 || fecha.charCodeAt(i)<48)
						{
						alert(error);
						return 0;
						}
					}
			dia=parseInt(fecha.substr(0,2),10);
			mes=parseInt(fecha.substr(2,2),10);
			ano=parseInt(fecha.substr(4,4),10);
			if(dia<1 || dia>31)
				{
				alert("Error: el dia (dd) debe estar comprendido entre 01 y 31");
				return 0;
				}
			if(mes<1 || mes>12)
				{
				alert("Error: el mes (mm) debe estar comprendido entre 01 y 12");
				return 0;	
				}
			if(ano<1970){ alert("Error: el año debe ser mayor de 1970"); return 0;}
			if(ano>2100){ alert("Error: el año no debe ser mayor de 2100"); return 0;}
			if(dia<10)dia='0'+dia;
			if(mes<10)mes='0'+mes;
			return(dia+'/'+mes+'/'+ano);
			}
		else {alert(error); return 0;}
		}
	}
function xredondeo(n,dec)
{
	var fact=1;
	for(i=1;i<=dec;i++)
		{
		fact*=10;
		}
	return Math.round(n*fact)/fact;
}
