﻿// 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!
//

Type.registerNamespace('OnLineExt');

OnLineExt.TBoxPostBackBehavior = function(element) {

    OnLineExt.TBoxPostBackBehavior.initializeBase(this, [element]);

    //variables 
    this._retArg = '';
    this._IsFocus = false;
    this._crPostBackHandler = null;
    this._onContinueHandler = null;
    this._UpdPanelName = null;

}

OnLineExt.TBoxPostBackBehavior.prototype = {

    initialize: function() {

        OnLineExt.TBoxPostBackBehavior.callBaseMethod(this, 'initialize');

        this._onContinueHandler = Function.createDelegate(this, this._onContinue);

        this._crPostBackHandler = Function.createDelegate(this, this._OnKeyPress);
        $addHandler(this.get_element(), 'keypress', this._crPostBackHandler); //'keypress'

        //this._focusinHandler = Function.createDelegate(this, this._inFocusIn);
        //$addHandler(this.get_element(), 'focus', this._focusinHandler); //'focusin'

        this.SetFocus();

        //debugger;

        this._bsearch = null;

        this.endRequestFunction = Function.createDelegate(this, this.endRequest);
        Sys.WebForms.PageRequestManager.getInstance().add_endRequest(this.endRequestFunction);
    },

    endRequest: function() {
        this.SetFocus();
    },

    SetFocus: function() {

        var el = this.get_element();

        // Пытыемся поставить фокус на этот элемент
        if (this._IsFocus && el != null) {
            var rect = el.getClientRects();
     //       if (rect != null && rect.buttom > 0) { // Условие его видимости , хотя бы донушко
                el.focus();
                el.select();
     //       }
        }
        //            this.get_element().selectionStart = 0;
        //            this.get_element().selectionEnd = this.get_element().textLenght;
    },

    dispose: function() {
        $removeHandler(this.get_element(), 'keypress', this._crPostBackHandler);
        OnLineExt.TBoxPostBackBehavior.callBaseMethod(this, 'dispose');
    },

    _onFocusIN: function(e) {

    },

    _OnKeyPress: function(e) {

        if (parseInt(e.charCode) == 13) {

            var parentNode = this.get_element().parentNode;
            if (parentNode != null) {
                parentNode = parentNode.parentNode;
                this._bsearch = FindShablon(parentNode, "bSearch");
                if (this._bsearch != null) {
                    this._bsearch.disabled = true;
                    window.setTimeout(this._onContinueHandler, 1000);
                }
            }

            var str = this.get_element().name;
            str = str.substring(0, str.length - 4);

            if (this._UpdPanelName == null)
                __doPostBack(str, this._retArg);
            else {
                var bFinded = UpdPanelPostTrigger(this._UpdPanelName);
                if (!bFinded) {
                    __doPostBack(str, this._retArg);
                }
            }

            e.preventDefault();
            return false;
        }
    },

    _onContinue: function() {
        this._bsearch.disabled = false;
    },

    get_UpdPanelName: function() {
        return this._UpdPanelName;
    },

    set_UpdPanelName: function(value) {
        if (this._UpdPanelName !== value) {
            this._UpdPanelName = value;
            this.raisePropertyChanged('UpdPanelName');
        }
    },


    get_retArg: function() {
        return this._retArg;
    },

    set_retArg: function(value) {
        this._retArg = value;
    },

    get_IsFocus: function() {
        return this._IsFocus;
    },

    set_IsFocus: function(value) {
        this._IsFocus = value;
    }
}

OnLineExt.TBoxPostBackBehavior.registerClass('OnLineExt.TBoxPostBackBehavior', AjaxControlToolkit.BehaviorBase , OnLineExt.Common.ISetFocus);
