1 String.prototype.trim = function(){ return this.replace(/^\s+|\s+$/g,'') }
2 String.prototype.escRegExp = function(){ return this.replace(/[\\$*+?()=!|,{}\[\]\.^]/g,'\\$&') }
3 String.prototype.unescHtml = function(){ var i,t=this; for(i in e) t=t.replace(new RegExp(i,'g'),e[i]); return t }
4 function Suggestions() { this.length=0; this.picked=0 }
5 var suggestions = new Suggestions();
6 var tagSearch='', lastEdit='';
7 var h={}, sections=[{},{},{},{},{},{}], selected={}, currentTag={},
8 e={'<':'<','>':'>','&':'&','"':'"'};
11 document.onkeydown = document.onkeypress = document.onkeyup = handler
12 h.suggest = document.getElementById("suggestionlist");
13 h.tags = document.getElementById("tags");
16 function handler(event) { var e=(event||window.event) //w3||ie
17 if (e.type == 'keyup') {
26 hideSuggestions(); break
27 case 38: case 40: break;
33 else if (e.type == "keypress") { lastEdit = h.tags.value }
36 function makeTag(parent, tag, js) {
37 parent.appendChild(document.createTextNode(" "+ tag))
40 function updateSuggestions() {
41 while (h.suggest.hasChildNodes()) h.suggest.removeChild(h.suggest.firstChild)
42 if(!getCurrentTag() || !currentTag.text) {
43 hideSuggestions(); return false
45 var tagArray = h.tags.value.toLowerCase().split(' '),
46 txt=currentTag.text.trim().escRegExp(), tagHash={}, t
47 for(t in tagArray) tagHash[tagArray[t]] = true;
49 var search = tagList.match(new RegExp(("(?:^| )("+txt+"[^ ]+)"), "gi"))
52 for (i=0; i<search.length; i++) {
53 var tl = search[i].trim()
54 if(tagHash[tl]) continue // do not suggest already typed tag
55 suggestions[suggestions.length] = makeTag(h.suggest, tl, 'complete')
58 if (suggestions.length > 0) { showSuggestions() }
59 else { hideSuggestions(); }
62 function getCurrentTag() {
63 if(h.tags.value == lastEdit) return true // no edit
64 if(h.tags == '') return false
66 var tagArray=h.tags.value.toLowerCase().split(' '), oldArray=lastEdit.toLowerCase().split(' '), currentTags = [], matched=false, t,o
69 if(typeof oldArray[o] == 'undefined') { oldArray.splice(o,1); break }
70 if(tagArray[t] == oldArray[o]) { matched = true; oldArray.splice(o,1); break; }
72 if(!matched) currentTags[currentTags.length] = t
75 // more than one word changed... abort
76 currentTag = { text:tagArray[currentTags[0]], index:currentTags[0] }
81 function hideSuggestions() { h.suggest.style.visibility='hidden' }
82 function showSuggestions() { h.suggest.style.visibility='visible' }