/////////////////////////////////////////////////////////////////
//                        XML functions                        //
/////////////////////////////////////////////////////////////////

function importXML(fn)
{
    if (document.implementation && document.implementation.createDocument) {
	xmlDoc = document.implementation.createDocument("", "", null);
	xmlDoc.onload = parseXML;
    }
    else if (window.ActiveXObject) {
	xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
	xmlDoc.onreadystatechange = tmp;
    } else {
	alert('Your browser can\'t handle this script');
	return;
    }
    xmlDoc.load(fn);
}

function tmp()
{
    if (xmlDoc.readyState == 4) parseXML();
}

function parseXML() {
    var x = xmlDoc.getElementsByTagName('trigger');
    myoutput = '';
    for (triggers=0;triggers<x.length;triggers++) {
        words = '';
        question = '';
        text = '';
        for (j=0;j<x[triggers].childNodes.length;j++) {
            // skip all non-tag (type 1) nodes (i.e., whitespace between tags)
            if (x[triggers].childNodes[j].nodeType != 1) continue;
            nm = x[triggers].childNodes[j].nodeName;
            vl = x[triggers].childNodes[j].childNodes[0].nodeValue+'\n';
            switch (nm) {
                case 'words': words = vl; break;
                case 'question': question = vl; break;
                case 'text': text = vl; break;
            }
        }
        if (words!='' || question!='' || text!='') {
            addTrigger(words,question,text)
        }
    }
}

/////////////////////////////////////////////////////////////////
//                      Klip5ter functions                     //
/////////////////////////////////////////////////////////////////

function ltrim(s) { return s.replace( /^[\t\n\r ]*/, "" ); }

function rtrim(s) { return s.replace( /[\t\n\r ]*$/, "" ); }

function trim(s) { return rtrim(ltrim(s)); }

function alterTextArea(tArea) {
    helperWindow.close();
    tArea.value = matchingTrigger.alterText + '\n' + tArea.value;
}

function noHelp() {
    helperWindow.close();
}

function displayHelper(hT) {
    helperWindow=window.open('','helperWin','directories=no,height=250,location=no,menubar=no,resizeable=no,scrollbars=no,status=no,toolbar=no,width=200');
    helperWindow.document.write('<html><head><title>Klip5ter</title>');
    helperWindow.document.write('<style type="text/css">\n')
    helperWindow.document.write('body { font-family: sans-serif; font-size: 14px; }\n')
    helperWindow.document.write('</style></head>');
    helperWindow.document.write('<body>')
    helperWindow.document.write('<div><img src="klip5ter.png" style="float:left">');
    helperWindow.document.write(hT.displayText);
    helperWindow.document.write('</div><div style="clear:both"><ul>');
    helperWindow.document.write('<li><a href="javascript:window.opener.alterTextArea(window.opener.currentTextArea)">Add Klip5ter\'s suggestion to my text</a><br>\n');
    helperWindow.document.write('<li><a href="javascript:window.opener.noHelp()">No, get lost, I don\'t want your help right now</a>\n');
    helperWindow.document.write('</ul></div>');
    helperWindow.document.write('</body></html>');
    helperWindow.document.close();
}

function changeInTextArea() {
    for (trig=0;trig<helperTriggers.length;trig++) {
        ht = helperTriggers[trig];
        if (ht.matches(this.value)) {
            currentTextArea = this;
            ht.activated = false;
            matchingTrigger = ht;
            displayHelper(ht);
        }
    }
}

function startHelper() {
    textareas = document.getElementsByTagName("textarea");
    allTags = document.getElementsByTagName("*");
    for (ta=0;ta<textareas.length;ta++) {
        textareas[ta].onkeypress = changeInTextArea;
    }
}

function klip5terInit() {
   // Check that we have all the requisites before starting
    if (RegExp && document.getElementsByTagName &&
       ((document.implementation && document.implementation.createDocument) ||
         window.ActiveXObject) ) 
         { 
            startHelper();
            // Open the XML triggers file and read all the triggers from it
            importXML('klip5ter.xml');
         } 
}

/////////////////////////////////////////////////////////////////
//                   helperTrigger objects                     //
/////////////////////////////////////////////////////////////////

// Define the helperTrigger object and methods
function showMatches(str) {
    if (this.activated) { 
        // if str.search(re) == -1 then it did not match
        return (str.search(this.matchRE) != -1);
    } else {
        return false;
    }
}

function addTrigger(words,displayText,alterText) {
    helperTriggers[helperTriggers.length] = new helperTrigger(words,displayText,alterText);
}

function text() {
    return this.displayText;
}

function helperTrigger(triggerWords,displayText,alterText) {
    this.matchRE = new RegExp(trim(triggerWords));
    this.displayText = displayText;
    this.alterText = alterText;
    this.matches = showMatches;
    this.activated = true;
}

/////////////////////////////////////////////////////////////////
//                          Startup                            //
/////////////////////////////////////////////////////////////////

var helperTriggers = new Array;
var currentTextArea;
var helperWindow;
var matchingTrigger;
var expression;
var inputtedText;

window.onload = klip5terInit;


