var activeWindowId  = '';
var oldActiveWindowId = '';
var globalZIndex = 100;
var draggingTitleBar = false;
var draggingResizeButton = false;
var windowArray = new Array();
var windowLeftArray  = new Object();
var windowTopArray = new Object();
var windowHeightArray  = new Object();
var windowWidthArray  = new Object();
var isNetscape = (navigator.appName == "Netscape");
var local_isIE6 = isItIE6();

initialDragTitleX = 0;
initialDragTitleY = 0;
initialDragResizeX = 0;
initialDragResizeY = 0;


function isItIE6() {    
  if (navigator.appVersion.indexOf("MSIE")!=-1) {
    temp = navigator.appVersion.split("MSIE");
    temp2 = temp[1].split("version");
    version = parseFloat(temp2[0]);
    return version < 7;
  }
  else
    return 0;
}

function FerantDHTMLWindow(params){
  this.Id = 'FerantDHTMLWindow1';
  this.StatusLeftMargin = '10';
  this.TitleLeftMargin = '10';
  this.BorderColor = 'White';
  this.BorderInactiveColor = 'Transparent';
  this.BorderWidth = '3';
  this.CloseBoxHeight = '16';
  this.CloseBoxSrc = 'img/Closebox.gif';
  this.CloseBoxWidth = '16';
  this.ContentColor = 'white';
  this.ContentInactiveColor = 'Transparent';
  this.ContentBottomBorderColor = 'Navy';
  this.ContentBottomBorderWidth = '0';
  this.ContentHTML = '';
  this.ContentLeftBorderColor = 'Navy';
  this.ContentLeftBorderWidth = '0';
  this.ContentPadding = '5';
  this.ContentRightBorderColor = 'Navy';
  this.ContentRightBorderWidth = '0';
  this.ContentTopBorderColor = 'Navy';
  this.ContentTopBorderWidth = '0';
  this.Dragable = 'Both';
  this.Height = '200';
  this.InnerBorderColor = 'Gray';
  this.InnerBorderStyle = 'solid';
  this.InnerBorderWidth = '1';
  this.Left = '-1';
  this.MaxHeight = '0';
  this.MaxWidth = '0';
  this.MinHeight = '0';
  this.MinWidth = '0';
  this.OuterBorderColor = 'Gray';
  this.OuterBorderInactiveColor = 'Transparent';
  this.OuterBorderStyle = 'Solid';
  this.OuterBorderWidth = '1';
  this.Resizable = 'Both';
  this.ResizeBoxHeight = '15';
  this.ResizeBoxSrc = 'img/resize_blue.gif';
  this.ResizeBoxWidth = '15';
  this.Shadow = '0';
  this.StatusBarHeight = '20';
  this.StatusBarHTML = '';
  this.StatusBarInactiveColor = 'Transparent';
  this.StatusBarText = '';
  this.StatusBarTextMargin = '5';
  this.StatusBarAlign = 'Center';
  this.StatusColor = 'Silver';
  this.TitleBarHeight = '20';
  this.TitleBarHTML = '';
  this.TitleBarText = '';
  this.ContentNoWrap = 'Normal';
  this.TitleBarAlign = 'Center';
  this.TitleColor = 'Navy';
  this.TitleBarInactiveColor = 'Transparent';
  this.Top = '-1';
  this.Width = '300';
  this.TitleFontSize = '11';
  this.TitleFontFamily = 'verdana,arial';
  this.TitleFontWeight = 'Bold';
  this.TitleFontColor = 'white';
  this.TitleFontStyle = 'Normal';
  this.ContentFontSize = '10';
  this.ContentFontFamily = 'verdana,arial';
  this.ContentFontWeight = 'Normal';
  this.ContentFontColor = 'black';
  this.ContentFontStyle = 'Normal';
  this.StatusFontSize = '10';
  this.StatusFontFamily = 'verdana,arial';
  this.StatusFontWeight = 'Normal';
  this.StatusFontColor = 'black';
  this.StatusFontStyle = 'Normal';
  for(x in params)
    this[x] = params[x];
  this._818  = parseInt(this.TitleBarHeight - this.CloseBoxHeight)/2;
  this.resizeButtonExists  = true;
  if (this.ResizeBoxHeight == 0 || this.ResizeBoxWidth == 0 || this.ResizeBoxHeight == '' || this.StatusBarHeight == 0)
    this.resizeButtonExists  = false;
  this.closeButtonExists  = true;
  if (this.CloseBoxHeight == 0 || this.CloseBoxWidth == 0 || this.CloseBoxHeight == '' || this.TitleBarHeight == 0)
    this.closeButtonExists  = false;
  if(typeof(windowArray[this.Id]) != 'undefined'){
     alert("Error! \n\nTwo Ferant DHTM windows found with the same id: '" + this.Id + "'. Each window must have a unique id.");
     return
  }
  windowArray[this.Id] = this;
  this.main_div = document.createElement("div");
  this.main_div.setAttribute("ownerid", this.Id);
  this.main_div.style.position = "absolute";
  this.main_div.style.display = "none";
  this.main_div.style.overflow = "hidden";
  this.main_div.style.borderStyle = this.OuterBorderStyle;
  this.main_div.style.borderWidth = this.OuterBorderWidth + 'px';
  this.main_div.style.borderColor = this.OuterBorderColor;
  this.main_div.style.backgroundColor = this.BorderColor;
  this.titleBarSpan = document.createElement("span");
  this.titleBarSpan.setAttribute("ownerid", this.Id);
  this.titleBarSpan.style.position = "absolute";
  this.titleBarSpan.style.left = this.BorderWidth +  "px";
  this.titleBarSpan.style.top = this.BorderWidth + "px";
  this.titleBarSpan.style.display = "none";
  this.titleBarSpan.style.backgroundColor=this.TitleColor;
  this.titleBarSpan.style.cursor='default';
  this.titleBarSpan.style.borderColor = this.InnerBorderColor;
  this.titleBarSpan.style.borderStyle='solid';
  this.titleBarSpan.style.borderWidth=this.InnerBorderWidth  + 'px';
  this.titleBarSpan.style.overflow="hidden";
  this.titleBarSpan.style.fontSize = this.TitleFontSize  + 'px';
  this.titleBarSpan.style.fontFamily = this.TitleFontFamily;
  this.titleBarSpan.style.fontWeight = this.TitleFontWeight;
  this.titleBarSpan.style.color = this.TitleFontColor;
  this.titleBarSpan.style.fontStyle = this.TitleFontStyle;
  if(!isNetscape)
    this.titleBarSpan.style.height = this.TitleBarHeight   + 2 * this.InnerBorderWidth  + 'px';
  else
    this.titleBarSpan.style.height = this.TitleBarHeight  + 'px';
  this.titleBarSpan.innerHTML = this.TitleBarText.replace(/</g,'&lt;').replace(/>/g,'&gt;');
  this.titleBarSpan.style.textAlign  = this.TitleBarAlign;
  this.titleBarSpan.style.whiteSpace = 'nowrap';
  this.titleBarSpan.style.paddingTop = Math.max(0,parseInt((this.TitleBarHeight - this.TitleFontSize )/2) - 1) + 'px';
  this.closeButtonSpan = document.createElement("span");
  this.closeButtonSpan.setAttribute("ownerid", this.Id);
  this.closeButtonSpan.style.position = "absolute";
  this.closeButtonSpan.style.left = "0px";
  this.closeButtonSpan.style.display = "none";
  this.closeButtonSpan.style.top = "0px";
  this.closeButtonSpan.style.height = this.CloseBoxHeight + "px";
  this.closeButtonSpan.style.width = this.CloseBoxWidth + "px";
  this.closeButtonSpan.style.cursor = "pointer";
  this.closeButtonSpan.style.overflow = "hidden";
  this.closeButtonSpan.innerHTML = "<img src='" + this.CloseBoxSrc + "' height = " + this.CloseBoxHeight + " width = " + this.CloseBoxWidth + " id='_787'>";
  this.contentDiv = document.createElement("div");
  this.contentDiv.setAttribute("ownerid", this.Id);
  this.contentDiv.style.backgroundColor = this.ContentColor;
  this.contentDiv.style.position = "absolute";
  this.contentDiv.style.display = "none";
  this.contentDiv.innerHTML = this.ContentHTML;
  this.contentDiv.style.left = this.BorderWidth  + 'px';
  this.contentDiv.style.top = parseInt(this.BorderWidth) + parseInt(this.TitleBarHeight) + parseInt(this.InnerBorderWidth)  + 'px';
  this.contentDiv.style.borderColor = this.InnerBorderColor;
  this.contentDiv.style.borderStyle = this.InnerBorderStyle;
  this.contentDiv.style.borderWidth = this.InnerBorderWidth  + 'px';
  this.contentDiv.style.whiteSpace = this.ContentNoWrap;
  this.contentDiv.style.overflow='auto';
  this.contentDiv.style.padding = this.ContentPadding + 'px ' + this.ContentPadding + 'px ' + this.ContentPadding + 'px ' + this.ContentPadding + 'px';
  if(this.ContentTopBorderWidth != 0){
    this.contentDiv.style.borderTopColor = this.ContentTopBorderColor;
    this.contentDiv.style.borderTopWidth = this.ContentTopBorderWidth  + 'px';
  }
  if(this.ContentRightBorderWidth != 0){
    this.contentDiv.style.borderRightColor = this.ContentRightBorderColor;
    this.contentDiv.style.borderRightWidth = this.ContentRightBorderWidth  + 'px';
  }
  if(this.ContentBottomBorderWidth != 0){
    this.contentDiv.style.borderBottomColor = this.ContentBottomBorderColor;
    this.contentDiv.style.borderBottomWidth = this.ContentBottomBorderWidth  + 'px';
  }
  if(this.ContentLeftBorderWidth != 0){
    this.contentDiv.style.borderLeftColor = this.ContentLeftBorderColor;
    this.contentDiv.style.borderLeftWidth = this.ContentLeftBorderWidth  + 'px';
  }
  this.contentDiv.style.fontSize = this.ContentFontSize  + 'px';
  this.contentDiv.style.fontFamily = this.ContentFontFamily;
  this.contentDiv.style.fontWeight = this.ContentFontWeight;
  this.contentDiv.style.color = this.ContentFontColor;
  this.contentDiv.style.fontStyle = this.ContentFontStyle;
  this.statusBarDiv = document.createElement("div");
  this.statusBarDiv.setAttribute("ownerid", this.Id);
  this.statusBarDiv.style.display = "none";
  this.statusBarDiv.style.position = "absolute";
  this.statusBarDiv.style.overflow = "hidden";
  this.statusBarDiv.style.left=this.BorderWidth  + 'px';
  this.statusBarDiv.style.backgroundColor=this.StatusColor;
  if (this.StatusBarHeight > 0)
    this.statusBarDiv.style.paddingTop = Math.max(0,parseInt((this.StatusBarHeight - this.StatusFontSize )/2) - 1)  + 'px';
  this.statusBarDiv.style.borderColor = this.InnerBorderColor;
  this.statusBarDiv.style.borderStyle='solid';
  this.statusBarDiv.style.borderWidth=this.InnerBorderWidth  + 'px';
  this.statusBarDiv.style.fontSize = this.StatusFontSize + 'px';
  this.statusBarDiv.style.fontFamily = this.StatusFontFamily;
  this.statusBarDiv.style.fontWeight = this.StatusFontWeight;
  this.statusBarDiv.style.color = this.StatusFontColor;
  this.statusBarDiv.style.fontStyle = this.StatusFontStyle;
  this.statusBarDiv.innerHTML = this.StatusBarText.replace(/</g,'&lt;').replace(/>/g,'&gt;');
  this.statusBarDiv.style.textAlign  = this.StatusBarAlign;
  this.statusBarDiv.style.whiteSpace = 'nowrap';
  if(this.TitleBarHTML != '')
    this.titleBarSpan.innerHTML = this.TitleBarHTML;
  else
    this.titleBarSpan.innerHTML = this.TitleBarText.replace(/</g,'&lt;').replace(/>/g,'&gt;');
  if(this.StatusBarHTML != '')
    this.statusBarDiv.innerHTML = this.StatusBarHTML;
  else
    this.statusBarDiv.innerHTML = this.StatusBarText.replace(/</g,'&lt;').replace(/>/g,'&gt;');
  this.resizeButtonSpan = document.createElement("span");
  this.resizeButtonSpan.setAttribute("ownerid", this.Id);
  this.resizeButtonSpan.style.position = "absolute";
  this.resizeButtonSpan.style.left = "0px";
  this.resizeButtonSpan.style.height = this.ResizeBoxHeight + "px";
  this.resizeButtonSpan.style.width = this.ResizeBoxWidth + "px";
  this.resizeButtonSpan.style.cursor = "nw-resize";
  this.resizeButtonSpan.style.display = "none";
  this.resizeButtonSpan.innerHTML = "<img src='" + this.ResizeBoxSrc + "' height = " + this.ResizeBoxHeight + " width=" + this.ResizeBoxWidth + " id='_753'>";
  this.shadowDiv = document.createElement("div");
  this.shadowDiv.setAttribute("ownerid", this.Id);
  this.shadowDiv.style.position = "absolute";
  this.shadowDiv.style.display = "none";
  this.shadowDiv.style.overflow = "hidden";
  this.shadowDiv.style.backgroundColor='gray';
  if(!isNetscape)
    this.shadowDiv.style.filter="alpha(opacity=50)";
  else
    this.shadowDiv.style.opacity=.5;
  this.resizeAreaDiv = document.createElement("div");
  this.resizeAreaDiv.setAttribute("ownerid", this.Id);
  this.resizeAreaDiv.style.backgroundColor = "transparent";
  this.resizeAreaDiv.style.position = "absolute";
  this.resizeAreaDiv.style.cursor = "nw-resize";
  this.resizeAreaDiv.style.width = "11px";
  this.resizeAreaDiv.style.height = "11px";
  this.resizeAreaDiv.style.overflow = "hidden";
  this.resizeAreaDiv.innerHTML = "&nbsp;";
  this.resizeAreaDiv.style.display = "none";
  this.main_div.appendChild(this.titleBarSpan);
  this.main_div.appendChild(this.contentDiv);
  document.body.appendChild(this.resizeButtonSpan);
  this.main_div.appendChild(this.statusBarDiv);
  document.body.appendChild(this.shadowDiv);
  document.body.appendChild(this.resizeAreaDiv);
  document.body.appendChild(this.main_div);
  document.body.appendChild(this.closeButtonSpan);
  this.resizeWindow = resizeWindow;
  this.moveWindow = moveWindow;
  this.attachEventHandlers = attachEventHandlers;
  this.shadowWindow = shadowWindow;
  this.unshadowWindow = unshadowWindow;
  this.reactivateWindow = reactivateWindow;
  this.deactivateWindow = deactivateWindow;
  this.UpdateTitleBarText = UpdateTitleBarText;
  this.UpdateTitleBarHTML = UpdateTitleBarHTML;
  this.UpdateContentHTML = UpdateContentHTML;
  this.UpdateStatusBarText = UpdateStatusBarText;
  this.UpdateStatusBarHTML = UpdateStatusBarHTML;
  this.OpenWindow = OpenWindow;
  this.CloseWindow = CloseWindow;
  this.eventHandlersAttached = false;
}


