function mostrar_calendario (INPUT,date,padre)
{ 
    ahora = date;

	var hora 	= ahora.getHours();
	var minutos = ahora.getMinutes();
	var diaSemana 	= ahora.getDay();
	var dia		= ahora.getDate();
	var mes 	= ahora.getMonth()+1;
	var anyo 	= ahora.getYear();
	
	ocultar_div('calendario'+INPUT);
	texto = " ";
	texto += "<div id='CALENDARIO"+INPUT+"' style='z-index:2 position:absolute' width='200px' height='150px'>";
	texto += "<table class='tablaCalendario' width='200px' height='150px' border='1'>";
	texto += "<tr><th colspan='7' height='25px'><table width='100%' height='25px'><tr><th><a style='underline:none' onmouseover=\"this.style.cursor='hand'\" onclick=\"mostrar_calendario('"+INPUT+"',new Date("+ahora.getYear()+","+(ahora.getMonth()-1)+","+ahora.getDate()+"), document.getElementById('"+padre.id+"'));\">&lt;&lt;</a></th><th width='100%'><div id='FECHA'>"+texto_mes(mes)+" de "+ahora.getYear()+"</div></th><th><a style='underline:none' onmouseover=\"this.style.cursor='hand'\" onclick=\"mostrar_calendario('"+INPUT+"',new Date("+ahora.getYear()+","+(ahora.getMonth()+1)+","+ahora.getDate()+"),document.getElementById('"+padre.id+"'));\">&gt;&gt;</a></th><th><img src='NOEXISTE' width='15px' height='1px'></th><th><a href=\"javascript:ocultar_div('calendario"+INPUT+"');\">Cerrar</a></th></tr></table></th></tr>";
	texto += "<tr><td align='center' valign='middle'><b>L</b></td><td align='center' valign='middle'><b>M</b></td><td align='center' valign='middle'><b>X</b></td><td align='center' valign='middle'><b>J</b></td><td align='center' valign='middle'><b>V</b></td><td align='center' valign='middle'><b>S</b></td><td align='center' valign='middle' style='color: white; background-color:#D25999'><B>D</b></td></tr>";
	texto += rellenar_mes(ahora.getMonth(), ahora.getYear(), INPUT);
	texto += "<tr><td colspan='7'></td></tr>";
	texto += "</table>";
	texto += "</div>";
	document.getElementById('calendario'+INPUT).innerHTML=texto;
	mostrar_div('calendario'+INPUT, padre);
}



//Funcion que devuelve una cadena con los meses para rellenar la tabla de mes
function rellenar_mes (mes, anyo,INPUT)
{
	aux = new Date(anyo,mes,1);
	dia1delmes = aux.getDay();
	//0 Domingo, 1 Lunes, ....
	if (dia1delmes==0) dia1delmes=6;
	else dia1delmes--;
	//0 Lunes, ...., 6 domingo;
	texto ="<tr>";

	diadelmes =1;
	//Rellenamos la primera semana:
	for (i=0;i<7;i++)
	{
		if (i>=dia1delmes)
		{
			texto +="<td width='25px' height='25px' align='center' valign='middle' "+((i==6)? "style='color: white; background-color:#D25999'" : "" )+"><a onmouseover=\"this.style.cursor='hand'\" onclick=\"document.getElementById('"+INPUT+"').focus(); document.getElementById('"+INPUT+"').value='"+diadelmes+"/"+(mes+1)+"/"+anyo+"'; document.getElementById('"+INPUT+"').blur();\";>"+diadelmes+"</a></td>";
			diadelmes++;
		}
		else {texto +="<td width='25px' height='25px'><img src='NOEXISTE' width='1px' height='1px'/></td>";}
		
	}
	texto +="</tr>";
	
	//Rellenamos el resto de semanas:
	for (i=0;diadelmes<=diasMes(mes+1,anyo);diadelmes++)
	{
		if (i==0) texto +="<tr>";
		texto +="<td width='25px' height='25px' align='center' valign='middle' "+((i==6)? "style='color: white; background-color:#D25999'" : "" )+"><a onmouseover=\"this.style.cursor='hand'\" onclick=\"document.getElementById('"+INPUT+"').focus(); document.getElementById('"+INPUT+"').value='"+diadelmes+"/"+(mes+1)+"/"+anyo+"'; document.getElementById('"+INPUT+"').blur();\";>"+diadelmes+"</a></td>";
		if (i==6) { texto +="</tr>"; i=-1;}
		i++;
	}
	
	return texto;
}

function diasMes(mes, anyo)
{
	retorno = 0;
	switch (parseInt(mes))
	{
		case (1): {retorno = 31;};break;
		case (2): { ((es_anyo_bisiesto(anyo)) ? retorno = 29 : retorno = 28 );};break;
		case (3): {retorno = 31;};break;
		case (4): {retorno = 30;};break;		
		case (5): {retorno = 31;};break;		
		case (6): {retorno = 30;};break;
		case (7): {retorno = 31;};break;
		case (8): {retorno = 31;};break;
		case (9): {retorno = 30;};break;						
		case (10): {retorno = 31;};break;
		case (11): {retorno = 30;};break;		
		case (12): {retorno = 31;};break;		
	}
	return retorno;
}

function texto_mes (mes)
{
	retorno = "";
	switch (parseInt(mes))
	{
		case (1): {retorno = "Enero";};break;
		case (2): {retorno = "Febrero";};break;
		case (3): {retorno = "Marzo";};break;
		case (4): {retorno = "Abril";};break;		
		case (5): {retorno = "Mayo";};break;		
		case (6): {retorno = "Junio";};break;
		case (7): {retorno = "Julio";};break;
		case (8): {retorno = "Agosto";};break;
		case (9): {retorno = "Septiembre";};break;						
		case (10): {retorno ="Octubre";};break;
		case (11): {retorno ="Noviembre";};break;		
		case (12): {retorno ="Diciembre";};break;
	}
	return retorno;
}
