﻿/*
+----------------------------------------------------------------------
+ .s. CALENDARIO
+ .s. JAVASCRIPT
+ .s. MÉTODOS PÚBLICOS: 
+		ShowCalendario(this);
+ .s. psEd -> STRING
+----------------------------------------------------------------------
*/

var _Calendario_vMeses				= new Array ("Enero","Febrero","Marzo","Abril","Mayo","Junio","Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre");
var _Calendario_vFullNombreDia		= new Array ("Lunes","Martes","Miércoles","Jueves","Viernes","Sábado","Domingo");

var _Calendario_fMaster;
var _Calendario_oDestino;
var _Calendario_bShow = false;

function ShowCalendario(psEd)
{
	_Calendario_oDestino = document.getElementById(psEd);
	
	//.s. Si existe oculto
	if (document.getElementById("divCalendario"))
	{
		if (document.getElementById("divCalendario").style.display == "")
		{
			document.getElementById("divCalendario").style.display	= "none";
			return;
		}
	}
	
	if (_Calendario_oDestino.value != "")
	{
		_Calendario_fMaster = _Calendario_StringToDate(_Calendario_oDestino.value);
	}
	else
	{
		_Calendario_fMaster = new Date();
	}
	
	return _Calendario_DoHTML();
}
/*
+----------------------------------------------------------------------
+ 
+
+----------------------------------------------------------------------
*/
function _Calendario_DoHTML()
{
	//var fNextMes	= new Date(pFecha.getFullYear(),pFecha.getMonth()+1,0);
	var fNextMes	= new Date(_Calendario_fMaster.getFullYear(),_Calendario_fMaster.getMonth()+1,0);
	
	var iLastDia	= fNextMes.getDate();
	
	var iMes		= _Calendario_fMaster.getMonth()+1;
		sMes		= iMes < 10 ? "0" + iMes : iMes;
	var sYear		= _Calendario_fMaster.getFullYear().toString().substr(2,2);
	var iDia		= 1;

	iDiaSemana = new Date(_Calendario_fMaster.getFullYear(),_Calendario_fMaster.getMonth(),1).getDay();
	
	if (iDiaSemana == 0) {iDiaSemana = 7}
	
	var sHTML = "";
	
	sHTML += "<table border=0 class='calendario_TbMain' cellspacing=0 cellpadding=0>";
	sHTML += "<tr>";
	sHTML += "<td colspan='7'>";
	sHTML += "<table border='0' cellspacing='0' cellpadding='0' style='width:100%'><tr>";
	sHTML += "<td class='calendario_BtAvanzaMesIzq' onclick='javascript:_Calendario_AvanzaMes(false)'><<</td>";
	sHTML += "<td class='calendario_NombreMes'>" + _Calendario_vMeses[_Calendario_fMaster.getMonth()] + " " + _Calendario_fMaster.getFullYear() + "</td>";
	sHTML += "<td class='calendario_BtAvanzaMesDrc' onclick='javascript:_Calendario_AvanzaMes(true)'>>></td>";
	sHTML += "</tr></table>";
	sHTML += "</td>";
	sHTML += "</tr>";

	sHTML += "<tr>";
	sHTML += "<td class='calendario_NombreDia'>Lu</td><td class='calendario_NombreDia'>Ma</td><td class='calendario_NombreDia'>Mi</td><td class='calendario_NombreDia'>Ju</td><td class='calendario_NombreDia'>Vi</td><td class='calendario_NombreDia'>Sa</td><td class='calendario_NombreDia'>Do</td>";
	sHTML += "</tr>";
	
	sHTML += "<tr class='calendario_TrDias'>";
	
	//.s Montando la primera semana
	for (i=1;i<iDiaSemana;i++)
	{
		sHTML += "<td class='calendario_NDia'>&nbsp;</td>";
	}
	
	for (i=iDiaSemana;i<=7;i++)
	{
		sDia	= iDia < 10 ? "0" + iDia : iDia;
		sFecha	= sDia + "/" + sMes + "/" + sYear; 
		
		sHTML += "<td class='calendario_NDia' onclick=\"JavaScript:_Calendario_SelDia('" + sFecha + "')\" style='cursor:pointer'>" + iDia + "</td>";
		iDia++;
	}
	
	//.s. Montando el resto de dias
	
	for (j=0;j<=4;j++)
	{
		sHTML += "<tr class='calendario_TrDias'>";
			for (i=1;i<=7;i++)
			{
				sDia	= iDia < 10 ? "0" + iDia : iDia;
				sFecha	= sDia + "/" + sMes + "/" + sYear; 
		
				sHTML += "<td class='calendario_NDia' onclick=\"JavaScript:_Calendario_SelDia('" + sFecha + "')\" style='cursor:pointer'>" + iDia + "</td>";
				iDia++;		
				if (iDia > iLastDia){ break };
			}
		
		if (iDia > iLastDia){ break };
		sHTML += "</tr>";
	}
	
	sHTML += "</tr>";
	sHTML += "</table>";
	
	//.s. Montando capa
	if (document.getElementById("divCalendario"))
	{
		var oDivCalendario = document.getElementById("divCalendario");
	}
	else
	{	
		var oDivCalendario = document.createElement("DIV");
		document.body.appendChild(oDivCalendario);
		oDivCalendario.style.position	= "absolute";
		oDivCalendario.id				= "divCalendario";
		oDivCalendario.className		= "Calendario_DivMain";
	}
	
	var Punto = _Calendario_Posicion(_Calendario_oDestino)
		oDivCalendario.style.left	= Punto.x + "px";
		oDivCalendario.style.top	= Punto.y + _Calendario_oDestino.offsetHeight + "px";
			
	oDivCalendario.innerHTML = sHTML;
	oDivCalendario.style.display = "";
	
	//.s. Control para que aparezca por encima del edit
	if ((oDivCalendario.offsetHeight + oDivCalendario.offsetTop) >= document.body.scrollHeight)
	{
		oDivCalendario.style.top = oDivCalendario.offsetTop - oDivCalendario.offsetHeight - _Calendario_oDestino.offsetHeight;
	}
	
	oDivCalendario.onmouseout = _Calendario_MouseOut;
	
	return oDivCalendario;
}