function OpenWindow(){
  draggingTitleBar = false;
  draggingResizeButton = false;
  if (this.Id != activeWindowId) {
    if (activeWindowId != '') {
      oldActiveWindowId = activeWindowId;
      windowArray[oldActiveWindowId].deactivateWindow();
    }
    activeWindowId = this.Id;
  }
  if (!this.eventHandlersAttached)
    this.attachEventHandlers();
  this.resizeButtonExists  = !(this.ResizeBoxHeight == 0 || this.ResizeBoxWidth == 0 || this.ResizeBoxSrc == '' || this.StatusBarHeight == 0 || this.Resizable == 'None');
  this.closeButtonExists  = !(this.CloseBoxHeight == 0 || this.CloseBoxWidth == 0 || this.CloseBoxSrc == '' || this.TitleBarHeight == 0)
  var x = 0;
  var y = 0;
  var h = this.Height;
  var w = this.Width;
  if (this.Left == -1)
    x = (!isNetscape) ? document.documentElement.clientWidth/2 - this.Width/2 + document.documentElement.scrollLeft : window.innerWidth/2 - this.Width/2 + window.pageXOffset;
  else
    x = (!isNetscape) ? this.Left + document.body.scrollLeft : this.Left + window.pageXOffset;
  if (this.Top == -1)
    y = (!isNetscape) ? document.documentElement.clientHeight/2 - this.Height/2 + document.documentElement.scrollTop : window.innerHeight/2 - this.Height/2 + window.pageYOffset;
  else
    y = (!isNetscape) ? this.Top + document.body.scrollTop : this.Top + window.pageYOffset;
  this.contentDiv.style.display = 'block';
  this.titleBarSpan.style.display = 'block';
  this.closeButtonSpan.style.display = (this.closeButtonExists) ? 'block' : 'none';
  this.resizeButtonSpan.style.display = (this.Resizable != 'none' && this.resizeButtonExists) ? 'block' : 'none';
  this.resizeAreaDiv.style.display = (this.Resizable.toLowerCase() == 'none' || this.resizeButtonExists ) ? "none" : "block";
  this.statusBarDiv.style.display = (this.StatusBarHeight > 0) ? 'block' : 'none';
  this.main_div.style.display = 'block';
  this.shadowDiv.style.display = (this.Shadow) ? 'block' : 'none';
  
  this.shadowDiv.style.zIndex=++globalZIndex;
  this.main_div.style.zIndex=++globalZIndex;
  this.resizeButtonSpan.style.zIndex=++globalZIndex;
  this.resizeAreaDiv.style.zIndex=++globalZIndex;
  this.closeButtonSpan.style.zIndex=++globalZIndex;
  this.resizeButtonSpan.style.zIndex=++globalZIndex;
  
  var paddingTitleTop = Math.max(0,parseInt((this.TitleBarHeight - this.TitleFontSize - 1)/2));
  if (this.TitleBarHTML != '') {
    this.titleBarSpan.style.padding = '0px 0px 0px 0px';
    paddingTitleTop = 0;
  }
  else {
    this.titleBarSpan.style.padding = paddingTitleTop + 'px 0px 0px ' + this.TitleLeftMargin + 'px';
  }
  this.titleBarSpan.style.height = ((!isNetscape) ? this.TitleBarHeight + 2*this.InnerBorderWidth : this.TitleBarHeight - paddingTitleTop)  + 'px';
  if (this.StatusBarHeight > 0) {
    var paddingStatusTop = Math.max(0,parseInt((this.StatusBarHeight - this.StatusFontSize - 1)/2));
    if(this.StatusBarHTML !='') {
      this.statusBarDiv.style.padding = '0px 0px 0px 0px';
      paddingStatusTop = 0;
    }
    else {
      this.statusBarDiv.style.padding = paddingStatusTop + 'px 0px 0px ' + this.StatusLeftMargin + 'px';
    }
    this.statusBarDiv.style.height= ((!isNetscape) ? this.StatusBarHeight + 2*this.InnerBorderWidth : this.StatusBarHeight - paddingStatusTop)  + 'px';
  }
  windowLeftArray[this.Id] = x;
  windowTopArray[this.Id] = y;
  windowHeightArray[this.Id] = h;
  windowWidthArray[this.Id] = w;
  this.resizeWindow(w,h,true);
  this.moveWindow(x,y,true);
  this.reactivateWindow();
  fixSelectsVisibility(this.Id);
}


