3
3
* version: 1.2 - 2006-11-17
13
if (typeof(bsn) == "undefined")
13
if (typeof(bsn) == "undefined")
17
if (typeof(_b.Autosuggest) == "undefined")
24
if (typeof(_bsn.Autosuggest) == "undefined")
39
_bsn.AutoSuggest = function (fldID, param)
20
alert("Autosuggest is already set!");
33
_b.AutoSuggest = function (id, param)
41
35
// no DOM - give up!
43
37
if (!document.getElementById)
49
43
// get field via DOM
51
this.fld = _bsn.DOM.getElement(fldID);
45
this.fld = _b.DOM.gE(id);
63
this.aSuggestions = [];
64
this.iHighlighted = 0;
69
63
// parameters object
71
this.oP = (param) ? param : {};
65
this.oP = param ? param : {};
75
if (!this.oP.minchars) this.oP.minchars = 1;
76
if (!this.oP.method) this.oP.meth = "get";
77
if (!this.oP.varname) this.oP.varname = "input";
78
if (!this.oP.className) this.oP.className = "autosuggest";
79
if (!this.oP.timeout) this.oP.timeout = 2500;
80
if (!this.oP.delay) this.oP.delay = 750;
81
if (!this.oP.offsety) this.oP.offsety = -5;
82
if (!this.oP.shownoresults) this.oP.shownoresults = true;
83
if (!this.oP.noresults) this.oP.noresults = "No results!";
84
if (!this.oP.maxheight && this.oP.maxheight !== 0) this.oP.maxheight = 250;
85
if (!this.oP.cache && this.oP.cache != false) this.oP.cache = true;
69
var k, def = {minchars:1, meth:"get", varname:"input", className:"autosuggest", timeout:2500, delay:500, offsety:-5, shownoresults: true, noresults: "No results!", maxheight: 250, cache: true, maxentries: 25};
72
if (typeof(this.oP[k]) != typeof(def[k]))
91
77
// set keyup handler for field
92
78
// and prevent autocomplete from client
96
82
// NOTE: not using addEventListener because UpArrow fired twice in Safari
97
//_bsn.DOM.addEvent( this.fld, 'keyup', function(ev){ return pointer.onKeyPress(ev); } );
83
//_b.DOM.addEvent( this.fld, 'keyup', function(ev){ return pointer.onKeyPress(ev); } );
99
this.fld.onkeypress = function(ev){ return pointer.onKeyPress(ev); }
100
this.fld.onkeyup = function(ev){ return pointer.onKeyUp(ev); }
85
this.fld.onkeypress = function(ev){ return p.onKeyPress(ev); };
86
this.fld.onkeyup = function(ev){ return p.onKeyUp(ev); };
102
88
this.fld.setAttribute("autocomplete","off");
120
_bsn.AutoSuggest.prototype.onKeyPress = function(ev)
106
_b.AutoSuggest.prototype.onKeyPress = function(ev)
123
109
var key = (window.event) ? window.event.keyCode : ev.keyCode;
206
_bsn.AutoSuggest.prototype.getSuggestions = function (val)
190
_b.AutoSuggest.prototype.getSuggestions = function (val)
209
193
// if input stays the same, do nothing
211
if (val == this.sInput)
195
if (val == this.sInp)
201
_b.DOM.remE(this.idAs);
215
207
// input length is less than the min required to trigger a request
216
// reset input string
219
210
if (val.length < this.oP.minchars)
213
this.nInpC = val.length;
220
var ol = this.nInpC; // old length
221
this.nInpC = val.length ? val.length : 0;
226
225
// if caching enabled, and user is typing (ie. length of input is increasing)
227
226
// filter results out of aSuggestions from last request
229
if (val.length>this.nInputChars && this.aSuggestions.length && this.oP.cache)
228
var l = this.aSug.length;
229
if (this.nInpC > ol && l && l<this.oP.maxentries && this.oP.cache)
232
for (var i=0;i<this.aSuggestions.length;i++)
232
for (var i=0;i<l;i++)
234
// if (this.aSuggestions[i].value.substr(0,val.length).toLowerCase() == val.toLowerCase())
235
if (this.aSuggestions[i].value.indexOf(val) != -1)
236
arr.push( this.aSuggestions[i] );
234
if (this.aSug[i].value.substr(0,val.length).toLowerCase() == val.toLowerCase())
235
arr.push( this.aSug[i] );
240
this.nInputChars = val.length;
241
this.aSuggestions = arr;
243
this.createList(this.aSuggestions);
239
this.createList(this.aSug);
250
246
// do new request
254
this.nInputChars = val.length;
257
249
var pointer = this;
250
var input = this.sInp;
258
251
clearTimeout(this.ajID);
259
this.ajID = setTimeout( function() { pointer.doAjaxRequest() }, this.oP.delay );
252
this.ajID = setTimeout( function() { pointer.doAjaxRequest(input) }, this.oP.delay );
269
_bsn.AutoSuggest.prototype.doAjaxRequest = function ()
262
_b.AutoSuggest.prototype.doAjaxRequest = function (input)
264
// check that saved input is still the value of the field
266
if (input != this.fld.value)
272
270
var pointer = this;
274
273
// create ajax request
275
var url = this.oP.script+this.oP.varname+"="+escape(this.fld.value);
275
if (typeof(this.oP.script) == "function")
276
var url = this.oP.script(encodeURIComponent(this.sInp));
278
var url = this.oP.script+this.oP.varname+"="+encodeURIComponent(this.sInp);
276
283
var meth = this.oP.meth;
284
var input = this.sInp;
278
var onSuccessFunc = function (req) { pointer.setSuggestions(req) };
286
var onSuccessFunc = function (req) { pointer.setSuggestions(req, input) };
279
287
var onErrorFunc = function (status) { alert("AJAX error: "+status); };
281
var myAjax = new _bsn.Ajax();
289
var myAjax = new _b.Ajax();
282
290
myAjax.makeRequest( url, meth, onSuccessFunc, onErrorFunc );
289
_bsn.AutoSuggest.prototype.setSuggestions = function (req)
297
_b.AutoSuggest.prototype.setSuggestions = function (req, input)
291
this.aSuggestions = [];
299
// if field input no longer matches what was passed to the request
300
// don't show the suggestions
302
if (input != this.fld.value)
293
309
if (this.oP.json)
319
335
this.idAs = "as_"+this.fld.id;
322
this.createList(this.aSuggestions);
339
_bsn.AutoSuggest.prototype.createList = function(arr)
338
this.createList(this.aSug);
355
_b.AutoSuggest.prototype.createList = function(arr)
341
357
var pointer = this;
344
362
// get rid of old list
345
363
// and clear the list removal timeout
347
_bsn.DOM.removeElement(this.idAs);
365
_b.DOM.remE(this.idAs);
348
366
this.killTimeout();
369
// if no results, and shownoresults is false, do nothing
371
if (arr.length == 0 && !this.oP.shownoresults)
351
375
// create holding div
353
var div = _bsn.DOM.createElement("div", {id:this.idAs, className:this.oP.className});
377
var div = _b.DOM.cE("div", {id:this.idAs, className:this.oP.className});
355
var hcorner = _bsn.DOM.createElement("div", {className:"as_corner"});
356
var hbar = _bsn.DOM.createElement("div", {className:"as_bar"});
357
var header = _bsn.DOM.createElement("div", {className:"as_header"});
379
var hcorner = _b.DOM.cE("div", {className:"as_corner"});
380
var hbar = _b.DOM.cE("div", {className:"as_bar"});
381
var header = _b.DOM.cE("div", {className:"as_header"});
358
382
header.appendChild(hcorner);
359
383
header.appendChild(hbar);
360
384
div.appendChild(header);
378
402
// (as HTML, not DOM)
380
404
var val = arr[i].value;
381
var st = val.toLowerCase().indexOf( this.sInput.toLowerCase() );
382
var output = val.substring(0,st) + "<em>" + val.substring(st, st+this.sInput.length) + "</em>" + val.substring(st+this.sInput.length);
385
var span = _bsn.DOM.createElement("span", {}, output, true);
405
var st = val.toLowerCase().indexOf( this.sInp.toLowerCase() );
406
var output = val.substring(0,st) + "<em>" + val.substring(st, st+this.sInp.length) + "</em>" + val.substring(st+this.sInp.length);
409
var span = _b.DOM.cE("span", {}, output, true);
386
410
if (arr[i].info != "")
388
var br = _bsn.DOM.createElement("br", {});
412
var br = _b.DOM.cE("br", {});
389
413
span.appendChild(br);
390
var small = _bsn.DOM.createElement("small", {}, arr[i].info);
414
var small = _b.DOM.cE("small", {}, arr[i].info);
391
415
span.appendChild(small);
394
var a = _bsn.DOM.createElement("a", { href:"#" });
418
var a = _b.DOM.cE("a", { href:"#" });
396
var tl = _bsn.DOM.createElement("span", {className:"tl"}, " ");
397
var tr = _bsn.DOM.createElement("span", {className:"tr"}, " ");
420
var tl = _b.DOM.cE("span", {className:"tl"}, " ");
421
var tr = _b.DOM.cE("span", {className:"tr"}, " ");
398
422
a.appendChild(tl);
399
423
a.appendChild(tr);
401
425
a.appendChild(span);
404
a.onclick = function () { pointer.setHighlightedValue(); return false; }
405
a.onmouseover = function () { pointer.setHighlight(this.name); }
428
a.onclick = function () { pointer.setHighlightedValue(); return false; };
429
a.onmouseover = function () { pointer.setHighlight(this.name); };
407
var li = _bsn.DOM.createElement( "li", {}, a );
431
var li = _b.DOM.cE( "li", {}, a );
409
433
ul.appendChild( li );
423
446
div.appendChild( ul );
426
var fcorner = _bsn.DOM.createElement("div", {className:"as_corner"});
427
var fbar = _bsn.DOM.createElement("div", {className:"as_bar"});
428
var footer = _bsn.DOM.createElement("div", {className:"as_footer"});
449
var fcorner = _b.DOM.cE("div", {className:"as_corner"});
450
var fbar = _b.DOM.cE("div", {className:"as_bar"});
451
var footer = _b.DOM.cE("div", {className:"as_footer"});
429
452
footer.appendChild(fcorner);
430
453
footer.appendChild(fbar);
431
454
div.appendChild(footer);
472
495
var pointer = this;
473
496
this.toID = setTimeout(function () { pointer.clearSuggestions() }, this.oP.timeout);
490
_bsn.AutoSuggest.prototype.changeHighlight = function(key)
513
_b.AutoSuggest.prototype.changeHighlight = function(key)
492
var list = _bsn.DOM.getElement("as_ul");
515
var list = _b.DOM.gE("as_ul");
499
n = this.iHighlighted + 1;
500
523
else if (key == 38)
501
n = this.iHighlighted - 1;
504
527
if (n > list.childNodes.length)
510
533
this.setHighlight(n);
515
_bsn.AutoSuggest.prototype.setHighlight = function(n)
538
_b.AutoSuggest.prototype.setHighlight = function(n)
517
var list = _bsn.DOM.getElement("as_ul");
540
var list = _b.DOM.gE("as_ul");
521
if (this.iHighlighted > 0)
522
545
this.clearHighlight();
524
this.iHighlighted = Number(n);
547
this.iHigh = Number(n);
526
list.childNodes[this.iHighlighted-1].className = "as_highlight";
549
list.childNodes[this.iHigh-1].className = "as_highlight";
529
552
this.killTimeout();
533
_bsn.AutoSuggest.prototype.clearHighlight = function()
556
_b.AutoSuggest.prototype.clearHighlight = function()
535
var list = _bsn.DOM.getElement("as_ul");
558
var list = _b.DOM.gE("as_ul");
539
if (this.iHighlighted > 0)
541
list.childNodes[this.iHighlighted-1].className = "";
542
this.iHighlighted = 0;
564
list.childNodes[this.iHigh-1].className = "";
547
_bsn.AutoSuggest.prototype.setHighlightedValue = function ()
570
_b.AutoSuggest.prototype.setHighlightedValue = function ()
549
if (this.iHighlighted)
551
this.sInput = this.fld.value = this.aSuggestions[ this.iHighlighted-1 ].value;
574
this.sInp = this.fld.value = this.aSug[ this.iHigh-1 ].value;
553
576
// move cursor to end of input (safari)
555
578
this.fld.focus();
556
579
if (this.fld.selectionStart)
557
this.fld.setSelectionRange(this.sInput.length, this.sInput.length);
580
this.fld.setSelectionRange(this.sInp.length, this.sInp.length);
560
583
this.clearSuggestions();
562
585
// pass selected object to callback function, if exists
564
587
if (typeof(this.oP.callback) == "function")
565
this.oP.callback( this.aSuggestions[this.iHighlighted-1] );
588
this.oP.callback( this.aSug[this.iHigh-1] );
581
_bsn.AutoSuggest.prototype.killTimeout = function()
604
_b.AutoSuggest.prototype.killTimeout = function()
583
606
clearTimeout(this.toID);
586
_bsn.AutoSuggest.prototype.resetTimeout = function()
609
_b.AutoSuggest.prototype.resetTimeout = function()
588
611
clearTimeout(this.toID);
589
612
var pointer = this;
590
613
this.toID = setTimeout(function () { pointer.clearSuggestions() }, 1000);
599
_bsn.AutoSuggest.prototype.clearSuggestions = function ()
622
_b.AutoSuggest.prototype.clearSuggestions = function ()
602
625
this.killTimeout();
604
var ele = _bsn.DOM.getElement(this.idAs);
627
var ele = _b.DOM.gE(this.idAs);
605
628
var pointer = this;
608
var fade = new _bsn.Fader(ele,1,0,250,function () { _bsn.DOM.removeElement(pointer.idAs) });
631
var fade = new _b.Fader(ele,1,0,250,function () { _b.DOM.remE(pointer.idAs) });
621
644
// AJAX PROTOTYPE _____________________________________________
624
if (typeof(_bsn.Ajax) == "undefined")
629
_bsn.Ajax = function ()
647
if (typeof(_b.Ajax) == "undefined")
652
_b.Ajax = function ()
632
655
this.isIE = false;
637
_bsn.Ajax.prototype.makeRequest = function (url, meth, onComp, onErr)
660
_b.Ajax.prototype.makeRequest = function (url, meth, onComp, onErr)
640
663
if (meth != "POST")
694
717
// DOM PROTOTYPE _____________________________________________
697
if (typeof(_bsn.DOM) == "undefined")
703
_bsn.DOM.createElement = function ( type, attr, cont, html )
720
if (typeof(_b.DOM) == "undefined")
726
_b.DOM.cE = function ( type, attr, cont, html )
705
728
var ne = document.createElement( type );
709
732
for (var a in attr)
712
if (typeof(cont) == "string" && !html)
735
var t = typeof(cont);
737
if (t == "string" && !html)
713
738
ne.appendChild( document.createTextNode(cont) );
714
else if (typeof(cont) == "string" && html)
739
else if (t == "string" && html)
715
740
ne.innerHTML = cont;
716
else if (typeof(cont) == "object")
741
else if (t == "object")
717
742
ne.appendChild( cont );
726
_bsn.DOM.clearElement = function ( id )
728
var ele = this.getElement( id );
733
while (ele.childNodes.length)
734
ele.removeChild( ele.childNodes[0] );
747
_bsn.DOM.removeElement = function ( ele )
749
var e = this.getElement(ele);
750
_b.DOM.gE = function ( e )
753
if (t == "undefined")
755
else if (t == "string")
757
var re = document.getElementById( e );
760
else if (typeof(re.appendChild) != "undefined" )
765
else if (typeof(e.appendChild) != "undefined")
774
_b.DOM.remE = function ( ele )
776
var e = this.gE(ele);
753
780
else if (e.parentNode.removeChild(e))
763
_bsn.DOM.replaceContent = function ( id, cont, html )
765
var ele = this.getElement( id );
770
this.clearElement( ele );
772
if (typeof(cont) == "string" && !html)
773
ele.appendChild( document.createTextNode(cont) );
774
else if (typeof(cont) == "string" && html)
775
ele.innerHTML = cont;
776
else if (typeof(cont) == "object")
777
ele.appendChild( cont );
788
_bsn.DOM.getElement = function ( ele )
790
if (typeof(ele) == "undefined")
794
else if (typeof(ele) == "string")
796
var re = document.getElementById( ele );
799
else if (typeof(re.appendChild) != "undefined" ) {
805
else if (typeof(ele.appendChild) != "undefined")
817
_bsn.DOM.appendChildren = function ( id, arr )
819
var ele = this.getElement( id );
825
if (typeof(arr) != "object")
828
for (var i=0;i<arr.length;i++)
831
if (typeof(cont) == "string")
832
ele.appendChild( document.createTextNode(cont) );
833
else if (typeof(cont) == "object")
834
ele.appendChild( cont );
846
_bsn.DOM.getPos = function ( ele )
848
var ele = this.getElement(ele);
789
_b.DOM.getPos = function ( e )
853
796
if (obj.offsetParent)
855
798
while (obj.offsetParent)
857
curleft += obj.offsetLeft
800
curleft += obj.offsetLeft;
858
801
obj = obj.offsetParent;
862
805
curleft += obj.x;
868
810
if (obj.offsetParent)
870
812
while (obj.offsetParent)
872
curtop += obj.offsetTop
814
curtop += obj.offsetTop;
873
815
obj = obj.offsetParent;
879
return {x:curleft, y:curtop}
821
return {x:curleft, y:curtop};
895
if (typeof(_bsn.Fader) == "undefined")
902
_bsn.Fader = function (ele, from, to, fadetime, callback)
837
if (typeof(_b.Fader) == "undefined")
844
_b.Fader = function (ele, from, to, fadetime, callback)
909
851
this.from = from;
912
this.callback = callback;
914
856
this.nDur = fadetime;
920
862
this.nID = setInterval(function() { p._fade() }, this.nInt);
926
_bsn.Fader.prototype._fade = function()
868
_b.Fader.prototype._fade = function()
928
870
this.nTime += this.nInt;
930
872
var ieop = Math.round( this._tween(this.nTime, this.from, this.to, this.nDur) * 100 );
931
873
var op = ieop / 100;
933
if (this.ele.filters) // internet explorer
875
if (this.e.filters) // internet explorer
937
this.ele.filters.item("DXImageTransform.Microsoft.Alpha").opacity = ieop;
879
this.e.filters.item("DXImageTransform.Microsoft.Alpha").opacity = ieop;
939
881
// If it is not set initially, the browser will throw an error. This will set it if it is not set yet.
940
this.ele.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity='+ieop+')';
882
this.e.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity='+ieop+')';
943
885
else // other browsers
945
this.ele.style.opacity = op;
887
this.e.style.opacity = op;
949
891
if (this.nTime == this.nDur)
951
893
clearInterval( this.nID );
952
if (this.callback != undefined)
894
if (this.cb != undefined)
959
_bsn.Fader.prototype._tween = function(t,b,c,d)
901
_b.Fader.prototype._tween = function(t,b,c,d)
961
903
return b + ( (c-b) * (t/d) );
b'\\ No newline at end of file'
b'\\ No newline at end of file'