function global_checkIfDocLoaded(doc, func)
{
	myReadyState = doc.readyState;
	if(myReadyState == null)
	{
		//We're on FireFox - it does not support readyStates
		myReadyState = 'complete';			
	}
	
	if(myReadyState != 'complete')
	{
		setTimeout(func, 500);
		return false;
	}
	return true;
}
	
function global_setSizes()
{
	global_GetResizableRootWindow().setSizes();
}

function global_ResizeAllIFrames()
{
	try
	{	
		global_GetResizableRootWindow().window_ResizeIFramesAndGetSelfHeight();
	}
	catch(ex)
	{
		//This exception is thrown when trying to resize external iframes
	}
}

function global_GetResizableRootWindow()
{
	var rootWindow = window;
	while(rootWindow.parent != null && rootWindow != rootWindow.parent && rootWindow.parent.global_GetResizableRootWindow != null)
	{
		rootWindow = rootWindow.parent;
	}
	return(rootWindow);
}
function global_GetResizableMainContentPage()
{
	var MainContentPage = window;
	
	while(MainContentPage.parent != MainContentPage && MainContentPage.parent.name != "mainContent" && MainContentPage.parent.global_GetResizableMainContentPage != null)
	{
		MainContentPage = MainContentPage.parent;
	}

	return(MainContentPage);
}

function window_ResizeIFramesAndGetSelfHeight()
{	
	var i;
	for(i = 0; i < this.frames.length; i++)
	{		
		var childWindow = this.frames[i];
		var childIFrame = this.document.getElementById(childWindow.name);
			
		childIFrame.width = '100%';
//		alert(childWindow.name + " - " + childIFrame.height);

		try
		{			
//alert(childWindow.name + ' - ' + childIFrame.getAttribute('noresize'))
			if ( (childIFrame.getAttribute('noresize') == null) || ((childIFrame.getAttribute('noresize') == false)) )
			{
				var scrolling = childIFrame.getAttribute('scrolling');
//			alert(scrolling);
				if(scrolling == 'yes' || scrolling == 'auto')
				{
//				alert('scrolling');
					
//					alert(childWindow.name + ' - ' + childWindow.document.body.scrollHeight + ' - ' + childIFrame.height);
					childIFrame.height = '100%';
				}
				else if(childWindow.window_ResizeIFramesAndGetSelfHeight != null)
				{
//					if(childWindow.name == 'messageArea')
//					{						
//						alert(childIFrame.height);
//					}
										
					childIFrame.height = childWindow.window_ResizeIFramesAndGetSelfHeight();
/*					
					if( (childIFrame.getAttribute('setZero') == 'true') && (childIFrame.height == 25) )
					{
						zeroValues = childIFrame.getAttribute('zeroValues');
						
						while(zeroValues.indexOf(',') != -1)
						{
							alert(zeroValues.left(zeroValues.indexOf(','))
						}
						
						if(childIFrame.height == zeroValues)
						{
							childIFrame.height = '0px';
						}
						
					}	
*/					
//				alert(childWindow.name + ": " + childIFrame.height);
				}
				else
				{
//				alert('Other Frame');
//				alert(childWindow.document.body.scrollHeight);
					childIFrame.height = childWindow.document.body.scrollHeight;
				}
			}
			else
			{
				childWindow.window_ResizeIFramesAndGetSelfHeight();
			}
		}
		catch(ex)
		{
//			alert(ex.message);
			childIFrame.height = 25;
		}
	}

	return(this.document.body.scrollHeight);
}

function global_GetElementPosition(elemID)
{ 
	var offsetTrail = document.getElementById(elemID);
	var offsetLeft = 0;
	var offsetTop = 0; 
	while (offsetTrail)
	{
		offsetLeft += offsetTrail.offsetLeft;
		offsetTop += offsetTrail.offsetTop; 
		offsetTrail = offsetTrail.offsetParent;
	} 
	
	if (navigator.userAgent.indexOf('Mac') != -1 && typeof document.body.leftMargin != 'undefined')
	{ 
		offsetLeft += document.body.leftMargin;
		offsetTop += document.body.topMargin;
	}
		
	return {left:offsetLeft,top:offsetTop};
}

// Gets the bottom left corner of an element.
function global_GetElementBottomLeftCorner(elm)
{
	var offsetTrail     = elm;
	
	var offsetLeft      = 0;
	var offsetTop       = 0; 
	var offsetBottom    = offsetTrail.clientHeight;

	while (offsetTrail)
	{
		offsetLeft += offsetTrail.offsetLeft;
		offsetTop += offsetTrail.offsetTop; 
		offsetTrail = offsetTrail.offsetParent;
	}

	if (navigator.userAgent.indexOf('Mac') != -1 && typeof document.body.leftMargin != 'undefined')
	{
		offsetLeft += document.body.leftMargin;
		offsetTop += document.body.topMargin;
	}

    offsetBottom += offsetTop;

	return {left:offsetLeft,bottom:offsetBottom};
}