/*
+----------------------------------------------------------------------
+ 
+
+----------------------------------------------------------------------
*/

function _Calendario_MouseOut(e)
{
	if (!e) var e = window.event;
	
	//.s. El origen me da lo mismo
	//var oOrigen = (window.event) ? e.srcElement : e.target;
	//if (tg.nodeName != 'DIV') return;
	
	var oDestino = (e.relatedTarget) ? e.relatedTarget : e.toElement;

	while (oDestino != null && oDestino.nodeName != "DIV" && oDestino.nodeName != "BODY" && oDestino.nodeName != "HTML")
		oDestino = oDestino.parentNode
		
	if(oDestino == null)
	{
		document.getElementById("divCalendario").style.display = "none"
		return
	}
	
	if (oDestino.nodeName == "BODY" || oDestino.nodeName == "HTML")
	{
		document.getElementById("divCalendario").style.display = "none"
		return
	}	
	
	//.s. Exista otra capa por ahí	
	if (oDestino.nodeName == "DIV" && oDestino.id != "divCalendario")
	{
		document.getElementById("divCalendario").style.display = "none"
		return
	}
}

/*
+----------------------------------------------------------------------
+ 
+
+----------------------------------------------------------------------
*/

function _Calendario_AvanzaMes(bAvanzar)
{
	if (bAvanzar)
	{
		_Calendario_fMaster = new Date(_Calendario_fMaster.getFullYear(),_Calendario_fMaster.getMonth()+1,1);
		
	} else {
	
		_Calendario_fMaster = new Date(_Calendario_fMaster.getFullYear(),_Calendario_fMaster.getMonth()-1,1);
	}
	
	_Calendario_DoHTML();
}

/*
+----------------------------------------------------------------------
+
+
+----------------------------------------------------------------------
*/

function _Calendario_Posicion(obj)
{
	var Punto = new Object();
	
	Punto.x = 0;
	Punto.y = 0;
	
	while ((obj.tagName != "BODY") && (obj.tagName != "HTML"))
	{
		Punto.x += obj.offsetLeft;
		Punto.y += obj.offsetTop;
		
		obj = obj.offsetParent;
	}
	
	//Punto.x	+= document.body.leftMargin;
	//Punto.y	+= document.body.topMargin;
	
	return Punto;
}

/*
+----------------------------------------------------------------------
+ 
+
+----------------------------------------------------------------------
*/

function _Calendario_StringToDate(sFecha)
{
	iYear	= 2000 + parseFloat(sFecha.substr(6,2));
	iMes	= sFecha.substr(3,2) - 1;
	iDia	= parseFloat(sFecha.substr(0,2))
	
	return new Date(iYear,iMes,iDia);
}

/*
+----------------------------------------------------------------------
+
+
+----------------------------------------------------------------------
*/

//	function Dimensiones(obj)
//	{
//		var Dimesion = new Object();
//		
//		Dimension.width		= obj.offsetWidth;
//		Dimension.height	= obj.height;
//		
//		return Dimension;
//	
//	}

/*
+----------------------------------------------------------------------
+
+
+----------------------------------------------------------------------
*/

function _Calendario_SelDia(sFecha)
{
	_Calendario_oDestino.value = sFecha;
	document.getElementById("divCalendario").style.display = "none"
	
}

/*
+----------------------------------------------------------------------
+
+
+----------------------------------------------------------------------
*/
//.s. 


