﻿var DialogDrag = false;
var DragStartX = null;
var DragStartY = null;
var DialogStartX = null;
var DialogStartY = null;
var WindowWidth = 0;
var WindowHeight = 0;

function ShowDialogPanel(title, detail, className, allowCancel)
{
  getId("divClearPane").style.display = "";
	getId("tblDialogPanel").style.display = "";

	getId("divDialogDetail").style.display = "";
	getId("divDialogButtons").style.display =  "";
	getId("tdDialogContent").className = "";
  getId("divDialogDetail").innerHTML = detail;
	getId("divDynamicDialogFrame").style.display = "none";
	
  getId("tdDialogHeading").innerHTML = title;
  getId("tdDialogHeading").className = className;
  getId("btnDialogOk").className = "Blue";
  
  if (allowCancel)
  {
    getId("btnDialogCancel").style.display = "";
    getId("btnDialogCancel").focus();
  }
  else
  {
    getId("btnDialogCancel").style.display = "none";
    getId("btnDialogOk").focus();
  }
  
  FixDialogPosition();
}

function ShowDynamicDialog(title, frameSrc, className, frameWidth, frameHeight, helpPage)
{
  getId("xDialogPanelType").value = "Dynamic";
  getId("divClearPane").style.display = "";
	getId("tblDialogPanel").style.display = "";

	getId("divDialogDetail").style.display = "none";
	getId("divDialogButtons").style.display = "none";
	getId("tdDialogContent").className = "Data";
  getId("divDialogDetail").innerHTML = "";
	getId("divDynamicDialogFrame").style.display = "";
	
  getId("tdDialogHeading").innerHTML = title;
  getId("tdDialogHeading").className = className;
  getId("btnDialogOk").className = "Blue";
  
  getId("DynamicDialogFrame").contentWindow.location.replace(frameSrc);
  if (frameWidth != null) getId("DynamicDialogFrame").style.width = frameWidth + "px";
  if (frameHeight != null) getId("DynamicDialogFrame").style.height = frameHeight + "px";
  if (helpPage != null)
  {
    hl = "<div style=\"float: right;\"><a class=\"DynamicHelpLink\" href=\"javascript:void(0);\" onclick=\"HelpCenter('" + helpPage + "'); return false;\"><span class=\"HLImage\"><img src=\"" + URL_AMWebRoot + "images/buttons/help.gif\" alt=\"\" /></span>Help Center</a></div>";
    getId("tdDialogHeading").innerHTML = hl + getId("tdDialogHeading").innerHTML;
  }
  
  FixDialogPosition();
}

// ********************************************************************************************************

function HideDialogPanel()
{
	getId("divClearPane").style.display = "none";
	getId("tblDialogPanel").style.display = "none";
  getId("DynamicDialogFrame").contentWindow.location.replace("about:blank");
  getId("xDialogPanelType").value = "";
}

function CloseDialogPanel(cancel)
{
  var panelType = getId("xDialogPanelType").value;
  HideDialogPanel();
	
	if (panelType == "SmartClick")
	{
	  SmartClick_Save();
	}
	else
	{
    if (getId("btnDialogCancel").style.display == "")
    {
      if (window.DialogOkHandler) DialogOkHandler();
	  }
	  else
	  {
      if (window.DialogCloseHandler) DialogCloseHandler();
	  }
	}
	return false;
}

function CancelDialogPanel()
{
  var panelType = getId("xDialogPanelType").value;
  HideDialogPanel();
  
	if (panelType == "SmartClick")
	{
	  SmartClick_Cancel();
	}
	else
	{
    if (getId("btnDialogCancel").style.display == "")
    {
      if (window.DialogCancelHandler) DialogCancelHandler();
	  }
	  else
	  {
      if (window.DialogCloseHandler) DialogCloseHandler();
	  }
  }
  return false;
}

// ********************************************************************************************************

function StartDialogDrag(e)
{
  getId("imgDialogDrag").style.display = "";
  getId("imgDialogDrag").style.width = getId("tblDialogPanel").offsetWidth - 12;
  getId("imgDialogDrag").style.height = getId("tblDialogPanel").offsetHeight - 37; 
  
	if (document.all)
	{
		DragStartX = e.x;
		DragStartY = e.y;
  }
  else
  {
		DragStartX = e.pageX;
		DragStartY = e.pageY;
  }
  
  DialogStartX = parseInt(getId("tblDialogPanel").style.left);
  DialogStartY = parseInt(getId("tblDialogPanel").style.top);
  DialogDrag = true;
  document.selection.empty();
}

function StopDialogDrag()
{
  if (DialogDrag)
  {
    DialogDrag = false;
    getId("imgDialogDrag").style.display = "none";
    document.selection.empty();
  }
}

function GetWindowSize()
{
  if (typeof(window.innerWidth) == 'number')
  {
    WindowWidth = window.innerWidth;
    WindowHeight = window.innerHeight;
  }
  else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight))
  {
    WindowWidth = document.documentElement.clientWidth;
    WindowHeight = document.documentElement.clientHeight;
  }
  else if (document.body && (document.body.clientWidth || document.body.clientHeight))
  {
    WindowWidth = document.body.clientWidth;
    WindowHeight = document.body.clientHeight;
  }
}

function FixDialogPosition(x, y)
{
  var Dialog = getId("tblDialogPanel");
  var DialogWidth = Dialog.offsetWidth;
  var DialogHeight = Dialog.offsetHeight;
  var DialogX = parseInt(Dialog.style.left);
  var DialogY = parseInt(Dialog.style.top);

  var NewPosX = 0;
  var NewPosY = 0;

  if (x != null && y != null)
  {
    NewPosX = DialogStartX + x - DragStartX;
    NewPosY = DialogStartY + y - DragStartY;
  }
  else
  {
    NewPosX = WindowWidth / 2 - DialogWidth / 2;
    NewPosY = WindowHeight / 2 - DialogHeight / 2;
  }
  
  if (NewPosX > WindowWidth - DialogWidth) NewPosX = WindowWidth - DialogWidth;
  if (NewPosY > WindowHeight - DialogHeight) NewPosY = WindowHeight - DialogHeight;
  if (NewPosX < 0) NewPosX = 0;
  if (NewPosY < 0) NewPosY = 0;

  Dialog.style.left = NewPosX + "px";
  Dialog.style.top = NewPosY + "px";
}