﻿





/*

This only works in IE.  

H scroll fixing only works
V scroll fixing only works
V+H puts the scrollbar in the questions, not in the data table
//


var divContent = null;
var divHeaderRow = null;
var divHeaderColumn = null;
var divHeaderRowColumn = null;
var headerRowFirstColumn = null;
var x;
var y;
var horizontal = false;
var vertical = false;

// Copy table to top and to left
function CreateScrollHeader(content, scrollHorizontal, scrollVertical)
{
	horizontal = scrollHorizontal;
	vertical = scrollVertical;

//alert('horizontal:' + horizontal + '  vertical:' + vertical);
// vertical: x scrollbar
// horizontal: y scrollbar
	if (content != null)
	{
		divContent = content;
		var headerRow = divContent.childNodes[0].childNodes[0].childNodes[0];
		x = divContent.childNodes[0].offsetWidth; // changing this figure stops the scroll freeze from working
		y = divContent.childNodes[0].offsetHeight;		// changing this figure stops the scroll freeze from working

		divHeaderRow = divContent.cloneNode(true);		

		if (horizontal)
		{
			divHeaderRow.style.height = headerRow.offsetHeight;
			divHeaderRow.style.overflow = "hidden";
			
			divContent.parentNode.insertBefore(divHeaderRow, divContent);
			divContent.childNodes[0].style.position = "absolute";
			divContent.childNodes[0].style.top = "-" + headerRow.offsetHeight;
			
			y = y - headerRow.offsetHeight;
		}
		
		divHeaderRowColumn = divHeaderRow.cloneNode(true);			
		
		headerRowFirstColumn = headerRow.childNodes[0];  // changing this figure doesn't change anything
		divHeaderColumn = divContent.cloneNode(true);
		divContent.style.position = "relative";

		if (vertical)
		{
			divContent.parentNode.insertBefore(divHeaderColumn, divContent);
			divContent.style.left = headerRowFirstColumn.offsetWidth;
			
			divContent.childNodes[0].style.position = "absolute";
			divContent.childNodes[0].style.left = "-" + headerRowFirstColumn.offsetWidth;
		}
		else
		{
		// removing this doesn't change anything
			divContent.style.left = 0;
		}
						
		if (vertical)
		{
		    // Adding to headerRowFirstColumn.offsetWidth increases the horizontal fixed column
            divHeaderColumn.style.width = (headerRow.innerHTML.split("reportCorner").length-1) * headerRowFirstColumn.offsetWidth;
//			divHeaderColumn.style.width = headerRowFirstColumn.offsetWidth;

			divHeaderColumn.style.overflow = "hidden";
			divHeaderColumn.style.zIndex = "99";
			
			divHeaderColumn.style.position = "absolute";
			divHeaderColumn.style.left = "0";

			//removing this line stops the frozen row columns from scrolling with the rest of the table
			addScrollSynchronization(divHeaderColumn, divContent, "vertical");
			x = x - headerRowFirstColumn.offsetWidth;
		}
		
		if (horizontal)
		{
			if (vertical)
			{
				divContent.parentNode.insertBefore(divHeaderRowColumn, divContent);
			}
			divHeaderRowColumn.style.position = "absolute";
			divHeaderRowColumn.style.left = "0";
			divHeaderRowColumn.style.top = "0";
			// Adding to this does nothing
			divHeaderRowColumn.style.width = headerRowFirstColumn.offsetWidth;
			
			divHeaderRowColumn.overflow = "hidden";
			divHeaderRowColumn.style.zIndex = "100";
			divHeaderRowColumn.style.backgroundColor = "#ffffff";
			
		}
		
		if (horizontal)
		{
			addScrollSynchronization(divHeaderRow, divContent, "horizontal");
		}
		
		if (horizontal || vertical)
		{
			window.onresize = ResizeScrollArea;
			ResizeScrollArea();
		}
	}
}


// Resize scroll area to window size.  Runs each time page content is changed or resized
function ResizeScrollArea()
{
// the height of the window.  Increase the figure if the page header size increases to avoid the window scrollbar
	var height = document.documentElement.clientHeight - 220;
	if (!vertical)
	{
		height -= divHeaderRow.offsetHeight;
	}
	var width = document.documentElement.clientWidth - 20;
	if (!horizontal)
	{
		width -= divHeaderColumn.offsetWidth;
	}
	var headerRowsWidth = 0;


    if (divContent.childNodes[0] != null) 
    {
	    divContent.childNodes[0].style.width = x;
	    divContent.childNodes[0].style.height = y;
    	
	    if (divHeaderRowColumn != null)
	    {
		    headerRowsWidth = divHeaderRowColumn.offsetWidth;
	    }

	    // If table width is larger than page size (lots of countries)
	    if (divContent.childNodes[0].offsetWidth > width)
	    {
            // this removes the divContent scrollbar
		    //divContent.style.width = x;
//		    divContent.childNodes[0].style.width = 5000; //Math.max(width - 80);
		    divContent.childNodes[0].style.width = Math.max(width - headerRowsWidth, 0);//-(headerRow.innerHTML.split("reportCorner").length-1) * headerRowFirstColumn.offsetWidth;
//alert('1');

// Changed these lines from divContent.style to divContent.childNodes[0].style to put X scrollbar on data table
//    	    divContent.childNodes[0].style.width = x;
		    divContent.childNodes[0].style.overflowX = "scroll";
		    divContent.childNodes[0].style.overflowY = "hidden";
	    }
	    else
	    {
//alert('2');
		    divContent.style.width = x;
		    divContent.childNodes[0].style.overflowX = "auto";
		    divContent.childNodes[0].style.overflowY = "auto";
	    }
    }
	if (divHeaderRow != null)
	{
		divHeaderRow.style.width = divContent.offsetWidth + headerRowsWidth;
	}

	// height
    if (divContent.childNodes[0] != null) 
    {
        // if table heigth is greated than page size (lots of questions)
	    if (divContent.childNodes[0].offsetHeight > height)
	    {
//alert('3');
		    divContent.style.height = Math.max(height, 80);
		    divContent.style.overflowY = "scroll";
	    }
	    else
	    {
//alert('4');
		    divContent.style.height = y;
		    divContent.style.overflowY = "hidden";
	    }
    }
    if (divHeaderColumn != null)
    {
	    divHeaderColumn.style.height = divContent.offsetHeight;
    }

    // check scrollbars
    // This block controls the position of the scrollbars relevant to the table
    if (divContent.style.overflowY == "scroll")
    {
	    divContent.style.width = (divContent.offsetWidth + 17);
    }
    if (divContent.style.overflowX == "scroll")
    {
	    divContent.style.height = divContent.offsetHeight + 17;
    }
    
    // Maybe the width of the whole table - not sure
    if (divContent.parentElement != null)
    {
	    divContent.parentElement.style.width = divContent.offsetWidth + headerRowsWidth;
    //divContent.parentElement.style.width = "100%";
    }
    // Hides the extra scrollbar for the questions column that should never be shown
    divHeaderColumn.style.overflowY = "hidden";
    // nothing
    //if (divContent.style.width > 200) divContent.style.width = divContent.style.width - 200;
    
}
/*
// Resize scroll area to window size.  Runs each time page content is changed or resized
function ResizeScrollArea()
{
// the height of the window.  Increase the figure if the page header size increases to avoid the window scrollbar
	var height = document.documentElement.clientHeight - 250;
	if (!vertical)
	{
		height -= divHeaderRow.offsetHeight;
	}
	var width = document.documentElement.clientWidth - 20;
	if (!horizontal)
	{
		width -= divHeaderColumn.offsetWidth;
	}
	var headerRowsWidth = 0;


    if (divContent.childNodes[0] != null) 
    {
	    divContent.childNodes[0].style.width = x;
	    divContent.childNodes[0].style.height = y;
    	
	    if (divHeaderRowColumn != null)
	    {
		    headerRowsWidth = divHeaderRowColumn.offsetWidth;
	    }

	    // If table width is larger than page size (lots of countries)
	    if (divContent.childNodes[0].offsetWidth > width)
	    {
            // this removes the divContent scrollbar
		    //divContent.style.width = x;
//		    divContent.childNodes[0].style.width = 5000; //Math.max(width - 80);
		    divContent.childNodes[0].style.width = Math.max(width - headerRowsWidth, 0);
//alert('1');

// Changed these lines from divContent.style to divContent.childNodes[0].style to put X scrollbar on data table
//    	    divContent.childNodes[0].style.width = x;
		    divContent.childNodes[0].style.overflowX = "scroll";
		    divContent.childNodes[0].style.overflowY = "hidden";
	    }
	    else
	    {
//alert('2');
		    divContent.style.width = x;
		    divContent.childNodes[0].style.overflowX = "auto";
		    divContent.childNodes[0].style.overflowY = "auto";
	    }
    }
	if (divHeaderRow != null)
	{
		divHeaderRow.style.width = divContent.offsetWidth + headerRowsWidth;
	}

	// height
    if (divContent.childNodes[0] != null) 
    {
        // if table heigth is greated than page size (lots of questions)
	    if (divContent.childNodes[0].offsetHeight > height)
	    {
//alert('3');
		    divContent.style.height = Math.max(height, 80);
		    divContent.style.overflowY = "scroll";
//		    divContent.style.overflowY = "scroll";
	    }
	    else
	    {
//alert('4');
		    divContent.style.height = y;
		    divContent.style.overflowY = "hidden";
	    }
    }
    if (divHeaderColumn != null)
    {
	    divHeaderColumn.style.height = divContent.offsetHeight;
    }

    // check scrollbars
    // This block controls the position of the scrollbars relevant to the table
    if (divContent.style.overflowY == "scroll")
    {
	    divContent.style.width = (divContent.offsetWidth + 17);
    }
    if (divContent.style.overflowX == "scroll")
    {
	    divContent.style.height = divContent.offsetHeight + 17;
    }
    
    // Maybe the width of the whole table - not sure
    if (divContent.parentElement != null)
    {
	    divContent.parentElement.style.width = divContent.offsetWidth + headerRowsWidth;
    }
    
    // Hides the extra scrollbar for the questions column that should never be shown
    divHeaderColumn.style.overflowY = "hidden";
    
    // nothing
    //if (divContent.style.width > 200) divContent.style.width = divContent.style.width - 200;
    
}




// ********************************************************************************
// Synchronize div elements when scrolling 
// from http://webfx.eae.net/dhtml/syncscroll/syncscroll.html
// ********************************************************************************
// This is a function that returns a function that is used
// in the event listener
function getOnScrollFunction(oElement) {
	return function () {
	// This sychronises the row scroll with the column scroll (removing it will not scroll the countries with the table)
		if (oElement._scrollSyncDirection == "horizontal" || oElement._scrollSyncDirection == "both")
			oElement.scrollLeft = event.srcElement.scrollLeft;
		if (oElement._scrollSyncDirection == "vertical" || oElement._scrollSyncDirection == "both")
			oElement.scrollTop = event.srcElement.scrollTop;
	};

}

// This function adds scroll syncronization for the fromElement to the toElement
// this means that the fromElement will be updated when the toElement is scrolled
function addScrollSynchronization(fromElement, toElement, direction) {
	removeScrollSynchronization(fromElement);
	
	fromElement._syncScroll = getOnScrollFunction(fromElement);
	fromElement._scrollSyncDirection = direction;
	fromElement._syncTo = toElement;
	toElement.attachEvent("onscroll", fromElement._syncScroll);
}

// removes the scroll synchronization for an element
function removeScrollSynchronization(fromElement) {
	if (fromElement._syncTo != null)
		fromElement._syncTo.detachEvent("onscroll", fromElement._syncScroll);

	fromElement._syncTo = null;
	fromElement._syncScroll = null;
	fromElement._scrollSyncDirection = null;
}

*/




