
//
//	22/03/10  Se corrige una declaración de Aux
//	28/01/10	Se reescribe de nuevo el objeto
//  27/01/10  Se añade la clase Seleccionado para marcar el día seleccionado y se quita la clase HOY
//	22/01/10	Se Añade la clase Hoy para marcar el día actual
//	21/09/09	Se Cambia la clase y el atributo conActo por conDatos
//  14/09/09 	Se añade la función de marcar dias
//						Se crea un evento por defecto para angendaondia
//

TCalendario._iInstancias = 1;

TCalendario.prototype.Ancho    = '';
TCalendario.prototype.Clase    = 'Calendario';
TCalendario.prototype.Dia      = null;
TCalendario.prototype.Mes      = null
TCalendario.prototype.Fecha    = '';
TCalendario.prototype.MaxFecha = null;
TCalendario.prototype.MinFecha = null;

TCalendario.prototype.onCargarMes   = null;
TCalendario.prototype.onDia         = null;
TCalendario.prototype.onSeleccionar = null;

TCalendario.prototype._iInstancia  = 0;
TCalendario.prototype._oTabla      = null;
TCalendario.prototype._oTBody      = null;


TCalendario.Anyo = function ()
{
	return new Date ().getFullYear ();
}


TCalendario.prototype._Calendario = function ()
{
	var Tr      = document.createElement ('tr');
	var DiaSem  = new Date (this.Mes [2], this.Mes [1] - 1, 1).getDay ();
	var UltDia  = new Date (this.Mes [2], this.Mes [1], 0).getDate ();
	var i       = 1;	
	var d       = 1;
	var j       = 1;

	if (this._oTBody) this._oTabla.removeChild (this._oTBody);
	this._oTBody = document.createElement ('tbody');

	$('idTD' + this._iInstancia + 'Mes', 'innerHTML', TCalendario.MesNombre (this.Mes [1]) + '&nbsp;&nbsp;' + this.Mes [2]);
	if (DiaSem == 0) DiaSem = 7;
	i = this._PreDias (Tr, DiaSem);
	
	while (d <= UltDia)
	{	while (i <= 7)
		{	if (d <= UltDia) this._DiaMes (Tr, d);
			else this._SinDia (Tr, j++);
			d++;
			i++;
		}
		this._oTBody.appendChild (Tr);
		if (d <= UltDia) Tr = document.createElement ('tr');
		i = 1
	}
	this._oTabla.appendChild (this._oTBody);
}


TCalendario.prototype.CargarMes = function (aDias)
{
	var Celda;
	
	for (var i = aDias.length - 1; i >= 0; i--)
	{	if ((Celda = this.Celda (aDias [i])))
		{	if (Celda.getAttribute ('Seleccionado')) Celda.className = 'Seleccionado ConDatos';
			else Celda.className = 'ConDatos';
			Celda.setAttribute ('conDatos', '1');
		}
	}
}


TCalendario.prototype._CargarMes = function ()
{
	if (this.onCargarMes)
	{	if (typeof (this.onCargarMes) == 'function') this.onCargarMes (this);
		else if (typeof (this.onCargarMes == 'string'))
		{	var Ajax = new TAjax ();
			var self = this;
			
			Ajax.Vars.Anyo  = self.Mes [2];
			Ajax.Vars.Mes   = self.Mes [1];
			Ajax.onCargar   = function (oPeticion) { self.CargarMes (oPeticion.asText ().split (',')); }
			Ajax.Cargar (this.onCargarMes);
		}
	}
}


TCalendario.prototype.Celda = function ()
{
	var Aux = $ ('idTD' + this._iInstancia + '-' + arguments [0]);
	var Pars = null;
	
	switch (arguments.length)
	{	case 1: return Aux;
		case 2: return Aux ? Aux [arguments [1]] : null;
		case 3: 
			if (Aux) 
			{	Pars = arguments [1].split ('.');
				for (var i = 0; i < Pars.length - 1; i++) Aux = Aux [Pars [i]];
			  Aux [Pars [i]] = arguments [2]; 
			}
			break;
		default: alert ('$, número de parámetros incorrectos.');
	}
}


