﻿// README
//
// There are two steps to adding a property:
//
// 1. Create a member variable to store your property
// 2. Add the get_ and set_ accessors for your property
//
// Remember that both are case sensitive!


/// <reference name="MicrosoftAjaxTimer.debug.js" />
/// <reference name="MicrosoftAjaxWebForms.debug.js" />
/// <reference name="AjaxControlToolkit.ExtenderBase.BaseScripts.js" assembly="AjaxControlToolkit" />



Type.registerNamespace('liveTextboxExtender');

liveTextboxExtender.liveTextboxExtenderBehavior = function(element) {
    liveTextboxExtender.liveTextboxExtenderBehavior.initializeBase(this, [element]);

    // TODO : (Step 1) Add your property variables here
    this._text = null;
    this._timer = null;
    this._tickHandler = null;
    this._keyupHandler = null;
    this._keydownHandler = null;
    this._focusHandler = null;
    this._blurHandler = null;
    this._clickHandler = null;

    this._TimeoutValue = null;
    this._WatermarkTextValue = null;

}
liveTextboxExtender.liveTextboxExtenderBehavior.prototype = {
    initialize : function() {
        liveTextboxExtender.liveTextboxExtenderBehavior.callBaseMethod(this, 'initialize');

        // TODO: Add your initalization code here

        this._tickHandler = Function.createDelegate(this, this._onTimerTick);
        
        this._timer = new Sys.Timer();
        this._timer.set_interval(this._TimeoutValue);
        this._timer.add_tick(this._tickHandler);
               
        this._keyupHandler = Function.createDelegate(this, this._onkeyup);
        $addHandler(this.get_element(), "keyup", this._keyupHandler);    
        
        this._keydownHandler = Function.createDelegate(this, this._onkeydown);
        $addHandler(this.get_element(), "keydown", this._keydownHandler);
        
        
        this._focusHandler = Function.createDelegate(this, this._onFocus);
        $addHandler(this.get_element(), 'focus', this._focusHandler);
                
//        this._blurHandler = Function.createDelegate(this, this._onBlur);
//        $addHandler(this.get_element(), 'blur', this._blurHandler);
//        
        this._clickHandler = Function.createDelegate(this, this._onClick);
        $addHandler(this.get_element(), 'click', this._clickHandler);
        
        
        this.get_element().focus();
    },

    dispose : function() {
    
        if(this._timer) {
            this._timer.dispose();
            this._timer = null;
        }
        this._tickHandler = null;
    
        // TODO: add your cleanup code here
        // Detach event handlers
        if (this._keyupHandler) {
            $removeHandler(this.get_element(), "keyup", this._keyupHandler);
            this._keyupHandler = null;
        }
        if (this._keydownHandler) {
            $removeHandler(this.get_element(), "keydown", this._keydownHandler);
            this._keydownHandler = null;
        }
        if (this._focusHandler) {
            $removeHandler(this.get_element(), "focus", this._focusHandler);
            this._focusHandler = null;
        }
        if (this._blurHandler) {
            $removeHandler(this.get_element(), "blur", this._blurHandler);
            this._blurHandler = null;
        }
        if (this._clickHandler) {
            $removeHandler(this.get_element(), "click", this._clickHandler);
            this._clickHandler = null;
        }

        liveTextboxExtender.liveTextboxExtenderBehavior.callBaseMethod(this, 'dispose');
    },
    
    _onFocus : function(evt) {
        /// <summary>
        /// Handler for the textbox's focus event
        /// </summary>
        /// <param name="evt" type="Sys.UI.DomEvent">
        /// Event info
        /// </param>
        if(this.get_element().value==this._WatermarkTextValue ){
            this.get_element().select();
        }
        
    }, 
       
    _onClick : function(evt) {
        /// <summary>
        /// Handler for the textbox's focus event
        /// </summary>
        /// <param name="evt" type="Sys.UI.DomEvent">
        /// Event info
        /// </param>
        if(this.get_element().value==this._WatermarkTextValue ){
            this.get_element().select();
        }        
    },

//    _onBlur : function() {
//        /// <summary>
//        /// Handle the textbox's blur event
//        /// </summary>
//        if(this.get_element().value.replace(/^\s+|\s+$/g,'') == ''  ){
//            this.get_element().value=this._WatermarkTextValue;
//        }
//        
//    },
    
    _onkeyup : function(ev) {
        var k = ev.keyCode ? ev.keyCode : ev.rawEvent.keyCode; 
        if ((k == (Sys.UI.Key.backspace) || k == (Sys.UI.Key.del)) &&  this.get_element().value==''){
            this.get_element().value="";
        }
        else{
            if (k != (Sys.UI.Key.tab) && k != (Sys.UI.Key.enter) && k != (Sys.UI.Key.esc) && k != (Sys.UI.Key.pageUp) && k != (Sys.UI.Key.pageDown) && k != (Sys.UI.Key.end) && k != (Sys.UI.Key.home) && k != (Sys.UI.Key.left) && k != (Sys.UI.Key.up) && k != (Sys.UI.Key.right) && k != (Sys.UI.Key.down) && this.get_element().value.replace(/^\s+|\s+$/g,"") != "" && this.get_element().value != this._WatermarkTextValue ) {
                this._timer.set_enabled(true);
            }
            else{        
                if(this.get_element().value.replace(/^\s+|\s+$/g,"") == ""){
                    this.get_element().value=this._WatermarkTextValue;
                    this.get_element().select();
                }
            }  
        }      
    },
    
    
    _onkeydown : function(ev) {
        this._timer.set_enabled(false);
    },


    _onTimerTick : function(sender, eventArgs) {
        this._timer.set_enabled(false);        
        if(this._text != this.get_element().value ) {
            this._text = this.get_element().value;
            this.get_element().onchange(); //compare to this.changed.invoke(this, Sys.EventArgs.Empty);
        }
    },
    // TODO: (Step 2) Add your property accessors here
    get_Timeout : function() {
        return this._TimeoutValue;
    },

    set_Timeout : function(value) {
        this._TimeoutValue = value;
    },
    
    get_WatermarkText : function() {
        return this._WatermarkTextValue;
    },

    set_WatermarkText : function(value) {
    
        this._WatermarkTextValue = value;
        this.get_element().value=value;
    }
}
liveTextboxExtender.liveTextboxExtenderBehavior.registerClass('liveTextboxExtender.liveTextboxExtenderBehavior', AjaxControlToolkit.BehaviorBase);

if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();