function attachEventHandlers() {
  this.eventHandlersAttached = true;


  this.titleBarSpan.onmousedown = function(e) {
    if (activeWindowId != this.getAttribute('ownerid') && oldActiveWindowId != '') {
      oldActiveWindowId = activeWindowId;
      activeWindowId = this.getAttribute('ownerid');
      windowArray[activeWindowId].reactivateWindow();
      windowArray[oldActiveWindowId].deactivateWindow();
    }
    else {
      activeWindowId = this.getAttribute('ownerid');
    }
    if (windowArray[activeWindowId].Shadow && !draggingTitleBar)
      windowArray[activeWindowId].shadowWindow();
    initialDragTitleX = (parseInt(windowArray[activeWindowId].main_div.style.left) - ((!isNetscape) ? (event.clientX + document.body.scrollLeft): e.pageX));
    initialDragTitleY = (parseInt(windowArray[activeWindowId].main_div.style.top) - ((!isNetscape) ? (event.clientY + document.body.scrollTop) : e.pageY));
    draggingTitleBar = true;
    return false;
  }
  if (this.closeButtonExists) {
    this.closeButtonSpan.onmousedown = function () {
      if(activeWindowId != this.getAttribute('ownerid') && activeWindowId != '')
        oldActiveWindowId = activeWindowId;
      activeWindowId = this.getAttribute('ownerid');
      if (oldActiveWindowId != '')
        windowArray[oldActiveWindowId].reactivateWindow();
      windowArray[this.getAttribute('ownerid')].CloseWindow();
      if (oldActiveWindowId != '') {
        activeWindowId = oldActiveWindowId;
        fixSelectsVisibility(activeWindowId);
      }
      oldActiveWindowId = '';
    };
  }
  this.main_div.onmousedown = function() {
    if (activeWindowId != this.getAttribute('ownerid') && oldActiveWindowId != '') {
      oldActiveWindowId = activeWindowId;
      activeWindowId = this.getAttribute('ownerid');
      windowArray[activeWindowId].reactivateWindow();
      windowArray[oldActiveWindowId].deactivateWindow();
    }
    else {
      activeWindowId = this.getAttribute('ownerid');
    }
    raiseActiveWindow();
  }
  if (this.Resizable != 'none') {
    this.resizeButtonSpan.onmousedown = function(e) {
      if (activeWindowId != this.getAttribute('ownerid') && oldActiveWindowId != '') {
        oldActiveWindowId = activeWindowId;
        activeWindowId = this.getAttribute('ownerid');
        windowArray[activeWindowId].reactivateWindow();
        windowArray[oldActiveWindowId].deactivateWindow();
      }
      else {
        activeWindowId = this.getAttribute('ownerid');
      }
      beginDraggingResize(e);
      return false;
    }

    this.resizeAreaDiv.onmousedown = function(e) {
      if (activeWindowId != this.getAttribute('ownerid') && oldActiveWindowId != '') {
        oldActiveWindowId = activeWindowId;
        activeWindowId = this.getAttribute('ownerid');
        windowArray[activeWindowId].reactivateWindow();
        raiseActiveWindow();
        windowArray[oldActiveWindowId].deactivateWindow();
      }
      else {
        activeWindowId = this.getAttribute('ownerid');
      }
      beginDraggingResize(e);
      return false;
    }
  }
 }


