﻿var global_popupDiv = null;
var global_maskDiv = null;
var global_disabledDivArray = null;
var global_iframe = null;

/* 
This function assumes we are popping up a "dialog" window over
a semi-transparent mask underwhich is a disabled web form.
The web-form may be made up of multiple top-level DIVs, so
an array is provided for that purpose.
*/

/* the following arguments are required */
/* popupDiv - the div to display in the center of the screen */
/* maskDiv - the div to display covering the web form */
/* disabledDivArray - the array of divs to disable (non-array element also allowed) */
function popupMaskedWindow(popupDiv, maskDiv, disabledDivArray) {
  if (!popupDiv || !maskDiv || !disabledDivArray) { return false; }

  global_popupDiv = popupDiv;
  global_maskDiv = maskDiv;
  global_disabledDivArray = disabledDivArray;

  var height = Math.max(document.body.scrollHeight, screen.height) + Number(document.body.topMargin) + Number(document.body.bottomMargin);
  var width = Math.max(document.body.scrollWidth, screen.width) - Number(document.body.leftMargin) - Number(document.body.rightMargin);

  // needed for firefox
  if (height.toString().indexOf('px') == -1) {
    height = height + 'px';
    width = width + 'px';
  }

  global_maskDiv.style.height = height;
  global_maskDiv.style.width = width;
  global_maskDiv.style.visibility = 'visible';
  global_maskDiv.style.display = 'block';

  popupDiv.style.zIndex = 11;
  global_maskDiv.style.zIndex = 10;

  if (isBrowser('Explorer6') || isBrowser('Explorer7')) {
    global_iframe = getGlobalIFrame(global_maskDiv);
    global_iframe.style.zIndex = 9;
    global_iframe.style.left = global_maskDiv.getAttribute('offsetLeft');
    global_iframe.style.top = global_maskDiv.getAttribute('offsetTop');
    global_iframe.style.width = global_maskDiv.getAttribute('offsetWidth');
    global_iframe.style.height = global_maskDiv.getAttribute('offsetHeight');
    global_iframe.setAttribute("className", "transparent0");
    global_iframe.style.visibility = 'visible';
  }

  global_popupDiv.style.display = 'block';

  var top = (getWindowHeight() / 2) - (global_popupDiv.clientHeight / 2);
  var left = (getWindowWidth() / 2) - (global_popupDiv.clientWidth / 2);

  if (top.toString().indexOf('px') == -1) { top += 'px'; }
  if (left.toString().indexOf('px') == -1) { left += 'px'; }

  global_popupDiv.style.top = top;
  global_popupDiv.style.left = left;

  if (isBrowser('Explorer6') || isBrowser('Explorer7')) {
    global_popupDiv.style.top = (getWindowHeight() / 2) - (global_popupDiv.clientHeight / 2) + getScrollTop();
    global_popupDiv.style.left = (getWindowWidth() / 2) - (global_popupDiv.clientWidth / 2) + getScrollLeft();
  }
  global_popupDiv.style.visibility = 'visible';
  
     
  global_disabledDivArray.setAttribute("className", "transparent50 ie6safeEntireScreen");
  if (isArray(global_disabledDivArray)) {
    for (var i in global_disabledDivArray) {
      if (global_disabledDivArray[i]) { global_disabledDivArray[i].disabled = true; }
    }
  } else { global_disabledDivArray.disabled = true; }



  if (isArray(global_popupDiv)) {
    for (var i in global_popupDiv) {
      if (global_popupDiv[i]) { global_popupDiv[i].disabled = false; }
    }
  } else { global_popupDiv.disabled = false; }

  return false;
}