TCalendario.prototype._DiaMes = function (oTr, iDia)
{
	var self = this;
	var Td   = document.createElement ('td');

	if (this.onDia) this.onDia (this, Td, iDia)
	else
	{	if (iDia == this.Dia [0] && this.Dia [1] == this.Mes [1] && this.Dia [2] == this.Mes [2])
		{ Td.setAttribute ('Seleccionado', '1');
			Td.className = 'Seleccionado';
		}
		Td.setAttribute ('conDatos', '0');
		Td.innerHTML = iDia;
	}
	Td.id = 'idTD' + this._iInstancia + '-' + iDia;
	if (Td.addEventListener) Td.addEventListener ('click', function (event) { self._onSeleccionar (Td, iDia); }, false);
	else if (Td.attachEvent) Td.attachEvent ('onclick', function () { self._onSeleccionar (Td, iDia); });
	oTr.appendChild (Td);
}


TCalendario.DiaSemana = function (iDia, iMes, iAnyo)
{
	var Fecha = new Date (iAnyo, iMes - 1, iDia);

	return Fecha.getDay () == 0 ? 7 : Fecha.getDay ();
}


TCalendario.prototype._MarcarDia = function (iDia)
{	
	var Td  = null;
	var Aux = null;
	
	if (this.Dia [1] == this.Mes [1] && this.Dia [2] == this.Mes [2])
	{ if ((Td = $ ('idTD' + this._iInstancia + '-' + this.Dia [0])))
		{	Td.setAttribute ('Seleccionado', '');
			Aux = Td.className.split (' ');
			for (var i = Aux.length - 1; i >= 0; i--)	if (Aux [i] == 'Seleccionado') Aux [i] = '';
			Td.className = Aux.join (' ');
		}		
	}	
	this.Dia [0] = iDia;
	this.Dia [1] = this.Mes [1];
	this.Dia [2] = this.Mes [2];
	if ((Td = $ ('idTD' + this._iInstancia + '-' + this.Dia [0])))
	{	Td.setAttribute ('Seleccionado', '1');
		Td.className += ' Seleccionado';
	}
}


TCalendario.prototype._MaxFecha = function ()
{
	if (! this.MaxFecha) return true;
	return new Date (this.Mes [1] > 11 ? this.Mes [2] + 1 : this.Mes [2], this.Mes [1] > 11 ? 0 : this.Mes [1], 
	                 this.MaxFecha.getDate (), this.MaxFecha.getHours (), this.MaxFecha.getMinutes (),
	                 this.MaxFecha.getSeconds (), this.MaxFecha.getMilliseconds () - 1) < this.MaxFecha;
}


TCalendario.Mes = function ()
{
	return new Date ().getMonth () + 1;
}


TCalendario.prototype._MesAnt = function ()
{
	if (this._MinFecha ())
	{	this.Mes [1]--;
		if (this.Mes [1] < 1)
		{	this.Mes [1] = 12;
			this.Mes [2]--;
		}
		this.Mes [0] = TCalendario.UltimoDia (this.Mes [1], this.Mes [2]);
		this._Calendario ();
		this._CargarMes ();
	}
}


TCalendario.prototype._MesSig = function ()
{
	if (this._MaxFecha ())
	{	this.Mes [1]++;
		if (this.Mes [1] > 12)
		{	this.Mes [1] = 1;
			this.Mes [2]++;
		}
		this.Mes [0] = 1;
		this._Calendario ();
		this._CargarMes ();
	}
}


TCalendario.MesNombre = function (iMes)
{
	var aMeses = ['', 'Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio', 'Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre'];
	
	return aMeses [iMes];
}


