﻿/* Miscellaneous */

function xmlAmpersandEscape(str)
{
	if (typeof str == "undefined") str = "";

	var result = "";

	for (var i = 0; i < str.length; ++i)
	{
		var c = str.charAt(i);

		if (c == "&")
		{
			if((str.length>(i+2))&&(str.charAt(i+1)=="a")&&(str.charAt(i+2)=="m")&&(str.charAt(i+3)=="p"))
			{
				result += c;
				continue;
			}
			result += "&amp;"
			continue;
		}

		result += c;
	}

	return result;
}

function processquotes(str)
{
	if (typeof str == "undefined") str = "";

	var result = "";

	for (var i = 0; i < str.length; ++i)
	{
		var c = str.charAt(i);


		if (c == '"' && i==0)
		{
			continue;
		}	
		else if (c == '"' && i>0)
		{
			result += " ";
			continue;
		}	

		result += c;
	}

	return result;
}

function XMLUnescape(str)
{
	if (typeof str == "undefined") str = "";

	var result = "";

	for (var i = 0; i < str.length; ++i)
	{
		var c1 = str.charAt(i);
		var c2 = str.charAt(i+1);


		if ((c1 == "'")&&(c2 == "'"))
		{
			result += "&quot;";
			i++;
			if(i==str.length-1)
				break;
			continue;
		}	

		if (c1 == "'")
		{
			result += "&#146";

			continue;
		}
		result += c1;
	}
	return result;
}

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;
		}	

		if (c == '"')
		{
			result += "''";
			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 xmlAttrUnEscape(str)
{
	if (typeof str == "undefined") str = "";

	var result = "";


		for (var i = 0; i < str.length; i++)
		{
			var c1 = ' ';
			if(i<str.length)
				c1 = str.charAt(i);
			if(c1 == '&')
			{

				var c2 = ' ';
				var c3 = ' ';
				var c4 = ' ';
				var c5 = ' ';

				if(i+1<str.length)
					c2 = str.charAt(i+1);
				if(i+2<str.length)
					c3 = str.charAt(i+2);
				if(i+3<str.length)
					c4 = str.charAt(i+3);
				if(i+4<str.length)
					c5 = str.charAt(i+4);
				if((c2 == 'a') && (c3 == 'm') && (c4 == 'p') && (c5 == ';'))
				{
					result += "&#38";
					
					i=i+5;
					continue;
				}	

				if((c2 == 'l') && (c3 == 't') && (c4 == ';') )
				{
					result += "&#60;";
					
					i=i+3;
					continue;
				}
				else if((c2 == 'g') && (c3 == 't') && (c4 == ';') )
				{
					result += "&#62;";
					i=i+3;
				
					continue;
				}
			}
			else if(c1 == '"')
			{
				result += "&#34;";
				continue;
			}

			result += c1;
		}
	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 jsDataEscape(str)
{
	if (typeof str == "undefined") str = "";

	var result = "";

	for (var i = 0; i < str.length; ++i)
	{
		var c = str.charAt(i);

		switch(c)
		{
			case 'À':
			case 'Á':
			case 'Â':
			case 'Ã':
			case 'Ä':
			case 'Å':
			case 'Æ':
			case 'Ç':
			case 'È':
			case 'É':
			case 'Ê':
			case 'Ë':
			case 'Ì':
			case 'Í':
			case 'Î':
			case 'Ï':
			case 'Ð':
			case 'Ñ':
			case 'Ò':
			case 'Ó':
			case 'Ô':
			case 'Õ':
			case 'Ö':
			case '×':
			case 'Ø':
			case 'Ù':
			case 'Ú':
			case 'Û':
			case 'Ü':
			case 'Ý':
			case 'ß':
			case 'à':
			case 'á':
			case 'â':
			case 'ã':
			case 'ä':
			case 'å':
			case 'æ':
			case 'ç':
			case 'è':
			case 'é':
			case 'ê':
			case 'ë':
			case 'ì':
			case 'í':
			case 'î':
			case 'ï':
			case 'ð':
			case 'ñ':
			case 'ò':
			case 'ó':
			case 'ô':
			case 'õ':
			case 'ö':
			case '÷':
			case 'ø':
			case 'ù':
			case 'ú':
			case 'û':
			case 'ü':
			case 'ý':
			case 'þ':
			case 'ÿ':
				result += "%";
				var cc = str.charCodeAt(i);
				result += toHexPair(cc);
				break;
			default:
				result += c;
				break;
		}		

	}

	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();

	var logged_in = (typeof common.opac.session.patron.usmarc != "undefined");
	if(logged_in)
	{
		return;
	}
	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.parent";
//	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("/m4/opac/m4opac.dll?installation=" + 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 = "/m4/opac/m4opac.dll?installation=" + 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.parent";
//	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("/m4/opac/m4opac.dll?installation=" + 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";

		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";
}
