var hoydia
function strisDigit(str) {
	var ch;
	var i 
	ch = ' ';
	if (str.length == 0)
        return false;
	for (i = 0; i < str.length; i++) {
		ch = str.charAt(i);
		if (ch < '0' || '9' < ch)
			return false;
    }
	return true;
}


function validafecha(diaobj,mesobj,annoobj)
{

//	dia = diaobj[diaobj.selectedIndex].value
//	mes = getMonthNumber(mesobj[mesobj.selectedIndex].value)
//	if (mes == 13)
//		mes = mesobj[mesobj.selectedIndex].value
//	anno = annoobj[annoobj.selectedIndex].value
//	dia = parseInt(dia,10)
//	mes = parseInt(mes,10)
//	anno = parseInt(anno,10)

	dia = parseInt(diaobj,10)
	mes = parseInt(mesobj,10)
	anno = parseInt(annoobj,10)

	//alert(dia + "/" + mes + "/" + anno)

	if (!strisDigit(dia))
	{	alert("Dia debe ser un número válido")
		diaobj.focus()
		return false
	}
	if (!strisDigit(mes))
	{	alert("Mes debe ser un número válido")
		mesobj.focus()
		return false
	}
	if (!strisDigit(anno))
	{	alert("Año debe ser un número válido")
		annoobj.focus()
		return false
	}
	
	if (anno.toString().length != 4)
	{	alert("El año ingresado no es un año válido. Debe ser un número de 4 dígitos.")
		return false
	}
	if (mes < 1 || 12 < mes)
	{	alert("El mes ingresado no es un mes válido")
			return false;
	}
	if (mes == 1 || mes == 3 || mes == 5 || mes == 7 || mes == 8
			|| mes == 10 || mes == 12)
	{	if (1 <= dia && dia <= 31)
			return true
		else
		{	
			alert("El dia ingresado no es un día válido, debe ser un número de 1 a 31, de acuerdo al mes seleccionado.")
			return (false)
		}
	}
	else if (mes == 2) {
		if (anno % 4 == 0 && (anno % 100 != 0 || anno % 400 == 0))
		{	if (1 <= dia && dia <= 29)
				return true
			else
			{	alert("dia debe ser un valor entre 1 y 29") 
				return (false)
			}
		}
		else
		{	if (1 <= dia && dia <= 28)
				return true
			else
			{	alert("dia debe ser un valor entre 1 y 28") 
				return (false)
			}
		}
	}
	else
	{	if (1 <= dia && dia <= 30)
			return true
		else
		{	alert("Día debe ser un valor entre 1 y 30")
			return (false)
		}
	}
}

function Datecmp(dia1, mes1, anno1, dia2, mes2, anno2) {
	var meses;

	meses = (anno2 - anno1) * 12 + (mes2 - mes1);
	if (meses > 0)
		return -1;
	if (meses < 0)
		return 1;
	/*	Si se trata del mismo mes (meses == 0) */
	if (dia1 > dia2)
		return 1;
	if (dia1 < dia2)
		return -1;
	return 0;
}

function valida_num( obj )
{
	if ( !strisDigit(obj.value))
	{	alert("Debe ingresar un número")
		obj.value = ""
		obj.focus()
		return false
	}
	return true
}

function valida_num_rut( obj )
{
	if ( !strisDigit(obj.value))
	{	alert("Debe ingresar un RUT válido")
		obj.value = ""
		obj.focus()
		return false
	}
	return true
}


function valida_dia( obj )
{
	if (valida_num(obj))
	{
		if (parseInt(obj.value,10) < 1	|| parseInt(obj.value,10) > 31 )
		{
			alert("El día debe ser un número entre 1 y 31")
			obj.focus()
			obj.value = ""
			return false
		}
		return true 
	}
	return false
}

function valida_mes( obj )
{
	if (valida_num(obj))
	{
		if (parseInt(obj.value,10) < 1	|| parseInt(obj.value,10) > 12 )
		{
			alert("El mes debe ser un número entre 1 y 12")
			obj.focus()
			obj.value = ""
			return false
		}
		return true 
	}
	return false
}

function valida_anno( obj )
{
	fecha = new Date()
	anno = fecha.getYear()
	anno_bajo = anno - 1
	anno = anno + 1
	if (valida_num(obj))
	{
		if (parseInt(obj.value,10) < 1999 || parseInt(obj.value,10) > anno )
		{
			alert("El año debe ser un número entre" + anno_bajo + " y " + anno)
			obj.focus()
			obj.value = ""
			return false
		}
		return true
	}
	return false
}


hoydia = new Date()

function cargafecha()
{
	window.document.forms[0].dia.selectedIndex = hoydia.getDate();
	window.document.forms[0].mes.selectedIndex = hoydia.getMonth() + 1;
	window.document.forms[0].anno.selectedIndex= 0
	if (window.document.forms[0].anno.value.length < 4)
	{ window.document.forms[0].anno.value = parseInt(window.document.forms[0].anno.value,10) + 1900 }
}

