// Objeto calendario.

var ns, ie;

ns = (document.layers)? true:false
ie = (document.all)? true:false

function calendario(nombre, texto, x, y, fondo) {
	this.mostrar = crearCalendario;
	this.esconder = esconderCalendario;
	this.sig = mesSiguiente;
	this.ant = mesAnterior;
	this.fondo = fondo || "#CCCC66";   // Color del fondo	
	
	//if (ns) { this.capa = "l" + this.capa; }
	
	document.write ("<style>A:link {text-decoration: none; color: #666666; font-family: Arial} A:visited {text-decoration: none; color: #666666; font-family: Arial} A:active {text-decoration: none; color: #666666;font-family: Arial} A:hover {text-decoration: UNDERLINE; color: #000033};} .calendarioBorde {background-color:#eaeaea} td.calendario {background-color:#eaeaea;}; td.calendarioBlanco {background-color:white}</style><DIV ID='capa" + nombre + "' STYLE='POSITION: absolute; TOP:" +y + "; LEFT:"+ x+"'></DIV>");
	
	this.capa = "capa" + nombre;
	this.name = nombre;	
	this.texto = texto;		
	this.fecha = new Date();
	
}

function escribeCapa (id, texto) {
	if (ns) {
		var lyr =  document.layers[id].document;
		lyr.open()
		lyr.write(texto)
		lyr.close()
	}
	else if (ie) document.all[id].innerHTML = texto
}

function crearCalendario () {
	var texto ="";
	texto += '<table border=0 width=150 height=105 STYLE="BACKGROUND-COLOR:"#CCCC66" cellspacing="0" cellpadding="0">';
	texto += '<tr>' + 
			  	'<td width="100%" class=calendarioBorde ><center>' + 
			  	 '<table border="0" width="110"  bgcolor="'+ this.fondo +'" cellspacing="0" cellpadding="0">' +
          			'<tr>' + 
            			'<td width="100%" colspan="7" class=calendarioBorde >';

	texto += cabecera(this.fecha, this.name, this.fondo);
	texto += mes(this.name, this.fecha, this.texto);			
	texto += "</td></tr></table>";		
	texto += "</td></tr></table>";		
	escribeCapa(this.capa, texto);
	if (ns) document.layers[this.capa].visibility = "show";
	//alert(this.fondo);
	if (ie) document.all(this.capa).style.visibility = "visible";
}

function esconderCalendario () {
//	escribeCapa(this.capa, "");
	if (ns) document.layers[this.capa].visibility = "hide";
	if (ie) document.all(this.capa).style.visibility = "hidden";
}

function mesAnterior () { 
	this.fecha.setMonth(this.fecha.getMonth() - 1);
	this.mostrar();
}

function mesSiguiente () { 
	this.fecha.setMonth(this.fecha.getMonth() + 1);
	this.mostrar();
}

function cabecera (fecha, name, fondo) {
	var meses = new Array("January","February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");	
	var texto ="";
	var mes = meses[fecha.getMonth()];	
	var ano = fecha.getYear();	
	if (ano < 1900) { ano += 1900 };
	texto += '<table border="0" width="145" bgcolor="'+ fondo +'" cellspacing="0" cellpadding="0">' +
			  '<tr>' + 
				'<td width=100% colspan="7" class=calendario >' + 
					'<table border="0" width="100%">' + 
						'<tr>' + 
							'<td class=calendario width="5%" align="center"><a class=enlace href=\"javascript:'+name+'.ant();\"><font face="Verdana" size="1" color="#E62324"><b>&laquo;</b></font></a></td>' + 
							'<td class=calendario width="90%" align="center"><font face="Verdana" size="1" color="#E62324"><b>' + mes + ' ' + ano + '</b></font></td>' +
							'<td class=calendario width="5%" align="center"><a class=enlace href=\"javascript:'+name+'.sig();\"><font face="Verdana" size="1" color="#E62324"><b>&raquo;</b></font></a></td>' + 
						'</tr>' + 
					'</table>' +
				'</td>' +
			  '</tr></table>';
	return texto;
}



function mes (id, fecha, nombre) {
// Imprime la sección correspondiente a los días del mes
	var dias = new Array('Lu','Ma','Mi','Ju','Vi','Sa','Do');	
	var monthlength = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
	var texto = "";
	texto += '<tr bgcolor=white>';
	for (i = 0; i < dias.length; i++) {
		texto += "<td class=calendarioBlanco width=10 height=10 align=center ><font face=verdana size=1 color=#CC0000>" + dias[i] + "</font></td>";
	}	
    texto += '</tr>';                 
 	var mes = fecha.getMonth(); 	
 	fecha.setDate(1);
 	var dia = fecha.getDay();
 	dia--;
 	if (dia <0)  dia=6; 	 	
    texto += '<tr>';
    col = dia;    
    for (i = 0; i < col; i++) {
    	texto += "<td class=calendarioBlanco  width=10 height=10 align=center><font face=verdana size=1 color=#FFFFFF>&nbsp;</font></td>";
    }
    var ano = fecha.getYear();
    if (ano < 1900) { ano += 1900 };
    for (i = 1; i <= monthlength[mes] ; i++) {    	
    	if (col == 7) { texto +="</tr><tr>"; col = 0;}
    	col++;
    	texto += "<td class=calendarioBlanco  width=10 height=10 align=center><a href=\"javascript:escoger("+nombre+",'" + i + "/" + Number(mes+1)  + "/" + ano + "'); " + id +".esconder();\"><font face=verdana size=1 color=#000000>" + i + "</font></a></td>";
    }   
    for (i = col; i < 7; i++) { texto += "<td class=calendarioBlanco  width=10 height=10 align=center><font face=verdana size=1 color=#FFFFFF>&nbsp;</font></td>"; }
    texto += '</tr>';
    return texto;
	
}

function escoger(id, fecha) {
	id.value = fecha;
}	