// Copyright 2003-7 The Cyber Hymnal

var childIsOpen
var error = "SCRIPT ERROR\n\n"
var citeWindow
var parentWindowName = "parentWindow"

//-----------------------------------------------------
/*
	PURPOSE	Ensures a function received all required arguments, no extra 
					arguments & (if so specified) no arguments are empty.
	RETURNS	True if the arguments validate, else false.
*/
function argsOk
(
	functionCallee		,	// Caller's callee object
	allArgsRequired	,	// True = All arguments are required
	emptyArgsAllowed	// True = Empty arguments are allowed
)
{
	ok = true
	for (i = 0; i < 1; ++i)
	{
		functionName = functionCallee.toString().split(' ')[1].split('(')[0]
		args = functionCallee.arguments
		expectedArgCount = functionCallee.length
		actualArgCount  = args.length
		if ((allArgsRequired && actualArgCount < expectedArgCount) ||
			(actualArgCount > expectedArgCount))
		{
			alert(error + "Function " + functionName + "() expected " + expectedArgCount + " arguments, but received " + actualArgCount + ".")
			ok = false
			break
		}
		if (emptyArgsAllowed)
			break
		for (j = 0; j < args.length; ++j)
		{
			if (args[j] != null && args[j].length == 0)
			{
				alert(error + "Function " + functionName + "() received an empty argument.")
				ok = false
				break
			}
		}
	}
	return ok
}
//------------------------------------------------
function cite
(
	name	,	// Source name (will appear on displayed page)
	file	,	// Name of file with bibliographic info (no path or extension)
	suffix		// Page number(s), volume, etc.
)
// PURPOSE	Writes HTML for a source citation link
// ENTRY	The variable root is defined, giving the path to the Web site root (no trailing slash)
{
	if (root == null)
		alert(error + "Root variable undefined")
	else if (argsOk(arguments.callee, true, true))
	{
		document.write('<a onmouseover="return stat(\'See bibliographic citation\')" onmouseout="return erase()" onclick="return openCitation(\'' + root + 'bib/' + file + '.htm\')" href="' + root + 'js/nojs.htm">' + name + '</a>')
		if (suffix.length > 0)
		{
		  if (suffix.search("p.") != -1)					// If page number already given
		  	prefix = ""									// Don't use prefix
		else if (suffix.indexOf('-') == -1 &&
			  suffix.indexOf(',') == -1)					// If just a single page
				prefix = "p."								// Use this
		else												// If multiple pages
				prefix = "pp."							// Use this
			document.write(", " + prefix + ' ' + suffix)
		}
	}
}
//------------------------------------------------
function closeChild()
{
	if (childIsOpen)
	  childWindow .close()
}
//------------------------------------------------
function closeCitation()
{
	if (citeWindow && citeWindow != null)
	  citeWindow.close()
}
//------------------------------------------------
function erase()
{
	return stat('')
}
//------------------------------------------------
// PURPOSE Write a request for photo(s)
// ENTRY	Init() has been called
function fot(name, better)
{
	if (better == null)
		better = false
	image("pho&shy;to", name, better)
}
//-----------------------------------------------------
function recognize
// PURPOSE	Insert a portait of a with a "special recognition border"
// ENTRY	Init() has been called
(
	caption		,	// Image caption (shown as tool tip on screen, as text below picture on printouts)
	file			,	// File name, without path (if optional extension is omitted, name must not have any periods; default extension is "jpg")
	width			,	// Image width (pixels)
	height		,	// Image height (pixels)
	rightAligned,	// True = Align image at right edge of screen (optional; default is false)
	credit			,	// Byline (optional)
	url				 	// URL for byline link (optional)
)
{
	portrait(caption, file, width, height, rightAligned, credit, url, "specialRecognition")
}
//-----------------------------------------------------
function illustrate
// PURPOSE	Draw a link to an illustration file
// ENTRY	Init() has been called
(
	caption		,	// Image caption (shown as tool tip on screen, as text below picture on printouts)
	file			,	// File name, without path (if optional extension is omitted, name must not have any periods; default extension is "jpg")
	width			,	// Image width (pixels)
	height		,	// Image height (pixels)
	rightAligned,	// True = Align image at right edge of screen (optional; default is false)
	credit			,	// Byline (optional)
	url				 	// URL for byline link (optional)
)
{
	if (argsOk(arguments.callee, false, false))
	{
		if (file.indexOf('.') == -1)
			file += ".jpg"
		var output = ""
		var tableClass
		if (rightAligned == true)
			tableClass = 'tableRight'
		if (tableClass == 'tableRight')
			output += '<table class="tableRight"><tr>'
		output += '<td width="' + width + '" class="enclosingImage">'
		output += '<img src="' + root + 'img/' + file + '" class="illustration" title="' + caption + '" width="' + width + '" height="' + height + '" />'
		output += '<br /><div class="imageCaption">' + caption + '</div>'
		if (credit != null)
		{
			var bylineHint
			output += '<div class="credit" width="' + width + '">' 
			if (url != null)
			{
			  if (url.indexOf("mailto:") == -1 && url.indexOf("http:") == -1)
			  	url = "http://" + url
				if (url.indexOf("mailto:") != -1)
					bylineHint = "Send e-mail"
				else
					bylineHint = "Visit external Web site (opens new window/tab)"
				output += '<a href="' + url + '" target="_blank" onmouseover="return stat(\'' + bylineHint + '\')" onmouseout="return erase()">'
			}
			if (credit.indexOf("&copy;") == -1)
				credit = "Courtesy of " + credit
			output += credit
			if (url != null)
				output += '</a>'
			output += '</div>'
		}
		output += '</td>'
		if (tableClass == 'tableRight')
			output += '</tr></table>'
		document.write(output)			
	}
}//------------------------------------------------
function image(item, name, better)
// PURPOSE Write a request for picture or photo
// ENTRY		Init() has been called
{
  if (better != null && better == true)
    betterString = 'better '
  else
    betterString = ''  
	if (argsOk(arguments.callee, true, false))
		document.write('<p class="request">If you have ac&shy;cess to a ' + betterString + item + ' of ' + name + ' that we could put on&shy;line, please <a href="' + root + 'misc/pics.htm" onmouseover="return stat(\'Learn how to send us images\')" onmouseout="return erase()">click here</a>.</p>')
}
//------------------------------------------------
 function lk(text, hint, url, key)