function getMonthNumber( mes )
{
	if ( mes == "Enero" ) 
		return 1
	else if ( mes == "Febrero" ) 
		return 2
	else if ( mes == "Marzo" ) 
		return 3
	else if ( mes == "Abril" ) 
		return 4
	else if ( mes == "Mayo" ) 
		return 5
	else if ( mes == "Junio" ) 
		return 6
	else if ( mes == "Julio" ) 
		return 7
	else if ( mes == "Agosto" ) 
		return 8
	else if ( mes == "Septiembre" ) 
		return 9
	else if ( mes == "Octubre" ) 
		return 10
	else if ( mes == "Noviembre" ) 
		return 11
	else if ( mes == "Diciembre" ) 
		return 12
	else
		return 13
}

function getMonth( mes )
{
	mes = parseInt(mes,10)
	if ( mes == 0 ) 
		return "Enero"
	else if ( mes == 1 ) 
		return "Febrero"
	else if ( mes == 2 ) 
		return "Marzo"
	else if ( mes == 3 ) 
		return "Abril"
	else if ( mes == 4 ) 
		return "Mayo"
	else if ( mes == 5 ) 
		return "Junio"
	else if ( mes == 6 ) 
		return "Julio"
	else if ( mes == 7 ) 
		return "Agosto"
	else if ( mes == 8 ) 
		return "Septiembre"
	else if ( mes == 9 ) 
		return "Octubre"
	else if ( mes == 10 ) 
		return "Noviembre"
	else 
		return "Diciembre"
}

function obtieneAnno( anno )
{
	anno = parseInt(anno,10)
	if (anno < 1899)
		anno +=1900
	return anno
}

function fillDays(name_select)
{
	deleteAllOptions(name_select)
	for (i=1; i <=31; i++)
		name_select.options[name_select.options.length] = addOption(i,i);
}

function fillMonths(name_select)
{
	deleteAllOptions(name_select)
	
	mes  = hoydia.getMonth()  //  mes va de 0 a 11
	lastMonth = mes -1  // podria ir de -1 a 10
	
	if ( lastMonth < 0 )
		lastMonth = 11
	
	    name_select.options[name_select.options.length] = addOption(getMonth(lastMonth),getMonth(lastMonth));
}

function fillAllMonths(name_select)
{
	deleteAllOptions(name_select)
	
	for (i=0; i <=11 ;i++)
		name_select.options[name_select.options.length] = addOption(getMonth(i),getMonth(i));
}


function fillYears(name_select)
{
	deleteAllOptions(name_select)
	
	anno  = hoydia.getYear()  //  anno puede ser cifra de 4 cifras o de 3
	if (anno < 1899)
		anno +=1900
	
	if (hoydia.getMonth() == 0)
	{	lastYear = anno -1  // podria ir desde año actual hasta un año antes si mes es enero
		name_select.options[name_select.options.length] = addOption(lastYear,lastYear);	
	}
	
//	name_select.options[name_select.options.length] = addOption(anno,anno);
}

function fill5YearsAtras(name_select)
{
	deleteAllOptions(name_select)
	
	anno = hoydia.getYear()
	if (anno < 1899)
		anno +=1900
	
	for( i=anno-5;i <= anno ; i++)
		name_select.options[name_select.options.length] = addOption(i,i);
}

function fill5YearsAdelante(name_select)
{
	deleteAllOptions(name_select)
	
	anno = hoydia.getYear()
	if (anno < 1899)
		anno +=1900
	
	for( i=anno;i < anno+5 ; i++)
		name_select.options[name_select.options.length] = addOption(i,i);
}

function fill10Years(name_select)
{
	deleteAllOptions(name_select)
	
	anno = hoydia.getYear()
	if (anno < 1899)
		anno +=1900
	
	for( i=anno -5 ;i < anno+5 ; i++)
		name_select.options[name_select.options.length] = addOption(i,i);
}


function fillBornYears(name_select)
{
	deleteAllOptions(name_select)
	
	anno = hoydia.getYear()
	if (anno < 1899)
		anno +=1900
	
	for( i=anno ;i > anno-90 ; i--)
		name_select.options[name_select.options.length] = addOption(i,i);
}

function addOption(texto,valor)
{
 var opt = new Option(texto,valor);
 return opt;
} 

function deleteAllOptions (select) {
  select.options.length = 1;
}

function menordeedad(dia, mes, anno)
{
	hoydia = new Date()
	if ( hoydia.getYear() < 1899 )
	{	annomenor18 = hoydia.getYear() + 1900  - 18}
	else
	{	annomenor18 = hoydia.getYear() - 18}
	if ( Datecmp(dia,mes,anno,hoydia.getDate(),hoydia.getMonth()+1,annomenor18) >= 0 )
		return true
	else
		return false
}