var servletPath = "/servlet/is/";

var addressWindow       = null;
var editAddressWindow   = null;
var navigatorWindow     = null;
var thesaurusWindow     = null;
var linkBrowserWindow   = null
var whatsNewWindow      = null;
var searchResultsWindow = null;
var changePasswdWindow  = null;

var busyStyle = 'margin-top:20px;font-family:Arial,Helvetica,sans-serif;font-size:14px;width:100%;height:80%;text-align:center;cursor:wait';
function setBusy (doc)
{
	try
	{
		var loc = top ? top.location : window.location;
		var server = loc.protocol + '//' + loc.host + '/';
		var body = doc.getElementsByTagName ('body')[0];
		if (body)
			body.innerHTML = '<div style="' + busyStyle + '">' + PAULA_JS_20 + '<br/><img src="' + server + 'Bitmaps/etc/Progress.gif"/></div>';
	}
	catch (ex) {}
}

function loadBusy (win, url)
{
	setBusy (win.document);
	win.location.href = url;
}

function makeWaitButton (button)
{
	button.value = PAULA_JS_21;
	button.disabled = true;
	button.style.cursor = 'wait';
}

function getAbsolutePos (el) 
{
	var SL = 0, ST = 0;
	var is_div = /^div$/i.test(el.tagName);
	if (is_div && el.scrollLeft)
		SL = el.scrollLeft;
	if (is_div && el.scrollTop)
		ST = el.scrollTop;
	var r = { x: el.offsetLeft - SL, y: el.offsetTop - ST };
	if (el.offsetParent) 
	{
		var tmp = getAbsolutePos (el.offsetParent);
		r.x += tmp.x;
		r.y += tmp.y;
	}
	return r;
}

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, w, h, windowname)
{
	var scroll = 'scrollbars,';
	if (! w)
		w = 450;
	if (! h)
	{	
		if (aParentWindow.document.body) 	// Only for IE
		{
			h = aParentWindow.document.body.scrollHeight;
			h += 70;
			if ((h+180) > screen.availHeight)
				h = screen.availHeight - 180;
			else
				scroll = '';
		}
		else
			h = 500;
	}
	h = (h>screen.availHeight-150) ? screen.availHeight-150 : (h<400) ? 400 : h;
	w = (w>screen.availWidth-150) ? screen.availWidth-150 : (w<450) ? 450 : w;
	
	var s = (windowname) ? windowname : aParentWindow.name;
	url = aParentWindow.document.location.protocol + "//" + aParentWindow.document.location.hostname + aParentWindow.document.location.pathname + aParentWindow.document.location.search;
	url += (url.charAt (url.length-1) == '/') ? '?' : '&';
	url += 'spawned=true' + aParentWindow.document.location.hash;
	url = (aURL!="") ? aURL : url;
	aParentWindow.open(url, s, scroll+"resizable,status,width="+w+",height="+h+",left=10,top=20");
}


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
		if (navigator.appVersion.indexOf ("MSIE") != -1)
			y -= 32; // height does not include the windows start bar in IE
		y = Math.max (0, y);
	}
	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 changePasswd (id)
{
	changePasswdWindow = executeObject ('Grantee', id, 'ChangePasswd', changePasswdWindow, 480, 300);
}


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 (lang)
{
	var request = servletPath + 'Grantee..login/?noNoAccessMessage=true';
	if (typeof (entry) != 'undefined' && entry.id)
		request += '&destination=' + entry.id;
	if (lang)
		request += '&lang=' + lang;
	location.href = request;
}

function openURL (url)
{
	var win = window.opener ? window.opener : parent;
	var loc = win.location;
	if (win.frames.length > 1)
		loc = win.Contents ? win.Contents.location : win.frames[1].location;
	loc.href = url;
}

function doPrint ()
{
	var scroll = 'scrollbars,';
	var y = 24;  
	var h = 40;
	if (document.body)  // only IE
	{
		h += (0.9 * document.body.scrollHeight)
		if ((h + 180) > screen.availHeight)
			h = screen.availHeight - 180;
		else
			scroll = '';
	}
	else h = 600;
			
	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"); //gleiches Fenster
	var url = loc.protocol + "//" + loc.host + loc.pathname + loc.search + (loc.search.length == 0 ? "?print=true" : "&print=true");
	scroll += 'nomenubar,notoolbar,resizable,status,screenY=24,width=700,height=' + h;
	
	printWindow = window.open (url, "STRING_Print", scroll);
	printWindow.focus ();
	//if (window.print)
		//window.setTimeout ("printWindow.print()", 1000); Aufruf durch Button
}

function editProfile ()
{
	var request = servletPath + 'Profile..Show/';
	if (typeof (entry) != 'undefined')
		request += '?destination=' + entry.id;
	location.href = request;
}