function FechaCorta(fecha)
{
	var dia = fecha.getDate();
	var texto = (dia < 10 ? '0' + dia : '' + dia);
	var mes = fecha.getMonth() + 1;
	texto += '/' + (mes < 10 ? '0' + mes : mes);
	texto += '/' + fecha.getFullYear();
	return texto;
}

function NombreMes(fecha)
{
	var nombreMes = new Array('enero', 'febrero', 'marzo', 'abril', 'mayo', 'junio', 'julio', 'agosto', 'septiembre', 'octubre',
		'noviembre', 'diciembre');
	return nombreMes[fecha.getMonth()];
}

function FechaLarga(fecha)
{
	var diaSemana = new Array('domingo', 'lunes', 'martes', 'miércoles', 'jueves', 'viernes', 'sábado');
	var texto = diaSemana[fecha.getDay()] + ', ' + fecha.getDate() + ' de ' + NombreMes(fecha) + ' de ' + fecha.getFullYear();
	return texto;
}

function FechaHora(fecha)
{
	var texto = FechaLarga(fecha);
	var hora = fecha.getHours();
	texto += ' a las ' + (hora < 10 ? '0' + hora : hora);
	var min = fecha.getMinutes();
	texto += ':' + (min < 10 ? '0' + min : min);
	var seg = fecha.getSeconds()
	texto += ':' + (seg < 10 ? '0' + seg : seg);
	return texto;
}

function UltimaModif()
{
	var fecha = new Date(document.lastModified);
	document.write('<small><i>Última modificación de esta página: ' + FechaLarga(fecha) + '</i><br>');
	document.write(' Para volver a la página anterior, haz clic <a href="javascript:history.go(-1)">aquí</a></small><br>');
}

function FechaDif(desde, hasta)
{
	var difDias = Math.floor((hasta.valueOf() - desde.valueOf()) / 86400000);
	return difDias;
}


function MostrarObjeto(objeto)
{
	var prop;
	var texto = "";
	for (prop in objeto)
		texto += prop + '=' + objeto[prop] + '<br>';
	return texto;
}

function Calendario(tituloCalendario, desdeFecha, hastaFecha)
{
	this.titulo = tituloCalendario;
	desdeFecha -= desdeFecha.getMilliseconds() + desdeFecha.getSeconds() * 1000 - desdeFecha.getMinutes() * 60000 - desdeFecha.getHours() * 3600000;
	this.desde = new Date(desdeFecha);
	hastaFecha -= hastaFecha.getMilliseconds() + hastaFecha.getSeconds() * 1000 - hastaFecha.getMinutes() * 60000 - hastaFecha.getHours() * 3600000;
	this.hasta = new Date(hastaFecha);
	this.fechas = new Array();
	this.eventos = new Array();
	this.textos = new Array();
	this.enlaces = new Array();
	this.festivos = new Array();
	this.AnadirEvento = Calendario_Evento;
	this.AnadirFestivo = Calendario_Festivo;
	this.Mostrar = Calendario_Mostrar;
}

function Calendario_Evento(dia, mes, anyo, tituloEvento, texto, enlace)
{
	var fecha = new Date(anyo, mes - 1, dia);
	with(this) {
		var i;
		for(i = fechas.length - 1; i >= 0 && fechas[i] > fecha; --i) {
			fechas[i + 1] = fechas[i];
			eventos[i + 1] = eventos[i];
			textos[i + 1] = textos[i];
		}
		++i;
		fechas[i] = fecha;
		eventos[i] = tituloEvento;
		textos[i] = texto;
		enlaces[i] = enlace
	}
}

function Calendario_Festivo(dia, mes, anyo)
{
	var fecha = new Date(anyo, mes - 1, dia);
	with(this) {
		var i;
		for(i = festivos.length - 1; i >= 0 && festivos[i] > fecha; --i) {
			festivos[i + 1] = festivos[i];
		}
		++i;
		festivos[i] = fecha;
	}
}


