/* Miscellaneous */

function xmlEscape(str)
{
	if (typeof str == "undefined") str = "";

	var result = "";

	for (var i = 0; i < str.length; ++i)
	{
		var c = str.charAt(i);

		if (c == "&")
		{
			result += "&amp;"
			continue;
		}

		if (c == "<")
		{
			result += "&lt;";
			continue;
		}	

		result += c;
	}

	return result;
}

function xmlAttrEscape(str)
{
	if (typeof str == "undefined") str = "";

	var result = "";

	for (var i = 0; i < str.length; ++i)
	{
		var c = str.charAt(i);

		if (c == "&")
		{
			result += "&amp;"
			continue;
		}

		if (c == "<")
		{
			result += "&lt;";
			continue;
		}
		
		if (c == "'")
		{
			result += "&apos;";
			continue;
		}	

		if (c == '"')
		{
			result += "&quot;";
			continue;
		}

		result += c;
	}

	return result;
}

function toHex(v)
{
	if (v >= 0 && v <= 9) return String.fromCharCode(0x30 + v);
	else if (v >= 10 && v <= 15) return String.fromCharCode(0x41 + (v - 10));

	return "";
}

function toHexPair(v)
{
	if (v < 0 || v > 255) return "";

	var h1 = Math.floor(v / 16);
	var h2 = v % 16;

	return toHex(Math.floor(v / 16)) + toHex(v % 16);
}

function htmlattrescape(str)
{
	if (typeof str == "undefined") str = "";

	var result = "";

	for (var i = 0; i < str.length; ++i)
	{
		var c = str.charAt(i);

		if (c == '"' || c == "'")
		{
			result += "%";

			var cc = str.charCodeAt(i);

			result += toHexPair(cc);

			continue;
		}

		result += c;
	}

	return result;
}

function jsescape(str)
{
	if (typeof str == "undefined") str = "";

	var result = "";

	for (var i = 0; i < str.length; ++i)
	{
		var c = str.charAt(i);

		if (c == "'" || c == '"' || c == "\\")
		{
			result += "\\";
		}

		result += c;
	}

	return result;
}

function getDocumentElementById(doc, id)
{
	if (doc.getElementById)
	{
		return doc.getElementById(id);
	}

	if (doc.all)
	{
		return doc.all[id];
	}

	if (doc.layers)
	{
		return doc.layers[id];
	}

	return null;
}

function getDocumentElementStyleById(doc, id)
{
	return getElementStyle(getDocumentElementById(doc, id));
}

function getElementStyle(elem)
{
	if (document.all || document.getElementById)
	{
		return elem["style"];
	}

	return elem;
}


/* Images */

function getScaledImage(img, alt, maxWidth, maxHeight)
{
	var iwidth = img.width;
	var iheight = img.height;

	var width = iwidth;
	var height = iheight;

	if (iwidth >= iheight)
	{
		if (width > maxWidth)
		{
			width = maxWidth;
			height = (iheight / iwidth) * width;
		}

		if (height > maxHeight)
		{
			height = maxHeight;
			width = (iwidth / iheight) * height;
		}
	}
	else
	{
		if (height > maxHeight)
		{
			height = maxHeight;
			width = (iwidth / iheight) * height;
		}

		if (width > maxWidth)
		{
			width = maxWidth;
			height = (iheight / iwidth) * width;
		}
	}

	return "<img src='" + img.src + "' alt='" + alt + "' width='" + width + "' height='" + height + "'/>";
}

function waitForScaledImage(id, dstid, alt, maxWidth, maxHeight)
{
	getDocumentElementById(main.document, id).onload = null;

	setTimeout("isScaledImageComplete('" + id + "', '" + dstid + "', '" + alt.replace("'", "\\'") + "', " + maxWidth + ", " + maxHeight + ")", 100);
}

function isScaledImageComplete(id, dstid, alt, maxWidth, maxHeight)
{
	if (!main.document.images[id].complete)
	{
		waitForScaledImage(id, dstid, alt, maxWidth, maxHeight);
		return;
	}

	var img = main.document.images[id];
	onScaledImageLoad(id, dstid, img, alt, maxWidth, maxHeight);
}

function onScaledImageLoad(plcid, dstid, img, alt, maxWidth, maxHeight)
{
	img.onload = null;

	if (!img.complete)
	{
		waitForScaledImage(plcid, dstid, alt, maxWidth, maxHeight);
		return;
	}

	getDocumentElementById(main.document, dstid).innerHTML = getScaledImage(img, alt, maxWidth, maxHeight);
}


/* Session Timeout */

var timeoutClockID = 0;
var timeoutWindow;

function startTimeout(win, seconds, dialogURL, wait, url, prompt)
{ 
	stopTimeout();

	timeoutWindow = win.parent;
	timeoutWindow.timeoutSeconds = seconds;
	timeoutWindow.timeoutURL = dialogURL;
	timeoutWindow.timeoutWait = wait;
	timeoutWindow.redirectURL = url;
	timeoutWindow.timeoutPrompt = prompt;

	timeoutClockID = setTimeout("confirmNewSession()", seconds * 1000);
}

function restartTimeout()
{
	stopTimeout();

	timeoutClockID = setTimeout("confirmNewSession()", timeoutWindow.timeoutSeconds * 1000);
}

