////////////////////////////////////////////////////////////////////////////////
//
// General Page Management Module
// Copyright 2003-2010 Russell Warburton
//
// Filename: /js-bin/page.js (javascript)
// Author: Russell Warburton (russell@warburnet.com.au)
//
// No portion of this computer program may be used without prior written 
// permission from the author. This computer program is protected by 
// international copyright laws.
//
// Created: --/--/--
// Updated: 12/09/10 - head comment section added
//
////////////////////////////////////////////////////////////////////////////////

var rootPath = "";
var documentPath = "";
var documentPathArray = "";

var browserWindowHeight = 0;
var browserWindowWidth = 0;

// Parse the document path
parseDocumentPath();

// Parse document path
function parseDocumentPath()
{
	rootPath = "/";

	// Remove the protocol string from the document URL
	documentPath = document.URL.split("//")[1];

	// Remove a bookmark string from the document URL
	documentPath = documentPath.split("#")[0];

	// Remove a query string from the document URL
	documentPath = documentPath.split("?")[0];

	// Remove the domain from the document URL
	if (documentPath.indexOf("/") != -1)
	{
		documentPath = documentPath.substring(documentPath.indexOf("/") + 1, documentPath.length);
	}

	// Split the document path into separate folders
	documentPathArray = documentPath.split("/");
}

// Get the url parameters
var URLParameters = getURLParameters();

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

function getURLParameters()
{

	var index = document.URL.indexOf('?');
	var Parameters = new Array();

	if (index != -1)
	{
		var ParameterPairs = document.URL.substring(index + 1, document.URL.length).split('&');

		for (var i = 0; i < ParameterPairs.length; i++)
		{
			var ParameterValues = ParameterPairs[i].split('=');
			Parameters[ParameterValues[0]] = ParameterValues[1];
		}
	}
	
	return Parameters;
}

function reloadPage()
{

	var ReloadPageURL = document.URL.split('?')[0];

	if ((document.body.scrollLeft > 0) || (document.body.scrollTop > 0))
		ReloadPageURL += "?xpos=" + document.body.scrollLeft + "&ypos=" + document.body.scrollTop;

	// Reload the page
	document.location.href = ReloadPageURL;
}

function onPageLoad()
{

	var xpos = unescape(URLParameters["xpos"]);
	var ypos = unescape(URLParameters["ypos"]);

	// Determine whether or not the x and y positions are defined
	if ((xpos != 'undefined') || (ypos != 'undefined'))
	{
		if (xpos == 'undefined') xpos = "0";
		if (ypos == 'undefined') ypos = "0";

		// Scroll window to correct position
		self.scrollTo(parseInt(xpos, 10), parseInt(ypos, 10));
	}
}

function addPageBookmark()
{
	// Add this page to the list of bookmarks
	window.external.AddFavorite(location.href, document.title);
}

function checkMatchingPaths(theLinkPathArray, theDocumentPathArray)
{
	var pathMatch = -1;
	var foldersMatched = 0;
	
	// Get the link filename and document filename
	var linkFilename = theLinkPathArray[theLinkPathArray.length - 1];
	var documentFilename = theDocumentPathArray[theDocumentPathArray.length - 1];

	// Compare the link path with the document path
	if (theLinkPathArray.length <= theDocumentPathArray.length)
	{
		// Compare each folder in the link path
		for (var index = 0; index < (theLinkPathArray.length - 1); index ++)
		{
			if (unescape(theLinkPathArray[index]) != unescape(theDocumentPathArray[index]))
			{
				// Reset the folders match counter
				foldersMatched = 0;

				break;
			}

			// Increment the folders matched counter
			foldersMatched = foldersMatched + 1;
		}

		var rootLink = ((linkFilename == "") || (linkFilename == "index.htm"));
		var rootDocument = ((documentFilename == "") || (documentFilename == "index.htm"));

		// Check whether the link is a parent of the document
		if ((foldersMatched > 0) || ((theLinkPathArray.length == 1) && (theDocumentPathArray.length == 1)))
		{
			// Check whether the link path is shorter than the document path
			if (theLinkPathArray.length < theDocumentPathArray.length)
			{
				if (rootLink)
				{
					// Calculate the path match score
					pathMatch = theDocumentPathArray.length - foldersMatched;
				}
			}
			else
			{
				if (linkFilename == documentFilename)
				{
					// The link and document are a perfect match
					pathMatch = 0;
				}
				else if (rootLink)
				{
					// Calculate the path match score
					pathMatch = theDocumentPathArray.length - foldersMatched;
				}
			}
		}
	}

	return pathMatch;
}

function createTranslator()
{

	document.write("<table border=\"0\" cellspacing=\"0\" cellpadding=\"2\">");
	document.write("<tr>");

	document.write("<td align=\"center\">");
	document.write("<a href=\"" + document.URL.split('?')[0] + "\"><img border=\"0\" height=\"20\" width=\"34\" src=\"/images/flags/uk.jpg\" alt=\"English\"><\/a>");
	document.write("</td>");

	document.write("<td align=\"center\">");
	document.write("<a href=\"http://translate.google.com/translate?u=" + document.URL.split('?')[0] + "&amp;sl=en&amp;hl=fr\"><img border=\"0\" height=\"20\" width=\"34\" src=\"/images/flags/france.jpg\" alt=\"French\"><\/a>");
	document.write("</td>");

	document.write("<td align=\"center\">");
	document.write("<a href=\"http://translate.google.com/translate?u=" + document.URL.split('?')[0] + "&amp;sl=en&amp;hl=de\"><img border=\"0\" height=\"20\" width=\"34\" src=\"/images/flags/germany.jpg\" alt=\"German\"><\/a>");
	document.write("</td>");

	document.write("</tr>");
	document.write("<tr>");

	document.write("<td align=\"center\">");
	document.write("<a href=\"http://translate.google.com/translate?u=" + document.URL.split('?')[0] + "&amp;sl=en&amp;hl=it\"><img border=\"0\" height=\"20\" width=\"34\" src=\"/images/flags/italy.jpg\" alt=\"Italian\"><\/a>");
	document.write("</td>");

	document.write("<td align=\"center\">");
	document.write("<a href=\"http://translate.google.com/translate?u=" + document.URL.split('?')[0] + "&amp;sl=en&amp;hl=pt\"><img border=\"0\" height=\"20\" width=\"34\" src=\"/images/flags/portugal.jpg\" alt=\"Portugese\"><\/a>");
	document.write("</td>");

	document.write("<td align=\"center\">");
	document.write("<a href=\"http://translate.google.com/translate?u=" + document.URL.split('?')[0] + "&amp;sl=en&amp;hl=es\"><img border=\"0\" height=\"20\" width=\"34\" src=\"/images/flags/spain.jpg\" alt=\"Spanish\"><\/a>");
	document.write("</td>");

	document.write("</tr>");
	document.write("</table>");

}

