var __aspxDialogFormCallbackStatus = "DialogForm";
var __aspxCurrentControlNameInDialog = "";
var __aspxAreKeyboardEventsInitialized = false;
function aspxAdjustControlsSizeInDialogWindow() {
 var control = aspxGetControlCollection().Get(__aspxCurrentControlNameInDialog); 
 var curDialog = control != null ? ASPxDialog.GetLastDialog(control) : null;
 if (curDialog != null)
  ASPxClientControl.GetControlCollection().AdjustControls(curDialog.GetDialogPopup().GetMainElement());
}
ASPxDialog = _aspxCreateClass(null, {
 constructor: function(name) {
  this.name = name;
  this.initInfo = null;  
  this.editorKeyDownProccesed = false;
  this.keyDownHandlers = {};
  this.InitializeKeyHandlers();
 },
 InitializeKeyHandlers: function() {
  this.AddKeyDownHandler("ESC", "HideDialog");
 },
 AddKeyDownHandler: function(shortcutString, handler) {
  this.keyDownHandlers[_aspxParseShortcutString(shortcutString)] = handler;
 },
 DoCustomAction: function(result, params) {
 },
 GetDialogCaptionText: function() {
  return "";
 },
 GetInitInfoObject: function() {
  return null;
 },
 InitializeDialogFields: function(initInfo) {
 },
 SetFocusInField: function() {
 },
 Execute: function(ownerControl, popupElementID) {
  this.ownerControl = ownerControl;
  __aspxCurrentControlNameInDialog = this.ownerControl.name;
  ASPxDialog.PushDialogToCollection(this.ownerControl, this);
  this.InitializePopupEvents();
  this.GetDialogPopup().SetHeaderText(this.GetDialogCaptionText());  
  if (_aspxIsExists(popupElementID))
   this.GetDialogPopup().ShowAtElementByID(popupElementID);
  else
   this.GetDialogPopup().Show();
  if (this.GetDialogContent(this.name) == null) {
   this.SendCallbackForDialogContent();
   this.ShowLoadingPanelInDialogPopup();
  }
  else {
   this.ExecuteInternal(this.GetDialogContent(this.name));
   _aspxRunStartupScripts();
   this.OnInitComplete();
  }
 },
 ExecuteInternal: function(result) {
  this.initInfo = this.GetInitInfoObject();
  this.GetDialogPopup().SetContentHtml(result);
  if (this.GetDialogPopup().IsVisible())
   this.GetDialogPopup().UpdatePosition();
 },
 GetDialogPopup: function() {
  if (_aspxIsExists(this.ownerControl.GetDialogPopupControl))
   return this.ownerControl.GetDialogPopupControl();
  return null;
 },
 AddDialogContentToHash: function(name, content) {
  this.GetDialogContentHashTable()[this.name] = content;
 },
 GetDialogContent: function(name) { 
  return _aspxIsExists(this.GetDialogContentHashTable()[this.name]) ? 
     this.GetDialogContentHashTable()[this.name] : null;
 },
 GetDialogContentHashTable: function() {
  if (_aspxIsExists(this.ownerControl.dialogContentHashTable))
   return this.ownerControl.dialogContentHashTable;
  return null;
 },
 InitializePopupEvents: function() {
  if (this.GetDialogPopup().CloseButtonClick.IsEmpty()) {
   var func = _aspxCreateEventHandlerFunction("aspxOnDialogCloseButtonClick", this.ownerControl.name, false);
   this.GetDialogPopup().CloseButtonClick.AddHandler(func);
   func = _aspxCreateEventHandlerFunction("aspxOnDialogClose", this.ownerControl.name, false);
   this.GetDialogPopup().CloseUp.AddHandler(func);
  }
 },
 InitCustomKeyboardHandling: function() {
  if (!__aspxAreKeyboardEventsInitialized) {
   __aspxAreKeyboardEventsInitialized = true;      
   _aspxAttachEventToDocument(__aspxWebKitFamily ? "keydown" : "keypress", aspxDialogDocumentKeypress);
   if (__aspxNetscapeFamily)
    this.ReplaceKBSIKeyDown();
  }
 },
 ReplaceKBSIKeyDown: function() { 
  var original = aspxKBSIKeyDown;
  aspxKBSIKeyDown = function(name, evt) {
   var isProcessed = original(name, evt);
   var ownerControl = aspxGetControlCollection().Get(__aspxCurrentControlNameInDialog);
   var curDialog = ownerControl != null ? ASPxDialog.GetLastDialog(ownerControl) : null;
   if (curDialog != null) curDialog.OnInnerEditorKeyDown(evt, isProcessed);
   return isProcessed;
  };
 },
 SendCallbackForDialogContent: function() {
  this.ownerControl.callbackOwner = this;
  this.ownerControl.SendCallback(__aspxDialogFormCallbackStatus, this.name, false);
 },
 ShowLoadingPanelInDialogPopup: function() {
  this.GetDialogPopup().SetContentHtml("");
  var contentElement = this.GetDialogPopup().GetWindowContentElement(-1);  
  this.ownerControl.CreateLoadingDiv(contentElement);
  this.ownerControl.CreateLoadingPanelInsideContainer(contentElement);
 },
 ShowLoadingPanelOverDialogPopup: function() {
  var offsetElement = _aspxGetParentByTagName(this.GetDialogPopup().GetWindowContentElement(-1), "table");
  this.ownerControl.CreateLoadingDiv(document.body, offsetElement);
  this.ownerControl.CreateLoadingPanelWithAbsolutePosition(document.body, offsetElement);
 },
 HideLoadingPanelOverDialogPopup: function() {
  this.ownerControl.HideLoadingDiv();
  this.ownerControl.HideLoadingPanel();
 }, 
 HideDialog: function() {
    this.GetDialogPopup().Hide();
    this.OnCloseButtonClick();
    this.OnClose();
 },
   OnCallback: function(result) {
  this.ExecuteInternal(result);
    this.AddDialogContentToHash(this.name, result);    
   },
 OnCallbackError: function(result, data) {
  this.ownerControl.callbackOwner = null;
 },
 OnEndCallback: function() {
  this.ownerControl.callbackOwner = null;
    this.OnInitComplete();
 },
 OnClose: function() {
  ASPxDialog.RemoveLastDialog(this.ownerControl);
 },
 OnCloseButtonClick: function() {
 },
   OnComplete: function(result, params) {
    this.GetDialogPopup().Hide();
  this.DoCustomAction(result, params);
   },
 OnDocumentKeyPress: function(evt) {
  if (!this.editorKeyDownProccesed) {
   var handler = this.keyDownHandlers[_aspxGetShortcutCode(evt.keyCode, evt.ctrlKey, evt.shiftKey, evt.altKey)];
   if(_aspxIsExists(handler))
    this[handler](evt);
  }
  this.editorKeyDownProccesed = false;
 },
 OnInnerEditorKeyDown: function(evt, isProcessed) {
  this.editorKeyDownProccesed = _aspxIsExists(isProcessed) ? !isProcessed : false;
 },
 OnInitComplete: function() {
  this.InitCustomKeyboardHandling();
  this.InitializeDialogFields(this.initInfo);
  this.SetFocusInField();
 }
});
ASPxDialog.PushDialogToCollection = function(ownerControl, dialog) {
 if (!_aspxIsExists(ownerControl.dialogArray))
  ownerControl.dialogArray = new Array();
 _aspxArrayPush(ownerControl.dialogArray, dialog);
}
ASPxDialog.GetLastDialog = function(ownerControl) {
 if (_aspxIsExists(ownerControl.dialogArray)) {
  var length = ownerControl.dialogArray.length;
  return length > 0 ? ownerControl.dialogArray[length - 1] : null;
 }
 return null;
}
ASPxDialog.RemoveLastDialog = function(ownerControl) {
 var array = ownerControl.dialogArray;
 if (_aspxIsExists(array) && array.length > 0)
  _aspxArrayRemoveAt(array, array.length - 1);
}
function aspxOnDialogCloseButtonClick(name) {
 var ownerControl = aspxGetControlCollection().Get(name);
 var curDialog = ownerControl != null ? ASPxDialog.GetLastDialog(ownerControl) : null;
 if (curDialog != null)
  return curDialog.OnCloseButtonClick();
}
function aspxOnDialogClose(name) {
 var ownerControl = aspxGetControlCollection().Get(name); 
 var curDialog = ownerControl != null ? ASPxDialog.GetLastDialog(ownerControl) : null;
 if (curDialog != null) 
  return curDialog.OnClose();
}
function aspxDialogComplete(result, params) {
 var ownerControl = aspxGetControlCollection().Get(__aspxCurrentControlNameInDialog);
 var curDialog = ownerControl != null ? ASPxDialog.GetLastDialog(ownerControl) : null;
 if (curDialog != null)
  return curDialog.OnComplete(result, params);
}
function aspxDialogDocumentKeypress(evt) {
 var ownerControl = aspxGetControlCollection().Get(__aspxCurrentControlNameInDialog);
 var curDialog = ownerControl != null ? ASPxDialog.GetLastDialog(ownerControl) : null;
 if (curDialog != null)
  ASPxDialog.GetLastDialog(ownerControl).OnDocumentKeyPress(evt);
}