document.onmousemove = function (e) {
  if(!draggingResizeButton && !draggingTitleBar) {
    return true;
  }
  else {
    if (activeWindowId != '') {
      if(!isNetscape) {
        if(event.button !=1 ) {
          draggingResizeButton = false;
          draggingTitleBar = false;
          return true;
        }
      }
      _716 = (!isNetscape) ? event.clientX + document.body.scrollLeft : e.pageX;
      _715 = (!isNetscape) ? event.clientY + document.body.scrollTop : e.pageY;
      endDragging();
      if (draggingResizeButton || draggingTitleBar ) {
        if(!isNetscape)
          event.returnValue = false;
      }
    }
  }
  return false;
};

document.onmouseup = function() {
  if (draggingTitleBar || draggingResizeButton)

    windowArray[activeWindowId].unshadowWindow();
  draggingResizeButton = false;

  draggingTitleBar = false;
}


function UpdateTitleBarText(TitleBarText){
  this.titleBarSpan.innerHTML = TitleBarText.replace(/</g,'&lt;').replace(/>/g,'&gt;');
}


function UpdateTitleBarHTML(TitleHTML) {
  this.titleBarSpan.innerHTML = TitleHTML;
}


function UpdateContentHTML(ContentHTML){
  this.contentDiv.innerHTML = ContentHTML;
}


