

var __aspxBESuffix = "_B";
var __aspxTEInputSuffix = "_I";
ASPxClientTextEdit = _aspxCreateClass(ASPxClientEdit, {
    constructor: function(name) {
        this.constructor.prototype.constructor.call(this, name);
        this.KeyDown = new ASPxClientEvent();
        this.KeyPress = new ASPxClientEvent();
        this.KeyUp = new ASPxClientEvent();
        this.TextChanged = new ASPxClientEvent();
    },
    FindInputElement: function(){
        return _aspxGetElementById(this.name + __aspxTEInputSuffix);
    },
    GetValue: function() {
        var input = this.GetInputElement();
        return (input.value == "" && this.convertEmptyStringToNull) ? null : input.value;
    },
    SetValue: function(value) {
        if(value == null)
            value = "";
        var element = this.GetInputElement();
        if(_aspxIsExistsElement(element))
            element.value = value;
    },
    SetSelectionInInput: function(startPos, endPos) {
        var inputElement = this.GetInputElement();
        if (__aspxIE) {
            var range = inputElement.createTextRange();
            range.collapse(true);
            range.moveStart("character", startPos);
            range.moveEnd("character", endPos - startPos);
            range.select();
        } else
            inputElement.setSelectionRange(startPos, endPos);
    },
    // size correction
    CollapseControl: function(checkSizeCorrectedFlag) {
        if (checkSizeCorrectedFlag && this.sizeCorrectedOnce)
            return;
        var mainElement = this.GetMainElement();
        if (!_aspxIsExistsElement(mainElement))
            return;
        var mainElementCurStyle = _aspxGetCurrentStyle(mainElement);
        if (_aspxIsExistsElement(mainElement) && this.widthCorrectionRequired && mainElementCurStyle.width != "" && mainElementCurStyle.width != "auto")
            this.GetInputElement().style.width = "0";
    },
    CorrectEditorWidth: function() {
        var inputElement = this.GetInputElement();
        _aspxSetOffsetWidth(inputElement, _aspxGetClearClientWidth(_aspxFindOffsetParent(inputElement)));
    },
    UnstretchInputElement: function(){
        var inputElement = this.GetInputElement();
        var mainElement = this.GetMainElement();
        var mainElementCurStyle = _aspxGetCurrentStyle(mainElement);
        if (_aspxIsExistsElement(mainElement) && _aspxIsExistsElement(inputElement) && inputElement.style.width == "100%" &&
            (mainElementCurStyle.width == "" || mainElementCurStyle.width == "auto"))
            inputElement.style.width = "";
    },
    
    RaiseValueChangedEvent: function() {
        var processOnServer = ASPxClientEdit.prototype.RaiseValueChangedEvent.call(this);
        if(_aspxIsExists(this.RaiseTextChanged))
            processOnServer = this.RaiseTextChanged(processOnServer);
        return processOnServer;
    },
    OnKeyDown: function(evt) {
        if(_aspxIsExists(this.RaiseKeyDown))
            this.RaiseKeyDown(evt);
    },
    OnKeyPress: function(evt) {
        if(_aspxIsExists(this.RaiseKeyPress))
            this.RaiseKeyPress(evt);
    },
    OnKeyUp: function(evt) {
        if(_aspxIsExists(this.RaiseKeyUp))
            this.RaiseKeyUp(evt);
    },
    // API
    RaiseKeyDown: function(evt){
        if(!this.KeyDown.IsEmpty()){
            var args = new ASPxClientEditKeyEventArgs(evt);
            this.KeyDown.FireEvent(this, args);
        }
    },
    RaiseKeyPress: function(evt){
        if(!this.KeyPress.IsEmpty()){
            var args = new ASPxClientEditKeyEventArgs(evt);
            this.KeyPress.FireEvent(this, args);
        }
    },
    RaiseKeyUp: function(evt){
        if(!this.KeyUp.IsEmpty()){
            var args = new ASPxClientEditKeyEventArgs(evt);
            this.KeyUp.FireEvent(this, args);
        }
    },
    RaiseTextChanged: function(processOnServer){
        if(!this.TextChanged.IsEmpty()){
            var args = new ASPxClientProcessingModeEventArgs(processOnServer);
            this.TextChanged.FireEvent(this, args);
            processOnServer = args.processOnServer;
        }
        return processOnServer;  
    },
    GetText: function(){
        var value = this.GetValue();
        return value != null ? value : "";
    },
    SetText: function (value){
        this.SetValue(value);
    },
    SelectAll: function() {
        this.SetSelection(0, -1, false);
    },
    SetCaretPosition: function(pos) {
        if (pos == -1)
            pos = this.GetText().length;
        this.SetSelection(pos, pos, true);
    },
    SetSelection: function(startPos, endPos, scrollToSelection) {    
        var textLen = this.GetText().length;
        if (endPos == -1 || endPos > textLen) 
            endPos = textLen;
        if (startPos > textLen) 
            startPos = textLen;
        if (startPos > endPos)
            return;
            
        this.SetSelectionInInput(startPos, endPos);
        var inputElement = this.FindInputElement();
        if (scrollToSelection && inputElement.tagName.toUpperCase() == 'TEXTAREA') {
            var scrollHeight = inputElement.scrollHeight;
            var approxCaretPos = startPos;
            var scrollTop = Math.max(Math.round((approxCaretPos / textLen) * scrollHeight - 
                inputElement.clientHeight / 2), 0);
            inputElement.scrollTop = scrollTop;
        }
    }
});
ASPxClientTextBoxBase = _aspxCreateClass(ASPxClientTextEdit, {
});
ASPxClientTextBox = _aspxCreateClass(ASPxClientTextBoxBase, {
    constructor: function(name) {
        this.constructor.prototype.constructor.call(this, name);
        this.isASPxClientTextBox = true;
    }
});

