/* Miscellaneous */

function escape(str)
{
	return str.replace(" ", "%20").replace("&amp;", "%26").replace("#", "%22");
}

function xmlEscape(str)
{
	return str.replace("&", "&amp;").replace("<", "&lt;");
}

function xmlAttrEscape(str)
{
	return str.replace("&", "&amp;").replace("\"", "&quot;").replace("'", "&apos;");
}

function jsescape(str)
{
	return str.replace("\"", "\\\"").replace("\'", "\\\'");
}

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;
}


/* UI */

function translate(id)
{
	return uiText[id];
}

function ui(id)
{
	document.write(uiText[id]);
}

function uiReplace(id, control, value)
{
	document.write(uiText[id].replace(control, value));
}

function uiReplaceNumber(id, value)
{
	uiReplace(id, "[#]", value);
}


/* 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(document, id).onload = null;

	setTimeout("isScaledImageComplete('" + id + "', '" + dstid + "', '" + alt.replace("'", "\\'") + "', " + maxWidth + ", " + maxHeight + ")", 100);
}

function isScaledImageComplete(id, dstid, alt, maxWidth, maxHeight)
{
	if (!document.images[id].complete)
	{
		waitForScaledImage(id, dstid, alt, maxWidth, maxHeight);
		return;
	}

	var img = 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(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;
	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.opener = timeoutWindow;
	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;
}


/* Index Browser dialog */

function doScan(session, installation, term, browsertype)
{
	doPositionedScan(session, installation, term, 1, browsertype);
}

function doPositionedScan(session, installation, term, position, browsertype)
{
	window.location.href = "/m3/apps/m3opac/" + installation + "?command=scan&session=" + session + "&db=" + getDocumentElementById(document, 'database').value + "&attr=" + getDocumentElementById(document, 'attribute').value + "&position=" + position + "&term=" + escape(term) + "&count=20&style=scan&xslparam=browsertype," + browsertype;
}


/* Cross References dialog */

function showAuthorityDialog(session, installation)
{
	var args = "width=640,height=480,menubar=no,status=yes,toolbar=no,location=no,resizable=yes,scrollbars=no";
	var dialog = window.open("/m3/apps/m3opac/" + installation + "?command=getSession&session=" + session + "&style=authority", "CrossReferences", centerDialog(args, 640, 480));
	dialog.opener = window;
	dialog.focus();
}

function doAuthority(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 (target != window)
	{
		top.close();
	}
}


/* E-Mail Librarian dialog */

function emailLibrarian(session, installation)
{
	var args = "width=640,height=480,menubar=no,status=nos,toolbar=no,location=no,resizable=yes,scrollbars=no";
	var dialog = window.open("/m3/apps/m3opac/" + installation + "?command=getSession&session=" + session + "&style=email-librarian", "EMailLibrarian", centerDialog(args, 640, 480));
	dialog.opener = window;
	dialog.focus();
}


/* menus */

function getAbsX(elt) { return (elt.x) ? elt.x : getAbsPos(elt,"Left"); }

function getAbsY(elt) { return (elt.y) ? 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";
}
