﻿//======================================================================================================================
//
//   Creation Date : 12/16/2006       Created By: jgiganti
//
//   Description   : This will allow taking action when the text changes in an ASP TextBox control
//

Type.registerNamespace('TextChangedExtender');

TextChangedExtender.TextChangedExtenderBehavior = function(element) 
{
   TextChangedExtender.TextChangedExtenderBehavior.initializeBase(this, [element]);

   this._savedText = null;    //Holds the value of the control

   this._timeOut = 1000000;       //Determines how long to wait before notifying a change has occurred
   this._timer = null;
   this._tickHandler = null;

   this._textChangedFunction = null;   //Used to notify a change has occurred

   this._keyDownHandler = null;        //Handlers for events that need to be examined
   this._keyUpHandler = null;
   this._blurHandler = null;
}

TextChangedExtender.TextChangedExtenderBehavior.prototype = 
{
    initialize : function() 
    {
      TextChangedExtender.TextChangedExtenderBehavior.callBaseMethod(this, 'initialize');

      var element = this.get_element();
      this._savedText= element.value;

      //Hook up all of the DOM event handlers
      $addHandlers(element, { keydown: this._onKeyDown,
                              keyup: this._onKeyUp,
                              blur: this._onBlur },
                   this);

      this.createTimer();
    },

    dispose : function() 
    {
      this.destroyTimer();

      //Free all of the DOM event handlers
      $clearHandlers(this.get_element());

      this.hook_TextChangedFunction(false);   //Remove any notification function

        TextChangedExtender.TextChangedExtenderBehavior.callBaseMethod(this, 'dispose');
    },

   createTimer : function()
   {
      if (!this._timer)
      {
          this._tickHandler = Function.createDelegate(this, this._onTimerTick);
          this._timer = new Sys.Timer();
          this._timer.set_interval(this._timeOut);
          this._timer.add_tick(this._tickHandler);
          this._timer.set_enabled(true);
      }
   },

   destroyTimer : function()
   {
      if (this._timer)
      {
          this._timer.remove_tick(this._tickHandler);
          this._timer.set_enabled(false);
          this._timer.dispose();
          this._timer = null;
          this._tickHandler = null;
      }
   },

   //Event Handlers
   _onKeyDown : function(evt)
   {
      this._timer.set_enabled(false);
   },

   _onKeyUp : function(evt)
   {
      if (evt.charCode != Sys.UI.Key.tab) //onBlur will notify
      {
        this._timer.set_enabled(true);
      }
   },

   _onBlur : function() //Notify immediately
   {
      this.handleTextChanged();
   },

   _onTimerTick : function()
   {
      this.handleTextChanged();
   },

   // Property Accessors
   get_Timeout : function()
   {
      return this._timeOut;
   },

   set_Timeout : function(value)
   {
      if (value != this._timeOut)
      {
         this._timeOut = value;

         if (this._timer)
         {
            this._timer.set_interval(this._timeOut);
         }

         this.raisePropertyChanged('Timeout');
      }
   },

   get_TextChangedFunction : function()
   {
      return this._textChangedFunction;
   },

   set_TextChangedFunction : function(value)
   {
      if (value != this._textChangedFunction)
      {
         this.hook_TextChangedFunction(false);//Un hook any previous notification functions

         this._textChangedFunction = value;
         this.hook_TextChangedFunction(true);

         this.raisePropertyChanged('OnTextChanged');
      }
   },

   //Text Change Processing
   hook_TextChangedFunction: function(addEvent)
   {
      if (this._textChangedFunction != null)
      {
         var fn = new Function('return ' + this._textChangedFunction + '(); ');

         if (addEvent)
         {
            this.add_textChanged(fn);
         }
         else
         {
            this.remove_textChanged(fn);
         }
      }
   },

   handleTextChanged : function()
   {
      this._timer.set_enabled(false);
      var curText = this.get_element().value;

      if (this._savedText != curText)
      {         
         this._savedText = curText;
         this.raiseEvent('textChanged');
      }
   },

   //AJAX class event handling
   add_textChanged : function(handler)
   {
      this.get_events().addHandler('textChanged', handler);
   },

   remove_textChanged : function(handler)
   {
      this.get_events().removeHandler('textChanged', handler);
   },

   raiseEvent : function(eventName, eventArgs)
   {
      var handler = this.get_events().getHandler(eventName);
      if (handler)
      {
         if (!eventArgs)
         {
            eventArgs = Sys.EventArgs.Empty;
         }
         handler(this, eventArgs);
      }
   }
}

TextChangedExtender.TextChangedExtenderBehavior.registerClass('TextChangedExtender.TextChangedExtenderBehavior', AjaxControlToolkit.BehaviorBase);

if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();