ASPxIdent.IsASPxClientTextBox = function(obj) {
    return _aspxIsExists(obj.isASPxClientTextBox) && obj.isASPxClientTextBox;
};

var __aspxMMinHeight = 34;
ASPxClientMemo = _aspxCreateClass(ASPxClientTextEdit, { 
    constructor: function(name) {
        this.constructor.prototype.constructor.call(this, name);                                
        this.isASPxClientMemo = true;
    },
    CollapseControl: function(checkSizeCorrectedFlag) {
        if (checkSizeCorrectedFlag && this.sizeCorrectedOnce)
            return;
        var mainElement = this.GetMainElement();
        var inputElement = this.GetInputElement();
        if (!_aspxIsExistsElement(mainElement) || !_aspxIsExistsElement(inputElement))
            return;
        ASPxClientTextEdit.prototype.CollapseControl.call(this, checkSizeCorrectedFlag);
        var mainElementCurStyle = _aspxGetCurrentStyle(mainElement);
        if (this.heightCorrectionRequired && _aspxIsExists(mainElement) && _aspxIsExists(inputElement)) {
            if (mainElement.style.height == "100%" || mainElementCurStyle.height == "100%") {
                mainElement.style.height = "0";
                mainElement.wasCollapsed = true;
            }
            inputElement.style.height = "0";
        }
    },
    CorrectEditorHeight: function() {
        var mainElement = this.GetMainElement();
        if (mainElement.wasCollapsed) {
            mainElement.wasCollapsed = null;
            _aspxSetOffsetHeight(mainElement, _aspxGetClearClientHeight(_aspxFindOffsetParent(mainElement)));
        }
        var inputElement = this.GetInputElement();
        var inputClearClientHeight = _aspxGetClearClientHeight(_aspxFindOffsetParent(inputElement)) - 2;
        if (__aspxIE) {
            var calculatedMainElementStyle = _aspxGetCurrentStyle(mainElement);
            inputClearClientHeight += _aspxPxToInt(calculatedMainElementStyle.borderTopWidth) + _aspxPxToInt(calculatedMainElementStyle.borderBottomWidth);
        }
        if (inputClearClientHeight < __aspxMMinHeight)
            inputClearClientHeight = __aspxMMinHeight;
        _aspxSetOffsetHeight(inputElement, inputClearClientHeight);
        mainElement.style.height = "100%";
    }
});

ASPxIdent.IsASPxClientMemo = function(obj) {
    return _aspxIsExists(obj.isASPxClientMemo) && obj.isASPxClientMemo;
};	
ASPxClientButtonEditBase = _aspxCreateClass(ASPxClientTextBoxBase, {
    constructor: function(name) {
        this.constructor.prototype.constructor.call(this, name);    
        this.ButtonClick = new ASPxClientEvent();
    },
    GetButton: function(number) {
        return _aspxGetElementById(this.name + __aspxBESuffix + number);
    },
    ProcessInternalButtonClick: function(number) {
        return false;
    },
    OnButtonClick: function(number){
        var processOnServer = this.autoPostBack || this.IsServerEventAssigned("ButtonClick");
        if(_aspxIsExists(this.RaiseButtonClick))
            processOnServer = this.RaiseButtonClick(number);
        if (!this.ProcessInternalButtonClick(number) && processOnServer)
            this.SendPostBack('BC:' + number);
    },
    SelectInputElement: function() {
        var element = this.GetInputElement();
        if(_aspxIsExistsElement(element)) {
            _aspxSetFocus(element);
            element.select();        
        }
    },
    RaiseButtonClick: function(number){
        var processOnServer = this.autoPostBack || this.IsServerEventAssigned("ButtonClick");
        if(!this.ButtonClick.IsEmpty()){
            var args = new ASPxClientButtonEditClickEventArgs(processOnServer, number);
            this.ButtonClick.FireEvent(this, args);
            processOnServer = args.processOnServer;
        }
        return processOnServer;
    }
});
ASPxClientButtonEdit = _aspxCreateClass(ASPxClientButtonEditBase, {
});
ASPxClientButtonEditClickEventArgs = _aspxCreateClass(ASPxClientProcessingModeEventArgs, {
    constructor: function(processOnServer, buttonIndex){
        this.constructor.prototype.constructor.call(this, processOnServer);
        this.buttonIndex = buttonIndex;
    }
});
ASPxClientEditKeyEventArgs = _aspxCreateClass(ASPxClientEventArgs, {
    constructor: function(htmlEvent) {
        this.constructor.prototype.constructor.call(this);
        this.htmlEvent = htmlEvent;
    }
});

function aspxETextChanged(name) {    
    var edit = aspxGetControlCollection().Get(name);
    if(edit)
        edit.OnTextChanged();    
}
function aspxEKeyDown(name, evt){
    var edit = aspxGetControlCollection().Get(name);
    if(edit != null)
        edit.OnKeyDown(evt);
}
function aspxEKeyPress(name, evt){
    var edit = aspxGetControlCollection().Get(name);
    if(edit != null)
        edit.OnKeyPress(evt);
}
function aspxEKeyUp(name, evt){
    var edit = aspxGetControlCollection().Get(name);
    if(edit != null)
        edit.OnKeyUp(evt);
}
function aspxBEClick(name,number){
    var edit = aspxGetControlCollection().Get(name);
    if(edit != null) 
        edit.OnButtonClick(number);
}
function _aspxSetFocusToTextEditWithDelay(name) {
    _aspxSetTimeout("var edit = aspxGetControlCollection().Get('" + name + "'); __aspxIE ? edit.SetCaretPosition(0) : edit.SetFocus();", 500);
}