﻿// Scrolling.js includes code used on the table page only.

var m_clCornerDiv;
var m_clHeaderRowDiv;
var m_clHeaderColumnDiv;
var m_clBodyDiv;
var m_nBodyoffsetWidth;
var m_nBodyoffsetHeight;

var M_nScrollbarWidth = 0;

//
function ScrollTableRelativeSize() {
   m_clCornerDiv = document.getElementById("cornerDiv");
   m_clHeaderRowDiv = document.getElementById("headerRowDiv");
   m_clHeaderColumnDiv = document.getElementById("headerColumnDiv");
   m_clBodyDiv = document.getElementById("bodyDiv");

   if (m_clCornerDiv != null) {
      m_clCornerDiv.style.display = "";
   }
   if (m_clHeaderRowDiv != null) {
      m_clHeaderRowDiv.style.display = "";
   }
   if (m_clHeaderColumnDiv != null) {
      m_clHeaderColumnDiv.style.display = "";
   }
   if (m_clBodyDiv != null) {
      m_clBodyDiv.style.display = "";
   }

   // Create drag and drop object.
   var clJsdnd = new GJsdnd();
   clJsdnd.Initialize(this);

   //
   ResizeCells();
   //
   if (m_clBodyDiv != null) {
      m_nBodyoffsetWidth = m_clBodyDiv.offsetWidth;
      m_nBodyoffsetHeight = m_clBodyDiv.offsetHeight;
   }
   //
   ResizeScrollArea();
   //
   if (m_clBodyDiv != null) {
      m_clBodyDiv.onscroll = SyncScroll;
   }
   //
   // The following code was commented out because "Onresize" was not working.
   //window.onresize = ResizeScrollArea;
}

