// ---------------------------------------------------------------------------------------------------
// These are Generic Functions for page that are designed to appear in 'Page Contents' window of the
// two top level index pages (Norwich-Pool_Index.htm & Leisure-Link_Index.htm).
// ---------------------------------------------------------------------------------------------------

// This function is called in-line in the page. It prevents the page from being opened in its own
// window. It ensures that the page is always displayed in the approriate top level index page,
// Norwich-Pool_Index.htm & Leisure-Link_Index.htm.
//
// NOTE: CookieFunctions.js must also be included in the page to get this function to work.
//
function PageMustBeOpenedInContentsWindow()
{
	if(window.parent == window)
	{
		// The page has been opened directly in it's own window.
		
		var strLocation = String(document.location);
		
		SetCookieItem("Temp", "StartUpPage", strLocation);
		
		if(GetWebSiteName() == "Norwich-Pool")
		{
			document.location = GetPathToRoot() + "Norwich-Pool_Index.htm";
		};
		
		if(GetWebSiteName() == "Leisure-Link")
		{
			document.location = GetPathToRoot() + "Leisure-Link_Index.htm";
		};
		
//		alert("document.location = " + document.location + "\n" +
//		      "document.url = " + document.url + "\n" +
//		      "document.URLUnencoded = " + document.URLUnencoded + "\n" +
//		      "document.domain = " + document.domain + "\n"
//		      );

	};

}
	
// Returns "Norwich-Pool" if this page is part of the Norwich Pool Web Site.
// Returns "Leisure-Link" if this page is part of the Leisure Link / Tunmore Leisure Web Site.
// Returns "" if this page is in the root directory.
//
function GetWebSiteName()
{
	var strWebSite = "";
	var strURL = document.URLUnencoded;
	var strDelim = "/";
		
	if(document.domain == "")
	{
		// We are running off-line on the local machine.
		// We need to use \\ here, because this is an escape character for "\".
		strDelim = "\\";
	};
		
	if(strURL.indexOf(strDelim + "Norwich-Pool" + strDelim) != -1)
	{
		// This page is part of the Norwich Pool Web Site.
		strWebSite = "Norwich-Pool";
	}
	else
	{	
		if(strURL.indexOf(strDelim + "Leisure-Link" + strDelim) != -1)
		{
			// This page is part of the Leisure Link / Tunmore Leisure Web Site.
			strWebSite = "Leisure-Link";
		};	
	};
						
	return strWebSite;
}

// Returns a string containing the correct number of "../" to return
// back to the root directory.
//	
function GetPathToRoot()
{
	var strPath = "";
	var intLevels = GetPageDirectoryLevel();
		
	for(var intLevel = 1; intLevel <= intLevels; ++intLevel)
	{
		strPath = strPath + "../";
	}
		
	return strPath;
}
	
// Returns the level of the page in the directory structure.
// 0 is returned for pages in the root directory of the web site.
// 1 is returned for pages at the next level (i.e. HomePage.htm).
// 2 is returned for pages at the second level and so on.
//
function GetPageDirectoryLevel()
{
	var intDirCnt = 0;
	var strURL = document.URLUnencoded;
	var strDomain = document.domain;
		
	if(strDomain == "")
	{
		// We are running off-line on the local machine.
		// Set the domain to name of the top directory, as stored on the PC.
		strDomain = "Norwich Pool Web Site"
	};
	
	var intStartPos = strURL.indexOf(strDomain);
		
	if(intStartPos == -1)
	{
		// Error ! - Domain not found
		// This should never happen.
	}
	else
	{
		intStartPos = intStartPos + strDomain.length + 1;
	
		var intCharCode = 0;
	
		for(var intPos = intStartPos; intPos < strURL.length ; ++intPos)
		{
			intCharCode = strURL.charCodeAt(intPos);
			
			// "/" - 47 (Separator used when online)
			// "\" - 92 (Separator used when local)
				
			if((intCharCode == 92) || (intCharCode == 47))
			{
				++intDirCnt;
			};
						
		};
		
//		alert("document.URLUnencoded = " + document.URLUnencoded + "\n" +
//	      	      "domain = " + strDomain + "\n" +
//	      	      "intStartPos = " + intStartPos + "\n" +
//	      	      "intDirCnt = " + intDirCnt + "\n"
//		     ); 
		     
	};
		
	return intDirCnt;
}