///*



//This should work in Firefox



var divContent = null;
var divHeaderRow = null;
var divHeaderColumn = null;
var divHeaderRowColumn = null;
var headerRowFirstColumn = null;
var x;
var y;
var horizontal = false;
var vertical = false;
var childElement = 0;

// Copy table to top and to left
function CreateScrollHeader(content, scrollHorizontal, scrollVertical)
{
    horizontal = scrollHorizontal;
    vertical = scrollVertical;

    if (content != null)
    {
        divContent = content;

        if (divContent.childNodes[childElement].tagName == null)
        {
            childElement = 1;
        }

        //var headerRow = divContent.childNodes[childElement].childNodes[childElement];
        var headerRow = divContent.childNodes[childElement].childNodes[childElement].childNodes[0];
        x = divContent.childNodes[childElement].offsetWidth;
        y = divContent.childNodes[childElement].offsetHeight;

        divHeaderRow = divContent.cloneNode(true);
        if (horizontal)
        {
            divHeaderRow.style.height = headerRow.offsetHeight + 'px';
            divHeaderRow.style.overflow = "hidden";

            divContent.parentNode.insertBefore(divHeaderRow, divContent);
            divContent.childNodes[childElement].style.position = "absolute";
            divContent.childNodes[childElement].style.top = "-" + headerRow.offsetHeight + 'px';

            y = y - headerRow.offsetHeight;
        }

        divHeaderRowColumn = divHeaderRow.cloneNode(true);
        //headerRowFirstColumn = headerRow.childNodes[childElement].childNodes[childElement];
        headerRowFirstColumn = headerRow.childNodes[childElement];
        divHeaderColumn = divContent.cloneNode(true);
        divContent.style.position = "relative";

        if (vertical)
        {
            divContent.parentNode.insertBefore(divHeaderColumn, divContent);
            divContent.style.left = headerRowFirstColumn.offsetWidth + 'px';

            divContent.childNodes[childElement].style.position = "absolute";
            divContent.childNodes[childElement].style.left = "-" + headerRowFirstColumn.offsetWidth + 'px';
        }
        else
        {
            divContent.style.left = "0px";
        }

        if (vertical)
        {
            divHeaderColumn.style.width = headerRowFirstColumn.offsetWidth + 'px';
            divHeaderColumn.style.overflow = "hidden";
            divHeaderColumn.style.zIndex = "99";

            divHeaderColumn.style.position = "absolute";
            divHeaderColumn.style.left = "0px";
            addScrollSynchronization(divHeaderColumn, divContent, "vertical");
            x = x - headerRowFirstColumn.offsetWidth;
        }

        if (horizontal)
        {
            if (vertical)
            {
                divContent.parentNode.insertBefore(divHeaderRowColumn, divContent);
            }
            divHeaderRowColumn.style.position = "absolute";
            divHeaderRowColumn.style.left = "0px";
            divHeaderRowColumn.style.top = "0px";
            divHeaderRowColumn.style.width = headerRowFirstColumn.offsetWidth + 'px';
            divHeaderRowColumn.overflow = "hidden";
            divHeaderRowColumn.style.zIndex = "100";
            divHeaderRowColumn.style.backgroundColor = "#ffffff";

        }

        if (horizontal)
        {
            addScrollSynchronization(divHeaderRow, divContent, "horizontal");
        }

        if (horizontal || vertical)
        {
            window.onresize = ResizeScrollArea;
            ResizeScrollArea();
        }
    }
}