function confirmNewSession()
{
	var args = "width=320,height=150,menubar=no,status=no,toolbar=no,location=no,resizable=yes,scrollbars=no";
	var dialog = timeoutWindow.open(timeoutWindow.timeoutURL, "SessionTimeout", centerDialog(args, 320, 150));
	dialog.focus();
}

function newSession()
{
	var urlWindow = timeoutWindow;
	var closeWindow = null;

	if (timeoutWindow.opener != null && timeoutWindow.opener != timeoutWindow)
	{
		urlWindow = timeoutWindow.opener;
		closeWindow = timeoutWindow;
	}

	urlWindow.location.href = timeoutWindow.redirectURL;

	if (closeWindow != null)
	{
		closeWindow.close();
	}
}

function stopTimeout()
{
	if (timeoutClockID) clearTimeout(timeoutClockID); 
	timeoutClockID = 0; 
}


/* General dialog box functions */

function centerDialog(args, dialogWidth, dialogHeight)
{
	var screenHeight = screen.height;
	var screenWidth = screen.width;
	var top = (screenHeight - dialogHeight) / 2;
	if (top < 0) top = 0;
	var left = (screenWidth - dialogWidth) / 2;
	if (left < 0) left = 0;
	return args + ",top=" + top + ",left=" + left;
}


/* Authority dialog */

function showAuthorityDialog(session, installation)
{
	var target = "window.opener";
	//var target = "new";
	var args = "width=640,height=520,menubar=no,status=yes,toolbar=no,location=no,resizable=yes,scrollbars=yes";
	var dialog = window.parent.open("/m3/apps/m3opac/" + installation + "?command=presentAuthority&target=" + target + "&session=" + session + "&style=authority", "CrossReferences", centerDialog(args, 640, 520));
	dialog.focus();
}

function doAuthority(win, target, session, dbid, installation, id, text)
{
	target.focus();
	target.location.href = "/m3/apps/m3opac/" + installation + "?command=search&session=" + session + "&db=" + dbid + "&attr1=9995&query1=" + id + "&query1d=\"" + text + "\"&style=results";

	if (win.opener != null)
	{
		win.close();
	}
}


/* E-Mail Librarian dialog */

function emailLibrarian(session, installation)
{
	var target = "window.opener";
	//var target = "new";
	var args = "width=640,height=480,menubar=no,status=nos,toolbar=no,location=no,resizable=yes,scrollbars=no";
	var dialog = window.parent.open("/m3/apps/m3opac/" + installation + "?command=getSession&nop=true&target=" + target + "&session=" + session + "&style=email-librarian", "EMailLibrarian", centerDialog(args, 640, 480));
	dialog.focus();
}

function getPrintLink(win, holdings)
{
	var result = "";

	if (typeof holdings == "undefined") holdings = false;

	if (window.print && !holdings)
	{
		result = "<a href='javascript:window.print()'>" + common.translate("PRINT") + "</a>";
	}
	else
	{
		var link = win.location.href;
		var parentLink = win.parent.location.href;
		
		if (link[link.length - 1] != "&")
		{
			link += "&";
		}

		link += "xslparam=print,true";
		//link += "target=new&xslparam=print,true";

		if (holdings)
		{
			link += "&holdings=true";
		}

		var newLink = "";

		for (var i = 0; i < link.length; ++i)
		{
			var c = link.charAt(i);

			if (c == '"' || c == "'" || c == ',' || c == '&' || c == '?' || c == '/' || c == '=')
			{
				newLink += "%25";

				var cc = link.charCodeAt(i);

				newLink += toHexPair(cc);

				continue;
			}

			newLink += c;
		}

		parentLink = common.htmlattrescape(parentLink) + "&xslparam=url," + newLink;

		result = "<a href='javascript:common.openPrint(\"" + parentLink + "\")'>" + common.translate("PRINT") + "</a>";
	}

	return result;
}

/* menus */

function getAbsX(elt)
{
	return (typeof elt.x != "undefined") ? elt.x : getAbsPos(elt, "Left");
}

function getAbsY(elt)
{
	return (typeof elt.y != "undefined") ? elt.y : getAbsPos(elt, "Top");
}

function getAbsPos(elt, which)
{
	iPos = 0;

	while (elt != null)
	{
		iPos += elt["offset" + which];
		elt = elt.offsetParent;
	}

	return iPos;
}

function showMenuCentered(id, width, item)
{
	var menu = getDocumentElementById(document, id);
	var ref = getDocumentElementById(document, item);

	var left = getAbsX(ref) + (ref.offsetWidth / 2) - (width / 2); if (left < 0) left = 0;

	getElementStyle(menu).left = left;
	getElementStyle(menu).top = getAbsY(ref) + ref.offsetHeight - 1;
	getElementStyle(menu).visibility = "visible";
}

function showMenu(id, item)
{
	var menu = getDocumentElementById(document, id);
	var ref = getDocumentElementById(document, item);

	getElementStyle(menu).left = getAbsX(ref);
	getElementStyle(menu).top = getAbsY(ref) + ref.offsetHeight - 1;
	getElementStyle(menu).visibility = "visible";
}

function hideMenu(id)
{
	var menu = getDocumentElementById(document, id);

	getElementStyle(menu).left = -32767;
	getElementStyle(menu).top = -32767;
	getElementStyle(menu).visibility = "hidden";
}

