﻿function getPageSizeOf( theDocument )
{
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY)
	{	
		xScroll = theDocument.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	}
	else if (theDocument.body.scrollHeight > theDocument.body.offsetHeight)
	{ // all but Explorer Mac
		xScroll = theDocument.body.scrollWidth;
		yScroll = theDocument.body.scrollHeight;
	}
	else
	{ // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = theDocument.body.offsetWidth;
		yScroll = theDocument.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight)
	{	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	}
	else if (theDocument.documentElement && theDocument.documentElement.clientHeight)
	{ // Explorer 6 Strict Mode
		windowWidth = theDocument.documentElement.clientWidth;
		windowHeight = theDocument.documentElement.clientHeight;
	}
	else if (theDocument.body)
	{ // other Explorers
		windowWidth = theDocument.body.clientWidth;
		windowHeight = theDocument.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight)
		pageHeight = windowHeight;
	else
		pageHeight = yScroll;

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth)
		pageWidth = windowWidth;
	else
		pageWidth = xScroll;

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

function getPageSize()
{
	return( getPageSizeOf( document ) );
}

var antirecurse;

function toggleSelects( theDoc, showHide )
{
	// Just in case it ends up recursing due to some dodgy JS implementation
	antirecurse++;
	if( antirecurse>9)
		return;
	
	selects = theDoc.getElementsByTagName("select");
    for (i = 0; i != selects.length; i++)
		selects[i].style.visibility = showHide;

	if( theDoc.frames.length > 0 )
	{
		for( i = 0; i < theDoc.frames.length; i++ )
			toggleSelects( theDoc.frames[i].document, showHide );
	}
}

function showLoadingOverlay()
{
	showLoadingOverlayOn( document );
}

function hideLoadingOverlay()
{
	hideLoadingOverlayOn( document );
}

function initLoadingOverlay()
{
	initLoadingOverlayOn( document );
}

function showLoadingOverlayOn( theDocument )
{
	_showLoadingOverlayOn( theDocument );
}

//Determine how much user scrolled page down
function GetScrollTop(){    
var scrollY = document.documentElement.scrollTop;
if(!scrollY)scrollY = document.body.scrollTop;            
if(!scrollY)scrollY = window.pageYOffset;               
if(!scrollY)scrollY = 0;
return scrollY;
}

function _showLoadingOverlayOn( theDocument )
{
	pageSize = getPageSize();
	
	var objOverlay = theDocument.getElementById('overlay');
	objOverlay.style.height = pageSize[1];
	objOverlay.style.display = 'block';
	
	
	// Centres image horizontally to screen
	// Centres vertically within main workarea (although actually a layer on parent document)
	var objOverlayImage = theDocument.getElementById('overlayImage');
	
	// Load seperate copy of the image, because the width/height isn't always
	// correctly reported in IE (tsh)
	var img = new Image();
	img.src = objOverlayImage.src;
		
    		
	//objOverlayImage.style.top = (( 448 - img.height ) / 2 ) + 142;	
	//objOverlayImage.style.top = (pageSize[1] / 2)  - (img.height / 2 );
	objOverlayImage.style.top = '250px';
	objOverlayImage.style.left = (( pageSize[0] - img.width ) / 2 );
	objOverlayImage.style.display = 'block';
	
	// Hide select boxes as they will 'peek' through the image in IE
	//antirecurse = 0;
	//toggleSelects( theDocument, "hidden" );
}

function hideLoadingOverlayOn( theDocument )
{
	// get objects
	var objOverlay = theDocument.getElementById('overlay');
	objOverlay.style.display = 'none';
	
	var objOverlayImage = theDocument.getElementById('overlayImage');
	objOverlayImage.style.display = 'none';

	// make select boxes visible
	toggleSelects( theDocument, "visible" );
}

// var excludes = [ "cmdDownloadDZA" ];

//var hrefIncludeReg = new RegExp("__doPostBack|.aspx");
//var hrefExcludeReg = new RegExp("iq_OW");

function initLoadingOverlayOn( theDoc )
{
	if (!theDoc.getElementsByTagName)
		return;

	// Display loading overlay earlier than window.onunload,
	// but without interfering with Postback (hopefully!)
	var anchors = theDoc.getElementsByTagName( "a" );

	for( var i = 0; i < anchors.length; i++ )
	{
		var anchor = anchors[i];
		if( anchor.getAttribute( "href" ) )
		{
			var href = anchor.getAttribute( "href" );
			var target = anchor.getAttribute( "target" );

            //If the href includes excluded keywords, then don't wire in the overlay
            //If the href includes keywords to wire in overlay (and its not a new popup window) then do it.
			if (!href.match(hrefExcludeReg) && (href.match(hrefIncludeReg) && target != "_blank"))
			{
				bSuppress = false;
				
				// Suppress the overlay if the class name of the anchor contains the keyword 'nooverlay'
			    var supressionClassKeywordStartIndex = anchor.className.indexOf("nooverlay");				
				bSuppress = (supressionClassKeywordStartIndex != -1);
							
				if( bSuppress == false )
				{
					var oldFunc = anchor.onclick;
					if( typeof anchor.onclick != 'function' )
						anchor.onclick = function () { window.top.showLoadingOverlay(); }
				}
			}
		}
	}
	
	// Add to form.onsubmit()
	var forms = theDoc.getElementsByTagName( "form" );
	for( var i = 0; i < forms.length; i++ )
	{
		var theForm = forms[i];
		var oldFunc = theForm.onsubmit;
		if( typeof theForm.onsubmit != 'function' )
			theForm.onsubmit = function () { showLoadingOverlay(); }
		else
			theForm.onsubmit = function () { if (oldFunc()) showLoadingOverlay(); }
	}
}

function createOverlay(theDoc, loadingImageSrc)
{
    var overlay = theDoc.createElement("div");   
    overlay.id = "overlay";   
    theDoc.body.appendChild(overlay);
   
    var overlayImage = theDoc.createElement("image");    
    overlayImage.id = "overlayImage";
    overlayImage.src = loadingImageSrc;
    theDoc.body.appendChild(overlayImage);
    
    getPageSizeOf(theDoc);
}
