﻿Type.registerNamespace('MyExtenders');

MyExtenders.UpdateTextBoxBase = function(element) {

    MyExtenders.UpdateTextBoxBase.initializeBase(this, [element]);

    this._delayValue = null;
    this._timeoutID = null;
    this._processHandler = null;
    this._callbackHandler = null;
    //this._uniqueID = null;
    this._lastText = null;
}

MyExtenders.UpdateTextBoxBase.prototype = {

    initialize: function() {
        MyExtenders.UpdateTextBoxBase.callBaseMethod(this, 'initialize');

        this._processHandler = Function.createDelegate(this, this._process);
        this._callbackHandler = Function.createDelegate(this, this._callback);

        var e = this.get_element();

        $addHandler(e, 'keyup', this._processHandler);
        $addHandler(e, 'dragend', this._processHandler);
        $addHandler(e, 'cut', this._processHandler);
        $addHandler(e, 'paste', this._processHandler);

        this._lastText = e.value;
    },

    dispose: function() {
        var e = this.get_element();

        $removeHandler(e, "keyup", this._processHandler);
        $removeHandler(e, "dragend", this._processHandler);
        $removeHandler(e, "cut", this._processHandler);
        $removeHandler(e, "paste", this._processHandler);

        MyExtenders.UpdateTextBoxBase.callBaseMethod(this, 'dispose');
    },

    _process: function() {
        if (this._timeoutID)
            window.clearTimeout(this._timeoutID);
        this._timeoutID = window.setTimeout(this._callbackHandler, this._delayValue);
    },

    _callback: function() {
        var e = this.get_element();
        if (e && e.value != this._lastText) {
            this._lastText = e.value;
            this.raiseSubmiting(Sys.EventArgs.Empty);
            this._dowork(e);
        }
    },

    _dowork: function(e) {
    },

    get_Delay: function() {
        return this._delayValue;
    },

    set_Delay: function(value) {
        if (this._delayValue != value) {
            this._delayValue = value;
            this.raisePropertyChanged("Delay");
        }
    },

    add_submiting: function(handler) {
        this.get_events().addHandler('submiting', handler);
    },

    remove_submiting: function(handler) {
        this.get_events().removeHandler('submiting', handler);
    },

    raiseSubmiting: function(eventArgs) {
        var handler = this.get_events().getHandler('submiting');
        if (handler) {
            handler(this, eventArgs);
        }
    }
}

MyExtenders.UpdateTextBoxBase.registerClass('MyExtenders.UpdateTextBoxBase', AjaxControlToolkit.ControlBase);

MyExtenders.UpdateTextBox = function(element) {

    MyExtenders.UpdateTextBox.initializeBase(this, [element]);

    this._uniqueID = null;
}

MyExtenders.UpdateTextBox.prototype = {

    initialize : function() {
        MyExtenders.UpdateTextBox.callBaseMethod(this, 'initialize');
    },

    dispose : function()
    {
        MyExtenders.UpdateTextBox.callBaseMethod(this, 'dispose');
    },
    
     _dowork : function(e) 
    {
        e.disabled = "disabled";
        setTimeout("__doPostBack('" + this._uniqueID + "')", 0);
    },
    
    get_UniqueID : function() {
        return this._uniqueID;
    },

    set_UniqueID : function(value)
    {
        if (this._uniqueID != value)
        {
            this._uniqueID = value;
            this.raisePropertyChanged("UniqueID");
        }
    }
}

MyExtenders.UpdateTextBox.registerClass('MyExtenders.UpdateTextBox', MyExtenders.UpdateTextBoxBase);

if (typeof(Sys) != 'undefined') Sys.Application.notifyScriptLoaded();