TCalendario.prototype._MinFecha = function ()
{

	if (! this.MinFecha) return true;
	return new Date (this.Mes [1] > 1 ? this.Mes [2] : this.Mes [2] - 1, this.Mes [1] > 1 ? this.Mes [1] - 2 : 11, 
	                 this.MinFecha.getDate (), this.MinFecha.getHours (), this.MinFecha.getMinutes (),
	                 this.MinFecha.getSeconds (), this.MinFecha.getMilliseconds ()) >= this.MinFecha;
}


TCalendario.prototype._onSeleccionar = function (oTD, iDia)
{
	this.Fecha = (iDia < 10 ? '0' : '') + iDia + '/';
	
	this.Fecha += (this.Mes [1] < 10 ? '0' : '') + this.Mes [1] + '/';
	this.Fecha += this.Mes [2];

	if (this.onSeleccionar && this.onSeleccionar (this, oTD, iDia)) this._MarcarDia (iDia);
	else this.Fecha = '';
}


TCalendario.prototype.PopUp = function (oEvent)
{
	var self = this;
	var Ventana = new TPopUp ();

	if (! oEvent) oEvent = window.event;
	if (oEvent.pageX)	Ventana.XY = [oEvent.pageX, oEvent.pageY];
	else Ventana.XY = [oEvent.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft), 
		                 oEvent.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop)];
	Ventana.Titulo = '';
	Ventana._Show ();
	this.Show (Ventana._oContenedor);
	Ventana._Centrar ();
}


TCalendario.prototype._PreDias = function (oTr, iDiaSem)
{
	var i = 1;

	if (this.Mes [1] > 0) var UltDia  = new Date (this.Mes [2], this.Mes [1] - 1, 0).getDate ();
	else var UltDia  = new Date (this.Mes [2] - 1, 11, 0).getDate ();

	UltDia -= iDiaSem - 2;

	while (i < iDiaSem) { this._SinDia (oTr, UltDia++); i++; }
	return i;
}


TCalendario.PrimerDiaAnyo = function ()
{
	var Fecha = new Date (new Date ().getFullYear (), 0, 1);
	var Dia   = Fecha.getDate ()
	var Mes   = Fecha.getMonth () + 1
	
	return  (Dia < 10 ? '0' + Dia : Dia) + '/' + (Mes < 10 ? '0' + Mes : Mes) + '/' + Fecha.getFullYear ();
}


TCalendario.prototype.setFecha = function (sFecha)
{
	if (typeof (sFecha) != 'undefined')
	{	var Aux = sFecha.split ('/');

		if (Aux [0])
		{	if (Aux [0]) this.Dia [0] = Aux [0].asInteger ();
			if (Aux [1]) this.Dia [1] = Aux [1].asInteger ();
			if (Aux [2]) this.Dia [2] = Aux [2].asInteger ();
		}
	}
}


TCalendario.prototype.Show = function (vIdentificador)
{
	var Contenedor = typeof (vIdentificador) == 'string' ? document.getElementById (vIdentificador) : vIdentificador;

	if (! Contenedor) alert ('Identificador no encontrado (' + vIdentificardor + ')');
	Contenedor.innerHTML = '';
	Contenedor.className = this.Clase; 

	this._oTabla = document.createElement ('table');

	this._oTabla.setAttribute ('align', 'center');
	this._oTabla.setAttribute ('border', '0');
	this._oTabla.setAttribute ('cellPadding', '0');
	this._oTabla.setAttribute ('cellSpacing', '0');
	this._oTabla.setAttribute ('width', '100%');
	this._THead ();
	this._Calendario ();
	Contenedor.appendChild (this._oTabla);
	if (this.Ancho) Contenedor.style.width = this.Ancho;
	this._MarcarDia (this.Dia [0]);	
	this._CargarMes ()
}


