/******************************************************************************
*                                                                             *
*       Copyright (c) 2005-2009 Xsilva Systems Inc. All rights reserved.      *
*                                                                             *
*******************************************************************************
*                                                                             *
* LightSpeed Web Store™ is commercial software. Only users who have purchased *
* a valid license through  http://www.xsilva.com/  and  accept  the terms of  *
* this License Agreement may install this product.                            *
*                                                                             *
*******************************************************************************
* THIS LightSpeed Web Store™ END-USER LICENSE AGREEMENT CONSISTING OF THIS    * 
* DOCUMENT TOGETHER WITH THE FULL TEXT OF THE SOFTWARE LICENSE AGREEMENT SET  *
* FORTH HEREINBELOW (THE “AGREEMENT”) IS A LEGAL AGREEMENT BETWEEN            *
* YOU AND YOUR COMPANY (COLLECTIVELY, "YOU") AND Xsilva ( THE "AUTHOR")       *  
* FOR THE SOFTWARE PRODUCT IDENTIFIED ABOVE (THE “SOFTWARE”),                 *
* WHICH INCLUDES COMPUTER SOFTWARE AND MAY INCLUDE ASSOCIATED MEDIA, PRINTED  *
* MATERIALS, AND "ONLINE" OR ELECTRONIC DOCUMENTATION (COLLECTIVELY, THE      *
* "SOFTWARE").  BY USING THE SOFTWARE, YOU SIGNIFY YOUR AGREEMENT TO ALL      *
* TERMS, CONDITIONS, AND NOTICES CONTAINED OR REFERENCED HEREIN. IF YOU ARE   *
* NOT WILLING TO  BE  BOUND  BY  THIS  AGREEMENT, DO NOT INSTALL OR USE THE   *
* SOFTWARE.                                                                   *
*                                                                             *
* PLEASE READ THE FULL TEXT OF SOFTWARE LICENSE AGREEMENT IN THE "COPYRIGHT"  *
* FILE PROVIDED WITH THIS DISTRIBUTION. THE AGREEMENT TEXT IS ALSO AVAILABLE  *
* AT THE FOLLOWING URL: http://www.xsilva.com/webstore_license.html           *
******************************************************************************/


/*
 * Autocomplete - jQuery plugin 1.0 Alpha
 *
 * Copyright (c) 2007 Dylan Verheul, Dan G. Switzer, Anjesh Tuladhar, Jörn Zaefferer
 *
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 *
 * Revision: $Id: jquery.autocomplete.js 1729 2007-04-17 20:04:10Z joern $
 *
 */


