//This file exists so I only have to maintain code in one place.

function GetURLArgs(strURL)
{
    var strResult = null;
	var iIndex = strURL.indexOf("?", 0);
	if (iIndex >= 0)
	{
		strResult = unescape(strURL.substr(iIndex + 1));
	}
	return strResult;
}

function GetMainFrameWindow()
{
	var Result = null;
	if (window.frames != null && window.frames.length > 0)
	{
		Result = window.frames["main"];
	}
	return Result;
}

function IsImageOnly(strURL)
{
	var bResult = false;
	
	var iPeriodPos = strURL.lastIndexOf(".");
	if (iPeriodPos >= 0)
	{
		var strExt = strURL.substr(iPeriodPos).toLowerCase();
		if (strExt == ".gif" || strExt == ".jpg" || strExt == ".bmp" || strExt == ".png")
		{
			bResult = true;
		}				
	}
	
	return bResult;
}

function OnLoadMainFrame()
{
	//If the current page was not launched with the Printable flag,
	//then we need to add a "View Printable Version" link.
	var Win = GetMainFrameWindow();
	if (Win != null)
	{
		var strURL = Win.location.href;
		var strArgs = GetURLArgs(strURL);
		if (strArgs != "Printable")
		{
			var Doc = Win.document;
			if (Doc != null && !IsImageOnly(strURL))
			{
				//Note: FireFox doesn't support setting innerText or pulling childNodes(0).
				var Div = Doc.createElement("div");
				Div.className = "PrintableLink";
				var Link = Doc.createElement("a");
				Link.href = strURL + "?Printable";
				Link.target="_blank";
				Link.appendChild(Doc.createTextNode("View Printable Version"));
				Div.appendChild(Link);
				
				if (Doc.body.childNodes.length > 0)
				{
					Doc.body.insertBefore(Div, Doc.body.firstChild);
				}
				else
				{
					Doc.body.appendChild(Div);
				}
			}
		}
	}
}

function EnsurePageIsFramed()
{
	if (top.location == self.location)
	{
		//Store the current URL for index.html to load
		var strURL = self.location.href;
		var strArgs = GetURLArgs(strURL);

	    if (strArgs == null)
		{ 
			//Handle Contents.htm and Banner.htm specially.
			//When we redirect to index.html they'll be shown
			//in their appropriate frames, so don't redisplay them
			//in the "Main" frame.
			if (strURL.indexOf("/Contents.htm", 0) >= 0 || strURL.indexOf("/Banner.htm", 0) >= 0)
			{
				top.location.href = "../index.html";
			}
			else
			{	   
				//Note: Set window.location.href because it works in IE
				//and Netscape.  window.navigate() only worked in IE.
				top.location.href = "../index.html?" + escape(strURL);
			} 
    		
		    //Note: I used to use cookies to pass the referring URL,
		    //but those stopped working when I changed to a relative
		    //path.  It appears that the whole document object gets
		    //recreated when you switch to a different root, and the
		    //cookies aren't preserved.
		}
	}
}

function LoadReferrerIfNecessary()
{
	var strURL = self.location.href
	var strReferringURL = GetURLArgs(strURL);
	var MainFrameWindow = GetMainFrameWindow();

	//Load the referring URL into the "main" frame.
	if (strReferringURL != null && MainFrameWindow != null)
	{
		MainFrameWindow.location.href = strReferringURL;
	}
}