function UpdateStatusBarText(StatusBarText){
  this.statusBarDiv.innerHTML = StatusBarText.replace(/</g,'&lt;').replace(/>/g,'&gt;');
}


function UpdateStatusBarHTML(StatusBarHTML) {
  this.statusBarDiv.innerHTML = StatusBarHTML;
}

function shadowWindow(){
  if (this.Shadow) {
    this.main_div.style.left = parseInt(this.main_div.style.left) + 2  + 'px';
    this.main_div.style.top = parseInt(this.main_div.style.top) + 2 + 'px';
    this.shadowDiv.style.left = parseInt(this.shadowDiv.style.left) + 2 +'px';
    this.shadowDiv.style.top =  parseInt(this.shadowDiv.style.top) + 2 + 'px';
    this.shadowDiv.style.display = 'none';
    if (this.resizeButtonExists) {
      this.resizeButtonSpan.style.left = parseInt(this.resizeButtonSpan.style.left) + 2  + 'px';
      this.resizeButtonSpan.style.top = parseInt(this.resizeButtonSpan.style.top) + 2  + 'px';
    }
    this.closeButtonSpan.style.left = parseInt(this.closeButtonSpan.style.left) + 2  + 'px';
    this.closeButtonSpan.style.top = parseInt(this.closeButtonSpan.style.top) + 2  + 'px';
    windowLeftArray[this.Id] += 2;
    windowTopArray[this.Id] += 2;
  }
}


