/// Recibe un select con meses, lee el nombre, 
/// busca otros select que tengan nombres parecidos 
/// (selectMes, luego selectDia, selectAno) y
/// con su referencia cambia los días con respecto a los
/// años y los meses.
function cambiaMes(obj)
{
	var objMes;
	var objDia;
	var objAno;
	var mesi;
	var i;
	var chilD;
	var textD;
	var diaSel;
	var esAno;
		
	objMes = window.document.getElementById(obj.id.toString());
	objDia = window.document.getElementById(obj.id.substring(0,obj.id.toString().length -3) + "Dia");
	objAno = window.document.getElementById(obj.id.substring(0,obj.id.toString().length -3) + "Ano");

	diaSel = objDia.options[objDia.selectedIndex].value * 1;
	
	for (i = objDia.options.length -1; i >= 0; i--)
	{
		objDia.options[i] = null;
	}

	// ya que tenemos poseción de el select de día, ahora hay que llenarlo con los valores correctos.

	// Qué mes es?

	mesi = objMes.options[objMes.selectedIndex].value * 1;
	try
	{
		esAno = objAno.options[objAno.selectedIndex].value;
	}
	catch(ex)
	{
		if (isNaN(objAno.value))
		{
			esAno = 1900;
		}
		else
		{
			esAno = objAno.value*1;
		}
	}

	if (mesi === 2)
	{
		if (isLeapYear(esAno))
		{
			for (i = 0; i <= 29; i++)
			{
				chilD = window.document.createElement("option");
				chilD.setAttribute("value", i);
				if (i === diaSel){chilD.setAttribute("selected", "selected");}
				
				if (i > 0)
				{
					textD = window.document.createTextNode(i.toString());
				}
				else
				{
					textD = window.document.createTextNode("---");
				}
				chilD.appendChild(textD);
				objDia.appendChild(chilD);
			}
		}
		else
		{
			for (i = 0; i <= 28; i++)
			{
				chilD = window.document.createElement("option");
				chilD.setAttribute("value", i);
				if (i === diaSel){chilD.setAttribute("selected", "selected");}
				if (i > 0)
				{
					textD = window.document.createTextNode(i.toString());
				}
				else
				{
					textD = window.document.createTextNode("---");
				}
				chilD.appendChild(textD);
				objDia.appendChild(chilD);
			}
		}
	}
	else if(mesi === 4 || mesi === 6 || mesi === 9 || mesi === 11)
	{
		for (i = 0; i <= 30; i++)
		{
			chilD = window.document.createElement("option");
			chilD.setAttribute("value", i);
			if (i === diaSel){chilD.setAttribute("selected", "selected");}
			if (i > 0)
				{
					textD = window.document.createTextNode(i.toString());
				}
				else
				{
					textD = window.document.createTextNode("---");
				}
			chilD.appendChild(textD);
			objDia.appendChild(chilD);
		}
	}
	else if (mesi > 0)
	{
		for (i = 0; i <= 31; i++)
		{
			chilD = window.document.createElement("option");
			chilD.setAttribute("value", i);
			if (i === diaSel){chilD.setAttribute("selected", "selected");}
			if (i > 0)
			{
				textD = window.document.createTextNode(i.toString());
			}
			else
			{
				textD = window.document.createTextNode("---");
			}
			chilD.appendChild(textD);
			objDia.appendChild(chilD);
		}
	}
	else
	{
		chilD = window.document.createElement("option");
		chilD.setAttribute("value", "0");
		textD = window.document.createTextNode("---");
		chilD.appendChild(textD);
		objDia.appendChild(chilD);
	}
}

/// Recibe el objeto correspondiente al año, busca el objeto que corresponda al mes, y lo envía a cambiaMes
function cambiaAno(obj)
{
	var objMes;
	
	objMes = window.document.getElementById(obj.id.substring(0,obj.id.toString().length -3) + "Mes");
	cambiaMes(objMes);
}

// Regresa si el año es bisiesto
function isLeapYear(year)
{
	var itIs;
	
	itIs = false;
	
	if(year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0)) 
	{
		// es bisiesto
		itIs = true;
	}
	
	return itIs;
}
