
ASPxClientButton = _aspxCreateClass(ASPxClientControl, {
    constructor: function(name) {
        this.constructor.prototype.constructor.call(this, name);

        this.isASPxClientButton = true;

        this.allowFocus = true;
        this.autoPostBackFunction = null;
        this.causesValidation = true;
        this.checked = false;
        this.clickLocked = false;
        this.groupName = "";
        this.focused = false;
        this.focusElementSelected = false;
        this.pressed = false;
        this.useSubmitBehavior = true;
        this.validationGroup = "";
        this.validationContainerID = null;
        
        this.buttonCell = null;
        this.contentDiv = null;
        this.checkedInput = null;
        this.buttonImage = null;
        this.internalButton = null;
        this.textElement = null; 
        this.textControl = null;
        this.textContainer = null;
        this.isTextEmpty = false;
        this.CheckedChanged = new ASPxClientEvent();
        this.GotFocus = new ASPxClientEvent();
        this.LostFocus = new ASPxClientEvent();    
        this.Click = new ASPxClientEvent();
    },
    InlineInitialize: function(){
        this.InitializeEvents();
        this.InitializeEnabled();
        this.InitializeChecked();
    },
    InitializeEnabled: function(){
        this.SetEnabledInternal(this.clientEnabled, true);
    },
    InitializeChecked: function(){
        this.SetCheckedInternal(this.checked, true);
    },
    InitializeEvents: function(){
        if (!this.isNative) {
            var element = this.GetInternalButton();
            if(_aspxIsExists(element))
                element.onfocus = null;
        
	        var textControl = this.GetTextControl();
	        if (_aspxIsExists(textControl)) {
                if (__aspxIE)
                    _aspxAttachEventToElement(textControl, "onmouseup", _aspxClearSelection);
                _aspxPreventElementDragAndSelect(textControl, false);
            }	         
        }
        
        var name = this.name;
        this.onClick = function() {
            var processOnServer = aspxBClick(name);
            if (!processOnServer) {
                var evt = _aspxGetEvent(arguments[0]);
                if (evt)
                    _aspxPreventEvent(evt);
            }
            return processOnServer;
        };
        this.onGotFocus = function() { aspxBGotFocus(name); };
        this.onLostFocus = function() { aspxBLostFocus(name); };
        this.onKeyUp = function(evt) { aspxBKeyUp(evt, name); };
        this.onKeyDown = function(evt) { aspxBKeyDown(evt, name); };    
        
        if (!this.isNative) {
	        this.AttachNativeHandlerToMainElement("focus", "SetFocus");
	        this.AttachNativeHandlerToMainElement("click", "DoClick");

                var element = this.GetInternalButton();
                if(element != null) element.name = this.uniqueID;
	    }
    },    
    AttachNativeHandlerToMainElement: function(handlerName, correspondingMethodName) {
        var mainElement = this.GetMainElement();
        if (!_aspxIsExistsElement(mainElement))
            return;
        eval("mainElement." + handlerName + " = function() { _aspxBCallButtonMethod('" + this.name + "', '" + correspondingMethodName + "'); }");
    },
    
    GetContentDiv: function(){
        if(!_aspxIsExistsElement(this.contentDiv))
            this.contentDiv = this.GetChild("_CD");
        return this.contentDiv;
    },                         
    GetButtonCell: function(){
        if(!_aspxIsExistsElement(this.buttonCell))
            this.buttonCell = this.GetChild("_B");
        return this.buttonCell;
    },   
    GetButtonCheckedInput: function(){
        if(!_aspxIsExistsElement(this.checkedInput))
            this.checkedInput = _aspxGetElementById(this.name + "_CH");
        return this.checkedInput;
    },  
    GetButtonImage: function(){
        if(!_aspxIsExistsElement(this.buttonImage))
            this.buttonImage = _aspxGetChildByTagName(this.GetButtonCell(), "IMG", 0);
        return this.buttonImage;
    },
    GetInternalButton: function(){
        if(!_aspxIsExistsElement(this.internalButton))
            this.internalButton = this.isNative ? this.GetMainElement() : _aspxGetChildByTagName(this.GetMainElement(), "INPUT", 0);
        return this.internalButton;
    },
    GetTextContainer: function() {
        if (!_aspxIsExists(this.textContainer)) {
            var textElement = this.GetTextElement();
            this.textContainer = _aspxGetChildByTagName(textElement, "SPAN", 0);
        }
        return this.textContainer;
    },
    GetTextElement: function(){
        if(!_aspxIsExistsElement(this.textElement)){
            var contentDiv = this.GetContentDiv();
            if (this.GetButtonImage() == null) 
                this.textElement = contentDiv;
            else {
                this.textElement = _aspxGetChildByTagName(contentDiv, "TD", 0);
                var img = _aspxGetChildByTagName(this.textElement, "IMG", 0);
                if (_aspxIsExists(img))
                    this.textElement = _aspxGetChildByTagName(contentDiv, "TD", 1);
            }
        }
        return this.textElement;
    },    
    GetTextControl: function(){
        if(!_aspxIsExistsElement(this.textControl))
            this.textControl = _aspxGetParentByTagName(this.GetTextElement(), "table");
        if (!_aspxIsExistsElement(this.textControl) || (this.textControl.id == this.name))
            this.textControl = this.GetTextElement();
        return this.textControl;
    },
    
    GetPostfixes: function(){
        return this.isNative ? [""] : ["_B"];
    },
    IsHovered: function(){
        var hoverElement = this.isNative ? this.GetMainElement() : this.GetButtonCell();
        return aspxGetStateController().currentHoverItemName == hoverElement.id;
    },      
    
    SetEnabledInternal: function(enabled, initialization){
        if(!this.enabled) return;
        if(!initialization || !enabled)
            this.ChangeEnabledStateItems(enabled);
        this.ChangeEnabledAttributes(enabled);
    },
    ChangeEnabledAttributes: function(enabled){
        if(this.isNative)
            this.GetMainElement().disabled = !enabled;
        else{
            var element = this.GetInternalButton();
            if(_aspxIsExists(element)) element.disabled = !enabled;;
        }
        this.ChangeEnabledEventsAttributes(_aspxChangeEventsMethod(enabled));
    },
    ChangeEnabledEventsAttributes: function(method){
        var element = this.GetMainElement();
        method(element, "click", this.onClick);
        if (this.allowFocus){
            if (!this.isNative) 
                element = this.GetInternalButton();
            method(element, "focus", this.onGotFocus);
            method(element, "blur", this.onLostFocus);
            if (!this.isNative){
                method(element, "keyup",  this.onKeyUp);
                method(element, "blur",  this.onKeyUp);
                method(element, "keydown", this.onKeyDown);
            }
        }
    },
    ChangeEnabledStateItems: function(enabled){
        if(!this.isNative){
            aspxGetStateController().SetElementEnabled(this.GetButtonCell(), enabled);
            this.UpdateFocusedStyle();
        }
    },

    OnFocus: function(){    
        if(!this.allowFocus) return false;
        
        this.focused = true;            
        if(this.isInitialized)
            this.RaiseFocus();            
        this.UpdateFocusedStyle();
    },        
    OnLostFocus: function() {
        if(!this.allowFocus) return false;
        
        this.focused = false;            
        if(this.isInitialized)
            this.RaiseLostFocus();
        this.UpdateFocusedStyle();
    },
    CauseValidation: function() {
        if (this.causesValidation && _aspxIsExistsType(typeof(ASPxClientEdit)))
            return (this.validationContainerID != null) ? ASPxClientEdit.ValidateEditorsInContainerById(this.validationContainerID, this.validationGroup) :
                ASPxClientEdit.ValidateGroup(this.validationGroup);
        else
            return true;
    },
    OnClick: function(){
        if (this.clickLocked)
            return true;
        else if(this.checked && this.groupName != "" && this.GetCheckedGroupList().length > 1)
            return;
        this.SetFocus();
        var isValid = this.CauseValidation();
        var processOnServer = this.autoPostBack;
        // Process CheckedChanged
        if (this.groupName != "") {
            if(this.GetCheckedGroupList().length == 1)
                this.SetCheckedInternal(!this.checked, false);
            else {
                this.SetCheckedInternal(true, false);
                this.ClearButtonGroupChecked(true);
            }
            processOnServer = this.RaiseCheckedChanged();
            if (processOnServer && isValid)
                this.SendPostBack("CheckedChanged");
        }
        // Process click
        processOnServer = this.RaiseClick();
        if (processOnServer && isValid){
            this.SendPostBack("Click");
            return true;
        }
        return false;
    },
    OnKeyUp: function(evt) {
       if(this.pressed)
            this.SetUnpressed();
    },
    OnKeyDown: function(evt) {
       if((evt.keyCode == ASPxKeyConsts.KEY_ENTER) || (evt.keyCode == ASPxKeyConsts.KEY_SPACE))
           this.SetPressed();
    },  

    GetChecked: function(){
        return this.groupName != "" ? this.GetButtonCheckedInput().value == "1" : false;
    },  
    GetCheckedGroupList: function(){
        var collection = aspxGetControlCollection();    
        var result = new Array();
        for(var name in collection.elements) {
            var element = collection.elements[name];
            if (element != null && ASPxIdent.IsASPxClientButton(element) &&
                (element.groupName == this.groupName))
                _aspxArrayPush(result, element);
        }
        return result;
    },
    ClearButtonGroupChecked: function(raiseCheckedChanged){
        var list = this.GetCheckedGroupList();
        for(var i = 0; i < list.length; i ++){
            if(list[i] != this && list[i].checked) {
                list[i].SetCheckedInternal(false, false);
                if(raiseCheckedChanged)
                    list[i].RaiseCheckedChanged();
            }
        }
    },
    ApplyCheckedStyle: function(){
        var stateController = aspxGetStateController();
        if(this.IsHovered()) 
            stateController.SetCurrentHoverElement(null);        
        stateController.SelectElementBySrcElement(this.GetButtonCell());
    }, 
    ApplyUncheckedStyle: function(){
        var stateController = aspxGetStateController();
        if(this.IsHovered()) 
            stateController.SetCurrentHoverElement(null);
        stateController.DeselectElementBySrcElement(this.GetButtonCell());
    },     
    SetCheckedInternal: function(checked, initialization){
        if(initialization && checked || (this.checked != checked)){
            this.checked = checked;
            var inputElement = this.GetButtonCheckedInput();
            if(_aspxIsExists(inputElement)) inputElement.value = checked ? "1" : "0";                                    
            if(checked)
                this.ApplyCheckedStyle();
            else
                this.ApplyUncheckedStyle();
        }
    },
    
    ApplyPressedStyle: function(){
        aspxGetStateController().OnMouseDownOnElement(this.GetButtonCell());
    },
    ApplyUnpressedStyle: function(){    
        aspxGetStateController().OnMouseUpOnElement(this.GetButtonCell());
    },
    SetPressed: function(){
        this.pressed = true;
        this.ApplyPressedStyle();
    }, 
    SetUnpressed: function(){    
        this.pressed = false;
        this.ApplyUnpressedStyle();        
    },
    
    SetFocus: function(){
        if (this.allowFocus && !this.focused){
            var element = this.GetInternalButton();
            if (_aspxIsFocusable(element) && _aspxGetActiveElement() != element) 
                element.focus();
         }       
    },    
    ApplyFocusedStyle: function(){
        if(this.focusElementSelected) return;
        aspxGetStateController().SelectElementBySrcElement(this.GetContentDiv());
        this.focusElementSelected = true;
    },
    ApplyUnfocusedStyle: function(){    
        if(!this.focusElementSelected) return;
        aspxGetStateController().DeselectElementBySrcElement(this.GetContentDiv());
        this.focusElementSelected = false;
    },
    UpdateFocusedStyle: function(){  
        if(this.isNative) return;
        if(this.enabled && this.clientEnabled && this.allowFocus && this.focused)
            this.ApplyFocusedStyle();
        else
            this.ApplyUnfocusedStyle();
    },
    
    SendPostBack: function(postBackArg){
        var arg = _aspxIsExists(postBackArg) ? postBackArg : "";
        if(_aspxIsExists(this.autoPostBackFunction))
            this.autoPostBackFunction(arg);
        else if(!this.useSubmitBehavior)
            ASPxClientControl.prototype.SendPostBack.call(this, arg);
        
        if(this.useSubmitBehavior && !this.isNative)
            this.ClickInternalButton();
    },
    ClickInternalButton: function(){
        var element = this.GetInternalButton();
        if(element != null) {
            this.clickLocked = true;
            if (__aspxNS)
                this.CreateUniqueIDCarrier(); // B93118
            element.click();
            if (__aspxNS)
                this.RemoveUniqueIDCarrier(); // B93118
            this.clickLocked = false;
        }
    },
    CreateUniqueIDCarrier: function() {
        var field = document.createElement("input");
        field.type = "hidden";
        field.name = this.uniqueID;
        field.id = this.GetUniqueIDCarrierID();
        var form = _aspxGetParentByTagName(this.GetMainElement(), "form");
        if (form)
            form.appendChild(field);
    },
    RemoveUniqueIDCarrier: function() {
        var field = document.getElementById(this.GetUniqueIDCarrierID());
        if (_aspxIsExists(field))
            field.parentNode.removeChild(field);
    },
    GetUniqueIDCarrierID: function() {
        return this.uniqueID + "_UIDC";
    },
    
    // API
    DoClick: function(){
        if(!this.enabled || !this.clientEnabled) return;
        
        var buttonElement = (this.isNative) ? this.GetMainElement() : this.GetInternalButton();
        if(_aspxIsExists(buttonElement))
            buttonElement.click();   
        else 
            this.OnClick();   
    }, 
    GetChecked: function(){
        return this.checked;
    }, 
    SetChecked: function(checked){
        this.SetCheckedInternal(checked, false);
        this.ClearButtonGroupChecked(false);
    },
    GetText: function(){
        if (this.isTextEmpty)
            return "";
        else
            return this.isNative ? this.GetMainElement().value : this.GetTextContainer().innerHTML
    },
    SetText: function(text){
        this.isTextEmpty = (text == null || text == "");
        if (this.isNative)
            this.GetMainElement().value = (text != null) ? text : "";
        else {
            var textContainer = this.GetTextContainer();
            textContainer.innerHTML = this.isTextEmpty ? "&nbsp;" : text;
        }
    }, 
    SetEnabled: function(enabled){
        if (this.clientEnabled != enabled) {
            if (!enabled && this.focused)
                this.OnLostFocus();
            this.clientEnabled = enabled;
            this.SetEnabledInternal(enabled, false);
        }
    }, 
    GetEnabled: function(){
        return this.enabled && this.clientEnabled;
    },
    Focus: function(){
        this.SetFocus();
    },

    RaiseCheckedChanged: function(){
        var processOnServer = this.autoPostBack || this.IsServerEventAssigned("CheckedChanged");
        if(!this.CheckedChanged.IsEmpty()){
            var args = new ASPxClientProcessingModeEventArgs(processOnServer);
            this.CheckedChanged.FireEvent(this, args);
            processOnServer = args.processOnServer;
        }
        return processOnServer;
    },
    RaiseFocus: function(){
        if(!this.GotFocus.IsEmpty()){
            var args = new ASPxClientEventArgs();
            this.GotFocus.FireEvent(this, args);
        }
    },
    RaiseLostFocus: function(){
        if(!this.LostFocus.IsEmpty()){
            var args = new ASPxClientEventArgs();
            this.LostFocus.FireEvent(this, args);
        }
    },
    RaiseClick: function(){
        var processOnServer = this.autoPostBack || this.IsServerEventAssigned("Click");
        if(!this.Click.IsEmpty()){
            var args = new ASPxClientProcessingModeEventArgs(processOnServer);
            this.Click.FireEvent(this, args);
            processOnServer = args.processOnServer;
        }
        return processOnServer;
    }
});

ASPxIdent.IsASPxClientButton = function(obj) {
    return _aspxIsExists(obj.isASPxClientButton) && obj.isASPxClientButton;
};

function _aspxBCallButtonMethod(name, methodName) {
    var button = aspxGetControlCollection().Get(name); 
    if (button != null)
        eval("button." + methodName + "()");
}

function aspxBGotFocus(name){
    var button = aspxGetControlCollection().Get(name); 
    if(button != null)
        return button.OnFocus();
}
function aspxBLostFocus(name){
    var button = aspxGetControlCollection().Get(name);
    if(button != null) 
        return button.OnLostFocus();
}
function aspxBClick(name){
    var button = aspxGetControlCollection().Get(name); 
    if(button != null)
        return button.OnClick();
}
function aspxBKeyDown(evt,name){
    var button = aspxGetControlCollection().Get(name); 
    if(button != null)
        button.OnKeyDown(evt);
}
function aspxBKeyUp(evt,name){
    var button = aspxGetControlCollection().Get(name); 
    if(button != null)
        button.OnKeyUp(evt);
}