///////////////////////////////////////////////////////////////////////////////////////////////
//
// journal.js : script for American Journal functionality on HROCH.NET
// Version 3.0 Signed by Sid Paral @ AARDVARK on 2005/06/28 18:14:51 (GMT-0700)
// Authentication (do not edit) $B0O0wg$
// Copyright (c) 2004-2005 by Sid Paral. All rights reserved.
//
///////////////////////////////////////////////////////////////////////////////////////////////
// COOKIE SUPPORT
///////////////////////////////////////////////////////////////////////////////////////////////
// name - name of the cookie
// value - value of the cookie
// [expires] - expiration date of the cookie (defaults to end of current session)
// [path] - path for which the cookie is valid (defaults to path of calling document)
// [domain] - domain for which the cookie is valid (defaults to domain of calling document)
// [secure] - Boolean value indicating if the cookie transmission requires a secure transmission
// * an argument defaults when it is assigned null as a placeholder
// * a null placeholder is not required for trailing omitted arguments

function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
	  ((expires) ? "; expires=" + expires.toGMTString() : "") +
	  ((path) ? "; path=" + path : "") +
	  ((domain) ? "; domain=" + domain : "") +
	  ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}

// name - name of the desired cookie
// * return string containing value of specified cookie or null if cookie does not exist

function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
	begin = dc.indexOf(prefix);
	if (begin != 0) return null;
  } else
	begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
	end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

// name - name of the cookie
// [path] - path of the cookie (must be same as path used to create cookie)
// [domain] - domain of the cookie (must be same as domain used to create cookie)
// * path and domain default if assigned null or omitted if no explicit argument proceeds

