var glInternetExplorer = ((document.all) && (!window.opera));
var glSafari = navigator.userAgent ? (navigator.userAgent.toLowerCase().indexOf("safari") > 0) : false;
var glFirefox = navigator.userAgent ? (navigator.userAgent.toLowerCase().indexOf("firefox") > 0) : false;

var goDivDisplayWebPage = null;
var goDivDisplayWebPageShadow = null;
var goOpenFloatingWindowOffset = 0;
// -------------------------------------------------------------------------------------------------------------------
// //
function getWebsiteName()
{
	var lcURL = window.location.href.toLowerCase();
	var lcTitle = "Unknown";

	if ((lcURL.indexOf("beoearth") != -1) || (lcURL.indexOf("eddie.") != -1))
	{
		lcTitle = "BeoEarth";
	}
	else if (lcURL.indexOf("beosphere") != -1)
	{
		lcTitle = "BeoSphere";
	}

	return (lcTitle);
}
// -------------------------------------------------------------------------------------------------------------------
// //
function changeWebsiteURLIfNeeded()
{
	var lcURL = window.location.href.toLowerCase();

	if (lcURL.indexOf(".net") != -1)
	{
		window.location.href = lcURL.replace(/.net/g, ".com");
	}
}
// -------------------------------------------------------------------------------------------------------------------
// //
function getSlogan()
{
	var lcSlogan;

	var lcWebsite = getWebsiteName().toLowerCase();
	if (lcWebsite.indexOf("beoearth") != -1)
	{
		lcSlogan = "Search. Chart. Discover.";
	}
	else
	{
		lcSlogan = "Search. Plot. Discover.";
	}

	return (lcSlogan);
}
// -------------------------------------------------------------------------------------------------------------------
//
function writeWebsiteName()
{
	document.write(getWebsiteName());
}
// -------------------------------------------------------------------------------------------------------------------
//
function writeSlogan()
{
	document.write(getSlogan());
}
// -------------------------------------------------------------------------------------------------------------------
//
function changeWebsiteTitle()
{
	window.document.title = getWebsiteName() + ": " + getSlogan();
}
// -------------------------------------------------------------------------------------------------------------------
//
function writeCopyright()
{
	var lnStartYear = 2009;
	var lnCurrentYear = getCurrentYear();
	var lcYears = lnStartYear.toString();

	if (lnStartYear != lnCurrentYear)
	{
		lcYears += "-" + lnCurrentYear;
	}
	document.write("Copyright&copy;&nbsp;" + lcYears + "&nbsp;") + writeWebsiteName() + document.write(". All rights reserved.");
}
// -------------------------------------------------------------------------------------------------------------------
function allTrim(tcString)
{
	while (tcString.substring(0, 1) == ' ')
	{
		tcString = tcString.substring(1, tcString.length);
	}
	while (tcString.substring(tcString.length - 1, tcString.length) == ' ')
	{
		tcString = tcString.substring(0, tcString.length - 1);
	}

	return (tcString);
}
// -------------------------------------------------------------------------------------------------------------------
// If you don't pass a time parameter, then IE, FireFox, etc. will buffer the
// results of the link and not call the link itself. So any changes to the
// databases will not be immediately reflected. With a timestamp
// parameter, the link is always different and never cached.
function buildJSInterfaceString(tcLink, tcArguments)
{
	var ldCurrent = new Date();

	// Returns the number of milliseconds since midnight Jan 1, 1970.
	var lcTimeStamp = ldCurrent.getTime().toString();

	var lcJSInterfaceLink = "/T/JSIXR.php?lnk=" + tcLink + "&timestamp=" + lcTimeStamp + tcArguments;

	return (lcJSInterfaceLink);
}
// -------------------------------------------------------------------------------------------------------------------
function pauseApplication(tnMilliseconds)
{
	var lnStartTime = new Date();
	var lnCurrentTime = null;

	do
	{
		lnCurrentTime = new Date();
	}
	while ((lnCurrentTime - lnStartTime) < tnMilliseconds);
}
// -------------------------------------------------------------------------------------------------------------------
// If you don't use this as an intermediary, then
// you will see something like "Torul, U&#287;urta&#351;&#305;, Turkey"
// rather than the correct display with the extended characters.
function convertHTMLCodesToCharacters(tcString)
{
	var lcDivID = "HTMLExtendedCharactersConversion";

	var loDiv = document.getElementById(lcDivID);
	if (!loDiv)
	{
		loDiv = document.createElement("div");
		loDiv.id = lcDivID;
		document.body.appendChild(loDiv);
	}

	// Make invisible
	loDiv.style.display = "none";
	loDiv.innerHTML = tcString;
	// The browser automatically makes the string readable.
	return (loDiv.innerHTML);
}
// -------------------------------------------------------------------------------------------------------------------
// From http://www.netlobo.com/url_query_string_javascript.html
function getURLParameter(toParameter)
{
	toParameter = toParameter.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
	var lcRegex = "[\\?&]" + toParameter + "=([^&#]*)";
	var loRegExp = new RegExp(lcRegex);

	var laResults = loRegExp.exec(window.location.href);

	if (laResults != null)
	{
		return (laResults[1]);
	}

	return ("");
}
// -------------------------------------------------------------------------------------------------------------------
function displayWebPageInDiv(tcPageLink)
{
	var loPage = getDisplayPageElement();
	var loShadow = getDisplayPageShadowElement();

	// Don't fade in the page: it's really annoying.
	setOpacity(loPage, 100);
	setOpacity(loShadow, 50);

	var lcIFrame = "<iframe id=\"" + getDisplayPageIFrameID() + "\" allowtransparency=\"true\" style=\"width:100%; height:100%;\" frameborder=0 src=\"" + tcPageLink + "\"></iframe>";
	// 'Cause FireFox JavaScript is much faster than all the others.
	var lnStep = glFirefox ? (-10) : (-20);

	var lcImageTable = "<img style=\"cursor:pointer; border: 1px white solid;\" src=\"/Images/FullScreen.jpg\" width=\"21\" height=\"21\" align=\"middle\" onclick = \"openFloatingWindow('"
			+ tcPageLink
			+ "')\" title=\"Expand window\" alt=\"\">&nbsp;<img style=\"cursor:pointer; border: 1px white solid;\" src=\"/Images/Close.jpg\" width=\"21\" height=\"21\" align=\"middle\" onclick = \"fadeDisplayWindow(100, "
			+ lnStep + ")\" title=\"Close window\" alt=\"\">";

	loPage.innerHTML = "<table width=\"100%\"height=\"100%\"><tr><td bgcolor=\"#007F1F\" align=\"right\" valign=\"top\" height=\"21\" width=\"100%\">" + lcImageTable
			+ "	</td></tr><tr><td valign=\"top\" width=\"100%\">" + lcIFrame + "	</td></tr></table>";
}
// -------------------------------------------------------------------------------------------------------------------
function openFloatingWindow(tcPageLink)
{
	var loDate = new Date();
	var lcWindowID = "winid" + loDate.getTime().toString();

	var lnWidth = Math.round(screen.width / 2.0);
	var lnHeight = Math.round(screen.height / 2.5);
	var lnMaxOffset = Math.round(screen.height / 3.0);

	goOpenFloatingWindowOffset += 20;
	if (goOpenFloatingWindowOffset > lnMaxOffset)
	{
		goOpenFloatingWindowOffset = 20;
	}
	var lcOffset = goOpenFloatingWindowOffset;

	var loWindow = window.open(tcPageLink, lcWindowID, "left=" + lcOffset + ", top=" + lcOffset + ", width=" + lnWidth + ", height=" + lnHeight + ", scrollbars=1, menubar=1, toolbar=0, sresizable=1");
	loWindow.focus();
}
// -------------------------------------------------------------------------------------------------------------------
function getDisplayPageElement()
{
	var lcDivID = "DisplayWebPageInDiv";

	var loDiv = document.getElementById(lcDivID);
	if (!loDiv)
	{
		loDiv = document.createElement("div");
		loDiv.id = lcDivID;
		document.body.appendChild(loDiv);
	}

	loDiv.style.visibility = "visible";
	loDiv.style.top = "10%";
	loDiv.style.left = "20%";
	loDiv.style.position = "absolute";
	loDiv.style.zIndex = 2;
	loDiv.style.width = "60%";
	loDiv.style.height = "80%";
	loDiv.style.backgroundColor = "white";
	loDiv.style.border = "1px solid";
	loDiv.style.borderColor = "black";

	return (loDiv);
}
// -------------------------------------------------------------------------------------------------------------------
function getDisplayPageShadowElement()
{
	var lcDivIDShadow = "DisplayWebPageShadowInDiv";
	var loDiv = document.getElementById(lcDivIDShadow);

	if (!loDiv)
	{
		loDiv = document.createElement("div");
		loDiv.id = lcDivIDShadow;
		document.body.appendChild(loDiv);
	}

	loDiv.style.visibility = "visible";
	loDiv.style.top = "12%";
	loDiv.style.left = "21%";
	loDiv.style.position = "absolute";
	loDiv.style.zIndex = 1;
	loDiv.style.width = "60%";
	loDiv.style.height = "80%";
	loDiv.style.backgroundColor = "black";
	loDiv.style.border = "0px";

	return (loDiv);
}
// -------------------------------------------------------------------------------------------------------------------
function getDisplayPageIFrameID()
{
	var lcDivIDShadow = "DisplayWebIFrame";
	return (lcDivIDShadow);
}
// -------------------------------------------------------------------------------------------------------------------
function setOpacity(toElement, tnOpacity)
{
	if (glInternetExplorer)
	{
		toElement.style.filter = "alpha (opacity=" + tnOpacity + ")";
	}
	else if (glSafari)
	{
		toElement.style.opacity = (tnOpacity / 100.0);
	}
	else
	{
		toElement.style.MozOpacity = (tnOpacity / 100.0);
	}
}
// -------------------------------------------------------------------------------------------------------------------
// This function should be called by a timer: otherwise it will never refresh
// for multiple calls.
function fadeDisplayWindow(tnOpacity, tnStep)
{
	var loPage = getDisplayPageElement();
	var loShadow = getDisplayPageShadowElement();

	var loIFrame = document.getElementById(getDisplayPageIFrameID());

	if (tnOpacity > 0)
	{
		setOpacity(loPage, tnOpacity);
		setOpacity(loShadow, tnOpacity / 2);
		// IFrames don't do well with opacity, so just hide it.
		loIFrame.style.visibility = "hidden";

		window.setTimeout("fadeDisplayWindow(" + (tnOpacity + tnStep) + ", " + tnStep + ")", 0);
	}
	else
	{
		loPage.style.visibility = "hidden";
		loShadow.style.visibility = "hidden";
	}
}
// -------------------------------------------------------------------------------------------------------------------

