objCalendario.Registry = [];
objCalendario.hoy = new Date();
objCalendario.arrMes=new Array("Enero","Febrero","Marzo","Abril","Mayo","Junio","Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre");

function objCalendario(strNombre)
{
	this.fecha=new Date();
	this.fechaActual=new Date();
	this.nombre=strNombre;
	this.eventoClick="";
	objCalendario.Registry[strNombre] = this;

	this.actualiza = function()
	{
		if (this.fecha.getFullYear()<100) {this.fecha.setYear(this.fecha.getFullYear()+2000);}
		begin_day = new Date (this.fecha.getFullYear(),this.fecha.getMonth(),1);
		begin_day_date = begin_day.getDay();
		end_day = new Date (this.fecha.getFullYear(),this.fecha.getMonth()+1,1);
		count_day = (end_day - begin_day)/1000/60/60/24;
		this.generaTabla(begin_day_date,Math.round(count_day));
	}

	this.generaTabla = function(begin,count)
	{
		this.iniciaTabla();
		j=0;
		if (begin!=0){i=begin-1;}else{i=6}
		for (c=1;c<count+1;c++)
		{
		    
		    colum_name = eval("cal_" + strNombre + "d"+i+"r"+j);
			if (i==5 || i==6) {strEstilo="cal_fes_txt"}
			else {strEstilo="cal_dia_txt"}
			if ((objCalendario.hoy.getDate() == c)&&(this.fecha.getMonth() == objCalendario.hoy.getMonth())&&(this.fecha.getFullYear() == objCalendario.hoy.getFullYear())) {strEstilo="cal_hoy_txt";};
			if ((this.fechaActual.getDate() == c)&&(this.fecha.getMonth() == this.fechaActual.getMonth())&&(this.fecha.getFullYear() == this.fechaActual.getFullYear())) {strEstilo="cal_act_txt";};
			colum_name.className =strEstilo;
			if (!document.all) {colum_name.textContent = c;} //firefox
			else {colum_name.innerText = c;} // Internet explorer
			i++;
			if (i==7){i=0;j++;}
		}
		if (!document.all)
		{//firefox
		    colum_name = eval("cal_" + strNombre + "_periodo");
		    colum_name.textContent=this.fecha.getFullYear();
		    colum_name = eval("cal_" + strNombre + "_mes");
		    colum_name.textContent=objCalendario.arrMes[this.fecha.getMonth()];
		}
	    else
		{// Internet explorer
		    colum_name = eval("cal_" + strNombre + "_periodo");
		    colum_name.innerText=this.fecha.getFullYear();
		    colum_name = eval("cal_" + strNombre + "_mes");
		    colum_name.innerText=objCalendario.arrMes[this.fecha.getMonth()];
		}
	    	
	}

	this.iniciaTabla = function()
	{
		for(j=0;j<6;j++)
		{
			for(i=0;i<7;i++)
			{
				colum_name = eval("cal_" + strNombre +"d"+i+"r"+j);
				if (!document.all) {colum_name.textContent = "";} //firefox
				else  {colum_name.innerText = "";} // Internet explorer
				colum_name.style.backgroundColor ="";
				colum_name.style.color ="";
				colum_name.className = "cal_dia_txt";
			}
		}
	}
	
	this.leeFechaActual = function()
	{
		return (this.fechaActual.getDate() + "/" + (this.fechaActual.getMonth()+1) + "/" + this.fechaActual.getFullYear())
	}
}

objCalendario.ponMesAnterior = function(strNombre)
{
	var obj = objCalendario.Registry[strNombre];
	
	if (obj.fecha.getMonth()==0)
	{
		obj.fecha.setMonth(11);
		obj.fecha.setYear(obj.fecha.getFullYear()-1);
	}
	else
	{
		obj.fecha.setMonth(obj.fecha.getMonth()-1);
	}
	obj.actualiza();
}

