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.focusElementSelected = false;
  this.pressed = false;
  this.useSubmitBehavior = true;
  this.validationGroup = "";
  this.validationContainerID = null;
  this.validateInvisibleEditors = false;
  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();
  this.PreventButtonImageDragging();
 },
 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");
  }
 },
 PreventButtonImageDragging: function() {
  var image = this.GetButtonImage();
  if(_aspxIsExists(image)) {
   if(__aspxNetscapeFamily)
    image.onmousedown = function(evt) {
     evt.cancelBubble = true;
     return false;
    };
   else
    image.ondragstart = function() {
     return false;
    };
  }
 },
 AttachNativeHandlerToMainElement: function(handlerName, correspondingMethodName) {
  var mainElement = this.GetMainElement();
  if (!_aspxIsExistsElement(mainElement))
   return;
  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();
   if(_aspxIsExists(element)) {
    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();
  }
 },
 RequiredPreventDoublePostback: function(){
  return __aspxFirefox; 
 },
 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, this.validateInvisibleEditors) :
    ASPxClientEdit.ValidateGroup(this.validationGroup, this.validateInvisibleEditors);
  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;
  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");
  }
  processOnServer = this.RaiseClick();
  if (processOnServer && isValid){
   var requiredPreventDoublePostback = this.RequiredPreventDoublePostback();
   if(requiredPreventDoublePostback)
    _aspxSetTimeout("_aspxBCallButtonMethod(\"" + this.name + "\", \"SendPostBack\", \"Click\" );", 0); 
   else
    this.SendPostBack("Click");
   return !requiredPreventDoublePostback;
  }
  return false;
 },
 OnKeyUp: function(evt) {
    if(this.pressed)
   this.SetUnpressed();
 },
 OnKeyDown: function(evt) {
    if(evt.keyCode == ASPxKey.Enter || evt.keyCode == ASPxKey.Space)
     this.SetPressed();
 },  
 GetChecked: function(){
  return this.groupName != "" ? this.GetButtonCheckedInput().value == "1" : false;
 },  
 GetCheckedGroupList: function(){
  var result = new Array();
  aspxGetControlCollection().ForEachControl(function(control) {
   if (ASPxIdent.IsASPxClientButton(control) && (control.groupName == this.groupName) && control.RenderExistsOnPage())
    _aspxArrayPush(result, control);
  }, this);
  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)
   return;
  var element = this.GetInternalButton();
  if(_aspxIsExists(element)) {
   var hiddenInternalButtonRequiresVisibilityToGetFocused = __aspxWebKitFamily  && !this.isNative ;
   if(hiddenInternalButtonRequiresVisibilityToGetFocused)
    ASPxClientButton.MakeHiddenElementFocusable(element);
   if(_aspxIsFocusable(element) && _aspxGetActiveElement() != element)
    element.focus();
   if(hiddenInternalButtonRequiresVisibilityToGetFocused)
    ASPxClientButton.RestoreHiddenElementAppearance(element);
  }
 },
 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){
  if(!this.enabled || !this.clientEnabled)
   return;
  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(_aspxIsExists(element)) {
   this.clickLocked = true;
   if (__aspxNetscapeFamily)
    this.CreateUniqueIDCarrier(); 
   _aspxDoElementClick(element);
   if (__aspxNetscapeFamily)
    this.RemoveUniqueIDCarrier(); 
   this.clickLocked = false;
  }
 },
 CreateUniqueIDCarrier: function() {
  var name = this.uniqueID;
  var id = this.GetUniqueIDCarrierID();
  var field = _aspxCreateHiddenField(name, id);
  var form = _aspxGetServerForm();
  form.appendChild(field);
 },
 RemoveUniqueIDCarrier: function() {
  var field = document.getElementById(this.GetUniqueIDCarrierID());
  if (_aspxIsExists(field))
   field.parentNode.removeChild(field);
 },
 GetUniqueIDCarrierID: function() {
  return this.uniqueID + "_UIDC";
 },
 DoClick: function(){
  if(!this.enabled || !this.clientEnabled)
   return;
  var button = this.isNative ? this.GetMainElement() : this.GetInternalButton();
  if(_aspxIsExists(button))
   _aspxDoElementClick(button);
  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;
 }
});
ASPxClientButton.Cast = ASPxClientControl.Cast;
ASPxClientButton.MakeHiddenElementFocusable = function(element) {
  element.__dxHiddenElementState = {
   parentDisplay: element.parentNode.style.display,
   height: element.style.height,
   width: element.style.width
  };
  element.parentNode.style.display = "block";
  element.style.height = "1px";
  element.style.width = "1px";
};
ASPxClientButton.RestoreHiddenElementAppearance = function(element) {
 var state = element.__dxHiddenElementState;
 element.parentNode.style.display = state.parentDisplay;
 element.style.height = state.height;
 element.style.width = state.width;
 delete element.__dxHiddenElementState;
};
ASPxIdent.IsASPxClientButton = function(obj) {
 return _aspxIsExists(obj.isASPxClientButton) && obj.isASPxClientButton;
};
function _aspxBCallButtonMethod(name, methodName, arg) {
 var button = aspxGetControlCollection().Get(name); 
 if (button != null)
  button[methodName](arg);
}
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);
}

