
TCalendario._iInstancias = 1;


TCalendario.prototype.Ancho    = '';
TCalendario.prototype.Depurar  = true;
TCalendario.prototype.Clase    = 'Calendario';
TCalendario.prototype.MaxFecha = null;
TCalendario.prototype.MinFecha = null;


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

TCalendario.prototype._iAnyo       = 0;
TCalendario.prototype._iMes        = 0;
TCalendario.prototype._iInstancia  = 0;
TCalendario.prototype._bShow       = false;
TCalendario.prototype._oTabla      = null;
TCalendario.prototype._oTBody      = null;




TCalendario.prototype._DiaMes = function (oTr, iDia)
{
	var self = this;
	var Td   = document.createElement ('td');
	
	if (this.onDia) this.onDia (this, Td, iDia)
	else 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.prototype._Dias = function ()
{
	var Tr      = document.createElement ('tr');
	var DiaSem  = new Date (this._iAnyo, this._iMes - 1, 1).getDay ();
	var UltDia  = new Date (this._iAnyo, this._iMes, 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', this._NomMes ());
	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);
	if (this._bShow && this.onShow) this.onShow (this);
}


TCalendario.prototype.getAnyo = function () { return this._iAnyo; }
TCalendario.prototype.getMes  = function () { return this._iMes; }


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


TCalendario.prototype._MesAnt = function ()
{
	if (this._MinFecha ())
	{	this._iMes--;
		if (this._iMes < 1)
		{	this._iMes = 12;
			this._iAnyo--;
		}
		this._Dias ();
	}
}


TCalendario.prototype._MesSig = function ()
{
	if (this._MaxFecha ())
	{	this._iMes++;
		if (this._iMes > 12)
		{	this._iMes = 1;
			this._iAnyo++;
		}
		this._Dias ();
	}
}


TCalendario.prototype._MinFecha = function ()
{

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


TCalendario.prototype._NomMes = function ()
{
	switch (this._iMes)
	{	case  1: return 'Enero ' + this._iAnyo;
		case  2: return 'Febrero ' + this._iAnyo;
		case  3: return 'Marzo ' + this._iAnyo;
		case  4: return 'Abril ' + this._iAnyo;
		case  5: return 'Mayo ' + this._iAnyo;
		case  6: return 'Junio ' + this._iAnyo;
		case  7: return 'Julio ' + this._iAnyo;
		case  8: return 'Agosto ' + this._iAnyo;
		case  9: return 'Septiembre ' + this._iAnyo;
		case 10: return 'Octubre ' + this._iAnyo;
		case 11: return 'Noviembre ' + this._iAnyo;
		case 12: return 'Diciembre ' + this._iAnyo;
	}
	return this._iAnyo;
}


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

	if (this._iMes > 0) var UltDia  = new Date (this._iAnyo, this._iMes - 1, 0).getDate ();
	else var UltDia  = new Date (this._iAnyo - 1, 11, 0).getDate ();

	UltDia -= iDiaSem - 2;

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


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

	if (! Contenedor && this.Depurar) 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._Dias ();
	Contenedor.appendChild (this._oTabla);
	if (this.Ancho) Contenedor.style.width = this.Ancho;
	this._bShow = true;
	if (this.onShow) this.onShow (this);
}


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 = this._NomMes ();
	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);
}


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

	this._iInstancia = TCalendario._iInstancias++;

	if (typeof (iMes) == 'undefined') iMes = Fecha.getMonth () + 1;
	if (typeof (iAnyo) == 'undefined') iAnyo = Fecha.getFullYear ();
	this._iMes  = iMes;
	this._iAnyo = iAnyo;
}


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


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._onSeleccionar = function (oTD, iDia)
{
	if (this.onSeleccionar) this.onSeleccionar (this, oTD, iDia);
}


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.Fecha = function (iDia)
{
	var Result = '';
	
	if (iDia < 10) Result += '0' + iDia + '/';
	else Result += iDia + '/';
	if (this.getMes () < 10) Result += '0' + this.getMes () + '/';
	else Result += this.getMes () + '/';
	Result += this.getAnyo ();
	return Result;
}


TCalendario.prototype.setFecha = function (sFecha)
{
	var Aux = sFecha.split ('/');
	
	if (Aux [1]) this._iMes  = Aux [1].asInteger ();
	if (Aux [2]) this._iAnyo = Aux [2].asInteger ();
}