function unshadowWindow() {
  draggingResizeButton = false;
  draggingTitleBar = false;
  if (this.Shadow) {
    if (!(this.main_div.style.display !='block')) {
      this.main_div.style.left = parseInt(this.main_div.style.left) - 2  + 'px';
      this.main_div.style.top = parseInt(this.main_div.style.top) - 2 + 'px';
      this.shadowDiv.style.left = parseInt(this.shadowDiv.style.left) - 2 +'px';
      this.shadowDiv.style.top = parseInt(this.shadowDiv.style.top) - 2 + 'px';
      this.shadowDiv.style.display='block';
      if (this.resizeButtonExists) {
        this.resizeButtonSpan.style.left = parseInt(this.resizeButtonSpan.style.left) - 2  + 'px';
        this.resizeButtonSpan.style.top = parseInt(this.resizeButtonSpan.style.top) - 2  + 'px';
      }
      this.closeButtonSpan.style.left = parseInt(this.closeButtonSpan.style.left) - 2  + 'px';
      this.closeButtonSpan.style.top = parseInt(this.closeButtonSpan.style.top) - 2  + 'px';
      windowLeftArray[this.Id] -= 2;
      windowTopArray[this.Id] -= 2;
    }
  }
}

function reactivateWindow() {
  this.titleBarSpan.style.backgroundColor = this.TitleColor;
  this.main_div.style.borderColor = this.OuterBorderColor;
  this.main_div.style.backgroundColor = this.BorderColor;
  this.statusBarDiv.style.backgroundColor = this.StatusColor;
  this.contentDiv.style.backgroundColor = this.ContentColor;
}

function deactivateWindow(){
  if (this.TitleBarInactiveColor != 'Transparent')
    this.titleBarSpan.style.backgroundColor = this.TitleBarInactiveColor;
  if (this.OuterBorderInactiveColor != 'Transparent')
    this.main_div.style.borderColor = this.OuterBorderInactiveColor;
  if (this.BorderWidth > 0 && this.BorderInactiveColor !='Transparent')
    this.main_div.style.backgroundColor = this.BorderInactiveColor;
  if (this.StatusBarInactiveColor != 'Transparent')
    this.statusBarDiv.style.backgroundColor = this.StatusBarInactiveColor;
  if (this.ContentInactiveColor != 'Transparent')
    this.contentDiv.style.backgroundColor = this.ContentInactiveColor;
  fixSelectsVisibility(this.Id);
}



function CloseWindow() {
  this.main_div.style.display = 'none';
  this.contentDiv.style.display = 'none';
  this.titleBarSpan.style.display = 'none';
  this.resizeButtonSpan.style.display = 'none';
  this.resizeAreaDiv.style.display = 'none';
  this.shadowDiv.style.display = 'none';
  this.statusBarDiv.style.display = 'none';
  this.closeButtonSpan.style.display = 'none';
  if (typeof(funcao_fechar) != "undefined" && this.Id=="DHTMLWindow1")
    eval(funcao_fechar);
}


function moveWindow(x, y, defaultMoveability){
  x = parseInt(x);
  y = parseInt(y);
  var verticallyDragable = defaultMoveability;
  if (this.Dragable == 'VerticalOnly' || this.Dragable == 'Both')
    verticallyDragable = true;
  var horizontallyDragable = defaultMoveability;
  if (this.Dragable == 'HorizontalOnly' || this.Dragable == 'Both')
    horizontallyDragable = true;
  if (horizontallyDragable) {
    this.main_div.style.left  = x + 'px'; 
    windowLeftArray[this.Id] = x;
  }
  if (verticallyDragable) {
    this.main_div.style.top  = y + 'px'; 
    windowTopArray[this.Id] = y;
   }
  if (this.Shadow  && horizontallyDragable)
    this.shadowDiv.style.left  = (x + 3) +'px';
  if (this.Shadow  && verticallyDragable)
    this.shadowDiv.style.top  = (y + 3) + 'px';
  if (horizontallyDragable)
    this.resizeAreaDiv.style.left  = (x - 9 + parseInt(this.main_div.style.width)) + 'px';
  if (verticallyDragable)
    this.resizeAreaDiv.style.top  = (y - 9 + parseInt(this.main_div.style.height)) + 'px';
  if (horizontallyDragable && this.closeButtonExists)
    this.closeButtonSpan.style.left = (x + windowWidthArray[this.Id]-2*this.OuterBorderWidth -2*this.BorderWidth -2*this.InnerBorderWidth  - this.CloseBoxWidth -this._818 + this.BorderWidth + this.OuterBorderWidth +this.InnerBorderWidth)  + 'px';
  if (verticallyDragable && this.closeButtonExists)
    this.closeButtonSpan.style.top = (y + this._818 + this.BorderWidth + this.OuterBorderWidth +this.InnerBorderWidth)  + 'px';
  if (horizontallyDragable && this.resizeButtonExists)
    this.resizeButtonSpan.style.left = (windowLeftArray[this.Id] + windowWidthArray[this.Id] - this.OuterBorderWidth - this.BorderWidth - this.InnerBorderWidth - this.ResizeBoxHeight)  + 'px';
  if (verticallyDragable && this.resizeButtonExists)
    this.resizeButtonSpan.style.top = (windowTopArray[this.Id] + windowHeightArray[this.Id] - this.OuterBorderWidth - this.BorderWidth - this.InnerBorderWidth - this.ResizeBoxWidth)  + 'px';
  window.setTimeout('fixSelectsVisibility("' + this.Id +'")',50);
}