//
function ResizeCells() {
   var nChildElement = 0;
   var clContentsTable = document.getElementById("ContentsTable");
   if (clContentsTable != null && clContentsTable.childNodes[0].tagName == null) {
      nChildElement = 1;
   }

   if (m_clCornerDiv != null && m_clHeaderColumnDiv != null) {
      // Find the last row of the 'Corner' table that contains all the 'Rows dimesnions names'
      var clCornerTableRows = m_clCornerDiv.childNodes[nChildElement].childNodes[nChildElement].childNodes;
      var nCornerTableTableLastRow = 0;
      for (var nRowIndex = 0; nRowIndex < clCornerTableRows.length; nRowIndex++) {
         if (clCornerTableRows[nRowIndex].tagName == "TR") {
            // If the width of the ‘div’ that contains the row dimension name is less than the width of the 
            // last column dimension name divided by the column span, then the width of the ‘div’ should be 
            // the width of the last column dimension name divided by the column span.
            for (var nColIndex = 0; nColIndex < clCornerTableRows[nRowIndex].childNodes.length; nColIndex++) {
               if (clCornerTableRows[nRowIndex].childNodes[nColIndex].tagName == "TH") {
                  if (nRowIndex > 0) {
                     if (clCornerTableRows[nCornerTableTableLastRow].firstChild.colSpan != null &&
                           (clCornerTableRows[nRowIndex].firstChild.colSpan == null ||
                              clCornerTableRows[nRowIndex].firstChild.colSpan == 1)) {
                        var nColSpan = clCornerTableRows[nCornerTableTableLastRow].firstChild.colSpan;
                        var nTopColIndex = parseInt(nColIndex / nColSpan);
                        var nTopWidth = clCornerTableRows[nCornerTableTableLastRow].childNodes[nTopColIndex].firstChild.offsetWidth;
                        var nBottomWidth = clCornerTableRows[nRowIndex].childNodes[nColIndex].firstChild.offsetWidth;
                        if (nBottomWidth < (nTopWidth / nColSpan)) {
                           clCornerTableRows[nRowIndex].childNodes[nColIndex].firstChild.style.width = nTopWidth / nColSpan;
                        }
                        else {
                           clCornerTableRows[nRowIndex].childNodes[nColIndex].firstChild.style.width = nBottomWidth;
                        }
                     }
                  }
               }
            }
            nCornerTableTableLastRow = nRowIndex;
         }
      }
      // Loop through the 'Rows dimesnions names' and set their width with the 
      // corresponding members of the first row of the 'HeaderColumn' table.
      var clHeaderColumnTableRows = m_clHeaderColumnDiv.childNodes[nChildElement].childNodes[nChildElement].childNodes;
      for (var nColIndex = 0; nColIndex < clCornerTableRows[nCornerTableTableLastRow].childNodes.length; nColIndex++) {
         if (clCornerTableRows[nCornerTableTableLastRow].childNodes[nColIndex].tagName == "TH") {
            SetWidth(
               clCornerTableRows[nCornerTableTableLastRow].childNodes[nColIndex],
               clHeaderColumnTableRows[0].childNodes[nColIndex]);
         }
      }
      // The code above did not adjust the width of the DIV elements inside second and subsequent column dim elements
      // Extra adjustment is still required
      for (var nRowIndex = 1; nRowIndex < clCornerTableRows.length - 1; nRowIndex++) {
         if (clCornerTableRows[nRowIndex + 1].tagName == "TR") {
            clCornerTableRows[nRowIndex].childNodes[0].firstChild.style.width =
               clCornerTableRows[0].childNodes[0].firstChild.offsetWidth;
         }
      }
   }

   if (m_clBodyDiv != null && m_clHeaderRowDiv != null) {
      // Find the last row of the 'HeaderRow' table that contains all the 'sorting arrows'
      var clHeaderRowTableRows = m_clHeaderRowDiv.childNodes[nChildElement].childNodes[nChildElement].childNodes;
      var nHeaderRowTableLastRow = 0;

      for (var nRowIndex = 0; nRowIndex < clHeaderRowTableRows.length; nRowIndex++) {
         if (clHeaderRowTableRows[nRowIndex].tagName == "TR") {
            var nTopColIndex = 0;
            var nBottomColIndex = 0;

            // If the width of the ‘div’ that contains a member of the last column dimension is less than the 
            // width of the ‘div’ that contains a member of the column dimension above divided by the column 
            // span, then the width of the fisrt ‘div’ should be the ‘div’ that contains a member of the column 
            // dimension above divided by the column span.
            for (var nColIndex = 0; nColIndex < clHeaderRowTableRows[nRowIndex].childNodes.length; nColIndex++) {
               if (clHeaderRowTableRows[nRowIndex].childNodes[nColIndex].tagName == "TH") {
                  if (nRowIndex > 0) {
                     if (clHeaderRowTableRows[nHeaderRowTableLastRow].firstChild.colSpan != null &&
                           (clHeaderRowTableRows[nRowIndex].firstChild.colSpan == null ||
                              clHeaderRowTableRows[nRowIndex].firstChild.colSpan == 1)) {
                        // Spr 11510: To handle the case when suppression of columns is applied.
                        if (nBottomColIndex == nColSpan) {
                           nTopColIndex += 1;
                           nBottomColIndex = 1;
                        }
                        else {
                           nBottomColIndex += 1;
                        }
                        var nColSpan = clHeaderRowTableRows[nHeaderRowTableLastRow].childNodes[nTopColIndex].colSpan;
                        var nTopWidth = clHeaderRowTableRows[nHeaderRowTableLastRow].childNodes[nTopColIndex].firstChild.offsetWidth;
                        var nBottomWidth = clHeaderRowTableRows[nRowIndex].childNodes[nColIndex].firstChild.offsetWidth;
                        if (nBottomWidth < (nTopWidth / nColSpan) && G_nBrowser != FIREFOX) {
                           clHeaderRowTableRows[nRowIndex].childNodes[nColIndex].firstChild.style.width = nTopWidth / nColSpan;
                        }
                     }
                  }
               }
            }
            nHeaderRowTableLastRow = nRowIndex;
         }
      }

      // Loop through the 'sorting arrows' and set their width with the 
      // corresponding members of the first row of the 'Body' table.
      var clBodyTableRows = m_clBodyDiv.childNodes[nChildElement].childNodes[nChildElement].childNodes;
      for (var nColIndex = 0; nColIndex < clHeaderRowTableRows[nHeaderRowTableLastRow].childNodes.length; nColIndex++) {
         if (clHeaderRowTableRows[nHeaderRowTableLastRow].childNodes[nColIndex].tagName == "TH") {
            SetWidth(
               clHeaderRowTableRows[nHeaderRowTableLastRow].childNodes[nColIndex],
               clBodyTableRows[0].childNodes[nColIndex]);
         }
      }
   }

   if (m_clCornerDiv != null && m_clHeaderRowDiv != null) {
      // Loop through column dimensions names and set their height with the height of their first member
      var clCornerTableRows = m_clCornerDiv.childNodes[nChildElement].childNodes[nChildElement].childNodes;
      var clHeaderRowRows = m_clHeaderRowDiv.childNodes[nChildElement].childNodes[nChildElement].childNodes;
      for (var nRowIndex = 0; nRowIndex < clCornerTableRows.length; nRowIndex++) {
         if (clCornerTableRows[nRowIndex].tagName == "TR") {
            SetHeight(
				   clCornerTableRows[nRowIndex].firstChild,
				   clHeaderRowRows[nRowIndex].firstChild);
         }
      }
   }

   if (m_clBodyDiv != null && m_clHeaderColumnDiv != null) {
      // Loop through the members of the innermost row dimension and set their height with 
      // the height of the first corresponding cell value.
      var clHeaderColumnTableRows = m_clHeaderColumnDiv.childNodes[nChildElement].childNodes[nChildElement].childNodes;
      var clBodyTableRows = m_clBodyDiv.childNodes[nChildElement].childNodes[nChildElement].childNodes;
      for (var nRowIndex = 0; nRowIndex < clHeaderColumnTableRows.length; nRowIndex++) {
         if (clHeaderColumnTableRows[nRowIndex].tagName == "TR") {
            var bFound = false;
            for (var nColIndex = clHeaderColumnTableRows[nRowIndex].childNodes.length - 1; nColIndex >= 0; nColIndex--) {
               if (!bFound && clHeaderColumnTableRows[nRowIndex].childNodes[nColIndex].tagName == "TH") {
                  // Chart labels cannot be centered for Netscape. 
                  // For IE and Firefox, the last hidden cell is used for calculation 
                  // instead of the cell that contains the labels
                  var clHeaderColumnTableLastLabel = clHeaderColumnTableRows[nRowIndex].childNodes[nColIndex];
                  if (document.aspnetForm.ReportType.value == "chart") {
                     if (G_nBrowser == NETSCAPE) {
                        clHeaderColumnTableLastLabel = clHeaderColumnTableRows[nRowIndex].childNodes[nColIndex - 1];
                     }
                     else if (nColIndex > 0) {
                        clHeaderColumnTableLastLabel.firstChild.style.height =
                           clHeaderColumnTableRows[nRowIndex].childNodes[nColIndex - 1].firstChild.offsetHeight;
                     }
                  }
                  SetHeight(
		               clHeaderColumnTableLastLabel,
		               clBodyTableRows[nRowIndex].firstChild);
                  bFound = true;
               }
            }
         }
      }
   }
}