// Resize scroll area to window size.
function ResizeScrollArea()
{

    var height = document.documentElement.clientHeight - 220;
    if (!vertical)
    {
        height -= divHeaderRow.offsetHeight;
    }
    var width = document.documentElement.clientWidth - 20;
    if (!horizontal)
    {
        width -= divHeaderColumn.offsetWidth;
    }

    var headerRowsWidth = 0;

    if (divContent.childNodes[0] != null) 
    {
        divContent.childNodes[childElement].style.width = x + 'px';
        divContent.childNodes[childElement].style.height = y + 'px';

        if (divHeaderRowColumn != null)
        {
           headerRowsWidth = divHeaderRowColumn.offsetWidth;
        }

    // width

        if (divContent.childNodes[childElement].offsetWidth > width)
        {
            divContent.style.width = Math.max(width - headerRowsWidth, 0) + 'px';
            divContent.style.overflowX = "scroll";
            divContent.style.overflowY = "auto";
        }
        else
        {
            divContent.style.width = x + 'px';
            divContent.style.overflowX = "auto";
            divContent.style.overflowY = "auto";
        }
    }


    if (divHeaderRow != null)
    {
        divHeaderRow.style.width = (divContent.offsetWidth + headerRowsWidth) + 'px';
    }

    // height
    if (divContent.childNodes[0] != null) 
    {
        if (divContent.childNodes[childElement].offsetHeight > height)
        {
            divContent.style.height = Math.max(height, 80) + 'px';
            divContent.style.overflowY = "scroll";
        }
        else
        {
            divContent.style.height = y + 'px';
            divContent.style.overflowY = "hidden";
        }
    }
    if (divHeaderColumn != null)
    {
        divHeaderColumn.style.height = divContent.offsetHeight + 'px';
    }

    // check scrollbars
    if (divContent.style.overflowY == "scroll")
    {
        divContent.style.width = (divContent.offsetWidth + 17) + 'px';
    }
    if (divContent.style.overflowX == "scroll")
    {
       divContent.style.height = (divContent.offsetHeight + 17) + 'px';
    }
    if (divContent.parentNode != null)
    {
        divContent.parentNode.style.width = (divContent.offsetWidth + headerRowsWidth) + 'px';
    }

}


