var servletPath = "/servlet/is/";

addressWindow       = null;
editAddressWindow   = null;
navigatorWindow     = null;
thesaurusWindow     = null;
linkBrowserWindow   = null
whatsNewWindow      = null;
searchResultsWindow = null;

function format ()
{
	if (arguments.length <= 1 ) return arguments[0];
	re = /(^[^%]*)%%(.*)/;
	newString = arguments[0];
	for (i=1; i<arguments.length; ++i)
	{
		newString = newString.replace (re, "$1 " + arguments[i] + " $2")
	}
	return newString;
}


function preloadImages (aDocument, anArray)
{
	var  img = new Image;
	for (i=0; i<anArray.length; ++i)
	{
		img.src = anArray[i] + '.gif';
		img.src = anArray[i] + 'X.gif';
	}
	aDocument.imageNames = anArray;
}
function hilite (aDocument, name, index) { aDocument.images[name].src = aDocument.imageNames[index] + 'X.gif'; }
function fade (aDocument, name, index) { aDocument.images[name].src = aDocument.imageNames[index] + '.gif'; }


function openUrlInNewWindow (aParentWindow, aURL)
{
	aParentWindow.open (aURL);
}


function spawnWindow (aWindow, w, h)
{
	var scroll = 'scrollbars,';
	
	if (! w)
		w = 400;
	if (! h)
	{
		if (aWindow.document.body) 	// Only for IE
		{
			h = aWindow.document.body.scrollHeight;
			h += 70;
			if ((h+180) > screen.availHeight)
				h = screen.availHeight - 180;
			else
				scroll = '';
		}
		else
			h = 600;
	}

	url = aWindow.document.location.protocol + "//" + aWindow.document.location.hostname + aWindow.document.location.pathname + aWindow.document.location.search;
	url += (url.charAt (url.length-1) == '/') ? '?' : '&';
	url += 'spawned=true' + aWindow.document.location.hash;
	
	var s = aWindow.name;
	s = "Contents" + (parseInt(s[7]) + 1);
	
	aWindow.open (url, s, scroll+'resizable,status,width=' + w + ',height=' + h);
}


function tee (event, id, etc)
{
	if (parent.MenuBar && parent.MenuBar.linkOrMenu)  
		return parent.MenuBar.linkOrMenu (event, id, etc);
	return true;
}


function displayAddress (id, noContent)
{
	addressWindow = executeObject ('Address', id, 'Display', addressWindow, 400, 400, 100, 100, noContent);
}

function newAddress (parentId)
{
	editAddressWindow = executeObject ('Address', null, 'Edit', editAddressWindow, 480, 690, null, null, null, '/?addTo=' + parentId);
}

function editAddress (id)
{
	editAddressWindow = executeObject ('Address', id, 'Edit', editAddressWindow, 480, 690);
}

function deleteAddress (id)
{
	if (confirm (format (STRING_Command_deleteAddress, id)))
	{
		url = servletPath + "Address." + id + ".delete/";
		form = document.forms['addressSearchForm'];
		if (form.name && form.name.value.length > 0)
			url += "?search=" + form.name.value;
		document.location.href = url;
	}
}

function showLinkBrowser (id, field, noTree)
{
	if (noTree)
		field += "&part=content";
	linkBrowserWindow = executeObject ('Entry', id, 'LinkBrowser', linkBrowserWindow, noTree ? 470 : 840, 600, 0, 0, null, '/?field=' + field);
}

function silentExecuteObject (type, id, aCommand, aWindow, w, h, x, y, noContent)
{
	executeObject (type, id, aCommand, aWindow, w, h, x, y, noContent);
}

function executeObject (type, id, aCommand, aWindow, w, h, x, y, noContent, parameters)
{
	if (! w)  w = 400;
	if (! h)  h = 300;
	if (! x)  x = 100;
	if (! y)  y = 100;
	rect = fitToScreen (x, y, w, h);
	x = rect.x; y = rect.y; w = rect.w; h = rect.h;

	aWindow = popupSingleton
	(
		aWindow,	type + aCommand,
		'scrollbars=yes,resizable=yes,width='   + w + 
																 ',height=' + h + 
																 ',top='    + y + 
																 ',left='   + x
	);
	if (id == null)
		id = '';
	if (!noContent && aWindow)
		aWindow.location.href = servletPath + type + '.' + id + '.' + aCommand + (parameters ? parameters : '');
	return aWindow;
}

function fitToScreen (x, y, w, h)
{
	var isOpera = navigator.appName.indexOf ("Opera") != -1 || navigator.userAgent.indexOf ("Opera") != -1;
	if (isOpera)
	{
		x = 0;
		y = 0;
		h += 40;
	}
	if (x + w > screen.availWidth)
		x = screen.width - w - 10;
	if (y + h > screen.availHeight)
		y = screen.height - h - 34;  // 34 = Windowtitle
	return { x:x, y:y, w:w, h:h };
}


var thesaurus = 
{
	fat: { url: '/Thesaurus/Start.html', width: 715, height: 500 },
	slim: { url: '/Thesaurus/Start.html', width: 250, height: 500 }
};

thesaurus.defaults = thesaurus.fat;


function execSearch ()
{
	var url = servletPath + "Entry..Search/";
	if (typeof (entry) != 'undefined')
		url += "?subTree=" + entry.id;
	location.href = url;
}


