1function addEvent(obj,event_name,func_name){
2	if (obj.attachEvent){
3		obj.attachEvent("on"+event_name, func_name);
4	}else if(obj.addEventListener){
5		obj.addEventListener(event_name,func_name,true);
6	}else{
7		obj["on"+event_name] = func_name;
8	}
9}
10function removeEvent(obj,event_name,func_name){
11	if (obj.detachEvent){
12		obj.detachEvent("on"+event_name,func_name);
13	}else if(obj.removeEventListener){
14		obj.removeEventListener(event_name,func_name,true);
15	}else{
16		obj["on"+event_name] = null;
17	}
18}
19function stopEvent(evt){
20	evt || window.event;
21	if (evt.stopPropagation){
22		evt.stopPropagation();
23		evt.preventDefault();
24	}else if(typeof evt.cancelBubble != "undefined"){
25		evt.cancelBubble = true;
26		evt.returnValue = false;
27	}
28	return false;
29}
30function getElement(evt){
31	if (window.event){
32		return window.event.srcElement;
33	}else{
34		return evt.currentTarget;
35	}
36}
37function getTargetElement(evt){
38	if (window.event){
39		return window.event.srcElement;
40	}else{
41		return evt.target;
42	}
43}
44function stopSelect(obj){
45	if (typeof obj.onselectstart != 'undefined'){
46		addEvent(obj,"selectstart",function(){ return false;});
47	}
48}
49function getCaretEnd(obj){
50	if(typeof obj.selectionEnd != "undefined"){
51		return obj.selectionEnd;
52	}else if(document.selection&&document.selection.createRange){
53		var M=document.selection.createRange();
54		try{
55			var Lp = M.duplicate();
56			Lp.moveToElementText(obj);
57		}catch(e){
58			var Lp=obj.createTextRange();
59		}
60		Lp.setEndPoint("EndToEnd",M);
61		var rb=Lp.text.length;
62		if(rb>obj.value.length){
63			return -1;
64		}
65		return rb;
66	}
67}
68function getCaretStart(obj){
69	if(typeof obj.selectionStart != "undefined"){
70		return obj.selectionStart;
71	}else if(document.selection&&document.selection.createRange){
72		var M=document.selection.createRange();
73		try{
74			var Lp = M.duplicate();
75			Lp.moveToElementText(obj);
76		}catch(e){
77			var Lp=obj.createTextRange();
78		}
79		Lp.setEndPoint("EndToStart",M);
80		var rb=Lp.text.length;
81		if(rb>obj.value.length){
82			return -1;
83		}
84		return rb;
85	}
86}
87function setCaret(obj,l){
88	obj.focus();
89	if (obj.setSelectionRange){
90		obj.setSelectionRange(l,l);
91	}else if(obj.createTextRange){
92		m = obj.createTextRange();
93		m.moveStart('character',l);
94		m.collapse();
95		m.select();
96	}
97}
98function setSelection(obj,s,e){
99	obj.focus();
100	if (obj.setSelectionRange){
101		obj.setSelectionRange(s,e);
102	}else if(obj.createTextRange){
103		m = obj.createTextRange();
104		m.moveStart('character',s);
105		m.moveEnd('character',e);
106		m.select();
107	}
108}
109String.prototype.addslashes = function(){
110	return this.replace(/(["\\\.\|\[\]\^\*\+\?\$\(\)])/g, '\\$1');
111}
112String.prototype.trim = function () {
113    return this.replace(/^\s*(\S*(\s+\S+)*)\s*$/, "$1");
114};
115function curTop(obj){
116	toreturn = 0;
117	while(obj){
118		toreturn += obj.offsetTop;
119		obj = obj.offsetParent;
120	}
121	return toreturn;
122}
123function curLeft(obj){
124	toreturn = 0;
125	while(obj){
126		toreturn += obj.offsetLeft;
127		obj = obj.offsetParent;
128	}
129	return toreturn;
130}
131function isNumber(a) {
132    return typeof a == 'number' && isFinite(a);
133}
134function replaceHTML(obj,text){
135	while(el = obj.childNodes[0]){
136		obj.removeChild(el);
137	};
138	obj.appendChild(document.createTextNode(text));
139}
140
141function actb(obj,ca){
142	/* ---- Public Variables ---- */
143	this.actb_timeOut = -1; // Autocomplete Timeout in ms (-1: autocomplete never time out)
144	this.actb_lim = 4;    // Number of elements autocomplete can show (-1: no limit)
145	this.actb_firstText = false; // should the auto complete be limited to the beginning of keyword?
146	this.actb_mouse = true; // Enable Mouse Support
147	this.actb_delimiter = new Array(';',',');  // Delimiter for multiple autocomplete. Set it to empty array for single autocomplete
148	this.actb_startcheck = 1; // Show widget only after this number of characters is typed in.
149	/* ---- Public Variables ---- */
150
151	/* --- Styles --- */
152	this.actb_bgColor = '#888888';
153	this.actb_textColor = '#FFFFFF';
154	this.actb_hColor = '#000000';
155	this.actb_fFamily = 'Verdana';
156	this.actb_fSize = '11px';
157	this.actb_hStyle = 'text-decoration:underline;font-weight="bold"';
158	/* --- Styles --- */
159
160	/* ---- Private Variables ---- */
161	var actb_delimwords = new Array();
162	var actb_cdelimword = 0;
163	var actb_delimchar = new Array();
164	var actb_display = false;
165	var actb_pos = 0;
166	var actb_total = 0;
167	var actb_curr = null;
168	var actb_rangeu = 0;
169	var actb_ranged = 0;
170	var actb_bool = new Array();
171	var actb_pre = 0;
172	var actb_toid;
173	var actb_tomake = false;
174	var actb_getpre = "";
175	var actb_mouse_on_list = 1;
176	var actb_kwcount = 0;
177	var actb_caretmove = false;
178	this.actb_keywords = new Array();
179	/* ---- Private Variables---- */
180
181	this.actb_keywords = ca;
182	var actb_self = this;
183
184	actb_curr = obj;
185
186	addEvent(actb_curr,"focus",actb_setup);
187	function actb_setup(){
188		addEvent(document,"keydown",actb_checkkey);
189		addEvent(actb_curr,"blur",actb_clear);
190		addEvent(document,"keypress",actb_keypress);
191	}
192
193	function actb_clear(evt){
194		if (!evt) evt = event;
195		removeEvent(document,"keydown",actb_checkkey);
196		removeEvent(actb_curr,"blur",actb_clear);
197		removeEvent(document,"keypress",actb_keypress);
198		actb_removedisp();
199	}
200	function actb_parse(n){
201		if (actb_self.actb_delimiter.length > 0){
202			var t = actb_delimwords[actb_cdelimword].trim().addslashes();
203			var plen = actb_delimwords[actb_cdelimword].trim().length;
204		}else{
205			var t = actb_curr.value.addslashes();
206			var plen = actb_curr.value.length;
207		}
208		var tobuild = '';
209		var i;
210
211		if (actb_self.actb_firstText){
212			var re = new RegExp("^" + t, "i");
213		}else{
214			var re = new RegExp(t, "i");
215		}
216		var p = n.search(re);
217
218		for (i=0;i<p;i++){
219			tobuild += n.substr(i,1);
220		}
221		tobuild += "<font style='"+(actb_self.actb_hStyle)+"'>"
222		for (i=p;i<plen+p;i++){
223			tobuild += n.substr(i,1);
224		}
225		tobuild += "</font>";
226			for (i=plen+p;i<n.length;i++){
227			tobuild += n.substr(i,1);
228		}
229		return tobuild;
230	}
231	function actb_generate(){
232		if (document.getElementById('tat_table')){ actb_display = false;document.body.removeChild(document.getElementById('tat_table')); }
233		if (actb_kwcount == 0){
234			actb_display = false;
235			return;
236		}
237		a = document.createElement('table');
238		a.cellSpacing='1px';
239		a.cellPadding='2px';
240		a.style.position='absolute';
241		a.style.top = eval(curTop(actb_curr) + actb_curr.offsetHeight) + "px";
242		a.style.left = curLeft(actb_curr) + "px";
243		a.style.backgroundColor=actb_self.actb_bgColor;
244		a.id = 'tat_table';
245		document.body.appendChild(a);
246		var i;
247		var first = true;
248		var j = 1;
249		if (actb_self.actb_mouse){
250			a.onmouseout = actb_table_unfocus;
251			a.onmouseover = actb_table_focus;
252		}
253		var counter = 0;
254		for (i=0;i<actb_self.actb_keywords.length;i++){
255			if (actb_bool[i]){
256				counter++;
257				r = a.insertRow(-1);
258				if (first && !actb_tomake){
259					r.style.backgroundColor = actb_self.actb_hColor;
260					first = false;
261					actb_pos = counter;
262				}else if(actb_pre == i){
263					r.style.backgroundColor = actb_self.actb_hColor;
264					first = false;
265					actb_pos = counter;
266				}else{
267					r.style.backgroundColor = actb_self.actb_bgColor;
268				}
269				r.id = 'tat_tr'+(j);
270				c = r.insertCell(-1);
271				c.style.color = actb_self.actb_textColor;
272				c.style.fontFamily = actb_self.actb_fFamily;
273				c.style.fontSize = actb_self.actb_fSize;
274				c.innerHTML = actb_parse(actb_self.actb_keywords[i]);
275				c.id = 'tat_td'+(j);
276				c.setAttribute('pos',j);
277				if (actb_self.actb_mouse){
278					c.style.cursor = 'pointer';
279					c.onclick=actb_mouseclick;
280					c.onmouseover = actb_table_highlight;
281				}
282				j++;
283			}
284			if (j - 1 == actb_self.actb_lim && j < actb_total){
285				r = a.insertRow(-1);
286				r.style.backgroundColor = actb_self.actb_bgColor;
287				c = r.insertCell(-1);
288				c.style.color = actb_self.actb_textColor;
289				c.style.fontFamily = 'arial narrow';
290				c.style.fontSize = actb_self.actb_fSize;
291				c.align='center';
292				replaceHTML(c,'\\/');
293				if (actb_self.actb_mouse){
294					c.style.cursor = 'pointer';
295					c.onclick = actb_mouse_down;
296				}
297				break;
298			}
299		}
300		actb_rangeu = 1;
301		actb_ranged = j-1;
302		actb_display = true;
303		if (actb_pos <= 0) actb_pos = 1;
304	}
305	function actb_remake(){
306		document.body.removeChild(document.getElementById('tat_table'));
307		a = document.createElement('table');
308		a.cellSpacing='1px';
309		a.cellPadding='2px';
310		a.style.position='absolute';
311		a.style.top = eval(curTop(actb_curr) + actb_curr.offsetHeight) + "px";
312		a.style.left = curLeft(actb_curr) + "px";
313		a.style.backgroundColor=actb_self.actb_bgColor;
314		a.id = 'tat_table';
315		if (actb_self.actb_mouse){
316			a.onmouseout= actb_table_unfocus;
317			a.onmouseover=actb_table_focus;
318		}
319		document.body.appendChild(a);
320		var i;
321		var first = true;
322		var j = 1;
323		if (actb_rangeu > 1){
324			r = a.insertRow(-1);
325			r.style.backgroundColor = actb_self.actb_bgColor;
326			c = r.insertCell(-1);
327			c.style.color = actb_self.actb_textColor;
328			c.style.fontFamily = 'arial narrow';
329			c.style.fontSize = actb_self.actb_fSize;
330			c.align='center';
331			replaceHTML(c,'/\\');
332			if (actb_self.actb_mouse){
333				c.style.cursor = 'pointer';
334				c.onclick = actb_mouse_up;
335			}
336		}
337		for (i=0;i<actb_self.actb_keywords.length;i++){
338			if (actb_bool[i]){
339				if (j >= actb_rangeu && j <= actb_ranged){
340					r = a.insertRow(-1);
341					r.style.backgroundColor = actb_self.actb_bgColor;
342					r.id = 'tat_tr'+(j);
343					c = r.insertCell(-1);
344					c.style.color = actb_self.actb_textColor;
345					c.style.fontFamily = actb_self.actb_fFamily;
346					c.style.fontSize = actb_self.actb_fSize;
347					c.innerHTML = actb_parse(actb_self.actb_keywords[i]);
348					c.id = 'tat_td'+(j);
349					c.setAttribute('pos',j);
350					if (actb_self.actb_mouse){
351						c.style.cursor = 'pointer';
352						c.onclick=actb_mouseclick;
353						c.onmouseover = actb_table_highlight;
354					}
355					j++;
356				}else{
357					j++;
358				}
359			}
360			if (j > actb_ranged) break;
361		}
362		if (j-1 < actb_total){
363			r = a.insertRow(-1);
364			r.style.backgroundColor = actb_self.actb_bgColor;
365			c = r.insertCell(-1);
366			c.style.color = actb_self.actb_textColor;
367			c.style.fontFamily = 'arial narrow';
368			c.style.fontSize = actb_self.actb_fSize;
369			c.align='center';
370			replaceHTML(c,'\\/');
371			if (actb_self.actb_mouse){
372				c.style.cursor = 'pointer';
373				c.onclick = actb_mouse_down;
374			}
375		}
376	}
377	function actb_goup(){
378		if (!actb_display) return;
379		if (actb_pos == 1) return;
380		document.getElementById('tat_tr'+actb_pos).style.backgroundColor = actb_self.actb_bgColor;
381		actb_pos--;
382		if (actb_pos < actb_rangeu) actb_moveup();
383		document.getElementById('tat_tr'+actb_pos).style.backgroundColor = actb_self.actb_hColor;
384		if (actb_toid) clearTimeout(actb_toid);
385		if (actb_self.actb_timeOut > 0) actb_toid = setTimeout(function(){actb_mouse_on_list=0;actb_removedisp();},actb_self.actb_timeOut);
386	}
387	function actb_godown(){
388		if (!actb_display) return;
389		if (actb_pos == actb_total) return;
390		document.getElementById('tat_tr'+actb_pos).style.backgroundColor = actb_self.actb_bgColor;
391		actb_pos++;
392		if (actb_pos > actb_ranged) actb_movedown();
393		document.getElementById('tat_tr'+actb_pos).style.backgroundColor = actb_self.actb_hColor;
394		if (actb_toid) clearTimeout(actb_toid);
395		if (actb_self.actb_timeOut > 0) actb_toid = setTimeout(function(){actb_mouse_on_list=0;actb_removedisp();},actb_self.actb_timeOut);
396	}
397	function actb_movedown(){
398		actb_rangeu++;
399		actb_ranged++;
400		actb_remake();
401	}
402	function actb_moveup(){
403		actb_rangeu--;
404		actb_ranged--;
405		actb_remake();
406	}
407
408	/* Mouse */
409	function actb_mouse_down(){
410		document.getElementById('tat_tr'+actb_pos).style.backgroundColor = actb_self.actb_bgColor;
411		actb_pos++;
412		actb_movedown();
413		document.getElementById('tat_tr'+actb_pos).style.backgroundColor = actb_self.actb_hColor;
414		actb_curr.focus();
415		actb_mouse_on_list = 0;
416		if (actb_toid) clearTimeout(actb_toid);
417		if (actb_self.actb_timeOut > 0) actb_toid = setTimeout(function(){actb_mouse_on_list=0;actb_removedisp();},actb_self.actb_timeOut);
418	}
419	function actb_mouse_up(evt){
420		if (!evt) evt = event;
421		if (evt.stopPropagation){
422			evt.stopPropagation();
423		}else{
424			evt.cancelBubble = true;
425		}
426		document.getElementById('tat_tr'+actb_pos).style.backgroundColor = actb_self.actb_bgColor;
427		actb_pos--;
428		actb_moveup();
429		document.getElementById('tat_tr'+actb_pos).style.backgroundColor = actb_self.actb_hColor;
430		actb_curr.focus();
431		actb_mouse_on_list = 0;
432		if (actb_toid) clearTimeout(actb_toid);
433		if (actb_self.actb_timeOut > 0) actb_toid = setTimeout(function(){actb_mouse_on_list=0;actb_removedisp();},actb_self.actb_timeOut);
434	}
435	function actb_mouseclick(evt){
436		if (!evt) evt = event;
437		if (!actb_display) return;
438		actb_mouse_on_list = 0;
439		actb_pos = this.getAttribute('pos');
440		actb_penter();
441	}
442	function actb_table_focus(){
443		actb_mouse_on_list = 1;
444	}
445	function actb_table_unfocus(){
446		actb_mouse_on_list = 0;
447		if (actb_toid) clearTimeout(actb_toid);
448		if (actb_self.actb_timeOut > 0) actb_toid = setTimeout(function(){actb_mouse_on_list = 0;actb_removedisp();},actb_self.actb_timeOut);
449	}
450	function actb_table_highlight(){
451		actb_mouse_on_list = 1;
452		document.getElementById('tat_tr'+actb_pos).style.backgroundColor = actb_self.actb_bgColor;
453		actb_pos = this.getAttribute('pos');
454		while (actb_pos < actb_rangeu) actb_moveup();
455		while (actb_pos > actb_ranged) actb_movedown();
456		document.getElementById('tat_tr'+actb_pos).style.backgroundColor = actb_self.actb_hColor;
457		if (actb_toid) clearTimeout(actb_toid);
458		if (actb_self.actb_timeOut > 0) actb_toid = setTimeout(function(){actb_mouse_on_list = 0;actb_removedisp();},actb_self.actb_timeOut);
459	}
460	/* ---- */
461
462	function actb_insertword(a){
463		if (actb_self.actb_delimiter.length > 0){
464			str = '';
465			l=0;
466			for (i=0;i<actb_delimwords.length;i++){
467				if (actb_cdelimword == i){
468					prespace = postspace = '';
469					gotbreak = false;
470					for (j=0;j<actb_delimwords[i].length;++j){
471						if (actb_delimwords[i].charAt(j) != ' '){
472							gotbreak = true;
473							break;
474						}
475						prespace += ' ';
476					}
477					for (j=actb_delimwords[i].length-1;j>=0;--j){
478						if (actb_delimwords[i].charAt(j) != ' ') break;
479						postspace += ' ';
480					}
481					str += prespace;
482					str += a;
483					l = str.length;
484					if (gotbreak) str += postspace;
485				}else{
486					str += actb_delimwords[i];
487				}
488				if (i != actb_delimwords.length - 1){
489					str += actb_delimchar[i];
490				}
491			}
492			actb_curr.value = str;
493			setCaret(actb_curr,l);
494		}else{
495			actb_curr.value = a;
496		}
497		actb_mouse_on_list = 0;
498		actb_removedisp();
499	}
500	function actb_penter(){
501		if (!actb_display) return;
502		actb_display = false;
503		var word = '';
504		var c = 0;
505		for (var i=0;i<=actb_self.actb_keywords.length;i++){
506			if (actb_bool[i]) c++;
507			if (c == actb_pos){
508				word = actb_self.actb_keywords[i];
509				break;
510			}
511		}
512		actb_insertword(word);
513		l = getCaretStart(actb_curr);
514	}
515	function actb_removedisp(){
516		if (actb_mouse_on_list==0){
517			actb_display = 0;
518			if (document.getElementById('tat_table')){ document.body.removeChild(document.getElementById('tat_table')); }
519			if (actb_toid) clearTimeout(actb_toid);
520		}
521	}
522	function actb_keypress(e){
523		if (actb_caretmove) stopEvent(e);
524		return !actb_caretmove;
525	}
526	function actb_checkkey(evt){
527		if (!evt) evt = event;
528		a = evt.keyCode;
529		caret_pos_start = getCaretStart(actb_curr);
530		actb_caretmove = 0;
531		switch (a){
532			case 38:
533				actb_goup();
534				actb_caretmove = 1;
535				return false;
536				break;
537			case 40:
538				actb_godown();
539				actb_caretmove = 1;
540				return false;
541				break;
542			case 13: case 9:
543				if (actb_display){
544					actb_caretmove = 1;
545					actb_penter();
546					return false;
547				}else{
548					return true;
549				}
550				break;
551			default:
552				setTimeout(function(){actb_tocomplete(a)},50);
553				break;
554		}
555	}
556
557	function actb_tocomplete(kc){
558		if (kc == 38 || kc == 40 || kc == 13) return;
559		var i;
560		if (actb_display){
561			var word = 0;
562			var c = 0;
563			for (var i=0;i<=actb_self.actb_keywords.length;i++){
564				if (actb_bool[i]) c++;
565				if (c == actb_pos){
566					word = i;
567					break;
568				}
569			}
570			actb_pre = word;
571		}else{ actb_pre = -1};
572
573		if (actb_curr.value == ''){
574			actb_mouse_on_list = 0;
575			actb_removedisp();
576			return;
577		}
578		if (actb_self.actb_delimiter.length > 0){
579			caret_pos_start = getCaretStart(actb_curr);
580			caret_pos_end = getCaretEnd(actb_curr);
581
582			delim_split = '';
583			for (i=0;i<actb_self.actb_delimiter.length;i++){
584				delim_split += actb_self.actb_delimiter[i];
585			}
586			delim_split = delim_split.addslashes();
587			delim_split_rx = new RegExp("(["+delim_split+"])");
588			c = 0;
589			actb_delimwords = new Array();
590			actb_delimwords[0] = '';
591			for (i=0,j=actb_curr.value.length;i<actb_curr.value.length;i++,j--){
592				if (actb_curr.value.substr(i,j).search(delim_split_rx) == 0){
593					ma = actb_curr.value.substr(i,j).match(delim_split_rx);
594					actb_delimchar[c] = ma[1];
595					c++;
596					actb_delimwords[c] = '';
597				}else{
598					actb_delimwords[c] += actb_curr.value.charAt(i);
599				}
600			}
601
602			var l = 0;
603			actb_cdelimword = -1;
604			for (i=0;i<actb_delimwords.length;i++){
605				if (caret_pos_end >= l && caret_pos_end <= l + actb_delimwords[i].length){
606					actb_cdelimword = i;
607				}
608				l+=actb_delimwords[i].length + 1;
609			}
610			var ot = actb_delimwords[actb_cdelimword].trim();
611			var t = actb_delimwords[actb_cdelimword].addslashes().trim();
612		}else{
613			var ot = actb_curr.value;
614			var t = actb_curr.value.addslashes();
615		}
616		if (ot.length == 0){
617			actb_mouse_on_list = 0;
618			actb_removedisp();
619		}
620		if (ot.length < actb_self.actb_startcheck) return this;
621		if (actb_self.actb_firstText){
622			var re = new RegExp("^" + t, "i");
623		}else{
624			var re = new RegExp(t, "i");
625		}
626
627		actb_total = 0;
628		actb_tomake = false;
629		actb_kwcount = 0;
630		for (i=0;i<actb_self.actb_keywords.length;i++){
631			actb_bool[i] = false;
632			if (re.test(actb_self.actb_keywords[i])){
633				actb_total++;
634				actb_bool[i] = true;
635				actb_kwcount++;
636				if (actb_pre == i) actb_tomake = true;
637			}
638		}
639
640		if (actb_toid) clearTimeout(actb_toid);
641		if (actb_self.actb_timeOut > 0) actb_toid = setTimeout(function(){actb_mouse_on_list = 0;actb_removedisp();},actb_self.actb_timeOut);
642		actb_generate();
643	}
644	return this;
645}