﻿// ModalWin controls client script
//================================
// ModalWin, AlertWin, ConfirmWin

/*
  KNOWN ISSUES in ModalWin client script
  ======================================
  // - FIXED - 1. If a DOCTYPE declaration is present on page ("view source"), the ModalWin is not positioned correctly.
  2. The .Show method cannot be called onload or before. window.setTimeout should be used in this case (until it is supported).
  3. ONLY IN CET LOCAL NETWORK (local intranet zone), SEEMS TO DISAPPEAR IN IE7: When the previous window.location is about:blank, the ModalWin throws javascript errors.
*/



 
// ModalWin Constructor
//---------------------

function ModalWin (sID, onEndCallBack, onStartCallBack, sSrc, sWinType, sBut1ID, sBut2ID, bAllwaysTop, sMaskColor)
//       ~~~~~~~~
{  
  this.id = sID;
  this.oWin = document.getElementById(this.id + "_Win");
  this.oMask = document.getElementById(this.id + "_Mask");

  //this is a new feature where mask indow can get other background color then its default(white)
  if (sMaskColor != null && sMaskColor != "" && sMaskColor != "white")
  {
    setTimeout("SetMaskColor('" + this.id + "_Mask" + "', '" + sMaskColor + "')", 100);
  }
  this.sSrc = sSrc;
  this.sWinType = sWinType;
  this.bGloablAllwaysTop = false;
  try
  {
    this.onEndCallBack = eval(onEndCallBack);
  }
  catch(e)
  {
    alert("ModalWin Error: function " + onEndCallBack + " is not defined");
  }
  try
  {
    this.onStartCallBack = eval(onStartCallBack);
  }
  catch(e)
  {
    alert("ModalWin Error: function " + onStartCallBack + " is not defined");
  }
  this.sBut1ID = sBut1ID;
  this.sBut2ID = sBut2ID;
  
  if((bAllwaysTop) && ((bAllwaysTop == true) || (bAllwaysTop == "true") || (bAllwaysTop == "True")))
  {     
    this.bGloablAllwaysTop = true;
  }
  else
  {
    this.bGloablAllwaysTop = false;
  }

  
}

// ModalWin Methods
//-----------------
ModalWin.prototype.Show = function (nWidth, nHeight, url, sKey, onEndCallBack, onStartCallBack)
{
  // the parameters are optional of course. .Show() is also valid.
  // parameters that are not supplied, default to the server-side settings.
  // use null to suppress defaults, use undefined to skip paramreter and use default.
  
  // load parameters:
  if (nWidth > 0)
    this.oWin.style.width = nWidth;
  if (nHeight > 0)
     this.oWin.style.height = nHeight;
  
  if (sKey != undefined)
    this.sKey = sKey;
  else
    this.sKey = this.id;
  if (url != undefined)
    this.sSrcTemp = url;
  else if (this.sSrc && this.sSrc.length > 0)
    this.sSrcTemp = this.sSrc;
  if (onStartCallBack != undefined)
    this.onStartCurrent = onStartCallBack;
  else
    this.onStartCurrent = this.onStartCallBack;
  if (onEndCallBack != undefined)
    this.onEndCurrent = onEndCallBack;
  else
    this.onEndCurrent = this.onEndCallBack;
  if (this.onStartCurrent)
    this.onStartCurrent(this.sKey)
  this.Visible = true;
  
  // show the window
  this.oWin.style.display = "";
  this.oMask.style.display = "";
  if (this.sSrcTemp && this.sSrcTemp.length > 0)
  {
    this.oWin.src = this.sSrcTemp;
  }
  else          
  {
    var innerDoc = this.oWin.contentWindow.document;
    innerDoc.body.innerHTML = this.oWin.innerHTML;
  }
  this.oMask.style.width = this.oMask.style.height = 0;
  this.Locate();
  var oClientObject = ModalWin.GetClientViewObject();
  this.oMask.style.width = Math.max(oClientObject.scrollWidth, oClientObject.clientWidth);
  this.oMask.style.height = Math.max(oClientObject.scrollHeight, oClientObject.clientHeight);
  this.oMask.style.left = 0;
  this.oMask.style.top = 0;
//  if (this.sSrcTemp && this.sSrcTemp.length > 0)
//    this.oWin.onreadystatechange = new Function("if (" + this.id + ".oWin.readyState == 'complete') " + this.id + ".AttachButtonEvents();");
//  else
//    this.AttachButtonEvents();
  if (!(this.sSrcTemp && this.sSrcTemp.length > 0))
    this.AttachButtonEvents();
  this.oWin.onreadystatechange = new Function("if (" + this.id + ".oWin.readyState == 'complete') " + this.id + ".AttachButtonEvents();");
  window.attachEvent("onscroll", new Function(this.id + ".Locate()"));
  window.attachEvent("onresize", new Function(this.id + ".Locate()"));
}

