﻿AjaxControlToolkit.LiveSearchBehavior = function(element) {
AjaxControlToolkit.LiveSearchBehavior.initializeBase(this, [element]);this._servicePath = null;this._serviceMethod = null;this._minimumPrefixLength = 3;this._completionSetCount = 10;this._completionInterval = 1000;this._completionListElementID = null;this._completionListElement = null;this._progressElementID = null;this._progressElement = null;this._textColor = 'windowtext';this._textBackground = 'window';this._timer = null;this._cache = null;this._currentPrefix = null;this._currentFilter1 = null;this._currentFilter2 = null;this._currentFilter3 = null;this._selectIndex = -1;this._focusHandler = null;this._blurHandler = null;this._keyDownHandler = null;this._mouseDownHandler = null;this._mouseUpHandler = null;this._mouseOverHandler = null;this._tickHandler = null;this._enableCaching = true;this._additionalFilter1ID = null;this._additionalFilter2ID = null;this._additionalFilter3ID = null;this._additionalFilter1 = null;this._additionalFilter2 = null;this._additionalFilter3 = null;this._enablePaging = true;this._pagingSize = 10;this._currentPage = 0;}
AjaxControlToolkit.LiveSearchBehavior.prototype = {
initialize: function() {
AjaxControlToolkit.LiveSearchBehavior.callBaseMethod(this, 'initialize');this._tickHandler = Function.createDelegate(this, this._onTimerTick);this._focusHandler = Function.createDelegate(this, this._onGotFocus);this._blurHandler = Function.createDelegate(this, this._onLostFocus);this._keyDownHandler = Function.createDelegate(this, this._onKeyDown);this._mouseDownHandler = Function.createDelegate(this, this._onListMouseDown);this._mouseUpHandler = Function.createDelegate(this, this._onListMouseUp);this._mouseOverHandler = Function.createDelegate(this, this._onListMouseOver);this._timer = new Sys.Timer();this.initializeTimer(this._timer);var element = this.get_element();this.initializeTextBox(element);if(this._completionListElementID !== null)
this._completionListElement = $get(this._completionListElementID);if (this._completionListElement == null ) {
this._completionListElement = document.createElement('DIV');this._completionListElement.id = this.get_id() + '_completionListElem';if (Sys.Browser.agent === Sys.Browser.Safari) {
document.body.appendChild(this._completionListElement);} else {
element.parentNode.appendChild(this._completionListElement);}
}
if(this._progressElementID !== null) {
this._progressElement = $get(this._progressElementID);if (this._progressElement != null)
this._progressElement.style.visibility = 'hidden';}
if(this._additionalFilter1ID !== null) {
this._additionalFilter1 = $get(this._additionalFilter1ID);this.initializeAdditionalFilter(this._additionalFilter1);} 
if(this._additionalFilter2ID !== null) {
this._additionalFilter2 = $get(this._additionalFilter2ID);this.initializeAdditionalFilter(this._additionalFilter2);} 
if(this._additionalFilter3ID !== null) {
this._additionalFilter3 = $get(this._additionalFilter3ID);this.initializeAdditionalFilter(this._additionalFilter3);} 
this.initializeCompletionList(this._completionListElement);},
get_completionInterval: function() {
return this._completionInterval;},
set_completionInterval: function(value) {
this._completionInterval = value;},
get_completionList: function() {
return this._completionListElement;},
set_completionList: function(value) {
this._completionListElement = value;},
get_completionSetCount: function() {
return this._completionSetCount;},
set_completionSetCount: function(value) {
this._completionSetCount = value;},
get_minimumPrefixLength: function() {
return this._minimumPrefixLength;},
set_minimumPrefixLength: function(value) {
this._minimumPrefixLength = value;},
get_serviceMethod: function() {
return this._serviceMethod;},
set_serviceMethod: function(value) {
this._serviceMethod = value;},
get_servicePath: function() {
return this._servicePath;},
set_servicePath: function(value) {
this._servicePath = value;},
get_enableCaching: function() {
return this._enableCaching;},
set_enableCaching: function(value) {
this._enableCaching = value;},
get_completionListElementID: function(){
return this._completionListElementID;},
set_completionListElementID: function(value) {
this._completionListElementID = value;}, 
get_progressElementID: function(){
return this._progressElementID;},
set_progressElementID: function(value) {
this._progressElementID = value;}, 
get_additionalFilter1ID: function(){
return this._additionalFilter1ID;},
set_additionalFilter1ID: function(value) {
this._additionalFilter1ID = value;}, 
get_additionalFilter2ID: function(){
return this._additionalFilter2ID;},
set_additionalFilter2ID: function(value) {
this._additionalFilter2ID = value;}, 
get_additionalFilter3ID: function(){
return this._additionalFilter3ID;},
set_additionalFilter3ID: function(value) {
this._additionalFilter3ID = value;}, 
get_additionalFilter1Value: function(){
if (this._additionalFilter1 != null) 
{
if (this._additionalFilter1.value) 
{
return this._additionalFilter1.value;}
if (this._additionalFilter1.options) 
{
return this._additionalFilter1.options[this._additionalFilter1.selectedIndex].value;}
}
return "";},
get_additionalFilter2Value: function(){
if (this._additionalFilter2 != null) 
{
if (this._additionalFilter2.value) 
{
return this._additionalFilter2.value;}
if (this._additionalFilter2.options) 
{
return this._additionalFilter2.options[this._additionalFilter2.selectedIndex].value;}
}
return "";},
get_additionalFilter3Value: function(){
if (this._additionalFilter3 != null) 
{
if (this._additionalFilter3.value) 
{
return this._additionalFilter3.value;}
if (this._additionalFilter3.options) 
{
return this._additionalFilter3.options[this._additionalFilter3.selectedIndex].value;}
}
return "";},
dispose: function() {
if(this._timer) { 
this._timer.dispose();this._timer = null;}
var element = this.get_element();if(element) {
$removeHandler(element, "focus", this._focusHandler);$removeHandler(element, "blur", this._blurHandler);$removeHandler(element, "keydown", this._keyDownHandler);}
this._tickHandler = null;this._focusHandler = null;this._blurHandler = null;this._keyDownHandler = null;this._mouseDownHandler = null;this._mouseUpHandler = null;this._mouseOverHandler = null;AjaxControlToolkit.LiveSearchBehavior.callBaseMethod(this, 'dispose');},
initializeTimer: function(timer) {
timer.set_interval(this._completionInterval);timer.add_tick(this._tickHandler);},
initializeTextBox: function(element) {
element.autocomplete = "off";$addHandler(element, "focus", this._focusHandler);$addHandler(element, "blur", this._blurHandler);$addHandler(element, "keydown", this._keyDownHandler);},
initializeAdditionalFilter: function(element) {
$addHandler(element, "change", this._tickHandler);$addHandler(element, "click", this._tickHandler);},
initializeCompletionList: function(element) {
},
_onMoveNext: function(ev) {
},
_onListMouseDown: function(ev) {
if (ev.target !== this._completionListElement) {
this._setText(ev.target.firstChild.nodeValue);}
},
_onListMouseUp: function(ev) {
this.get_element().focus();},
_onListMouseOver: function(ev) {
var item = ev.target;this._selectIndex = -1;},
_onGotFocus: function(ev) {
this._timer.set_enabled(true);},
_onKeyDown: function(ev) {
var k = ev.keyCode ? ev.keyCode : ev.rawEvent.keyCode;if (k === Sys.UI.Key.esc) {
ev.preventDefault();}
else if (k === Sys.UI.Key.enter) {
ev.preventDefault();}
else {
this._timer.set_enabled(true);}
},
_onLostFocus: function() {
this._timer.set_enabled(false);},
_onMethodComplete: function(result, context, methodName) {
this._update(context, result, true);if (this._progressElement != null)
this._progressElement.style.visibility = 'hidden';},
_onMethodFailed: function(err, response, context) {
if (this._progressElement != null)
this._progressElement.style.visibility = 'hidden';},
_onTimerTick: function(sender, eventArgs) {
if (this._servicePath && this._serviceMethod) {
var text = this.get_element().value;if (text.trim().length < this._minimumPrefixLength && this.get_additionalFilter1Value() == "" && this.get_additionalFilter2Value() == "" && this.get_additionalFilter3Value() == "") {
this._currentPrefix = null;this._update('', null, false);return;}
if (this._currentPrefix !== text || this._currentFilter1 !== this.get_additionalFilter1Value() || this._currentFilter2 !== this.get_additionalFilter2Value() || this._currentFilter3 !== this.get_additionalFilter3Value()) {
this._currentPrefix = text;this._currentFilter1 = this.get_additionalFilter1Value();this._currentFilter2 = this.get_additionalFilter2Value();this._currentFilter3 = this.get_additionalFilter3Value();if (this._cache && this._cache[text]) {
this._update(text, this._cache[text], false);return;}
if (this._progressElement != null)
this._progressElement.style.visibility = 'visible';Sys.Net.WebServiceProxy.invoke(this.get_servicePath(), this.get_serviceMethod(), false,
{ 
prefixText : this._currentPrefix, 
count: this._completionSetCount,
additionalFilter1 : this.get_additionalFilter1Value(), 
additionalFilter2 : this.get_additionalFilter2Value(), 
additionalFilter3 : this.get_additionalFilter3Value() 
},
Function.createDelegate(this, this._onMethodComplete),
Function.createDelegate(this, this._onMethodFailed),
text);}
}
},
_setText: function(text) {
this._timer.set_enabled(false);this._currentPrefix = text;var element = this.get_element();var control = element.control;if (control && control.set_text) {
control.set_text(text);}
else {
element.value = text;}
},
_update: function(prefixText, completionItems, cacheResults) {
if (cacheResults && this.get_enableCaching()) {
if (!this._cache) {
this._cache = {};}
this._cache[prefixText] = completionItems;}
this._completionListElement.innerHTML = '';this._selectIndex = -1;if (completionItems && completionItems.length) {
this._completionListElement.innerHTML = completionItems.join(" ");var elementBounds = CommonToolkitScripts.getBounds(this.get_element());this._completionListElement.style.width = Math.max(1, elementBounds.width - 2) + 'px';}
else {
}
} 
}
AjaxControlToolkit.LiveSearchBehavior.descriptor = {
properties: [ {name: 'completionInterval', type: Number},
{name: 'completionList', isDomElement: true},
{name: 'completionListElementID', type: String},
{name: 'progressElementID', type: String},
{name: 'additionalFilter1ID', type: String},
{name: 'additionalFilter2ID', type: String},
{name: 'additionalFilter3ID', type: String},
{name: 'completionSetCount', type: Number},
{name: 'minimumPrefixLength', type: Number},
{name: 'serviceMethod', type: String},
{name: 'servicePath', type: String},
{name: 'enableCaching', type: Boolean} ]
}
AjaxControlToolkit.LiveSearchBehavior.registerClass('AjaxControlToolkit.LiveSearchBehavior', AjaxControlToolkit.BehaviorBase);
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();