
primerAnio = 1910;
var meses = new Array("Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre");

function populate(objForm,selectIndex) {

	timeA = new Date(objForm.year.options[objForm.year.selectedIndex].text, objForm.month.options[objForm.month.selectedIndex].value,1);
	timeDifference = timeA - 86400000;
	timeB = new Date(timeDifference);
	var daysInMonth = timeB.getDate();
	
	for (var i = 0; i < objForm.day.length; i++) {
		objForm.day.options[0] = null;
	}
	
	for (var i = 0; i < daysInMonth; i++) {
		objForm.day.options[i] = new Option(i+1);
	}
	
	document.f1.day.options[0].selected = true;
	
}

function actualizaFecha(id_controlFecha, id_controlAnio, id_controlMes, id_controlDia) {
	controlFecha = document.getElementById(id_controlFecha);
	controlAnio = document.getElementById(id_controlAnio);
	controlMes = document.getElementById(id_controlMes);
	controlDia = document.getElementById(id_controlDia);
	diaSeleccionado = controlDia.selectedIndex;
	
	timeA = new Date(controlAnio.options[controlAnio.selectedIndex].value, controlMes.options[controlMes.selectedIndex].value,1);
	timeDifference = timeA - 86400000;
	timeB = new Date(timeDifference);
	var daysInMonth = timeB.getDate();
	
	// VACÍO EL CONTROL DEL DÍA:
	controlDia.options.length = 0;
	
	// PONGO LOS DÍAS:
	// PRIMERO PONGO EL TÍTULO "Día" con value 0
	var opcion = document.createElement("OPTION");
	opcion.text="Día";
	opcion.value=0;	
	controlDia.options.add(opcion);	
	//
	for (var i = 0; i < daysInMonth; i++) {
		var opcion = document.createElement("OPTION");
		var valor = i+1;
		if(valor < 10){
			valor =  '0' + valor;
		}
		opcion.text=valor;
		opcion.value=valor;
		/*
		opcion.text=i+1;
		opcion.value=i+1;		
		*/
		controlDia.options.add(opcion);		
	}
	// SI ESTABA SELECCIONADO EL 31 PERO SE CAMBIÓ A UN MES QUE NO TIENE TANTOS DÍAS, PONE EN 0 EL DÍA
	if(diaSeleccionado <= (daysInMonth+1)){
		controlDia.options[diaSeleccionado].selected=true;
	}
	else{
		controlDia.options[0].selected=true;
	}	
	// FECHA RESULTANTE:
	anioFinal = controlAnio.options[controlAnio.selectedIndex].value;
	mesFinal = controlMes.options[controlMes.selectedIndex].value;
	diaFinal = controlDia.options[controlDia.selectedIndex].value;
	// SI ALGUNO DE LOS VALORES ES 0 VACÍA EL CAMPO DE LA FECHA:
	if(anioFinal != 0 && mesFinal != 0 && diaFinal != 0){
		controlFecha.value = anioFinal + '-' + mesFinal + '-' + diaFinal;
	}
	else{
		controlFecha.value = '';
	}	
}

function setAnios(id) {
	list = document.getElementById(id);
	timeC = new Date();
	anioActual = timeC.getFullYear();
	rango = anioActual - primerAnio + 1;
	
	// VACÍO EL CONTROL:
	list.options.length = 0;	
	// PONGO EL TÍTULO "Año" con value 0
	var opcion = document.createElement("OPTION");
	opcion.text="Año";
	opcion.value=0;	
	list.options.add(opcion);
	//		
	for (var i = 0; i < rango; i++) {
		var opcion = document.createElement("OPTION");
		var valor = primerAnio + i;
		opcion.text=valor;
		opcion.value=valor;
		list.options.add(opcion);		
	}
}

function setMeses(id) {
	list = document.getElementById(id);
	// VACÍO EL CONTROL:
	list.options.length = 0;	
	// PONGO EL TÍTULO "Mes" con value 0
	var opcion = document.createElement("OPTION");
	opcion.text="Mes";
	opcion.value=0;	
	list.options.add(opcion);
	//	
	for (var i = 1; i < (meses.length + 1); i++) {
		var opcion = document.createElement("OPTION");
		var valor = i;
		if(i < 10){
			valor =  '0' + valor;
		}
		opcion.text=meses[i - 1];
		opcion.value=valor;
		list.options.add(opcion);		
	}
}

function setDias(id) {
	list = document.getElementById(id);
	// VACÍO EL CONTROL:
	list.options.length = 0;	
	// PONGO EL TÍTULO "Mes" con value 0
	var opcion = document.createElement("OPTION");
	opcion.text="Día";
	opcion.value=0;	
	list.options.add(opcion);
	//
}

function initCalendarios() {
	setAnios('anio');
	setMeses('mes');
	setDias('dia');				
}

function initCalendario(prefijo) {
	setAnios(prefijo + '_anio');
	setMeses(prefijo + '_mes');
	setDias(prefijo + '_dia');				
}