/* the following arguments are supported */
/* popupDiv - the div to display in the center of the screen */
/* maskDiv - the div to display covering the web form */
/* disabledDivArray - the array of divs to disable (non-array element also allowed) */
function hideMaskedWindow() {
  global_popupDiv.style.visibility = 'hidden';
  global_popupDiv.style.display = 'none';
  global_maskDiv.style.visibility = 'hidden';
  global_maskDiv.style.display = 'none';

  if (isBrowser('Explorer6') || isBrowser('Explorer7')) {
    global_iframe.style.visibility = 'hidden';
    global_iframe.style.display = 'none';
  }

  if (isArray(global_disabledDivArray)) {
    for (var i in global_disabledDivArray) {
      if (global_disabledDivArray[i]) { global_disabledDivArray[i].disabled = false; }
    }
  } else { global_disabledDivArray.disabled = false; }

  return false;
}

function getScrollTop() {
  var scrollTop = document.documentElement.scrollTop;
  if (scrollTop == 0) {
    if (window.pageYOffset) { scrollTop = window.pageYOffset; }
    else { scrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0; }
  }
  return scrollTop;
}

function getScrollLeft() {
  var scrollLeft = document.documentElement.scrollLeft;
  if (scrollLeft == 0) {
    if (window.pageXOffset) { scrollLeft = window.pageXOffset; }
    else { scrollLeft = (document.body.parentElement) ? document.body.parentElement.scrollLeft : 0; }
  }
  return scrollLeft;
}

function getWindowHeight() {
  var y = 0;
  if (self.innerHeight) {
    y = self.innerHeight;
  }
  else if (document.documentElement && document.documentElement.clientHeight) {
    y = document.documentElement.clientHeight;
  }
  else if (document.body.clientHeight) {
    y = document.body.clientHeight;
  }
  return y;
}

function getWindowWidth() {
  var x = 0;
  if (self.innerWidth) {                                                          // firefox, chrome
    x = self.innerWidth - 3;
  }
  else if (document.documentElement && document.documentElement.clientWidth) {    // ie
    x = document.documentElement.clientWidth;
  }
  else if (document.body.clientWidth) {
    x = document.body.clientWidth;
    alert("popup.js: this wasn't expected");
  }
  return x + getScrollBarWidth();
}

function getScrollBarWidth() {
  var inner = document.createElement("p");
  inner.style.width = "100%";
  inner.style.height = "200px";

  var outer = document.createElement("div");
  outer.style.position = "absolute";
  outer.style.top = "0px";
  outer.style.left = "0px";
  outer.style.visibility = "hidden";
  outer.style.width = "200px";
  outer.style.height = "150px";
  outer.style.overflow = "hidden";
  outer.appendChild(inner);

  document.body.appendChild(outer);
  var w1 = inner.offsetWidth;
  outer.style.overflow = "scroll";
  var w2 = inner.offsetWidth;
  if (w1 == w2) w2 = outer.clientWidth;

  document.body.removeChild(outer);

  return (w1 - w2);
}


/*********************************************************************************************************************/

/* the following was adapted from online sources */
/* it provides the help popup functionality for  */
/* the loan application. It has been heavily     */
/* modified.                                     */

/*********************************************************************************************************************/

var jsAreaShowTime = 700;
var JsAreas = new Object();

function GetArea(id) {
  return JsAreas[id] ? JsAreas[id] : (JsAreas[id] = new JsArea(id));
}

// A single popup window
function JsArea(id) {
  this.AreaId = id;
}

// Function to return the DIV Layer
JsArea.prototype.ContentArea = function() {
  var divArea = document.getElementById(this.AreaId);
  if (divArea != null) {
    return divArea;
  }
  return null;
}

var activeAreaId = null;
var timeoutId = 0;