var ModalWin_nInitialScrollHeight = 800;  
window.attachEvent ("onload", function(){var oClientObject = ModalWin.GetClientViewObject(); ModalWin_nInitialScrollHeight = oClientObject.scrollHeight})
ModalWin.prototype.Locate = function() 
{
  var oClientObject = ModalWin.GetClientViewObject();
  //alert("locate")
  this.oWin.style.left = 0;
  this.oWin.style.top = 0;
  this.oWin.style.left = (oClientObject.scrollWidth - oClientObject.clientWidth - oClientObject.scrollLeft) + (oClientObject.clientWidth - this.oWin.style.pixelWidth) / 2;

  var nScrollTop = oClientObject.scrollTop;
  if (ModalWin_nInitialScrollHeight > nScrollTop + this.oWin.style.pixelHeight) {
    nWinTop = nScrollTop + Math.abs(this.oWin.style.pixelHeight - document.documentElement.offsetHeight) / 2;
  }
  else {
    nWinTop = ModalWin_nInitialScrollHeight - this.oWin.style.pixelHeight;
  }
  if (nWinTop < 0)
    nWinTop = Math.abs(this.oWin.style.pixelHeight - oClientObject.clientHeight) / 2

  this.oWin.style.top = nWinTop;

  if (this.bGloablAllwaysTop) {
    this.oWin.style.top = 0;
  }
  // adjust the top position of the modal win when inside absolute positioned iframe when that frame exceeds the client visible area
  if (window.frameElement != null) {
    if (window.frameElement.currentStyle.position == "absolute") 
    {
      var oParentClientObject = ModalWin.GetClientViewObject(window.parent);
      // if the parent frame height is larger than the visible client area
      if (oParentClientObject.clientHeight < window.frameElement.offsetHeight)
      {
        nWinTop = -window.frameElement.getBoundingClientRect().top + Math.abs(oParentClientObject.clientHeight - this.oWin.style.pixelHeight) / 2
        this.oWin.style.top = nWinTop;
      }
    }
  }
}

ModalWin.prototype.AttachButtonEvents = function ()
{
  var oClientObject = ModalWin.GetClientViewObject();
  ModalWin_nInitialScrollHeight = oClientObject.scrollHeight;
  this.oWin.contentWindow.ModalWin_EndDialog = new Function("oReturnValue", "window." + this.id + ".EndDialog(oReturnValue);");
  var innerDoc = this.oWin.contentWindow.document;
  if (this.sWinType)
  {
    switch (this.sWinType)
    {
      case "alert":
        innerDoc.getElementById(this.sBut1ID).attachEvent("onclick", this.oWin.contentWindow.ModalWin_EndDialog);
        break;
      case "confirm":
        innerDoc.getElementById(this.sBut1ID).attachEvent("onclick", new Function("window." + this.id + ".EndDialog(true);"));
        innerDoc.getElementById(this.sBut2ID).attachEvent("onclick", new Function("window." + this.id + ".EndDialog(false);"));
        break;
      default:
        break;
    }
  }
  if (!this.sSrc && innerDoc.styleSheets.length == 0)
  {
    for (var i = 0; i < document.styleSheets.length; i++)
      innerDoc.createStyleSheet(document.styleSheets[i].href);
  }
  if (this.oWin.allowTransparency)
  {
    innerDoc.body.style.background = "transparent";
  }
}
ModalWin.prototype.Hide = function ()
{
  this.oWin.style.display = 
  this.oMask.style.display = "none";
  if (this.oWin.src && this.oWin.src.length > 0)
    this.oWin.src = "";
  this.Visible = false;
}
ModalWin.prototype.EndDialog = function (oReturnValue)
{
  this.Hide();
  this.oWin.onreadystatechange = "";
  if (typeof(this.onEndCurrent) == "function")
  {
    if (this.sWinType == "alert")
      this.onEndCurrent(this.sKey);
    else
      this.onEndCurrent(oReturnValue, this.sKey);
  }
}
ModalWin.prototype.GetDocument = function ()
{
  return this.oWin.contentWindow.document;
}

// static helpers
// get object that can be queried for client area dimensions (varies by DOCTYPE declaration)
ModalWin.GetClientViewObject = function(win)
{
  if (typeof(win) === typeof(undefined))
    win = window;
    
  //if Doctype is XHTML 1.0 Transitional then document.documentElement.client* = value and document.body.client* = 0
  //if Doctype is missing then document.documentElement.client* = 0 and document.body.client* = value
  var oClientView = win.document.documentElement;
  if (!(oClientView.clientHeight > 0 || oClientView.clientWidth > 0))
    oClientView = win.document.body;
    
  return oClientView;
}
//=======================================

//set mask background color with setTimeout in order to be synchronized and sure that the iframe body is loaded
function SetMaskColor(sMaskWindowName, sColor)
{
  var oMaskWin = document.getElementById(sMaskWindowName);
  if (oMaskWin != null && (oMaskWin.readyState == "complete" || oMaskWin.readyState == "interactive"))
  {
    if (oMaskWin.contentWindow == null ||
    oMaskWin.contentWindow.document == null ||
    oMaskWin.contentWindow.document.body == null)
      alert('its null');
    oMaskWin.contentWindow.document.body.style.background = sColor;
  }
  else
  {
    setTimeout("SetMaskColor('" + sMaskWindowName + "', '" + sColor + "')", 100);
  }
}