// PURPOSE Write a text link
{
  if (url.indexOf('#') == -1)
   url += ".htm"
  document.write("<a href='" + url + "' onmouseover='return stat(\"" + hint + "\")' onmouseout='return erase()'>" + text + "</a>" )
}
//------------------------------------------------
function mail()
{
	document.write(', would you <a href="' + root + 'misc/mail.htm" onmouseover="return stat(\'Get our e-mail address\')" onmouseout="return erase()">send us an e-mail</a>? Thanks!')
}
//------------------------------------------------
function openChild(page, width, height)
{
	childIsOpen = true
	childWindow = open(page, "ChildWnd", "toolbar=no, status=no, menubar=no, resizable=yes, scrollbars=yes, height=" + height + ", width=" + width)
	return true
}
//------------------------------------------------
function openCitation(child)
{
	window.name = parentWindowName
	citeWindow = open(child, "citeWindow", "toolbar=no, status=no, menubar=yes, resizable=yes, scrollbars=yes, height=20, width=500")
	return false
}
//------------------------------------------------
function pic(name, better)
// PURPOSE	Write a request for pictures(s)
// ENTRY		Init() has been called
{
	if (better == null)
		better = false
	image("pic&shy;ture", name, better)
}
//-----------------------------------------------------
function portrait
// PURPOSE	Inserts a portrait
// ENTRY	Init() has been called
(
	caption		,	// Image caption (shown as tool tip on screen, as text below picture on printouts)
	file			,	// File name, without path (if optional extension is omitted, name must not have any periods; default extension is "jpg")
	width			,	// Image width (pixels)
	height		,	// Image height (pixels)
	rightAligned,	// True = Align image at right edge of screen (optional; default is false)
	credit			,	// Byline (optional)
	url				,	// URL for byline link (optional)
	imageClass		// Image class (optional; default is "portrait")
)
{
	if (argsOk(arguments.callee, false, false))
	{
		if (file.indexOf('.') == -1)
			file += ".jpg"
		var output = ""
		if (imageClass == null)
			imageClass = 'portrait'
		var tableClass
		if (rightAligned == true)
			tableClass = 'tableRight'
		if (tableClass == 'tableRight')
			output += '<table class="tableRight"><tr>'
		output += '<td width="' + width + '" class="enclosingImage">'
		output += '<img src="' + root + 'img/' + file + '" class="' + imageClass + '" title="' + caption + '" width="' + width + '" height="' + height + '" />'
		output += '<br /><div class="imageCaption">' + caption + '</div>'
		if (credit != null)
		{
			var bylineHint
			output += '<div class="credit" width="' + width + '">Courtesy of '
			if (url != null)
			{
			  if (url.indexOf("mailto:") == -1 && url.indexOf("http:") == -1)
			  	url = "http://" + url
				if (url.indexOf("mailto:") != -1)
					bylineHint = "Send e-mail"
				else
					bylineHint = "Visit external Web site (opens new window/tab)"
				output += '<a href="' + url + '" target="_blank" onmouseover="return stat(\'' + bylineHint + '\')" onmouseout="return erase()">'
			}
			output += credit
			if (url != null)
				output += '</a>'
			output += '</div>'
		}
		output += '</td>'
		if (tableClass == 'tableRight')
			output += '</tr></table>'
		document.write(output)			
	}
}
//------------------------------------------------
function redirect(page)
{
	parentWindow = window.open('', parentWindowName)
	parentWindow.location = page
	closeCitation()
	return false
}
//------------------------------------------------
function stat(message)
{
	window.status = message
	return true
}
//------------------------------------------------
