function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}
           /*3OGGO3(^
       ~Q@@@BBBBB@#(B@@S/
     O@@BBBBBBBBBB@O R@B@@O
    O@BBB@@@@@@BBB@G S@@@@@O		WOCO - Word Complete
  /B@BBB@t    /#@B@( //     /		Controlled by Hakan Bilgin © 2005
 ~@BBB@R(       BB~   /tC/  %@/		http://hbi.challenger.se
 O@BB@#CCQ##s  /R(  e#@@O  C@@s
/@BBBBBBBBB@O (@( /@#7 /6R@@BB@/
%@BBBBBBB@B~ KR    ^(CG3   t@B@%
3@BBBBBB@O (@R   Q@@@@Q^ /#@BB@%
%@BBBBB@#  K@e   K@@BC  tR@BBB@t
^B@B@SC3  %BB%   ^^ /sB@BBBBB@R^
 %KC/      //(     C@@BBBBBBB@t
  QQQ#G    e@@/     C@BBBBBB@R
   K@@#   sRRB@%S@@@(C@BBBB@Q
    (B@K 7@BBBBBBBB@S6@BB@R/
     ~R@G e@BBBBBBB@GS@B@#/			The sourcecode is licensed under a Creative Commons License.
        (#@S#@BBBB@#Q@Q(			http://creativecommons.org/licenses/LGPL/2.1/
             ^/~~/*/

