// cxapelisto.js

// This script iterates through the DOM tree of a document
// on load, searching for Esperanto letters encoded using
// the 'x'-system (cx, sx, ux, etc). It then converts these
// characters to their corresponding Unicode equivalents.

// Cxe tiu programeto marsxas tra la arbo DOM-a de dokumento
// kiam gxi sxargas, sercxas por litero esperanta enkodiita
// kun la ikso-sistemo (cx, sx, ux, ktp). Gxi konvertas cxi
// tiujn karaktrojn al iliaj ekvivalentoj unikodoj.

var Cre = /C[Xx]/g;
var Gre = /G[Xx]/g;
var Hre = /H[Xx]/g;
var Jre = /J[Xx]/g;
var Sre = /S[Xx]/g;
var Ure = /U[Xx]/g;
var cre = /cx/g;
var gre = /gx/g;
var hre = /hx/g;
var jre = /jx/g;
var sre = /sx/g;
var ure = /ux/g;

function anstatauxigu(t)
{
    t = t.replace(Cre, String.fromCharCode(264));
    t = t.replace(Gre, String.fromCharCode(284));
    t = t.replace(Hre, String.fromCharCode(292));
    t = t.replace(Jre, String.fromCharCode(308));
    t = t.replace(Sre, String.fromCharCode(348));
    t = t.replace(Ure, String.fromCharCode(364));
    t = t.replace(cre, String.fromCharCode(265));
    t = t.replace(gre, String.fromCharCode(285));
    t = t.replace(hre, String.fromCharCode(293));
    t = t.replace(jre, String.fromCharCode(309));
    t = t.replace(sre, String.fromCharCode(349));
    t = t.replace(ure, String.fromCharCode(365));
    return t;
}

function marsxu(el)
{   
    var e = el.firstChild;
    while (e != null) {
        if (e.nodeType == 1) { // Node.ELEMENT_NODE
            if (e.attributes != null) {
                for (var i = 0; i < e.attributes.length; i++) {
                    var a = e.attributes.item(i);
                    if (a.nodeValue != null) {
                        var t = String(a.nodeValue);
                        if (t.search(/x/i) > 0) {
                            a.nodeValue = anstatauxigu(t);
                        }
                    }
                }
            }
            marsxu(e);
        } else if (e.nodeType == 3) { // Node.TEXT_NODE
            if (e.data.search(/x/i) > 0) {
                e.data = anstatauxigu(e.data);
            }
        }
        e = e.nextSibling;
    }
}

function cxapelu()
{   
    marsxu(document);
}

window.onload = cxapelu;
