var nuevo_boton = false;
var abiertos	= [0, 0, 0, 0, 0, 0, 0, 0];
var abierto	= 0;
var actividad	= [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
var boton_auxiliar = "";
var validez = 30; //***
var caduca = new Date();  //***
caduca.setTime(caduca.getTime() + (validez*24*60*60*1000)); //**

var _cursor = (document.all) ? "hand" : "pointer";
var colores = [
	"#ffff00", "#ff0000",
	"#c0c0c0", "#000000",
	"#add8e6", "#000000",
	"#90ee90", "#000000"
	];

function inicio()	{
	for (var i = 48; i < 141; i ++) {
	eval("document.getElementById('b" + i + "').style.cursor=\"" + _cursor + "\"");
	}
	actualizar();
	if (location.protocol == "http:")	{
		var colos = leeCookie('Colores');
		if (colos != null) colores = colos.split(",");
	}
	ponColores();
}

<!-------------------      funciones          -------------------->

var modulos = [false, false, false, false, false, false];
var ventanas = [false, false, false, false, false, false];
var pagina = ["griego.html", "flechas.html", "conjuntos.html", "color.html", "Letras.html", "config.html"];
var ancho = [700, 300, 500, 600, 800, 300];
var alto = [50, 50, 150, 350, 300, 250];

function leeCookie(name){
    var cname = name + "=";               
    var dc = document.cookie;             
    if (dc.length > 0) {              
      begin = dc.indexOf(cname);       
      if (begin != -1) {           
        begin += cname.length;       
        end = dc.indexOf(";", begin);
        if (end == -1) end = dc.length;
          return unescape(dc.substring(begin, end));
      } 
    }
    return null;
  }

function escribeCookie(name, value, expires) {
  document.cookie = name + "=" + escape(value) + 
    ((expires == null) ? "" : "; expires=" + expires.toGMTString()); 
}

function modulo (i)	{
if (!modulos[i]) {
	var datos = "width=" + ancho[i] + ",height=" + alto[i];
	var pag = pagina[i];
	ventanas[i] = window.open(pag, "", datos);
	modulos[i] = true;
	}
	else {
	ventanas[i].close();
	modulos[i] = false;
	}
}

function no_popup(n)	{
	modulo(n);
}

function fin()	{
	for (var i = 0; i < modulos.length; i ++)
		if (modulos[i]) ventanas[i].close();
}

function actualizar()	{
	if (!nuevo_boton)
	document.getElementById("cadena").innerHTML = document.getElementById("texto").value;
	document.getElementById("texto").focus();
	if (codigoAbierto)
		verCodigo.document.getElementById("codigo").innerHTML = document.getElementById("texto").value;
}

var leyenda	= ["superíndice", "subíndice","Lista numerada", "Lista sin numerar", "línea de lista", "tabla", 
	"línea de tabla", "celda", "grande", "pequeño", "cursiva", "negrita", "subrayado", "tachado", "cita", "centrado"];
var vacio	= "No hay etiquetas abiertas";
var	barra	= ["</", "<" ];
var _activos	= ["sup>", "sub>", "ol>", "ul>", "li>", "table>", "tr>", "td>", 
		"big>", "small>", "i>", "b>", "u>", "s>", "cite>", "center>"];

function pon(x)	{
	actividad[x] ++; 
	abiertos[abierto++] = x; 
	document.getElementById("saca").className = "boton";
	document.getElementById("saca").innerHTML = "cerrar " + leyenda[x];
	document.getElementById("texto").value += "<" + _activos[x];
	actualizar();
}

function saca(x)	{
	actividad[x] --; 
	//abiertos[abierto] = x; 
	if (abierto == 0) document.getElementById("saca").className = "normal";
	document.getElementById("texto").value += "</" + _activos[x];
	document.getElementById("saca").innerHTML = (abierto == 0) ? vacio : "cerrar " + leyenda[abiertos[abierto - 1]];
	actualizar();
}

function sacador()	{
	if (abierto > 0) saca(abiertos[--abierto]);
}

function poner(x)	{ //*** Modificado por el amigo JavierB
	valorFinal=document.getElementById("texto").value;
	valorFinal += x;
	valor=document.getElementById("texto").value;
	if ((valor.substr(valor.length-7)=="</font>") && (x.substr(0,1)=="<")){
		pos=valor.lastIndexOf("font face=");
		if (pos>0) {
			tipo=x.substr(12,5);
			cad=valor.substr(pos+11,5);
			if (cad==tipo) {
				x=x.substring(x.indexOf("&"),x.indexOf(";")+1);
				cad=valor.substr(0,valor.length-7);
				valorFinal=cad+ x +"</font>";
			}
		}
	}
	document.getElementById("texto").value = valorFinal;
	actualizar();
}

function poner_alfa(x)	{
	document.getElementById("texto").value += "&" + x;
	actualizar();
}

function simbolo(x)	{ //***Modificado por el amigo JavierB
	valorFinal=document.getElementById("texto").value;
	valorFinal += "<font face='symbol'>&#" + x + "</font>";
	valor=document.getElementById("texto").value;
	if (valor.substr(valor.length-7)=="</font>") {
		pos=valor.lastIndexOf("font face=");
		if (pos>0) {
			cad=valor.substr(pos+11,6);
			if (cad=="symbol") {
				cad=valor.substr(0,valor.length-7);
				valorFinal=cad+"&#" + x +"</font>";
			}
		}
	}
	document.getElementById("texto").value = valorFinal;
	actualizar();
}

function sub(x)	{
	document.getElementById("texto").value += "<sub>" + x + "</sub>";
	actualizar();
}

function sup(x)	{
	document.getElementById("texto").value += "<sup>" + x + "</sup>";
	actualizar();
}

function pegar()		{ // pega en la posición del cursor el contenido del portapapeles
	document.getElementById("texto").focus();
	document.execCommand("paste");
	actualizar();
}

function copiar()		{ // en construcción... debería copiar sólo lo seleccionado
	document.getElementById("texto").select();
	document.execCommand("copy");
	document.getElementById("texto").focus();
}

function borrar()		{
	document.getElementById("texto").value = "";
	actualizar();
}

function seleccionar()		{ // copia el contenido completo de la ventana al portapapeles
	document.getElementById("texto").select();
	document.execCommand("copy");
	document.getElementById("texto").focus();
}

var entrada;

function sumar_boton()		{
	if (nuevo_boton)	alert("¡Listillo!");
	else	{
		nuevo_boton = true;
		_entrada = document.getElementById("cadena").innerHTML;
		_estado  = document.getElementById("texto").value;
		document.getElementById("texto").value = "";
		var salida  	= "<table width=100% ><tr>";
		salida		+= "<td class=normal >Introduzca leyenda:</td>";
		salida		+= "<td class=contenedor ><input onblur= texto.focus() type=text id=nuevo /></td>";
		salida		+= "<td style=cursor:" + _cursor + " class=boton onclick=macro() >Aceptar</td>";
		salida		+= "<td style=cursor:" + _cursor + " class=boton onclick=restaurar() >cancelar</td>";
		salida		+="</tr></table>";
		document.getElementById("cadena").innerHTML = salida;
		document.getElementById("nuevo").focus();
	}
}

var macros = new Array(60);
function	pon_macro(x){
	poner(macros[x]);
};

function macro()	{
	el_boton(true, 
		document.getElementById("nuevo").value, 
		document.getElementById("texto").value);
	restaurar();
};

function restaurar()	{
	document.getElementById("cadena").innerHTML = _entrada;
	document.getElementById("texto").value = _estado;
	nuevo_boton = false;
	document.getElementById("texto").focus();
}

var n_boton = 0;
var botonera = "<table cellpading=0 cellspacing=0 margin=0 ><tr>";

function el_boton(estado, x, y)	{
var 	auxi = "<td class=boton style='border-width:3;border-style:groove;cursor:" + _cursor +"'";
	auxi += " onclick=pon_macro(" + n_boton + ") >" + x + " </td>";
	if (estado)
		{
		macros[n_boton] = document.getElementById("texto").value;
		if (n_boton++ % 15 == 0) botonera += "</tr><tr>";
		botonera += auxi;
		document.getElementById("aux").innerHTML = botonera + "</tr></table>";
		}
}

function ponColores()	{
var botones = document.getElementsByTagName("TD");
	for (var i = 0; i < botones.length; i ++)	{
		if (botones[i].className == "popup")	{
			botones[i].style.backgroundColor = colores[0];
			botones[i].style.color = colores[1];
		}
		if (botones[i].className == "boton")	{
			botones[i].style.backgroundColor = colores[2];
			botones[i].style.color = colores[3];
		}
		if (botones[i].className == "hundible")	{
			botones[i].style.backgroundColor = colores[4];
			botones[i].style.color = colores[5];
		}
		if (botones[i].className == "control")	{
			botones[i].style.backgroundColor = colores[6];
			botones[i].style.color = colores[7];
		}
	}
	escribeCookie ('Colores', colores.join(",") ,caduca) //***
}

var resultado = "";
function recoger(x)	{
	document.getElementById("texto").value += x;
	actualizar();
}

var secciones = document.getElementsByTagName("div");
function esconder(area)	{
if (secciones[area].style.display == 'none')
	secciones[area].style.display = '';
else
	secciones[area].style.display = 'none';
}

function mostrar_todo()	{
for (var i = 0; i < secciones.length; i ++)
	secciones[i].style.display = '';
}

var codigoAbierto = false;
function ventanaCodigo () {
	codigoAbierto = !codigoAbierto;
	if (codigoAbierto) verCodigo=window.open("codigo.html","","directories=false");
	else verCodigo.close();
}

function expandir()	{
	for (var i = 0; i < ventanas.length; i ++)
		if (ventanas[i]) ventanas[i].ini();
}