TCalendario.prototype._SinDia = function (oTr, iDia)
{
	var Td = document.createElement ('td');
	
	Td.innerHTML = iDia; 
	Td.className = 'SinDia';
	oTr.appendChild (Td);
}


TCalendario.prototype._THead = function ()
{
	var Head = document.createElement ('thead');
	
	this._TopMes (Head);
	this._TopDias (Head);
	this._oTabla.appendChild (Head);
}


TCalendario.prototype._TopDias = function (oHead)
{
	var Tr = document.createElement ('tr');
	var Td = document.createElement ('td');
	
	Tr.className = 'NomDias';
	
	Td.innerHTML = 'Lu';
	Tr.appendChild (Td);

	Td = document.createElement ('td');
	Td.innerHTML = 'Ma';
	Tr.appendChild (Td);

	Td = document.createElement ('td');
	Td.innerHTML = 'Mi';
	Tr.appendChild (Td);

	Td = document.createElement ('td');
	Td.innerHTML = 'Ju';
	Tr.appendChild (Td);
	
	Td = document.createElement ('td');
	Td.innerHTML = 'Vi';
	Tr.appendChild (Td);

	Td = document.createElement ('td');
	Td.innerHTML = 'Sa';
	Tr.appendChild (Td);

	Td = document.createElement ('td');
	Td.innerHTML = 'Do';
	Tr.appendChild (Td);

	oHead.appendChild (Tr);
}


TCalendario.prototype._TopMes = function (oHead)
{
	var self  = this;
	var Tr    = document.createElement ('tr');
	var Td    = document.createElement ('td');
//	var Img   = document.createElement ('img');
	
	Tr.className = 'Mes';
	Td.className = 'MesAnt';
//	Img.src = '/css/imagenes/cal_anterior.gif';
//	Td.appendChild (Img);
	Td.innerHTML = '&lt;';
	if (Td.addEventListener) Td.addEventListener ('click', function (event) { self._MesAnt (); }, false);
	else if (Td.attachEvent) Td.attachEvent ('onclick', function () { self._MesAnt (); });
	Tr.appendChild (Td);

	Td = document.createElement ('td');
	Td.setAttribute ('colSpan', 5);
	Td.innerHTML = TCalendario.MesNombre (this.Mes [1]) + '&nbsp;&nbsp;' + this.Mes [2];
	Td.className = 'Mes';
	Td.id = 'idTD' + this._iInstancia + 'Mes';
	Tr.appendChild (Td);

//	Img = document.createElement ('img');
//	Img.src = '/css/imagenes/cal_siguiente.gif';
	Td = document.createElement ('td');
	Td.className = 'MesSig';
	Td.innerHTML = '&gt;';
	if (Td.addEventListener) Td.addEventListener ('click', function (event) { self._MesSig (); }, false);
	else if (Td.attachEvent) Td.attachEvent ('onclick', function () { self._MesSig (); });
	Tr.appendChild (Td);
	
	oHead.appendChild (Tr);
}


TCalendario.UltimoDia = function (iMes, iAnyo)
{
	// El día 0 de un mes es siempre el ultimo día del mes anterior;
	return new Date(iAnyo, iMes, 0).getDate();
}


function TCalendario (iDia, iMes, iAnyo)
{
	var Fecha = new Date ();

	this.Dia = Array ();
	this.Mes = Array ();
	this._iInstancia = TCalendario._iInstancias++;

	this.Dia [0] = typeof (iDia)  == 'undefined' || ! iDia  ? Fecha.getDate ()      : iDia;
	this.Dia [1] = typeof (iMes)  == 'undefined' || ! iMes  ? Fecha.getMonth () + 1 : iMes;
	this.Dia [2] = typeof (iAnyo) == 'undefined' || ! iAnyo ? Fecha.getFullYear ()  : iAnyo;

	this.Mes [0] = this.Dia [0];
	this.Mes [1] = this.Dia [1];
	this.Mes [2] = this.Dia [2];
}


////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////