function Calendario_Mostrar()
{
	var f, i, mes, anyo, col, ds, j, tits, tiene, linea, ifest, tdia, hoy;
	with(this) {
		document.writeln('<div align="center">');
		document.writeln('<table border="1" cellpadding="5" cellspacing="0" width="100%">');
    	document.writeln('<tr>');
    	document.writeln('<td width="100%" colspan="3">');
        document.writeln('<a name="calendario"></a><h2 align="center">' + titulo + '</h2>');
      	document.writeln('</td></tr>');
		i = 0;
		mes = -1;
		col = 3;
		ifest = 0;
		j = new Date();
		hoy = new Date(j.getFullYear(),j.getMonth(),j.getDate(),0,0,0,0);
		for(f = new Date(desde); f <= hasta; f.setDate(f.getDate() + 1)) { 
			if (f.getMonth() != mes) {
				// PEND: Finalizar el mes en curso
				if (mes != -1) {
					if (ds != 0) {
						for(j = ds; j <= 6; ++j)
							document.writeln('<td width="' + (ds < 5 ? 14 : 15) + '%" align="center">&nbsp;</td>');
					}
					document.writeln('</table>');
					document.writeln('</div>');
					document.writeln('</td>');
				}
				if (col == 3) {
					if (mes != -1) document.write('</tr>');
					col = 1;
					document.write('<tr>');
				} else {
					++col;
				}
				mes = f.getMonth();
				document.writeln('<td width="33%" valign=top><div align="center">');
				document.writeln('<b>' + NombreMes(f).toUpperCase() + ' ' + f.getFullYear() + '</b>');
				document.writeln('<table border="1" cellpadding="0" cellspacing="0" width="100%">');
				document.writeln('<tr>');
				document.writeln('<td width="14%" align="center">L</td>');
				document.writeln('<td width="14%" align="center">M</td>');
				document.writeln('<td width="14%" align="center">X</td>');
				document.writeln('<td width="14%" align="center">J</td>');
				document.writeln('<td width="14%" align="center">V</td>');
				document.writeln('<td width="15%" align="center"><font color="#FF0000">S</font></td>');
				document.writeln('<td width="15%" align="center"><font color="#FF0000">D</font></td>');
				document.writeln('</tr><tr>');
				ds = f.getDay();
				if (ds == 0) ds = 7;
				for(j = 1; j < ds; ++j)
					document.writeln('<td width="14%" align="center">&nbsp;</td>');
			}
			tiene = false;
			tits = "";
			anch = ""
			ds = f.getDay();
			while (i < fechas.length && fechas[i].valueOf() == f.valueOf()) {
				if (tits.length > 0) tits += ', ';
				tits += eventos[i];
				if (!tiene) anch = '#e' + f.getFullYear() + '_' + f.getMonth() + '_' + f.getDate();
				tiene = true;
				++i;
			}
			if (ds == 1) 
				document.writeln('<tr>');
			document.write('<td width="' + ((ds == 0 || ds == 6) ? 15 : 14 ) + '%" align="center" title="' + tits +'" ' 
				+ (f.valueOf() == hoy.valueOf() ? ' bgcolor="#9999CC"' : '') + '>');
			tdia = "l";
			if (ifest < festivos.length && festivos[ifest].valueOf() == f.valueOf()) {
				++ifest;
				tdia = "f";
			} else if (ds == 0) {
				tdia = "f";
			} else if (ds == 6) {
				tdia = "s";
			}
			if (tiene) { 
				tdia = "e";
				document.write('<a href="' + anch + '">');
			}
			if (tdia == 'f' || tdia == 'e') document.write('<b>');
			switch(tdia) {
			case 'e': document.write('<font color="#FFFF00">'); break;
			case 'f': case 's': document.write('<font color="#FF0000">'); break;
			}
			document.write(f.getDate());
			if (tdia != "l") document.write('</font>');
			if (tdia == 'f' || tdia == 'e')	document.write('</b>');
			if (tiene) document.write('</a>');
			document.writeln('</td>');
			if (ds == 0)
				document.writeln('</tr>');
		}
		if (mes != -1) {
			if (ds != 0) {
				for(j = ds; j <= 6; ++j)
					document.writeln('<td width="' + (ds < 5 ? 14 : 15) + '%" align="center">&nbsp;</td>');
			}
			document.writeln('</table>');
			document.writeln('</div>');
			document.writeln('</td>');
			document.writeln('</tr>');
		}
		document.writeln('</table>');
		document.writeln('</div>');
		document.writeln('<p align="center"><small>NOTA: marcados los <b><u><font color="#FFFF00">acontecimientos</font></u></b>, '
			+ '<b><font color ="#FF0000">festivos</font></b> y la fecha de <span style="background-color: #9999CC">hoy</span>.</small></p>');
		for(i = 0; i < fechas.length; ++i) {
			linea = '<b>'
			if (f != fechas[i]) {
				anch = 'e' + fechas[i].getFullYear() + '_' + fechas[i].getMonth() + '_' + fechas[i].getDate();
				linea += '<a name="' + anch + '">';
			} 
			linea += FechaLarga(fechas[i]).toUpperCase();
			linea += ': ' + eventos[i].toUpperCase();
			if (f != fechas[i])
				linea += '</a>';
			linea += '</b><br>';
			document.writeln(linea);
			document.write(textos[i]);
			if (enlaces[i]) document.write('<br>Para más información, haz click <a href="' + enlaces[i] + '" target="_self">aquí</a>.');
			document.writeln('<br><small><a href="#calendario">Volver al calendario</a></small></p>');
			f = fechas[i];
		}
	}
}

function ventana_imagen(tit, imag)
{
	var s, i, ancho, alto;
	
	i = new Image();
	i.src = imag;
	ancho = i.width + 30;
	alto = i.height + 80;
	w_imagen = window.open('', 'Imagen', 'toolbar=no,location=no,directories=no,width='+ancho+', height='+alto+',resizable=yes,scrollbars=yes');
	w_imagen.document.open();
	s = '<html><title>' + tit + '</title>';
	s += '<body bgcolor="#000066"><img src="' + imag + '" alt=" ' + imag + ' no disponible"><br>';
	s += '<form><p align="center"><input type="button" name="imprimir" value="Imprimir" onclick="window.print()">&nbsp;' +
		'<input type="button" name="cerrar" value="Cerrar" onclick="window.close()">' +
		'</form>';
	s += '</body></html>';
	w_imagen.document.write(s);
	w_imagen.document.close();
	delete(i);
}

var caution = false;
function setCookie(name, value, expires, path, domain, secure) {
	var curCookie = name + "=" + escape(value) +
		((expires) ? "; expires=" + expires.toGMTString() : "") +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		((secure) ? "; secure" : "")
	if (!caution || (name + "=" + escape(value)).length <= 4000)
		document.cookie = curCookie
	else
		if (confirm("Cookie exceeds 4KB and will be cut!"))
			document.cookie = curCookie
}

function getCookie(name) {
	var prefix = name + "="
	var cookieStartIndex = document.cookie.indexOf(prefix)
	if (cookieStartIndex == -1)
		return null
	var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length)
	if (cookieEndIndex == -1)
		cookieEndIndex = document.cookie.length
	return unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex))
}

function deleteCookie(name, path, domain) {
	if (getCookie(name)) {
		document.cookie = name + "=" +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		"; expires=Thu, 01-Jan-70 00:00:01 GMT"
	}
}

// Fin