jQuery.fn.extend({autocomplete:function(urlOrData,options){var isUrl=typeof urlOrData=="string";options=jQuery.extend({},jQuery.Autocompleter.defaults,{url:isUrl?urlOrData:null,data:isUrl?null:urlOrData,delay:isUrl?jQuery.Autocompleter.defaults.delay:10},options);return this.each(function(){new jQuery.Autocompleter(this,options)})},result:function(handler){return this.bind("result",handler)},search:function(){return this.trigger("search")}});jQuery.Autocompleter=function(input,options){var KEY={UP:38,DOWN:40,DEL:46,TAB:9,RETURN:13,ESC:27,COMMA:188};var $input=$(input).attr("autocomplete","off").addClass(options.inputClass);var timeout;var previousValue="";var cache=jQuery.Autocompleter.Cache(options);var hasFocus=0;var lastKeyPressCode;var select=jQuery.Autocompleter.Select(options,input,selectCurrent);var ca_downpressed=false;$input.keydown(function(event){lastKeyPressCode=event.keyCode;switch(event.keyCode){case KEY.UP:event.preventDefault();if(select.visible()){select.prev()}else{onChange(0,true)}break;case KEY.DOWN:ca_downpressed=true;event.preventDefault();if(select.visible()){select.next()}else{onChange(0,true)}break;case options.multiple&&jQuery.trim(options.multipleSeparator)==","&&KEY.COMMA:case KEY.RETURN:if(ca_downpressed&&selectCurrent()){ca_downpressed=false;event.preventDefault()}break;case KEY.ESC:select.hide();break;default:clearTimeout(timeout);timeout=setTimeout(onChange,options.delay);break}}).keypress(function(){}).focus(function(){hasFocus++}).blur(function(){hasFocus=0;hideResults()}).click(function(){if(hasFocus++>1&&!select.visible()){onChange(0,true)}}).bind("search",function(){function findValueCallback(q,data){var result;if(data&&data.length){for(var i=0;i<data.length;i++){if(data[i].result.toLowerCase()==q.toLowerCase()){result=data[i];break}}}$input.trigger("result",result&&[result.data,result.value])}jQuery.each(trimWords($input.val()),function(i,value){request(value,findValueCallback,findValueCallback)})});hideResultsNow();function selectCurrent(){var selected=select.selected();if(!selected)return false;var v=selected.result;previousValue=v;if(options.multiple){var words=trimWords($input.val());if(words.length>1){v=words.slice(0,words.length-1).join(options.multipleSeparator)+options.multipleSeparator+v}v+=options.multipleSeparator}$input.val(v);hideResultsNow();$input.trigger("result",[selected.data,selected.value]);return true}function onChange(crap,skipPrevCheck){if(lastKeyPressCode==KEY.DEL){select.hide();return}var currentValue=$input.val();if(!skipPrevCheck&&currentValue==previousValue)return;previousValue=currentValue;currentValue=lastWord(currentValue);if(currentValue.length>=options.minChars){$input.addClass(options.loadingClass);if(!options.matchCase)currentValue=currentValue.toLowerCase();request(currentValue,receiveData,stopLoading)}else{stopLoading();select.hide()}};function trimWords(value){if(!value){return[""]}var words=value.split(jQuery.trim(options.multipleSeparator));var result=[];jQuery.each(words,function(i,value){if(jQuery.trim(value))result[i]=jQuery.trim(value)});return result}function lastWord(value){if(!options.multiple)return value;var words=trimWords(value);return words[words.length-1]}function autoFill(q,sValue){if(options.autoFill&&(lastWord($input.val()).toLowerCase()==q.toLowerCase())&&lastKeyPressCode!=8){$input.val($input.val()+sValue.substring(lastWord(previousValue).length));jQuery.Autocompleter.Selection(input,previousValue.length,previousValue.length+sValue.length)}};function hideResults(){clearTimeout(timeout);timeout=setTimeout(hideResultsNow,200)};function hideResultsNow(){select.hide();clearTimeout(timeout);stopLoading();if(options.mustMatch){if($input.val()!=previousValue){}}};function receiveData(q,data){if(data&&data.length&&hasFocus){stopLoading();select.display(data,q);autoFill(q,data[0].value);select.show()}else{hideResultsNow()}};function request(term,success,failure){if(!options.matchCase)term=term.toLowerCase();var data=cache.load(term);if(data&&data.length){success(term,data)}else if((typeof options.url=="string")&&(options.url.length>0)){jQuery.post(options.url,{q:lastWord(term),limit:options.max,Qform__FormId:options.extraParams['Qform__FormId'],Qform__FormState:$("#Qform__FormState").val(),Qform__FormUpdates:'',Qform__FormCheckableControls:'',Qform__FormParameter:lastWord(term),Qform__FormEvent:'QAutoCompleteTextBoxEvent',Qform__FormCallType:'Ajax',Qform__FormControl:options.extraParams['Qform__FormControl']},function(data){var parsed=options.parse&&options.parse(data)||parse(data);cache.add(term,parsed);success(term,parsed)})}else{failure(term)}}function parse(data){var parsed=[];var rows=data.split("\n");for(var i=0;i<rows.length;i++){var row=jQuery.trim(rows[i]);if(row){row=row.split("|");parsed[parsed.length]={data:row,value:row[0],result:options.formatResult&&options.formatResult(row)||row[0]}}}return parsed}function stopLoading(){$input.removeClass(options.loadingClass)}};jQuery.Autocompleter.defaults={inputClass:"ac_input",resultsClass:"ac_results",loadingClass:"ac_loading",minChars:1,delay:400,matchCase:false,matchSubset:true,matchContains:false,cacheLength:10,mustMatch:false,extraParams:{},selectFirst:true,max:10,autoFill:false,width:0,multiple:false,multipleSeparator:", "};jQuery.Autocompleter.Cache=function(options){var data={};var length=0;function matchSubset(s,sub){if(!options.matchCase)s=s.toLowerCase();var i=s.indexOf(sub);if(i==-1)return false;return i==0||options.matchContains};function add(q,value){if(length>options.cacheLength){this.flush()}if(!data[q]){length++}data[q]=value}if(options.data){var stMatchSets={},nullData=0;if(!options.url)options.cacheLength=1;stMatchSets[""]=[];jQuery.each(options.data,function(i,rawValue){value=options.formatItem?options.formatItem(rawValue,i+1,options.data.length):rawValue;var firstChar=value.charAt(0).toLowerCase();if(!stMatchSets[firstChar])stMatchSets[firstChar]=[];var row={value:value,data:rawValue,result:options.formatResult&&options.formatResult(rawValue)||value};stMatchSets[firstChar].push(row);if(nullData++<options.max){stMatchSets[""].push(row)}});jQuery.each(stMatchSets,function(i,value){options.cacheLength++;add(i,value)})}return{flush:function(){data={};length=0},add:add,load:function(q){if(!options.cacheLength||!length)return null;if(data[q])return data[q];if(options.matchSubset){for(var i=q.length-1;i>=options.minChars;i--){var c=data[q.substr(0,i)];if(c){var csub=[];jQuery.each(c,function(i,x){if(matchSubset(x.value,q)){csub[csub.length]=x}});return csub}}}return null}}};jQuery.Autocompleter.Select=function(options,input,select){var CLASSES={ACTIVE:"ac_over"};var element=jQuery("<div>").hide().addClass(options.resultsClass).css("position","absolute").appendTo("body");var list=jQuery("<ul>").appendTo(element).mouseover(function(event){active=jQuery("li",list).removeClass(CLASSES.ACTIVE).index(target(event));jQuery(target(event)).addClass(CLASSES.ACTIVE)}).mouseout(function(event){jQuery(target(event)).removeClass(CLASSES.ACTIVE)}).click(function(event){jQuery(target(event)).addClass(CLASSES.ACTIVE);select();input.focus();return false});var listItems,active=-1,data,term="";if(options.width>0)element.css("width",options.width);function target(event){var element=event.target;while(element.tagName!="LI")element=element.parentNode;return element}function moveSelect(step){active+=step;wrapSelection();listItems.removeClass(CLASSES.ACTIVE).eq(active).addClass(CLASSES.ACTIVE)};function wrapSelection(){if(active<0){active=listItems.size()-1}else if(active>=listItems.size()){active=0}}function limitNumberOfItems(available){return(options.max>0)&&(options.max<available)?options.max:available}function dataToDom(){var num=limitNumberOfItems(data.length);for(var i=0;i<num;i++){if(!data[i])continue;function highlight(value){return value.replace(new RegExp("("+term+")","gi"),"<strong>$1</strong>")}jQuery("<li>").html(options.formatItem?highlight(options.formatItem(data[i].data,i+1,num)):highlight(data[i].value)).appendTo(list)}listItems=list.find("li");if(options.selectFirst){listItems.eq(0).addClass(CLASSES.ACTIVE);active=0}}return{display:function(d,q){data=d;term=q;list.empty();dataToDom();list.bgiframe()},next:function(){moveSelect(1)},prev:function(){moveSelect(-1)},hide:function(){element.hide();active=-1},visible:function(){return element.is(":visible")},current:function(){return this.visible()&&(listItems.filter("."+CLASSES.ACTIVE)[0]||options.selectFirst&&listItems[0])},show:function(){var offset=jQuery(input).offset({scroll:false,border:false});element.css({width:options.width>0?options.width:jQuery(input).width(),top:offset.top+input.offsetHeight,left:offset.left}).show()},selected:function(){return data&&data[active]}}};jQuery.Autocompleter.Selection=function(field,start,end){if(field.createTextRange){var selRange=field.createTextRange();selRange.collapse(true);selRange.moveStart("character",start);selRange.moveEnd("character",end);selRange.select()}else if(field.setSelectionRange){field.setSelectionRange(start,end)}else{if(field.selectionStart){field.selectionStart=start;field.selectionEnd=end}}field.focus()};