//
function SetWidth(element1, element2) {
   if (element1.firstChild.offsetWidth < element2.firstChild.offsetWidth) {
      element1.firstChild.style.width = element2.firstChild.offsetWidth + "px";
   }
   else {
      element2.firstChild.style.width = element1.firstChild.offsetWidth + "px";
   }
}

//
function SetHeight(element1, element2) {
   if (element1.firstChild.offsetHeight < element2.firstChild.offsetHeight) {
      element1.firstChild.style.height = element2.firstChild.offsetHeight + "px";
   }
   else {
      element2.firstChild.style.height = element1.firstChild.offsetHeight + "px";
   }
}

function getScrollerWidth() {
   var scr = null;
   var inn = null;
   var wNoScroll = 0;
   var wScroll = 0;

   // Outer scrolling div
   scr = document.createElement('div');
   scr.style.position = 'absolute';
   scr.style.top = '-1000px';
   scr.style.left = '-1000px';
   scr.style.width = '100px';
   scr.style.height = '50px';
   // Start with no scrollbar
   scr.style.overflow = 'hidden';

   // Inner content div
   inn = document.createElement('div');
   inn.style.width = '100%';
   inn.style.height = '200px';

   // Put the inner div in the scrolling div
   scr.appendChild(inn);
   // Append the scrolling div to the doc
   document.body.appendChild(scr);

   // Width of the inner div without scrollbar
   wNoScroll = inn.offsetWidth;
   // Add the scrollbar
   scr.style.overflow = 'auto';
   // Width of the inner div width scrollbar
   wScroll = inn.offsetWidth;
   if (G_nBrowser == IE) {
      // for IE only do an extra step - the first action doesn't get it right
      wScroll = scr.scrollWidth;
   }

   // Remove the scrolling div from the doc
   document.body.removeChild(document.body.lastChild);

   // Pixel width of the scroller
   return (wNoScroll - wScroll);
}

