function $_business_dictionary_() { var elements = new Array(); for (var i = 0; i < arguments.length; i++) { var element = arguments[i]; if (typeof element == 'string') element = document.getElementById(element); if (arguments.length == 1) return element; elements.push(element); } return elements; } var $A_business_dictionary_ = function(iterable) { if (!iterable) return []; if (iterable.toArray) { return iterable.toArray(); } else { var results = []; for (var i = 0; i < iterable.length; i++) results.push(iterable[i]); return results; } } Function.prototype.bind = function() { var __method_business_dictionary_ = this, args = $A_business_dictionary_(arguments), object = args.shift(); return function() { return __method_business_dictionary_.apply(object, args.concat($A_business_dictionary_(arguments))); } } if(typeof(XMLHttpRequest) == "undefined") { var XMLHttpRequest = function() { return new ActiveXObject("Microsoft.XMLHTTP"); } } function CSuggestion(fieldName) { this.objField = $_business_dictionary_(fieldName); this.lastRequest = ''; this.timer = false; this.hideTimeOut = 500; this.searchUrl = ''; this.searchKey = ''; this.searchTarget = ''; this.selectedIndex = -1; } //autocomplete="off" CSuggestion.prototype = { init: function( obj ) { this.searchUrl = obj.searchUrl?obj.searchUrl:''; this.searchKey = obj.searchKey?obj.searchKey:''; this.searchTarget = obj.searchTarget?obj.searchTarget:''; this.requestUrl = obj.requestUrl?obj.requestUrl:''; this.objField.onkeyup = this.getStrings.bind(this); this.objField.onmouseover = this.hideTimerReset.bind(this); this.objField.onmouseout = this.hideTimerSet.bind(this); }, resetSelectedIndex: function() { this.selectedIndex = -1; }, getStrings: function( evt ) { evt = evt ? evt : window.event; var srcEl = (evt.target) ? evt.target : evt.srcElement; if ( srcEl && srcEl.value != this.lastRequest ) { if ( evt.keyCode != 38 && evt.keyCode != 40 && evt.keyCode != 9 ) { this.request(this.requestUrl+srcEl.value); this.lastRequest = srcEl.value; this.resetSelectedIndex(); } } }, request: function ( url ) { var el = document.getElementById('result_js'); if( el ) document.body.removeChild( el ); var element = document.createElement('script'); element.setAttribute('src', url+'&sug_mode=js'); element.setAttribute( 'id', 'result_js'); document.body.appendChild(element); //this.processResponce( business_dictionary_get_words() ); }, proccessResponce: function(xml){ //alert(xml); var objContainer = this.createContainer(); var words = xml.getElementsByTagName('words'); if ( words && words[0] ) { objContainer.innerHTML = ''; var word_arr = words[0].getElementsByTagName('word'); if ( word_arr && word_arr.length ) { for (var i = 0; i < word_arr.length; i++ ) { { var word = document.createElement('A'); word.onfocus = function ( evt ) { var evt = (evt) ? evt : ((window.event) ? event : null); var target = (evt.target) ? evt.target : evt.srcElement; this.objField.value = target.firstChild.nodeValue; this.objField.focus(); }.bind(this); word.onmouseover = function ( evt ) { var evt = (evt) ? evt : ((window.event) ? event : null); var target = (evt.target) ? evt.target : evt.srcElement; if ( this.selectedIndex != -1 ) { var els = $_business_dictionary_('suggestions').getElementsByTagName('A'); for ( var i = 0; i < els.length; i++ ) els[i].className = 'out'; } this.selectedIndex = target.rel; target.className = 'over'; }.bind(this); word.onmouseout = function ( evt ) { var els = $_business_dictionary_('suggestions').getElementsByTagName('A'); for ( var i = 0; i < els.length; i++ ) els[i].className = 'out'; }.bind(this); word.setAttribute('tag', word_arr[i].getAttribute('id')); //word.setAttribute( 'target', '_blank' ); word.onclick = function ( evt ) { var evt = (evt) ? evt : ((window.event) ? event : null); var target = (evt.target) ? evt.target : evt.srcElement; this.updateStatistic( target.getAttribute( 'tag' ) ); return true; }.bind(this); word.setAttribute('rel',i); word.setAttribute('href', word_arr[i].getAttribute('url')); word.setAttribute('target', this.searchTarget); word.innerHTML = word_arr[i].firstChild.nodeValue; objContainer.appendChild(word); } } objContainer.style.display = ''; } else { objContainer.style.display = 'none'; } } }, hideSuggestions: function() { if ( $_business_dictionary_('suggestions') ) { $_business_dictionary_('suggestions').style.display = 'none'; this.lastRequest = ''; this.resetSelectedIndex(); } }, hideTimerSet: function() { this.timer = setTimeout(this.hideSuggestions.bind(this), this.hideTimeOut); }, hideTimerReset: function() { if (this.timer) clearTimeout(this.timer); }, createContainer: function () { if ( $_business_dictionary_('suggestions') ) return $_business_dictionary_('suggestions'); var body = document.getElementsByTagName('body'); if ( body && body[0] ) { var container = document.createElement('DIV'); container.id = 'suggestions'; container.style.display = 'none'; container.style.position = 'absolute'; container.onmouseover = this.hideTimerReset.bind(this); container.onmouseout = this.hideTimerSet.bind(this); body[0].appendChild(container); this.containerPosition(container); } return container; }, setSuggestFocus: function ( action ) { if ( $_business_dictionary_('suggestions') ) { var els = $_business_dictionary_('suggestions').getElementsByTagName('A'); if ( els ) { for ( var i = 0; i < els.length; i++ ) els[i].className = 'out'; if ( action == 'down' ) this.selectedIndex++; else this.selectedIndex--; switch( this.selectedIndex ) { case -1: case -2: this.selectedIndex = els.length-1; break; case els.length: this.selectedIndex = 0; break; } els[this.selectedIndex].className = 'over'; if ( $_business_dictionary_('suggestions') && $_business_dictionary_('suggestions').style.display == '' ) els[this.selectedIndex].focus(); return; } } }, updateStatistic: function ( term_id ){ }, gotoDefinition: function () { if ( $_business_dictionary_('suggestions') ) { var els = $_business_dictionary_('suggestions').getElementsByTagName('A'); if ( els && this.selectedIndex>=0 && els[this.selectedIndex] ) { var t = els[this.selectedIndex].getAttribute('tag'); if( t > 0 ) this.updateStatistic( t ); //location.href = els[this.selectedIndex].href; return true; } } return false; }, handleArrowKeys: function ( evt ) { var evt = (evt) ? evt : ((window.event) ? event : null); if (evt) { switch (evt.keyCode) { case 38: //up this.setSuggestFocus('up'); break; case 40: //down this.setSuggestFocus('down'); break; } } }, formSubmit: function() { if ( !this.gotoDefinition() ) { return true; } return true; }, containerPosition: function ( container ) { container.style.left = (this.getLeftPos(this.objField.offsetParent)+0)+'px'; container.style.top = (this.getTopPos(this.objField.offsetParent)+22)+'px'; }, getLeftPos: function (obj) { var res = 0; while (obj) { res += obj.offsetLeft; obj = obj.offsetParent; } return res; }, getTopPos: function (obj) { var res = 0; while (obj) { res += obj.offsetTop; obj = obj.offsetParent; } return res; } } function setSelectionRange(input, selectionStart, selectionEnd) { if (input.setSelectionRange) { input.focus(); input.setSelectionRange(selectionStart, selectionEnd); } else if (input.createTextRange) { var range = input.createTextRange(); range.collapse(true); range.moveEnd('character', selectionEnd); range.moveStart('character', selectionStart); range.select(); } } var el = document.getElementById('bd_box_search_field'); el.autocomplete = 'off'; el.setAttribute( 'autocomplete', 'off' ); empty_filter = false; function filter_active( sender ){ // alert('active'); if( empty_filter ){ empty_filter = false; sender.value = ''; } } function filter_inactive(sender){ // alert('inactive'); if( sender.value == '' ){ empty_filter = true; sender.value = 'Enter a word or phrase...'; //setSelectionRange( sender, 0,0 ); } } //onsubmit="return validate_filter()" var t = document.getElementById('bd_box_search_field'); t.focus(); empty_filter = true; first = true; t.onfocus= function(){ if(empty_filter && !first){ this.value=''; empty_filter=false; } first = false; } t.value = 'Enter a word or phrase...'; t.focus(); setSelectionRange( t, 0,0 ); var bd_box_suggestion = new CSuggestion('bd_box_search_field'); bd_box_suggestion.init( { searchUrl: 'http://www.businessdictionary.com/search.php', searchKey: 'search', searchTarget: '_blank', requestUrl: 'http://www.businessdictionary.com/search.php?mode=get_words&search=' } ); document.onkeyup = bd_box_suggestion.handleArrowKeys.bind(bd_box_suggestion); document.getElementById('bd_box_sf').onsubmit = bd_box_suggestion.formSubmit.bind(bd_box_suggestion);