woco = {
	init : function (dbSrc, oEl, iElWidth) {
		oEl = (typeof(oEl) == 'string')? document.getElementById(oEl) : oEl;
		if (!oEl) return;
        if (woco.active && woco.oName == oEl.id) return;
		oEl.onkeypress = woco.doKey;
		oEl.onkeydown = woco.doSysKey;
		
		xWocoDoc = xdbc.load(dbSrc).documentElement;
		if (xWocoDoc) {
			woco.active = true;
			dbParam = xWocoDoc.getAttribute('param').slice(1,-1).split('][');
			woco.charCase = (dbParam[1] == 'true');
			woco.speChars = dbParam[2];
			//woco.rgx = new RegExp('[\\w'+ woco.speChars +']', 'ig');
            woco.rgx = new RegExp('[^\;]','ig');
			//woco.db = ' '+ xWocoDoc.text.replace(/\n|\t/g, ' ') +' ';
            woco.db = xWocoDoc.text.replace(/\n|\t/g,'');
            woco.oName = oEl.id;
            woco.oWidth = iElWidth;
		}
	},
	doKey : function () {
		if (event.ctrlKey || !woco.active) return;
		tKey = String.fromCharCode(event.keyCode);
		//if (!event.shiftKey) tKey = tKey.toLowerCase();
        tKey = tKey.toUpperCase();
		if (!tKey.match(woco.rgx)) return;
		rng = document.selection.createRange();
		crng = rng.duplicate();
		crng.moveEnd('character');
		if (document.selection.type == 'Text') document.selection.clear();
		if (crng.text.match(woco.rgx)) return;
		rng.text = tKey;
		rng.moveStart('textedit', -1);
		//rgx = new RegExp(' '+ rng.text.trim() +'[\\w'+ woco.speChars +']+', 'ig');
        //alert('\;' + woco.regEscape(rng.text) +'[^\;]+\;');
        rgx = new RegExp('\;' + woco.regEscape(rng.text) +'[^\;]+\;', 'ig');
		woco.match = woco.db.match(rgx);
		if (woco.match && woco.match != 'null') {
            woco.showMatches(woco.match);
			woco.mInt = 0;
			woco.complete();
		}else{
            woco.hideMatches();
        }
		woco.cancelEvent();
	},
	complete : function () {
		suggestedWord = woco.match[woco.mInt].trim().substr(rng.text.length+1).replace(/\;/g,'');
		woco.rng = document.selection.createRange();
		woco.rng.text = suggestedWord;
		woco.rng.moveStart('character', -suggestedWord.length);
		woco.rng.select();
		woco.cancelEvent();
	},
	cancelEvent	: function () {
		event.returnValue = null;
		event.cancelBubble = true;
	},
	doSysKey : function () {
        //alert(event.keyCode);
		switch (event.keyCode) {
            case 8:
                woco.hideMatches();
                break;
			case 9,13:
				if (woco.rng && woco.match) {
					woco.cancelEvent();
					if (woco.charCase) {
						woco.rng.moveStart('textedit', -1);
						woco.rng.expand('textedit');
						woco.rng.text = woco.match[woco.mInt].trim();
					} else woco.rng.text = woco.rng.text;
					woco.rng.select();
                    woco.hideMatches();
				}
				break;
			case 27:
				woco.active = !woco.active;
				break;
			case 38:
				if (!woco.match) return;
				woco.mInt--;
				if (woco.mInt < 0) woco.mInt = woco.match.length-1;
				woco.complete();
				return;
			case 40:
				if (!woco.match) return;
				woco.mInt++;
				if (woco.mInt >= woco.match.length) woco.mInt = 0;
				woco.complete();
				return;
		}
		woco.match = null;
	},
    showMatches : function(arrMatches) {
        maxMatches = 10;
        if (typeof arrMatches == 'object') {
            newHTML = '';
            newMinSize = 1;
            newX = findPosX(document.getElementById(woco.oName));
            newY = findPosY(document.getElementById(woco.oName));
            oMatchOut = document.getElementById('Tip');
            if (arrMatches.length>1) {
                oMatchOut.style.left = newX + woco.oWidth - 25;
                oMatchOut.style.top = newY + 5;
                //oMatchOut.innerHTML = '<b>Mogelijkheden:</b>&nbsp;<br />';
                if (maxMatches<arrMatches.length){
                    newMinSize = maxMatches;
                }else{
                    newMinSize = arrMatches.length;
                }
                newHTML = '<select id=\"TipSelect\" class=\"tooltipselect\" onClick=\"woco.selectMatch();\" size=\"'+newMinSize+'\">';
                for (i=0;i<arrMatches.length;i++) {
                    //if (i<maxMatches) {
                        //oMatchOut.innerHTML += arrMatches[i].replace(/\;/g,'')+'&nbsp;<br />';
                        newHTML += '<option>'+arrMatches[i].replace(/\;/g,'')+'</option>';
                    //}
                }
                /*if (i>maxMatches) {
                    oMatchOut.innerHTML += '<b>... '+(arrMatches.length-maxMatches)+' meer</b>';
                }*/
                newHTML += '</select>';
                oMatchOut.innerHTML = newHTML;
                oMatchOut.style.display = 'block';
            }else{
                oMatchOut.innerHTML = '';
                oMatchOut.style.display = 'none';
            }
        }
    },
    hideMatches: function() {
       if (woco.active) {
           oMatchOut = document.getElementById('Tip');
           oMatchOut.innerHTML = '';
           oMatchOut.style.display = 'none';
       }
    },
    selectMatch: function() {
        oTipSelect = document.getElementById('TipSelect');
        for (i=0;i<oTipSelect.length;i++) {
            if (oTipSelect.options[i].selected == true ) {
                //alert(oTipSelect.options[i].text);
                document.getElementById(woco.oName).value = oTipSelect.options[i].text;
                woco.hideMatches();
            }
        }
    },
    regEscape: function(sText) {
       sText = sText.replace(/([^A-Z0-9])/g,'\\$1');
       return sText;
    }
}