function popupThesaurusFor (caller, w, h, url)
{
	if (!url)  url = thesaurus.defaults.url;
	if (! w)  w = thesaurus.defaults.width;
	if (! h)  h = thesaurus.defaults.height;

	thesaurusWindow = popupSingleton
	(
		thesaurusWindow,
		'Thesaurus',
		'scrollbars=yes,resizable=yes,width=' + w + ',height=' + h
	);
	thesaurusWindow.document.location.href = url;
}


function popupNavigator (depth, template)
{
	var id = 1;
	if (typeof (entry) != 'undefined')
		id = entry.id;
	else if (typeof (Contents) != 'undefined' && typeof (Contents.entry) != 'undefined')
		id = Contents.entry.id;
	popupNavigatorOn (id, depth, 420, 500, template);
}


function popupNavigatorOn (anId, depth, w, h, template)
{
	if (! depth)  depth = 2;
	if (! w)  w = 420;
	if (! h)  h = 500;

	navigatorWindow = popupSingleton
	(
		navigatorWindow,
		'Navigator',
		'scrollbars=yes,resizable=yes,width=' + w + ',height=' + h + ',status=no,screenX=0,screenY=0,left=0,top=0'
	);
	var ref = servletPath + anId + '/?command=tree&maxLevel=' + depth;
	if (template)
		ref += "&template=" + template;
	if (navigatorWindow)
		navigatorWindow.location.href = ref;
}

function popupSearchResults (url)
{
	searchResultsWindow = popupSingleton
	(
		searchResultsWindow,
		'SearchResults',
		'scrollbars=yes,resizable=yes,width=480,height=550,status,screenX=0,screenY=0,left=0,top=0'
	);
	if (url != null)
		searchResultsWindow.document.location.href = url;
}


function popupWhatsNew ()
{
	whatsNewWindow = popupSingleton
	(
		whatsNewWindow,
		'News',
		'scrollbars=yes,resizable=yes,width=550,height=550,status,screenX=0,screenY=0,left=0,top=0'
	);
	whatsNewWindow.document.location.href = servletPath + 'Global..ShowNews/?daysBack=14';
}


function popupSingleton (aWindow, windowName, windowParams)
{
	if (aWindow && !aWindow.closed && aWindow.document) 
	{ 
		aWindow.focus ();
		return aWindow;
	}
	var loc = navigator.appName.indexOf ("Opera") == -1 ? '' : window.frames[0].location;
	aWindow = window.open
	(
		loc,
		windowName,
		windowParams
	);

	if (aWindow && !aWindow.closed && aWindow.document) 
		aWindow.focus ();
	
	return aWindow;
}

function login ()
{
	var request = servletPath + 'Grantee..login/?noNoAccessMessage=true';
	if (typeof (entry) != 'undefined' && entry.id)
		request += '&destination=' + entry.id;
	location.href = request;
}

function openURL (url)
{
	if (window.opener.frames.length > 1)
	{
		if (window.opener.Contents)
			window.opener.Contents.location.href = url;
		else
			window.opener.frames[1].location.href = url;
	}
	else
		window.opener.location.href = url;
}

function doPrint ()
{
	var loc = window.document.location;
	//loc.href = loc.protocol + "//" + loc.host + loc.pathname + loc.search + (loc.search.length == 0 ? "?print=true" : "&print=true");
	loc.href += (loc.search.length == 0 ? "?print=true" : "&print=true");
}

function editProfile ()
{
	var request = servletPath + 'Profile..Show/';
	if (typeof (entry) != 'undefined')
		request += '?destination=' + entry.id;
	location.href = request;
}


////////////////////////////////////////////////////////////////////////
//
//	SelectX
//
////////////////////////////////////////////////////////////////////////

function SelectX (aSelectWidget)
{
	// Declare instance methods ...
	//
	this.add              = add;
	this.addAll           = addAll;
	this.contains         = contains;
	this.getSelectedItem  = getSelectedItem;
	this.getSelectedItems = getSelectedItems;
	this.removeSelected   = removeSelected;
	this.removeAll        = removeAll;
	this.selectItem       = selectItem;
	this.selectAll        = selectAll;

	this.selectWidget = aSelectWidget;
	this.options      = aSelectWidget.options;

	function add (text, value)
	{
		this.options[this.options.length] = new Option (text, value);
	}

	function addAll (opts)
	{
		for (var i = 0; i < opts.length; i++)
		{
			if (! this.contains (opts[i].value))
				this.options[this.options.length] = new Option (opts[i].text, opts[i].value);
		}
	}

	function contains (value)
	{
		for (var i = 0; i < this.options.length; i++)
		{
			if (this.options[i].value == value)
				return true;
		}
		return false;		
	}
	
	function getSelectedItem ()
	{
		sel = this.selectWidget.selectedIndex;
		return sel == -1 ? null : this.options[sel];
	}
	
	function getSelectedItems ()
	{
		sels = new Array ();
		count = 0;
		for (var i = 0; i < this.options.length; i++)
		{
			if (this.options[i].selected)
				sels[count++] = this.options[i];
		}
		return sels;
	}
	
	function removeAll ()
	{
		size = this.options.length;
		for (var i = size - 1; i >= 0; i--)
			this.options[i] = null;
	}
	
	function removeSelected ()
	{
		size = this.options.length;
		for (var i = size - 1; i >= 0; i--)
		{
			if (this.options[i].selected)
				this.options[i] = null;
		}
	}
	
	function selectItem (value)
	{
		for (var i = 0; i < this.options.length; i++)
		{
			if (this.options[i].value == value)
			{
				this.options[i].selected = true;
				break;
			}
		}
	}
	
	function selectAll ()
	{
		for (var i = 0; i < this.options.length; i++)
			this.options[i].selected = true;
	}
}