function resizeWindow(w, h, defaultResizability){
  w = Math.max(w , 2*this.OuterBorderWidth + 2*this.BorderWidth + 2*this.InnerBorderWidth + this._818 + this.CloseBoxWidth);
  h = Math.max(h ,this.StatusBarHeight + 2*this.OuterBorderWidth +2*this.BorderWidth + 3*this.InnerBorderWidth + parseInt(this.TitleBarHeight));
  if (this.MaxHeight > 0)
    h = Math.min(h , this.MaxHeight);
  if (this.MinHeight > 0)
    h = Math.max(h , this.MinHeight);
  if (this.MaxWidth > 0)
    w = Math.min(w , this.MaxWidth);
  if (this.MinWidth > 0)
    w = Math.max(w , this.MinWidth);
   verticallyResizable = defaultResizability;
  if (this.Resizable  == 'Both' || this.Resizable  == 'VerticalOnly')
    verticallyResizable = true;
   var horizontallyResizable = defaultResizability;
  if (this.Resizable  == 'Both' || this.Resizable  == 'HorizontalOnly')
    horizontallyResizable = true;


  if (horizontallyResizable)
    this.main_div.style.width = ((!isNetscape) ? w : w-2*this.OuterBorderWidth)  + 'px';
  if (verticallyResizable)
    this.main_div.style.height = ((!isNetscape) ? h : h-2*this.OuterBorderWidth)  + 'px';
  if (horizontallyResizable)
    windowWidthArray[this.Id] = w;
  if (verticallyResizable)
    windowHeightArray[this.Id] = h;
  if (horizontallyResizable && this.closeButtonExists)
    this.closeButtonSpan.style.left = (windowLeftArray[this.Id] + w-2*this.OuterBorderWidth -2*this.BorderWidth - 2*this.InnerBorderWidth  - this.CloseBoxWidth - this._818 + this.BorderWidth + this.OuterBorderWidth +this.InnerBorderWidth)  + 'px';
  if (verticallyResizable && this.closeButtonExists)
    this.closeButtonSpan.style.top = (windowTopArray[this.Id] + this._818 + this.BorderWidth + this.OuterBorderWidth +this.InnerBorderWidth)  + 'px';
  if (horizontallyResizable && !isNetscape)
    this.titleBarSpan.style.width = (w-2*this.OuterBorderWidth-2*this.BorderWidth)  + 'px';
  if (horizontallyResizable && isNetscape)
    this.titleBarSpan.style.width = (w-2*this.OuterBorderWidth -2*this.BorderWidth -2*this.InnerBorderWidth - parseInt((this.TitleBarHTML == '') ? this.TitleLeftMargin : 0))  + 'px';
  if (this.Shadow && horizontallyResizable)
    this.shadowDiv.style.width = w +'px';
  if (this.Shadow && verticallyResizable)
    this.shadowDiv.style.height = h +'px';


























  if (!isNetscape) {
    if(horizontallyResizable)
      this.contentDiv.style.width = (w -2*this.OuterBorderWidth - 2*this.BorderWidth)  + 'px';
  }
  else {
    if(horizontallyResizable) {
      this.contentDiv.style.width = (w - this.ContentRightBorderWidth  - this.ContentLeftBorderWidth - 2*this.ContentPadding  -2*this.OuterBorderWidth -2*this.BorderWidth -2*this.InnerBorderWidth)  + 'px';
    }
  }


  if (verticallyResizable) {
    if (this.StatusBarHeight > 0) {
      if(!isNetscape)
        this.contentDiv.style.height = (h - this.StatusBarHeight - this.TitleBarHeight - 2*this.BorderWidth - 2*this.OuterBorderWidth - 2*this.InnerBorderWidth)  + 'px';
      else
        this.contentDiv.style.height = (h - this.ContentTopBorderWidth - this.ContentBottomBorderWidth - this.StatusBarHeight - 2*this.ContentPadding - this.TitleBarHeight - 2*this.BorderWidth - 2*this.OuterBorderWidth - 4*this.InnerBorderWidth)  + 'px';
    }
    else {
       if(!isNetscape)
         this.contentDiv.style.height = (h - this.StatusBarHeight - this.TitleBarHeight - 2*this.BorderWidth -2 *this.OuterBorderWidth - this.InnerBorderWidth)  + 'px';
       else
         this.contentDiv.style.height = (h - this.ContentTopBorderWidth - this.ContentBottomBorderWidth - this.StatusBarHeight - 2*this.ContentPadding - this.TitleBarHeight - 2*this.BorderWidth - 2*this.OuterBorderWidth - 3*this.InnerBorderWidth)  + 'px';
    }
  }


  if (horizontallyResizable && this.StatusBarHeight > 0 && !isNetscape)
    this.statusBarDiv.style.width = (w - 2*this.OuterBorderWidth - 2*this.BorderWidth)  + 'px' ;
  if (horizontallyResizable && this.StatusBarHeight > 0 && isNetscape)
    this.statusBarDiv.style.width = (w - 2*this.OuterBorderWidth - 2*this.BorderWidth - 2*this.InnerBorderWidth - parseInt((this.StatusBarHTML =='')?this.StatusLeftMargin:0))  + 'px';
  if (verticallyResizable && this.StatusBarHeight > 0)
    this.statusBarDiv.style.top  = (h  - 2*this.OuterBorderWidth - this.BorderWidth - 2*this.InnerBorderWidth - this.StatusBarHeight)  + 'px';
  if (horizontallyResizable && this.resizeButtonExists)
    this.resizeButtonSpan.style.left = (windowLeftArray[this.Id] + windowWidthArray[this.Id] - this.OuterBorderWidth - this.BorderWidth - this.InnerBorderWidth - this.ResizeBoxHeight)  + 'px';
  if (verticallyResizable && this.resizeButtonExists)
    this.resizeButtonSpan.style.top = (windowTopArray[this.Id] + windowHeightArray[this.Id] - this.OuterBorderWidth - this.BorderWidth - this.InnerBorderWidth - this.ResizeBoxWidth)  + 'px';
  if (horizontallyResizable && !defaultResizability && !this.resizeButtonExists && this.Resizable != 'none')
    this.resizeAreaDiv.style.left  = (parseInt(this.main_div.style.left) - 9 + w) +'px';
  if (verticallyResizable && !defaultResizability && !this.resizeButtonExists && this.Resizable  != 'none')
    this.resizeAreaDiv.style.top  = (parseInt(this.main_div.style.top)  - 9 + h) + 'px';
  window.setTimeout('fixSelectsVisibility("' + this.Id +'")', 50);
}