//
function ResizeScrollArea() {
   var clDivContents = document.getElementById("DivContentsScroll");
   var clFooterArea = document.getElementById("FooterArea");
   var nClientWidth = 0;
   var nClientHeight = 0;
   var nFooterAreaHeight = 0;
   var nObjLeft = 0;
   var nObjTop = 0;
   var nObjWidth = 0;
   var nObjHeight = 0;
   var nScrollbarWidth;
   var scrollX = true;
   var scrollY = true;

   if (M_nScrollbarWidth == 0) {
      M_nScrollbarWidth = getScrollerWidth();
   }
   nScrollbarWidth = M_nScrollbarWidth;
   if (clFooterArea != null) {
      nFooterAreaHeight = clFooterArea.offsetHeight;
   }

   //
   if (m_clBodyDiv != null) {
      m_clBodyDiv.style.overflow = "scroll";
      m_clBodyDiv.style.overflowX = "scroll";
      m_clBodyDiv.style.overflowY = "scroll";
      if (G_nBrowser == IE) {
         nClientWidth = document.body.clientWidth;
         nClientHeight = document.body.clientHeight;
      }
      else {
         nClientWidth = window.innerWidth;
         nClientHeight = window.innerHeight;
      }
      nObjLeft = getSize(m_clBodyDiv, WIDTH_USEDUP);
      nObjWidth = nClientWidth - nObjLeft;
      if (nObjWidth > m_nBodyoffsetWidth || nObjWidth <= 0) {
         nObjWidth = m_nBodyoffsetWidth;
         m_clBodyDiv.style.overflowX = "hidden";
         scrollX = false;
      }
      nObjTop = getSize(m_clBodyDiv, HEIGHT_USEDUP);
      nObjHeight = nClientHeight - nObjTop - nFooterAreaHeight;
      if (nObjHeight > m_nBodyoffsetHeight || nObjHeight <= 0) {
         nObjHeight = m_nBodyoffsetHeight;
         m_clBodyDiv.style.overflowY = "hidden";
         scrollY = false;
      }
   }

   switch (G_nBrowser) {
      case IE:
         if (m_clBodyDiv != null) {
            m_clBodyDiv.style.posHeight = nObjHeight + nScrollbarWidth;
            m_clBodyDiv.style.posWidth = nObjWidth + nScrollbarWidth;
         }
         if (m_clHeaderColumnDiv != null) {
            m_clHeaderColumnDiv.style.posHeight = nObjHeight;
            m_clHeaderColumnDiv.style.overflow = "hidden";
         }
         if (m_clHeaderRowDiv != null) {
            m_clHeaderRowDiv.style.posWidth = nObjWidth;
            m_clHeaderRowDiv.style.overflow = "hidden";
         }
         if (scrollX && scrollY) {
            if (m_clBodyDiv != null) {
               m_clBodyDiv.style.posHeight = nObjHeight;
               m_clBodyDiv.style.posWidth = nObjWidth;
            }
            if (m_clHeaderColumnDiv != null) {
               m_clHeaderColumnDiv.style.posHeight = nObjHeight - nScrollbarWidth;
            }
            if (m_clHeaderRowDiv != null) {
               m_clHeaderRowDiv.style.posWidth = nObjWidth - nScrollbarWidth;
            }
         }
         if (!scrollX) {
            if (m_clBodyDiv != null) {
               m_clBodyDiv.style.posHeight = m_clBodyDiv.offsetHeight - nScrollbarWidth;
            }
         }
         if (!scrollY) {
            if (m_clBodyDiv != null) {
               m_clBodyDiv.style.posWidth = m_clBodyDiv.offsetWidth - nScrollbarWidth;
            }
         }
         break;
      default:
         if (m_clBodyDiv != null) {
            m_clBodyDiv.style.height = nObjHeight + nScrollbarWidth;
            m_clBodyDiv.style.width = nObjWidth + nScrollbarWidth;
         }
         if (m_clHeaderColumnDiv != null) {
            m_clHeaderColumnDiv.style.height = nObjHeight;
            m_clHeaderColumnDiv.style.overflow = "hidden";
         }
         if (m_clHeaderRowDiv != null) {
            m_clHeaderRowDiv.style.width = nObjWidth;
            m_clHeaderRowDiv.style.overflow = "hidden";
         }
         if (scrollX && scrollY) {
            if (m_clBodyDiv != null) {
               m_clBodyDiv.style.height = nObjHeight;
               m_clBodyDiv.style.width = nObjWidth;
            }
            if (m_clHeaderColumnDiv != null) {
               m_clHeaderColumnDiv.style.height = nObjHeight - nScrollbarWidth;
            }
            if (m_clHeaderRowDiv != null) {
               m_clHeaderRowDiv.style.width = nObjWidth - nScrollbarWidth;
            }
         }
         if (!scrollX && !scrollY) {
            if (m_clBodyDiv != null) {
               m_clBodyDiv.style.overflow = "hidden";
            }
         }
         else {
            if (scrollX && !scrollY) {
               if (m_clBodyDiv != null) {
                  m_clBodyDiv.style.overflow = "-moz-scrollbars-horizontal";
                  m_clBodyDiv.style.width = nObjWidth;
               }
               if (m_clHeaderRowDiv != null) {
                  m_clHeaderRowDiv.style.width = nObjWidth;
                  m_clHeaderRowDiv.style.overflow = "hidden";
               }
            }
            else if (scrollY & !scrollX) {
               if (m_clBodyDiv != null) {
                  m_clBodyDiv.style.height = nObjHeight;
                  m_clBodyDiv.style.overflow = "-moz-scrollbars-vertical";
               }
               if (m_clHeaderColumnDiv != null) {
                  m_clHeaderColumnDiv.style.height = nObjHeight;
                  m_clHeaderColumnDiv.style.overflow = "hidden";
               }
            }
         }
         break;
   }

   //
   if (clDivContents != null) {
      clDivContents.style.visibility = 'visible';
   }
}

//
function SyncScroll() {
   if (m_clBodyDiv != null) {
      if (m_clHeaderRowDiv != null) {
         m_clHeaderRowDiv.scrollLeft = m_clBodyDiv.scrollLeft;
      }
      if (m_clHeaderColumnDiv != null) {
         m_clHeaderColumnDiv.scrollTop = m_clBodyDiv.scrollTop;
      }
   }
}