// ********************************************************************************
// Synchronize div elements when scrolling
// from http://webfx.eae.net/dhtml/syncscroll/syncscroll.html
// ********************************************************************************
// This is a function that returns a function that is used
// in the event listener
function getOnScrollFunction(oElement, srcElement) {
    return function () {
        if (oElement._scrollSyncDirection == "horizontal" || oElement._scrollSyncDirection == "both")
        oElement.scrollLeft = srcElement.scrollLeft;
        if (oElement._scrollSyncDirection == "vertical" || oElement._scrollSyncDirection == "both")
        oElement.scrollTop = srcElement.scrollTop;
    };

}

// This function adds scroll syncronization for the fromElement to the toElement
// this means that the fromElement will be updated when the toElement is scrolled
function addScrollSynchronization(fromElement, toElement, direction) {
    removeScrollSynchronization(fromElement);

    fromElement._syncScroll = getOnScrollFunction(fromElement, toElement);
    fromElement._scrollSyncDirection = direction;
    fromElement._syncTo = toElement;
    if (toElement.addEventListener) {
    toElement.addEventListener("scroll", fromElement._syncScroll, false);
    } else {
    toElement.attachEvent("onscroll", fromElement._syncScroll);
    }
}

// removes the scroll synchronization for an element
function removeScrollSynchronization(fromElement) {
    if (fromElement._syncTo != null) {
        if (fromElement._syncTo.removeEventListener) {
            fromElement._syncTo.removeEventListener("scroll", fromElement._syncScroll, false);
        } else {
            fromElement._syncTo.detachEvent("onscroll", fromElement._syncScroll);
        }
    }

    fromElement._syncTo = null;
    fromElement._syncScroll = null;
    fromElement._scrollSyncDirection = null;
}
//*/

