function popup_error()
{
	alert('Your browser is currently blocking popups!\nPlease allow popups for TollerLinks to work properly.');
}


// open the details window
function details( id, lang )
{
    var win = window.open( '/public/links/details.zml?lang=' + lang + '&id=' + id,
		'mytollernet_linkdetails', 
		'width=500,height=300,location=no,menubar=no,toolbar=no,resizable=yes,scrollbars=yes,status=no', 
		false );
	if( win==null )
	{
		popup_error();
		return;
	}
    if( win.opener==null )
		win.opener = self;
    win.focus();
	top.details_window = win;
}

// open the entry window
function addentry( args )
{
    var win = window.open( '/public/links/entry.zml?' + args,
		'mytollernet_linkentry', 
		'width=700,height=700,location=no,menubar=no,toolbar=no,resizable=yes,scrollbars=yes,status=no', 
		false );
	if( win==null )
	{
		popup_error();
		return;
	}
    if( win.opener==null )
		win.opener = self;
    win.focus();
	top.entry_window = win;
}

// open the about window
function about( args )
{
    var win = window.open( '/public/links/about.zml?' + args,
		'mytollernet_about', 
		'width=650,height=700,location=no,menubar=no,toolbar=no,resizable=yes,scrollbars=yes,status=no', 
		false );
	if( win==null )
	{
		popup_error();
		return;
	}
    if( win.opener==null )
		win.opener = self;
    win.focus();
}

// open the updates window
function updates( args )
{
    var win = window.open( '/public/links/updates.zml?' + args,
		'mytollernet_updates', 
		'width=650,height=400,location=no,menubar=no,toolbar=no,resizable=yes,scrollbars=yes,status=no', 
		false );
	if( win==null )
	{
		popup_error();
		return;
	}
    if( win.opener==null )
		win.opener = self;
    win.focus();
}

// open the registration window
function register( args )
{
    var win = window.open( '/public/links/register.zml?' + args,
		'mytollernet_register', 
		'width=600,height=500,location=no,menubar=no,toolbar=no,resizable=yes,scrollbars=yes,status=no', 
		false );
	if( win==null )
	{
		popup_error();
		return;
	}
    if( win.opener==null )
		win.opener = self;
    win.focus();
	top.register_window = win;
}

// find the handle frame
function find_handler()
{
    var hw = null;

	if( window.opener!=null && window.opener.closed )
		window.opener = null;

    if( typeof( top.handler )=='object' )
		hw = top.handler;
    else if( window.opener!=null && typeof( window.opener.top.handler )=='object' )
		hw = window.opener.top.handler;
    return hw;
}

// log using of a link
function use_link( id, a )
{
	var ret = true;
    var h = find_handler();

	if( a!=null )
	{
		var targ = a.target;
		if( nav )
		{
			// cannot detect shift-clicking in netscape right now..
		}
		else
		{
			// open in new window
			if( window.event.shiftKey )
				targ = null;
		}

		var w = window.open( a.href, targ, '', false );
		if( w==null )
		{
			popup_error();
		}
		else
		{
			w.focus();
			ret = false;
		}
	}

    if( h!=null )
	 	h.location = '/public/links/log_use.zml?id=' + id;

	return ret;
}

// rate a link
function rate( id, rating, lang )
{
    var h = find_handler();

    if( h!=null )
	 	h.location = '/public/links/rate_link.zml?id=' + id + '&rating=' + rating + '&lang=' + lang;
	else
		alert('Error! Cannot find main (TollerLinks) window.\nClose all browser windows and try again!');

	return true;
}

// report problem
function report_problem( id, lang )
{
    var h = find_handler();
	if( h==null )
	{
		alert('Error! Cannot find main link window.');
		return false;
	}

	var q = 'Enter a brief description of the problem with this link';
	var msg = 'Link is broken';

	if( lang=='sv' )
	{
		q = 'Skriv en kort beskrivning av problemet med denna länk';
		msg = 'Länken är sönder';
	}
	if( lang=='fi' )
	{
		q = 'Kirjoita lyhyt kuvaus tämän linkin ongelmasta';
		msg = 'Linkki ei toimi';
	}

	msg = prompt( q, msg );

	if( msg!=null && msg!='' )
		h.location = '/public/links/report_problem.zml?id=' + id + '&lang=' + lang + '&msg=' + escape(msg);
}

// check URL for uniqueness
function check_url( fld, lang, id )
{
    var h = find_handler();
	if( h==null )
		return true;
	
	var ref = '/public/links/check_url.zml?lang=' + lang;
	if( id!=null )
		ref = ref + '&id=' + id;
	ref = ref + '&url=' + escape(fld.value);
	h.location = ref;

	return false;
}

// make proper action target when submitting the searchform
function submit_searchbox()
{
    if( document.searchform.loc.selectedIndex==0 &&
		document.searchform.cat.selectedIndex<=1 &&
		document.searchform.q.value=='' )
    {
		document.searchform.action = 'index_page.zml';
    }
    else
    {
		document.searchform.action = 'more.zml';
    }

    return true;
}


// handle pressing of enter
var nav = window.Event ? true : false;
if( nav )
{
    window.captureEvents(Event.KEYDOWN);
    window.onkeydown = NS_KeyDown;
}
else
{
    document.onkeydown = MS_KeyDown;
}

function NS_KeyDown(e)
{
    if( e.which == 13 )
	{
		if( e.target.name=='q' )
			document.searchform.submit.click();

		if( e.target.type != 'textarea' && e.target.type != 'submit' 
			&& e.target.name != 'q' )
	    {
			return false;
	    }
	}
    return true;
}

function MS_KeyDown()
{
    if( event.keyCode == 13 )
	{
		if( event.srcElement.name=='q' )
			document.searchform.submit.click();
			
		if( event.srcElement.type != 'textarea' && 
			event.srcElement.type != 'submit' && event.srcElement.name != 'q' )
			return false;
	}
	return true;
}


// make sure the toplevel title is correct
if( self.location.href != top.location.href && self.document.title!='' )
    top.document.title = self.document.title;