/*Query String Methods*/
function PageQuery(q) 
{
	if(q.length > 1) 
	{
		this.q = q.substring(1, q.length);
	}
	else 
	{
		this.q = null;
	}
	
	this.keyValuePairs = new Array();
	if(q) 
	{
		for(var i=0; i < this.q.split("&").length; i++) 
		{
			this.keyValuePairs[i] = this.q.split("&")[i];
		}
	}

	this.getKeyValuePairs = function() { return this.keyValuePairs; }
	this.getValue = function(s) 
	{
		for(var j=0; j < this.keyValuePairs.length; j++) 
		{
			if(this.keyValuePairs[j].split("=")[0] == s)
			{
				return this.keyValuePairs[j].split("=")[1];
			}
		}

		return false;
	}

	this.getParameters = function() 
	{
		var a = new Array(this.getLength());
		for(var j=0; j < this.keyValuePairs.length; j++) 
		{
			a[j] = this.keyValuePairs[j].split("=")[0];
		}
		return a;
	}

	this.getLength = function() 
	{ 
		return this.keyValuePairs.length;
	}
}

function queryString(key)
{
	var page = new PageQuery(window.location.search); 
	return unescape(page.getValue(key)); 
}

function global_SetStyle(element, newWidth, newHeight)
{
	try
	{
		if(newWidth != '')
		{
			element.getAttribute('style').setAttribute('width', newWidth);
		}
		if(newHeight != '')
		{
			element.getAttribute('style').setAttribute('height', newWidth);
		}
	}
	catch(ex)
	{
		var styleStr = '';
		
		if(newWidth != '')
		{
			styleStr += 'width:' + newWidth + ';';
		}
		if(newHeight != '')
		{
			styleStr += 'height:' + newHeight + ';';
		}
		
		element.setAttribute('style',styleStr);
	}		
}


////////////////////////////////////////////////////////////////////////////
/////  Wait Bar


// Show wait bar and add the specified page to a queue of pages processing information simulteaneously.
function global_ShowWaitBar(pageId)
{
    pageId = pageId.substring(pageId.lastIndexOf('/') + 1, pageId.length)
    rootWindow = global_GetResizableRootWindow();

    //Place inside a try/catch because whe the page is a stand-alone its root window may not have the waitBarDiv
    try
    {
        waitBarDiv = rootWindow.document.getElementById('waitBarDiv');
        waitBarDiv.className = '';

	    mainContent = rootWindow.document.getElementById('mainContent');

	    if(rootWindow.waitBarPages == null)
	    {
	        rootWindow.waitBarPages = new Array();
	    }
	    rootWindow.waitBarPages[rootWindow.waitBarPages.length] = pageId;

        if(mainContent != null)
        {
            waitBarDiv.style.top  = global_GetElementBottomLeftCorner(mainContent).bottom - 38;
            waitBarDiv.style.left = global_GetElementBottomLeftCorner(mainContent).left + 8;
        }
    }
    catch(ex)
    {
    }
}

// Hide the wait bar, if we have not pages processing information.
function global_HideWaitBar(pageId)
{
    // pageId it's the complete url to the page, so we clean it up a little bit.
    pageId = pageId.substring(pageId.lastIndexOf('/') + 1, pageId.length)

    rootWindow = global_GetResizableRootWindow();
     
    //Place inside a try/catch because whe the page is a stand-alone its root window may not have the waitBarDiv
    try
    {
        if(rootWindow.waitBarPages == null)
        {
            rootWindow.waitBarPages = new Array();
        }

        if(rootWindow.waitBarPages.length == 1 && rootWindow.waitBarPages[0] == pageId)
        {
            rootWindow.waitBarPages.length = 0;
        }
        else
        {
            for (var i = 0; i < rootWindow.waitBarPages.length - 1; i++)
            {
                if (rootWindow.waitBarPages[i] != pageId)
                {
                    rootWindow.waitBarPages[i] = rootWindow.waitBarPages[i + 1];
                }
                else 
                {
                    rootWindow.waitBarPages.length = rootWindow.waitBarPages.length - 1;
                }
            }
        }
        
        if(rootWindow.waitBarPages.length == 0)
        {
            waitBarDiv = rootWindow.document.getElementById('waitBarDiv');
            waitBarDiv.className = 'noDisplay';
            
            rootWindow.waitBarPages = null;
        }
    }
    catch(ex)
    {
    }
}

// Always hide the wait bar, no matter what's happening.
function global_HideWaitBarAtomic()
{
    try
    {
        rootWindow = global_GetResizableRootWindow();
        
        waitBarDiv = rootWindow.document.getElementById('waitBarDiv');
        waitBarDiv.className = 'noDisplay';
            
        rootWindow.waitBarPages = null;
    }
    catch(ex)
    {
    }
}

//Confirmation windows
var answerFunction;

function global_myConfirm(text, button1Text, button2Text, answerFunc) 
{
    var rootWindow = global_GetResizableRootWindow();
    
    var box = rootWindow.document.getElementById("confirmBox");
    if (box == null)
    {
        if(confirm(text))
        {
            answerFunc(true);
        }
        else
        {
            answerFunc(false);
        }
        return;
    }
   
    box.getElementsByTagName("p")[0].firstChild.nodeValue = text;
    var links = box.getElementsByTagName("a");
	
    links[0].innerText = button1Text;
    links[1].innerText = button2Text;
    rootWindow.answerFunction = answerFunc;
    box.className = 'paleBlueBackground fullLightBorder fullPadding';
}

function global_answer(response) 
{	
    document.getElementById("confirmBox").className = 'noDisplay';
    rootWindow.answerFunction(response);
}