// Function to be called
// by Web forms to show the Popup Window
function PopupArea(env, areaId, contentsArray, mainDiv, popupDelay) {
  var targetControl;
  var e;
  if (!env) { e = window.event; }
  else { e = env; }
  if (e.target) { targetControl = e.target; }
  else if (e.srcElement) { targetControl = e.srcElement; }

  while (targetControl.clientWidth == 0) {
    targetControl = targetControl.parentElement;
  }

  if (targetControl && targetControl.nodeType == 3) // defeat Safari bug
  { targetControl = targetControl.parentNode; }

  var area = GetArea(areaId);
  area.prepDisplayArea(contentsArray, mainDiv, areaId);

  var popupWidth = document.getElementById(areaId).clientWidth;
  var popupHeight = document.getElementById(areaId).clientHeight;

  var pos = findPos(targetControl, popupWidth, popupHeight, mainDiv);

  area.popareaup(pos.x, pos.y, contentsArray, targetControl, mainDiv, popupDelay);
}

JsArea.prototype.prepDisplayArea = function(contentsArray, mainDiv, areaId) {

  var a = document.getElementById(areaId);
  // popup div -> ul -> li
  var ul = a.childNodes[1];
  var li = ul.childNodes[0];

  var myClass = li.className;

  ul.innerHTML = "";
  for (var i in contentsArray) {
    if (myClass.length == 0) { ul.innerHTML += "<li>" + contentsArray[i] + "</li>"; }
    else { ul.innerHTML += "<li class=\'" + myClass + "\'>" + contentsArray[i] + "</li>"; }
  }

  var divLayer = this.ContentArea();
  divLayer.style.display = "none";
  divLayer.style.visibility = "visible";
  divLayer.style.visibility = "hidden";

  return false;
}

function findPos(aCtrl, popupWidth, popupHeight, mainDiv) {
  var pos = { x: 0, y: 0 };
  //var trace = '';

  var ctrl = aCtrl;
  if (ctrl.offsetParent) {
    while (true) {
      pos.x += ctrl.offsetLeft;
      pos.y += ctrl.offsetTop;
      if (!ctrl.offsetParent) { break; }
      ctrl = ctrl.offsetParent;
    }
  }
  else if (ctrl.x) {
    pos.x += ctrl.x;
    pos.y += ctrl.y;
  }
  else {
    alert('unsupported browser (popup.js:263)');
  }

  BrowserDetect.init();

  switch (BrowserDetect.browser) {
    case 'Safari':
      pos.x += 5;
      break;
    case 'Opera':
      pos.x += 4;
      break;
    case 'Chrome':
      pos.x += 3;
      break;
    case 'Firefox':
      pos.x += 4;
      break;
    case 'Explorer':
      switch (BrowserDetect.version) {
        case 6:
        case 7:
          pos.x += 2;
          break;
        case 8:
          pos.x += 8;
          break;
        default:
          alert('unsupported browser (popup.js:291)');
          break;
      }
      break;
    default:
      alert('unsupported browser (popup.js:296)');
      break;
  }

  //trace += 'x=' + pos.x + 'px, y=' + pos.y + '\r\n';
  //var c = document.getElementsByName('debugTextArea');
  //c[0].value = trace;

  return pos;
}

// Function to show the DIV Layer
JsArea.prototype.popareaup = function(x, y, contentsArray, targetControl, mainDiv, popupDelay) {
  var trace = "";
  if (mainDiv == null) {
    mainDiv = document.getElementsByTagName("div")[0];
  }

  if (activeAreaId != null)
    jsAreaClose(activeAreaId);

  var divLayer = this.ContentArea();
  global_popupDiv = divLayer;
  divLayer.style.visibility = "hidden";
  divLayer.style.position = "absolute";
  divLayer.style.display = "block";
  divLayer.onmouseover = JsAreaMouseOver;
  divLayer.onmouseout = jsAreaMouseOut;
  activeAreaId = this.AreaId;

  var popupPadding = 5;

  x += targetControl.clientWidth + popupPadding;
  y += targetControl.clientHeight + popupPadding;

  if (x + divLayer.clientWidth + 20 - getScrollLeft() > getWindowWidth()) {
    x -= (divLayer.clientWidth + targetControl.clientWidth + 2 * popupPadding);
    if (x < getScrollLeft()) { x = getScrollLeft(); }
  }

  if (y + divLayer.clientHeight - getScrollTop() > getWindowHeight()) {
    y -= (divLayer.clientHeight + targetControl.clientHeight + 2 * popupPadding);
    if (y < getScrollTop()) { y = getScrollTop(); }
  }

  divLayer.style.left = x + "px";
  divLayer.style.top = y + "px";

  clearTimeout(timeoutId);
  timeoutId = setTimeout(delayedPopup, popupDelay);

  //var c = document.getElementsByName('debugArea');
  //c[0].value = trace;

  return false;
}