function deleteCookie(name, path, domain) {
  if (getCookie(name)) {
	document.cookie = name + "=" +
	((path) ? "; path=" + path : "") +
	((domain) ? "; domain=" + domain : "") +
	"; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

// date - any instance of the Date object
// * hand all instances of the Date object to this function for "repairs"

function fixDate(date) {
  var base = new Date(0);
  var skew = base.getTime();
  if (skew > 0)
	date.setTime(date.getTime() - skew);
}

///////////////////////////////////////////////////////////////////////////////////////////////
// language switch support
///////////////////////////////////////////////////////////////////////////////////////////////

var		g_bDebugAlerts			=	false;	// turn on for debugging
var		g_sThisDocLanguage		=	"";		// undefined on creation, use SetupJournalLanguageSwitch
var		g_sThisDocument			=	"";		// undefined on creation, use SetupJournalLanguageSwitch
var		g_sAlternateDocument	=	"";		// undefined on creation, use SetupJournalLanguageSwitch
var		g_bCanSwitchLanguage	=	true;	// indicate to parent that language switch is supported
var		g_bIsJournalFrame		=	false;	// indicate to client that this is a frame supporting language switch (default off)
var		g_sFrameLeft			=	"";		// undefined on creation, use SetupJournalTopLanguageSwitch
var		g_sFrameRight			=	"";		// undefined on creation, use SetupJournalTopLanguageSwitch

function SetJournalLanguage (sLanguage)
{
	var	tNow					= 	new Date();
	fixDate(tNow);
	tNow.setTime(tNow.getTime() + 365 * 24 * 60 * 60 * 1000);
	setCookie ("JournalLanguage", sLanguage, tNow, "/");
	var	sReadback				=	getCookie ("JournalLanguage");
//	alert ("Set Journal Language = \"" + sLanguage + "\", read back \"" + sReadback + "\"");
	return (sReadback == sLanguage);
}

function SetupJournalLanguageSwitch(sThisDocLanguage, sAlternateDocument, iWatchdogPeriod)
{
	g_sThisDocument				=	extractDocumentNameFromPathname(location.pathname);

	if (g_bDebugAlerts)
		alert ("In " + g_sThisDocument + "::SetupJournalLanguageSwitch\r\n\r\n"
				+ "ThisDocLanguage=\"" + sThisDocLanguage + "\"\r\n"
				+ "AlternateDocument=\"" + sAlternateDocument + "\"");

	g_sThisDocLanguage			=	sThisDocLanguage;
	g_sAlternateDocument		=	sAlternateDocument;

	var	g_bHasTop				=	(top.location != location)
								&&	(eval("top.g_bIsJournalFrame") == true);

	if (g_bHasTop)
	{
		if (g_bDebugAlerts)
			alert ("want to tell top ("
					+ extractDocumentNameFromPathname(top.location.pathname)
					+ ") to switch to " + sThisDocLanguage);
		top.SwitchJournalLanguageInFrames (sThisDocLanguage);
	}
	else
	{
		if (g_bDebugAlerts)
			alert ("has no top");
	}
}

function SetupJournalTopLanguageSwitch(sFrameLeft, sFrameRight)
{
	g_sThisDocument				=	extractDocumentNameFromPathname(location.pathname);
	g_bIsJournalFrame			=	true;
	g_sFrameLeft				=	sFrameLeft;
	g_sFrameRight				=	sFrameRight;

	if (g_bDebugAlerts)
		alert ("In " + g_sThisDocument + "::SetupJournalTopLanguageSwitch\r\n\r\n"
				+ "FrameLeft=\"" + sFrameLeft + "\"; supports language switch="
				+ ((g_sFrameLeft != "") && (eval(g_sFrameLeft + ".g_bCanSwitchLanguage") == true)) + "\r\n"
				+ "FrameRight=\"" + sFrameRight + "\"; supports language switch="
				+ ((g_sFrameRight != "") && (eval(g_sFrameRight + ".g_bCanSwitchLanguage") == true)));
}

function SwitchJournalLanguage (sDesiredLanguage)
{
	if ((g_sThisDocLanguage != "") &&
		(g_sAlternateDocument != "") &&
		(sDesiredLanguage != "") &&
		(sDesiredLanguage != g_sThisDocLanguage))
	{
		var	sNewLoc				=	extractPathFromPathname (location.href) + g_sAlternateDocument;
		if (g_bDebugAlerts)
			alert ("In " + g_sThisDocument + "::SwitchJournalLanguage(\""
					+ sDesiredLanguage + "\")\r\n\r\nSwitching to " + sNewLoc);
		window.location			=	sNewLoc;
	}
	else
	{
		if (g_bDebugAlerts)
			alert ("In " + g_sThisDocument + "::SwitchJournalLanguage(\"" + sDesiredLanguage + "\")\r\n\r\nNot switching.");
	}
}

function SwitchJournalLanguageInFrames (sDesiredLanguage)
{
	if (g_bDebugAlerts)
		alert ("In " + g_sThisDocument + "::SwitchJournalLanguageInFrames(\"" + sDesiredLanguage + "\")");

	if ((g_sFrameLeft != "") && (eval(g_sFrameLeft + ".g_bCanSwitchLanguage") == true))
		eval(g_sFrameLeft + ".SwitchJournalLanguage(sDesiredLanguage)");
	if ((g_sFrameRight != "") && (eval(g_sFrameRight + ".g_bCanSwitchLanguage") == true))
		eval(g_sFrameRight + ".SwitchJournalLanguage(sDesiredLanguage)");
}

function extractDocumentNameFromPathname (sPathName)
{
	var	iSlash					=	sPathName.lastIndexOf("/");
	var iBSlash					=	sPathName.lastIndexOf("\\");
	if (iBSlash > iSlash)
		iSlash					=	iBSlash;
	return (iSlash < 0)	? sPathName : sPathName.substr(iSlash+1);
}

function extractPathFromPathname (sPathName)
{
	var	iSlash					=	sPathName.lastIndexOf("/");
	var iBSlash					=	sPathName.lastIndexOf("\\");
	if (iBSlash > iSlash)
		iSlash					=	iBSlash;
	return (iSlash < 0)	? sPathName : sPathName.substring(0, iSlash+1);
}

///////////////////////////////////////////////////////////////////////////////////////////////
