﻿if (typeof (bsn) == "undefined") {
_b = bsn = {};
}
if (typeof (_b.Autosuggest) == "undefined")
_b.Autosuggest = {};
else {
//alert("Autosuggest is already set!");
}
_b.AutoSuggest = function (id, param) {
if (!document.getElementById)
return 0;
this.fld = _b.DOM.gE(id);
if (!this.fld)
return 0;
this.sInp = "";
this.nInpC = 0;
this.aSug = [];
this.aSugCache = new Object();
this.iHigh = 0;
this.oP = param ? param : {};
var k, def = { minchars: 2, meth: "POST", varname: "input", className: "autosuggest", timeout: 7000, delay: 50, offsety: 1, shownoresults: false, noresults: "", maxheight: 250, cache: true, maxentries: 15, textParameterName: 'prefixText', countParameterName: 'count', methodName: 'GetSuggestionList', xmlNameSpace: 'http://jbecker.com/AutoComplete', siteId: '0', siteParameterName: 'site', boxHeaderText: 'Suggestions...', widthModifier: 80, submitOnClick: 'true', useHeader: false, useSectionHeaders: true };
for (k in def) {
if (typeof (this.oP[k]) != typeof (def[k]))
this.oP[k] = def[k];
}
var s, sections = { brands: { id: 1, text: "Brands" }, categories: { id: 2, text: "Categories" }, products: { id: 3, text: "Products" }, popularSearches: { id: 4, text: "Popular Searches"} };
this.oS = sections;
var p = this;
this.fld.onkeypress = function (ev) { return p.onKeyPress(ev); };
this.fld.onkeyup = function (ev) { return p.onKeyUp(ev); };
this.fld.onkeydown = function (ev) { return p.onKeyDown(ev); };
this.fld.setAttribute("autocomplete", "off");
};
if (typeof(_b.Utility) == "undefined")
_b.Utility = {};
_b.Utility.fireEvent = function(obj, evt)
{
var fireOnThis = obj;
if (fireOnThis)
{
if(document.createEvent) 
{
var evObj = document.createEvent('MouseEvents');
if (evObj)
{
evObj.initEvent(evt, true, false);
fireOnThis.dispatchEvent(evObj);
}
} 
else if (document.createEventObject) 
{
fireOnThis.fireEvent('on' + evt);
}
}
}
_b.Utility.fireEvent2 = function(element, event)
{ 
if (document.createEventObject)
{ 
// dispatch for IE 
var evt = document.createEventObject(); 
return element.fireEvent('on' + event, evt) 
} 
else
{ 
// dispatch for firefox + others 
var evt = document.createEvent("HTMLEvents"); 
evt.initEvent(event, true, true ); // event type,bubbling,cancelable 
return !element.dispatchEvent(evt); 
} 
} 
_b.AutoSuggest.prototype.onKeyPress = function(ev)
{
var key = (window.event) ? window.event.keyCode : ev.keyCode;
var RETURN = 13;
var TAB = 9;
var ESC = 27;
var bubble = 1;
switch(key)
{
case RETURN:
//this.setHighlightedValue();
var list = _b.DOM.gE("as_ul");
if (list)
{
var li = null;
if (list.childNodes)
{
li = list.childNodes[this.iHigh-1];
}
if (li)
{
var link = document.getElementById("as_item_" + this.iHigh);
if (link)
{
var myHref = "";
if (link.href)
{
myHref = this.trim(link.href);
}
if (myHref != "" && !(/#$/.test(myHref)))
{
window.location.href = link.href;
}
else
{
this.setHighlightedValue()
if (this.oP && this.oP.submitOnClick != null && this.oP.submitOnClick == 'true')
{
this.submitForm(this.fld);
}
}
}
return false;
}
}
break;
case ESC:
this.clearSuggestions();
break;
}
return bubble;
};
_b.AutoSuggest.prototype.onKeyDown = function (ev) {
clearTimeout(this.keyPressDelayTimeout);
}
_b.AutoSuggest.prototype.onKeyUp = function (ev) {
var key = (window.event) ? window.event.keyCode : ev.keyCode;
var pointer = this;
var ARRUP = 38;
var ARRDN = 40;
var bubble = 1;
switch (key) {
case ARRUP:
this.changeHighlight(key);
bubble = 0;
break;
case ARRDN:
this.changeHighlight(key);
bubble = 0;
break;
default:
this.keyPressDelayTimeout = setTimeout(function () { pointer.getSuggestions(pointer.fld.value) }, 500);
}
return bubble;
};
_b.AutoSuggest.prototype.trim = function(str)
{
var str = str.replace(/^\s\s*/, '');
var ws = /\s/;
var i = str.length;
while (ws.test(str.charAt(--i)));
return str.slice(0, i + 1);
}
_b.AutoSuggest.prototype.getSuggestions = function (val) {
if (val == this.sInp)
return 0;
_b.DOM.remE(this.idAs);
_b.DOM.remE(this.idAsFrame);
this.sInp = val;
if (this.trim(val).length < this.oP.minchars) {
this.aSug = [];
this.nInpC = val.length;
return 0;
}
var ol = this.nInpC; // old length
this.nInpC = val.length ? val.length : 0;
var l = this.aSug.length;
if (this.nInpC > ol && l && l < this.oP.maxentries && this.oP.cache) {
var arr = [];
for (var i = 0; i < l; i++) {
if (this.aSug && this.aSug[i] && (this.aSug[i].value.substr(0, val.length).toLowerCase() == val.toLowerCase()))
arr.push(this.aSug[i]);
}
this.aSug = arr;
this.createList(this.aSug);
return false;
}
else if (this.aSugCache[val]) {
this.aSug = this.aSugCache[val].slice(0);
this.createList(this.aSug);
}
else {
var pointer = this;
var input = this.sInp;
clearTimeout(this.ajID);
this.ajID = setTimeout(function () { pointer.doAjaxRequest(input) }, this.oP.delay);
}
return false;
};
_b.AutoSuggest.prototype.doAjaxRequest = function (input)
{
if (input != this.fld.value)
{
return false;
}
var pointer = this;
var input = this.sInp;
var onSuccessFunc = function (req) { pointer.setSuggestions(req, input) };
var onErrorFunc = function (status) { };
//var onErrorFunc = function (status) { alert("AJAX error: "+status); };
var myAjax = new _b.Ajax();
output = myAjax.makeRequest(this.oP, input, onSuccessFunc, onErrorFunc);
};
_b.AutoSuggest.prototype.setSuggestions = function (req, input) {
if (input != this.fld.value)
return false;
this.aSug = [];
if (this.oP.json) {
output = null;
if (window.ActiveXObject) //basically is detecting if we're using IE
{
if (req.responseXML != null)
if (req.responseXML.selectSingleNode("//string") != null)
output = req.responseXML.selectSingleNode("//string").text;
}
else {
if (req.responseXML != null)
if (req.responseXML.firstChild != null)
if (req.responseXML.firstChild.firstChild != null)
output = req.responseXML.firstChild.firstChild.nodeValue;
}
if (output != null && output != '') {
var jsondata = eval('(' + output + ')');
for (var i = 0; i < jsondata.results.length; i++) {
this.aSug.push({ 'id': jsondata.results[i].id, 'value': jsondata.results[i].value, 'info': jsondata.results[i].info, 'url': jsondata.results[i].url, 'typeid': jsondata.results[i].typeid });
}
}
}
else {
var xml = req.responseXML;
var results = xml.getElementsByTagName('results')[0].childNodes;
for (var i = 0; i < results.length; i++) {
if (results[i].hasChildNodes())
this.aSug.push({ 'id': results[i].getAttribute('id'), 'value': results[i].childNodes[0].nodeValue, 'info': results[i].getAttribute('info') });
}
}
this.idAs = "as_" + this.fld.id;
this.idAsFrame = "as_frame_" + this.fld.id;
this.aSugCache[input] = this.aSug.slice(0);
this.createList(this.aSug);
};
_b.AutoSuggest.prototype.createList = function(arr)
{
var pointer = this;
_b.DOM.remE(this.idAs);
_b.DOM.remE(this.idAsFrame);
this.killTimeout();
if (arr.length == 0 && !this.oP.shownoresults)
return false;
var div = _b.DOM.cE("div", {id:this.idAs, className:this.oP.className}); 
if (this.oP.useHeader)
{
var hcorner = _b.DOM.cE("div", {className:"as_corner"});
var header = _b.DOM.cE("div", {className:"as_header"});
header.innerHTML = this.oP.boxHeaderText;
header.appendChild(hcorner);
div.appendChild(header);
}
var ul = _b.DOM.cE("ul", {id:"as_ul"});
currentSection = 0;
currentSectionLabel = "";
currentSectionId = "";
highlightOffset = 0;
var insertDummiesArray = [];
for (var i=0;i<arr.length;i++)
{
if (this.oP.useSectionHeaders && this.oS)
{
if (currentSection != arr[i].typeid)
{
currentSection = arr[i].typeid;
switch (currentSection)
{
case 1:
currentSectionLabel = this.oS.brands.text;
currentSectionId = "section_header_brands";
break;
case 2:
currentSectionLabel = this.oS.categories.text;
currentSectionId = "section_header_categories";
break;
case 3:
currentSectionLabel = this.oS.products.text;
currentSectionId = "section_header_products";
break;
case 4:
currentSectionLabel = this.oS.popularSearches.text;
currentSectionId = "section_header_popular_searches";
break;
default:
currentSectionLabel = "";
currentSectionId = "";
break;
}
if (currentSectionLabel != "")
{
var sspan = _b.DOM.cE("span", {}, "<em class=\"autosuggest_em\">" + currentSectionLabel + "</em>", true);
var sa = _b.DOM.cE("a", {});
var stl = _b.DOM.cE("span", {className:"tl"}, " ");
var str = _b.DOM.cE("span", {className:"tr"}, " ");
sa.appendChild(stl);
sa.appendChild(str);
sa.appendChild(sspan);
var sheader = _b.DOM.cE("li", {id: currentSectionId, className:"as_section_header"}, sa);
ul.appendChild(sheader);
highlightOffset++;
insertDummiesArray.push(i);
}
}
}
var val = arr[i].value;
var st = val.toLowerCase().indexOf(this.sInp.toLowerCase());
var output = val.substring(0,st) + "<em class=\"autosuggest_em\">" + val.substring(st, st+this.sInp.length) + "</em>" + val.substring(st+this.sInp.length);
var span = _b.DOM.cE("span", {}, output, true);
var myHref = '#';
var myTitle = '';
if (arr[i].url && arr[i].url != '')
{
myHref = arr[i].url;
myTitle = arr[i].value;
}
var a = _b.DOM.cE("a", { href: myHref, title: myTitle });
var tl = _b.DOM.cE("span", {className:"tl"}, " ");
var tr = _b.DOM.cE("span", {className:"tr"}, " ");
a.appendChild(tl);
a.appendChild(tr);
a.appendChild(span);
a.name = i+1+highlightOffset;
a.id = "as_item_" + a.name;
if (myHref != '#')
{
//non-op
}
else if (this.oP && this.oP.submitOnClick != null && this.oP.submitOnClick == 'true')
{
a.onclick = function () { pointer.setHighlightedValue(); pointer.submitForm(pointer.fld); return false; };
}
else
{
a.onclick = function () { pointer.setHighlightedValue(); return false; };
}
a.onmouseover = function () { pointer.setHighlight(this.name); };
var li = _b.DOM.cE("li", {}, a);
ul.appendChild(li);
}
if (arr.length == 0 && this.oP.shownoresults)
{
var li = _b.DOM.cE("li", {className:"as_warning"}, this.oP.noresults);
ul.appendChild(li);
}
if (insertDummiesArray && insertDummiesArray.length > 0)
{
for (var i=0;i<insertDummiesArray.length;i++)
{
this.aSug.splice(insertDummiesArray[i], 0, null);
}
}
div.appendChild(ul);
var fcorner = _b.DOM.cE("div", {className:"as_corner"});
var fbar = _b.DOM.cE("div", {className:"as_bar"});
var footer = _b.DOM.cE("div", {className:"as_footer"});
footer.appendChild(fcorner);
footer.appendChild(fbar);
div.appendChild(footer);
var pos = _b.DOM.getPos(this.fld);
// Presence of the iframe causes an unnecessary request to the current page's URL, 
// which can cause issues (eg, adding an additional item to the cart when the item
// was already added using the query string params on the cart page). Removing
// all usage of iframes here, assuming it only exists for backward compatibility with
// ancient browsers whose DIV support couldn't be relied on, anyway... - LJB 6/25/2010
/*
var iframe = _b.DOM.cE("iframe", {id:this.idAsFrame, className:this.oP.className, src: "javascript:'<html><body background=\"#ffffff\">&nbsp;</body></html>'"});
iframe.style.left = pos.x + "px";
iframe.style.display = "none";
iframe.style.top = ( pos.y + this.fld.offsetHeight + this.oP.offsety ) + "px";
iframe.onmouseover = function(){ pointer.killTimeout() };
iframe.onmouseout = function(){ pointer.resetTimeout() };
document.getElementsByTagName("body")[0].appendChild(iframe);
*/
div.style.left = pos.x + "px";
div.style.top = ( pos.y + this.fld.offsetHeight + this.oP.offsety ) + "px";
div.style.width = this.fld.offsetWidth + this.oP.widthModifier + "px";
div.onmouseover = function(){ pointer.killTimeout() };
div.onmouseout = function(){ pointer.resetTimeout() };
div.style.zIndex = div.style.zIndex + 100000000;
document.getElementsByTagName("body")[0].appendChild(div);
/*
//these are set after div added to doc because until then, div has zero for it's height and width
iframe.style.width = this.fld.offsetWidth + this.oP.widthModifier + "px";
iframe.style.zIndex = div.style.zIndex;
iframe.style.height = div.offsetHeight + "px";
var iframeRef = _b.DOM.gE(this.idAsFrame);
if (iframeRef)
{
iframeRef.style.display = "block";
}
*/
this.iHigh = 0;
var pointer = this;
this.toID = setTimeout(function () { pointer.clearSuggestions() }, this.oP.timeout);
};
_b.AutoSuggest.prototype.changeHighlight = function(key)
{ 
var list = _b.DOM.gE("as_ul");
if (!list)
return false;
var n;
var direction = 1;
if (key == 40)
n = this.iHigh + 1;
else if (key == 38)
{
n = this.iHigh - 1;
direction = -1;
}
var li = list.childNodes[n-1];
if (li)
{
if (li.id.lastIndexOf("section_header_"))
{
}
else
{
n = n + direction;
}
}
if (n > list.childNodes.length)
n = list.childNodes.length;
if (n < 1)
{
n = 1;
this.clearHighlight();
}
this.setHighlight(n);
};
_b.AutoSuggest.prototype.setHighlight = function(n)
{
var list = _b.DOM.gE("as_ul");
if (!list)
return false;
if (this.iHigh > 0)
this.clearHighlight();
this.iHigh = Number(n);
if (n != 1)
{
list.childNodes[this.iHigh-1].className = "as_highlight";
}
this.killTimeout();
};
_b.AutoSuggest.prototype.clearHighlight = function()
{
var list = _b.DOM.gE("as_ul");
if (!list)
return false;
if (this.iHigh > 0)
{
//list.childNodes[this.iHigh-1].className = "";
var li = list.childNodes[this.iHigh-1];
if (li)
{
if (li.id.lastIndexOf("section_header_"))
{
li.className = "";
}
else
{
li.className = "as_section_header";
}
}
this.iHigh = 0;
}
};
_b.AutoSuggest.prototype.setHighlightedValue = function ()
{
if (this.iHigh)
{
if (!this.aSug[this.iHigh-1])
{
return false;
}
this.sInp = this.fld.value = this.aSug[ this.iHigh-1 ].value;
this.fld.focus();
if (this.fld.selectionStart)
this.fld.setSelectionRange(this.sInp.length, this.sInp.length);
this.clearSuggestions();
if (typeof(this.oP.callback) == "function")
this.oP.callback( this.aSug[this.iHigh-1] );
return true;
}
return false;
};
_b.AutoSuggest.prototype.killTimeout = function()
{
clearTimeout(this.toID);
};
_b.AutoSuggest.prototype.resetTimeout = function()
{
clearTimeout(this.toID);
var pointer = this;
this.toID = setTimeout(function () { pointer.clearSuggestions() }, 1000);
};
_b.AutoSuggest.prototype.clearSuggestions = function ()
{
this.killTimeout();
var ele = _b.DOM.gE(this.idAs);
var eleFrame = _b.DOM.gE(this.idAsFrame);
var pointer = this;
if (ele)
{
var fade = new _b.Fader(ele,1,0,250,function () { _b.DOM.remE(pointer.idAs) });
}
if (eleFrame)
{
var fade2 = new _b.Fader(eleFrame,1,0,250,function () { _b.DOM.remE(pointer.idAsFrame) });
}
};
_b.AutoSuggest.prototype.submitForm = function(fld)
{
if (fld)
{
if (fld.form)
{
fld.form.submit();
}
}
};
if (typeof(_b.DOM) == "undefined")
_b.DOM = {};
_b.DOM.cE = function ( type, attr, cont, html )
{
var ne = document.createElement( type );
if (!ne)
return 0;
for (var a in attr)
ne[a] = attr[a];
var t = typeof(cont);
if (t == "string" && !html)
ne.appendChild( document.createTextNode(cont) );
else if (t == "string" && html)
ne.innerHTML = cont;
else if (t == "object")
ne.appendChild( cont );
return ne;
};
_b.DOM.gE = function ( e )
{
var t=typeof(e);
if (t == "undefined")
return 0;
else if (t == "string")
{
var re = document.getElementById( e );
if (!re)
return 0;
else if (typeof(re.appendChild) != "undefined" )
return re;
else
return 0;
}
else if (typeof(e.appendChild) != "undefined")
return e;
else
return 0;
};
_b.DOM.remE = function ( ele )
{
var e = this.gE(ele);
if (!e)
return 0;
else if (e.parentNode.removeChild(e))
return true;
else
return 0;
};
_b.DOM.getPos = function ( e )
{
var e = this.gE(e);
var obj = e;
var curleft = 0;
if (obj.offsetParent)
{
while (obj.offsetParent)
{
curleft += obj.offsetLeft;
obj = obj.offsetParent;
}
}
else if (obj.x)
curleft += obj.x;
var obj = e;
var curtop = 0;
if (obj.offsetParent)
{
while (obj.offsetParent)
{
curtop += obj.offsetTop;
obj = obj.offsetParent;
}
}
else if (obj.y)
curtop += obj.y;
return {x:curleft, y:curtop};
};
if (typeof(_b.Fader) == "undefined")
_b.Fader = {};
_b.Fader = function (ele, from, to, fadetime, callback)
{ 
if (!ele)
return 0;
this.e = ele;
this.from = from;
this.to = to;
this.cb = callback;
this.nDur = fadetime;
this.nInt = 50;
this.nTime = 0;
var p = this;
this.nID = setInterval(function() { p._fade() }, this.nInt);
};
_b.Fader.prototype._fade = function()
{
this.nTime += this.nInt;
var ieop = Math.round( this._tween(this.nTime, this.from, this.to, this.nDur) * 100 );
var op = ieop / 100;
if (this.e.filters) // internet explorer
{
try
{
this.e.filters.item("DXImageTransform.Microsoft.Alpha").opacity = ieop;
} 
catch (e) 
{ 
this.e.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity='+ieop+')';
}
}
else // other browsers
{
this.e.style.opacity = op;
}
if (this.nTime == this.nDur)
{
clearInterval( this.nID );
if (this.cb != undefined)
this.cb();
}
};
_b.Fader.prototype._tween = function(t,b,c,d)
{
return b + ( (c-b) * (t/d) );
};
if (typeof(_b.Ajax) == "undefined")
_b.Ajax = {};
_b.Ajax = function ()
{
this.req = {};
this.isIE = false;
};
_b.Ajax.prototype.makeRequest = function (options, input, onComp, onErr) {
if (options.meth != "POST") options.meth = "GET";
this.onComplete = onComp;
this.onError = onErr;
var pointer = this;
reqData = options.textParameterName + "=" + input + "&" + options.countParameterName + "=" + options.maxentries + "&" + options.siteParameterName + "=" + options.siteid;
if (window.XMLHttpRequest) {
this.req = new XMLHttpRequest();
try { this.req.timeout = 500; } catch (err) { }
this.req.onreadystatechange = function () { pointer.processReqChange() };
this.req.open(options.meth, options.url + "/" + options.methodName, true);
this.req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
this.req.send(reqData);
}
else if (window.ActiveXObject) {
this.req = new ActiveXObject("Microsoft.XMLHTTP");
if (this.req) {
this.req.onreadystatechange = function () { pointer.processReqChange() };
try { this.req.timeout = 500; } catch (err) { }
this.req.open(options.meth, options.url + "/" + options.methodName, true);
this.req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
this.req.send(reqData);
}
}
};
_b.Ajax.prototype.processReqChange = function()
{
if (this.req.readyState == 4) {
if (this.req.status == 200)
{
this.onComplete( this.req );
} else {
this.onError( this.req.status );
}
}
};