objCalendario.ponMesSiguiente = function(strNombre)
{
	var obj = objCalendario.Registry[strNombre];
	
	if (obj.fecha.getMonth()==11)
	{
		obj.fecha.setMonth(0);
		obj.fecha.setYear(obj.fecha.getFullYear()+1);
	}
	else
	{
		obj.fecha.setMonth(obj.fecha.getMonth()+1);
	}
	obj.actualiza();
}

objCalendario.ponPeriodoAnterior = function(strNombre)
{
	var obj = objCalendario.Registry[strNombre];
	
	obj.fecha.setYear(obj.fecha.getFullYear()-1);
	obj.actualiza();
}

objCalendario.ponPeriodoSiguiente = function(strNombre)
{
	var obj = objCalendario.Registry[strNombre];
	
	obj.fecha.setYear(obj.fecha.getFullYear()+1);
	obj.actualiza();
}

objCalendario.ponFechaActualCelda = function(objCelda, strNombre)
{
	var obj = objCalendario.Registry[strNombre];
    obj.fechaActual.setYear(obj.fecha.getFullYear());
    obj.fechaActual.setMonth(obj.fecha.getMonth());
    //if (!document.all)  {obj.fechaActual.setDate(parseInt(objCelda.textContent));} //firefox
	//else {obj.fechaActual.setDate(parseInt(objCelda.innerText));} //IE
    if (!document.all)  {obj.fechaActual.setDate(eval(objCelda.textContent));} //firefox
	else {obj.fechaActual.setDate(eval(objCelda.innerText));} //IE
	obj.actualiza();
	if (obj.eventoClick!="") {	eval(obj.eventoClick); };
}

objCalendario.ponFechaActual = function(strNombre, strFecha)
{
	var obj = objCalendario.Registry[strNombre];
	var intPos, intPos2,intDia,intMes,intPeriodo;
	var strFechaActual=strFecha;
	intDia=objCalendario.hoy.getDate();
	intMes=objCalendario.hoy.getMonth();
	intPeriodo=objCalendario.hoy.getFullYear();
	if (strFechaActual)
	{
		intPos=0;
		intPos2=strFechaActual.indexOf('/');
		if (intPos2>=0)
		{
		    intDia=strFechaActual.substr(intPos, intPos2);
			if (intDia>31) {intDia=31};
			if (intDia<=1) {intDia=1};
			intPos=intPos2+1; intPos2=strFechaActual.indexOf('/', intPos);
			if (intPos2>=0)
			{
				//NO FUNCIONA LA TRANSFORMACION A MES CON "parseInt", así que usamos "eval"
				//intMes=parseInt(strFechaActual.substr(intPos, intPos2-intPos))-1;
				intMes=eval(strFechaActual.substr(intPos, intPos2-intPos))-1;
				if (intMes>11) {intMes=11};
				if (intMes<0) {intMes=0};
				intPos=intPos2+1; intPos2=strFechaActual.length;
				if (intPos2>=0)
				{
					//intPeriodo=parseInt(strFechaActual.substr(intPos, intPos2-intPos));
					intPeriodo=eval(strFechaActual.substr(intPos, intPos2-intPos));
					if (intPeriodo>2100) {intPeriodo=2100};
					if (intPeriodo<1) {intPeriodo=1};
					if (intPeriodo<100) {intPeriodo+=2000};
				}
			}
		}
	}
	obj.fecha.setDate(intDia);
	obj.fechaActual.setDate(intDia);
	obj.fecha.setMonth(intMes);
	obj.fechaActual.setMonth(intMes);
	obj.fecha.setYear(intPeriodo);
	obj.fechaActual.setYear(intPeriodo);
	obj.actualiza();
}

objCalendario.leeFechaActual = function(strNombre)
{
	var obj = objCalendario.Registry[strNombre];
	return (obj.fechaActual.getDate() + "/" + (obj.fechaActual.getMonth()+1) + "/" + obj.fechaActual.getFullYear());
}