/*
woco_moz = {
	init : function (dbSrc, oEl) {
		oEl = (typeof(oEl) == 'string')? document.getElementById(oEl) : oEl;
		oEl.addEventListener("keypress", woco_moz.doKey, false);
		
		xWocoDoc = xdbc.load(dbSrc).documentElement;
		if (xWocoDoc) {
			woco_moz.active = true;
			dbParam = xWocoDoc.getAttribute('param').slice(1,-1).split('][');
			woco_moz.charCase = (dbParam[1] == 'true');
			woco_moz.speChars = dbParam[2];
			woco_moz.rgx = new RegExp('[\\w'+ woco_moz.speChars +']', 'ig');
			woco_moz.db = ' '+ xWocoDoc.textContent.replace(/\n|\t/g, ' ') +' ';
		}
	},
	doKey : function (e) {
		if (!woco_moz.active) return;
		oEl = e.target;
		tKey = String.fromCharCode(e.charCode);
		if (!tKey.match(woco_moz.rgx)) return woco_moz.doSysKey(e);
		
		rngStart = oEl.selectionStart;
		rngEnd = oEl.selectionEnd;
		selEnd = (rngStart != rngEnd)? rngEnd : null ;
		if (!selEnd && oEl.value.substring(rngStart, rngStart+1).match(woco_moz.rgx)) return;
		
		frgStart = rngStart;
		while (frgStart > 0 && oEl.value.substring(frgStart-1, frgStart).match(woco_moz.rgx)) frgStart--;
		wordFragment = oEl.value.substring(frgStart, rngStart) + tKey;
		rgx = new RegExp(' '+ wordFragment +'[\\w]+', 'ig');
		woco_moz.match = woco_moz.db.match(rgx);
		
		if (woco_moz.match && woco_moz.match != 'null') {
			woco_moz.mInt = 0;
			woco_moz.complete(e);
		}
	},
	doSysKey : function (e) {
		oEl = e.target;
		switch (e.keyCode) {
			case 9:
				if (!woco_moz.match) return;
				if (woco_moz.charCase) {
					frgStart = oEl.selectionStart;
					while (frgStart > 0 && oEl.value.substring(frgStart-1, frgStart).match(woco_moz.rgx)) frgStart--;
					nValue = oEl.value.substring(0, frgStart);
					nValue += woco_moz.match[woco_moz.mInt].trim();
					nValue += oEl.value.substring(oEl.selectionEnd, oEl.value.length);
					oEl.value = nValue;
					cPos = frgStart + woco_moz.match[woco_moz.mInt].trim().length;
					oEl.setSelectionRange(cPos, cPos);
				} else oEl.setSelectionRange(oEl.selectionEnd, oEl.selectionEnd);
				woco_moz.cancelEvent(e);
				break;
			case 27:
				woco_moz.active = !woco_moz.active;
				break;
			case 38:
				if (!woco_moz.match) return;
				woco_moz.mInt--;
				if (woco_moz.mInt < 0) woco_moz.mInt = woco_moz.match.length-1;
				tKey = '';
				rngStart = oEl.selectionStart;
				selEnd = oEl.selectionEnd;
				woco_moz.complete(e);
				return;
			case 40:
				if (!woco_moz.match) return;
				woco_moz.mInt++;
				if (woco_moz.mInt >= woco_moz.match.length) woco_moz.mInt = 0;
				tKey = '';
				rngStart = oEl.selectionStart;
				selEnd = oEl.selectionEnd;
				woco_moz.complete(e);
				return;
		}
		woco_moz.match = null;
	},
	complete : function (e) {
		if (!woco_moz.match) return;
		oEl = e.target;
		swFrag = woco_moz.match[woco_moz.mInt].trim().substr(rngStart-frgStart+tKey.length);
		selEnd = selEnd || rngStart;
		nValue = oEl.value.substring(0, rngStart);
		nValue += tKey + swFrag;
		nValue += oEl.value.substring(selEnd, oEl.value.length);
		oEl.value = nValue;
		cPos = rngStart + tKey.length;
		oEl.setSelectionRange(cPos, cPos + swFrag.length);
		woco_moz.cancelEvent(e);
	},
	cancelEvent	: function (e) {
		e.stopPropagation();
		e.preventDefault();
	}
}
*/