function delayedPopup() {
  if (global_popupDiv) { global_popupDiv.style.visibility = "visible"; }
  if (isBrowser('Explorer6') || isBrowser('Explorer7')) {
    global_popupDiv.style.zIndex = 2;
    global_iframe = getGlobalIFrame(global_popupDiv);
    global_iframe.style.visibility = 'visible';
  }
}

function getGlobalIFrame(ctrl) {
  if (global_iframe) {
    global_iframe.parentElement.removeChild(global_iframe);
  }
  global_iframe = buildMatchingIFrame(ctrl);
  initGlobalIFrame(ctrl);
  return global_iframe;
}

function initGlobalIFrame(ctrl) {
  global_iframe.style.left = ctrl.getAttribute('offsetLeft');
  global_iframe.style.top = ctrl.getAttribute('offsetTop');
  global_iframe.style.width = ctrl.getAttribute('offsetWidth');
  global_iframe.style.height = ctrl.getAttribute('offsetHeight');
  global_iframe.style.position = 'absolute';
  global_iframe.style.display = 'block';
}

function buildMatchingIFrame(ctrl) {
  var iframe = null;
  if (!ctrl) { return null; }

  var iframe = document.createElement('iframe');
  ctrl.parentElement.appendChild(iframe);

  return iframe;
}

// Function to hide the DIV Layer
JsArea.prototype.hide = function() {
  clearTimeout(timeoutId);

  var divLayer = this.ContentArea();
  if (divLayer != null) {
    divLayer.style.visibility = 'hidden';
    if (global_iframe) { global_iframe.style.visibility = 'hidden'; }
  }
  return false;
}

// Function to hide the DIV Layer
function jsAreaClose(areaId) {
  GetArea(areaId).hide();
  activeAreaId = divHangTimer = null;
}

var divHangTimer = null;

// Function to keep the Div Layer
// showing for a "period" of time
// after that period, if the mouse
// has been outside the DIV Layer, 
// it will be hidden automatically
function KeepArea(areaId) {
  if (areaId == activeAreaId && divHangTimer != null) {
    clearTimeout(divHangTimer);
    divHangTimer = null;
  }
}

// Function to release the DIV Layer
function RelArea(areaId) {
  if (areaId == activeAreaId && divHangTimer == null)
    divHangTimer = setTimeout('jsAreaClose(\'' + areaId + '\')', jsAreaShowTime);
}

// Function fired when mouse is over the 
// DIV Layer, used to keep the layer showing
function JsAreaMouseOver(env) {
  var e;
  if (!env) { e = window.event; }
  else { e = env; }
  var targ = e.target ? e.target : e.srcElement;
  KeepArea(activeAreaId);
}

// Function that fires when mouse is out of
// the scope of the DIV Layer
function jsAreaMouseOut(env) {
  var e;
  if (!env) { e = window.event; }
  else { e = env; }
  var targ = e.relatedTarget ? e.relatedTarget : e.toElement;
  var activeAreaView = document.getElementById(activeAreaId);
  if (activeAreaView != null && !jsAreaContains(activeAreaView, targ))
    RelArea(activeAreaId);
}

function jsAreaContains(parent, child) {
  while (child)
    if (parent == child) return true;
  else
    child = child.parentNode;

  return false;
}

