// This always called 'onload' when a page starts:
// If this page was called with an URL with a #fragmentlabel
// then scroll that fragment into view. Otherwise focus the
// keyboard cursor into search-box at the top of the page.
function page_start()
{
    if (!open_fragment()) {
	setKbCursor('search_box_txt');
    }
}
//----------------------------------------------------------------
function open_fragment()
{
    // Was this page called with an URL with a fragment label?
    // If so, then scroll that fragment into the viewport.
    // Note: use only to go to "display:block;" places.
    var hashcharpos = window.location.href.lastIndexOf('#');
    if (hashcharpos != -1) {
        // Indeed: isolate that fragment's name:
        var fragmentlabel = window.location.href.substring(hashcharpos+1);
        // and turn the display-visibility of that part of the page on:
        toggle_by_id(fragmentlabel, "block");
        document.getElementById(fragmentlabel).scrollIntoView();
    }
    return (hashcharpos != -1); // true if called with fragment label
}
//----------------------------------------------------------------
// Focus keyboard cursor into the Search-box
function setKbCursor(an_id)
{
    var this_element = document.getElementById(an_id);

    /* Give this element the keyboard focus & cursor: */
    if (this_element != null) this_element.focus();
    return true;
}
//----------------------------------------------------------------
// Open a new window next to the old one.
// Used for all "external" web sites
var NwWindow;
function openWin(URL, winid, width, height) {

	var features = '';

	if (width == null) { width=974; height=900; }
	else if (height == null) { height=900; }

	features += ',resizable=yes';
	features += ',menubar=yes';
	features += ',toolbar=yes';
	features += ',location=yes';
	features += ',scrollbars=yes';

	NwWindow = window.open(URL, winid,
	'width='+width+',height='+height+features);
}
function closeWin() {
	if (NwWindow && !NwWindow.closed) NwWindow.close();
}
//----------------------------------------------------------------
// Call Google Maps 
// location example: 'Toernooiveld+104%2C+Nijmegen%2C+6525+EC'
function mapURL(location)
{
        var city   = document.getElementById("mapcity").value;
        var pcode  = document.getElementById("mappcode").value;
        var theURL = "http://maps.google.co.uk/maps";

        theURL += '?';
	if ((city != '')||(pcode != '')) {
        	theURL += 'saddr='+pcode+'%2C'+city;
        	theURL += '&';
	}
	theURL += 'daddr='+location;

        window.open(theURL, "new",
                "width=1000,height=900,scrollbars=yes,resizable=yes");
}
//----------------------------------------------------------------
// Send an e-mail
function set_mail_action(domain, formId, mbox)
{
    var m, s, target;
    m = mbox+'@'+domain;
    s = 'subject=Mail%20sent%20via%20the%20DLOC%20website';
    m = 'mailto:'+m+'?'+s;
    if (document.getElementById) {
        target = document.getElementById(formId);
	target.setAttribute('action', m);
    }
}
function set_mail_href(domain, aId, mbox)
{
    var m, s, target;
    m = mbox+'@'+domain;
    s = 'subject=Mail sent via the DLOC website';
    m = 'mailto:'+m+'?'+s;
    if (document.getElementById) {
        target = document.getElementById(aId);
	target.setAttribute('href', m);
    }
}
//----------------------------------------------------------------
// Print page
function printPage()
{
    window.print()
    return true;
}
//----------------------------------------------------------------
// Toggle visibility. This routine is for the simple cases.
function toggle_by_id(targetId, displaystyle) {
    var target, image, image_srcname;

    if (document.getElementById) {
        target = document.getElementById(targetId);

        if (target.style.display == 'none') {
            // set the requested display style
            target.style.display = displaystyle;
	    image_srcname = '/images/minnetje.10.png';
        } else {
            target.style.display = 'none';
	    image_srcname = '/images/plusje.10.png';
        }
	// If a corresponding  ..._plusmin (image) element exists: 
	if (image = document.getElementById(targetId+'_plusmin')) {
	    image.src = image_srcname;
	}
    }
    return false;
}

//----------------------------------------------------------------
// Make visible and go to that place on the page:
function view_visibility_fromtop(nodeID, titletxt)
{
    toggle_visibility_fromtop(nodeID, titletxt, 1);
    // Following doesn't seem to work in Konqueror:
    document.getElementById(nodeID).scrollIntoView();
    return false;
}

// Next three routines work together.
// Recursively search the tree below the given node
// and toggle the display-style (none<->default) of children
// who have the given title text. titletxt may be an array of texts.
// Beware: in a table this technique has the disadvantage that
// column-width can not be calculated accurately because of the
// step-wise operation.
// Visibility toggle routine one: the entry point routine
function toggle_visibility_fromtop(nodeID, titletxt, always_vis)
{
    var a_node, browser;

    if (titletxt === null) return false;
    if ((a_node = document.getElementById(nodeID)) === null) return false;
    if (navigator.appName == 'Microsoft Internet Explorer') {
	browser = 'IE';
    } else {
	browser = 'NS';
    }
	
    if (typeof(titletxt) != 'object') {
	// 2nd call parm must always be an array:
        var tarray = [ titletxt ];
        toggle_visibility_recursive(a_node, tarray, browser, always_vis);
    } else {
        toggle_visibility_recursive(a_node, titletxt, browser, always_vis);
    }
    return false; // click has no further href-action
}

// Visibility toggle routine two: the recursive tree walker.
// Do not call this routine directly.
function toggle_visibility_recursive(node_elt, titletxt, whichbrowser, always_vis)
{
    var child_elt, next_elt;

    for (child_elt=node_elt.firstChild; child_elt != null; child_elt=next_elt) {
	if (child_elt.nextSibling !== null) {
            next_elt = child_elt.nextSibling;
	} else {
	    next_elt = null;
	}
        if (child_elt.title && check_txt(child_elt.title, titletxt) &&
                           child_elt.style && child_elt.style.display) {
            if (!always_vis && (child_elt.style.display != "none")) { // if it is visible,
                  child_elt.style.display = "none";  // then make it invisible.
   	    } else { // restore the visible situation:
		if (whichbrowser == 'IE') {
		    // IE crashes if we don't set 'inline' here
                    child_elt.style.display = 'inline';
		} else switch(child_elt.tagName) {
  		case 'table': case 'TABLE':
                    // Firefox messes up the lay out if we set 'inline' here
                    child_elt.style.display = 'table';
  		    break;
  		case 'tr': case 'TR':
                    // Firefox messes up the lay out if we set 'inline' here
                    child_elt.style.display = 'table-row';
                    break;
  		case 'th': case 'TH': case 'td': case 'TD':
                    // Firefox messes up the lay out if we set 'inline' here
                    child_elt.style.display = 'table-cell';
  		    break;
  		default:
                    child_elt.style.display = "inline"; // effectively: inherit
  		}
            }
        }
        if (child_elt.hasChildNodes()) {
            toggle_visibility_recursive(child_elt, titletxt, whichbrowser);
        }
    }
}
// Visibility toggle routine three: find elements with the proper titles.
// This routine checks whether a given text is present in a given array:
// Do not call this routine directly.
function check_txt(a_text, a_textarray)
{
// Zou ook moeten kunnen: if (a_text in a_textarray).... ?????
    for (var i in a_textarray) {
	if (a_text == a_textarray[i]) return true;
    }
    return false;
}
//----------------------------------------------------------------