function endDragging() {
  if (draggingTitleBar) {
    var x = _716 + initialDragTitleX;
    var y = _715 + initialDragTitleY;
    windowArray[activeWindowId].moveWindow(x, y ,false);
  }
  if (draggingResizeButton) {
    var _708 = _716 + initialDragResizeX;
    var _707 = _715 + initialDragResizeY;
    windowArray[activeWindowId].resizeWindow(_708, _707, false);
  }
  fixSelectsVisibility(this.id);
  return !(draggingResizeButton || draggingTitleBar);
}


function beginDraggingResize(e) {
  initialDragResizeX = parseInt(windowArray[activeWindowId].main_div.style.width) - ((!isNetscape) ? (event.clientX + document.body.scrollLeft) : e.pageX);
  initialDragResizeY = (parseInt(windowArray[activeWindowId].main_div.style.height) - ((!isNetscape) ? (event.clientY + document.body.scrollTop) : e.pageY));
  if (windowArray[activeWindowId].Shadow  && !draggingResizeButton) 
  windowArray[activeWindowId].shadowWindow();
  raiseActiveWindow();
  draggingResizeButton = true;
}


function raiseActiveWindow(){
  windowArray[activeWindowId].shadowDiv.style.zIndex = ++globalZIndex;
  windowArray[activeWindowId].main_div.style.zIndex = ++globalZIndex;
  windowArray[activeWindowId].resizeAreaDiv.style.zIndex = ++globalZIndex;
  windowArray[activeWindowId].closeButtonSpan.style.zIndex = ++globalZIndex;
  windowArray[activeWindowId].resizeButtonSpan.style.zIndex = ++globalZIndex;
  fixSelectsVisibility(this.Id);
}

function boxUnderWindow(Left, Top, Width, Height, realId){
  for (j in windowArray) {
    if (
      (
        (windowArray[j].main_div.style.display != 'none') && 
        (
          ((Left <= windowLeftArray[j]) && (windowLeftArray[j] <= Left + Width)) || 
          ((windowLeftArray[j] <= Left) && (Left <= windowLeftArray[j] + windowWidthArray[j]))
        ) && 
        (
          ((Top <= windowTopArray[j]) && (windowTopArray[j] <= Top + Height)) || 
          ((windowTopArray[j] <= Top) && (Top <= windowTopArray[j] + windowHeightArray[j]))
        )
      ) && 
      (
        (realId == '') || 
        (windowArray[realId].main_div.style.zIndex < windowArray[j].main_div.style.zIndex)
      )
    )
      return true;
  }
  return false;
}

function calcTotalOffsetTop(node){
  var result  = 0;
  if (node.offsetParent) {
    while (node.offsetParent) {
      result += node.offsetTop;
      node = node.offsetParent;
    }
  } 
  else if (node.y) {
    result += node.y;
  }
  return result;
}

 function calcTotalOffsetLeft(node) {
  var result = 0;
  if (node.offsetParent) {
    while (node.offsetParent) {
      result += node.offsetLeft;
      node = node.offsetParent;
    }
  } else if(node.x) {

    result += node.x;
  }
  return result;
}

function fixSelectsVisibility(id) {
  if (local_isIE6) {
    arrayOfSelects = document.getElementsByTagName('select');
    var realId;
    var flag = false;
    for (i=0; i < arrayOfSelects.length; i++){
      node = arrayOfSelects[i];
      node2 = arrayOfSelects[i];
      realId = '';
      while (node2.parentElement && !(realId = node2.getAttribute("ownerid"))) {
        node2 = node2.parentElement;
      }
      if (realId == null)
        realId = "";;
      Left = calcTotalOffsetLeft(arrayOfSelects[i]);
      Top = calcTotalOffsetTop(arrayOfSelects[i]);
      Width = arrayOfSelects[i].offsetWidth;
      Height = arrayOfSelects[i].offsetHeight;
      if (boxUnderWindow(Left, Top, Width, Height, realId))
        arrayOfSelects[i].style.visibility = 'hidden';
      else
        arrayOfSelects[i].style.visibility = 'visible';
    }
  }
}
