  // Get the contents of any <element> whose id is set (not Netscape 4)
  function getMenuHTML(id) {
    if(document.getElementById && document.getElementById(id))
      return document.getElementById(id).innerHTML;
    else if(document.all && document.all[id])
      return document.all[id].innerHTML;
    // Can only write to document.layers (Netscape 4), so
    else if(document.layers && document.layers[id])
      return '';
  }

  // Set the contents of any <element> whose id is set
  function setMenuHTML(id, txt) {
    if(document.getElementById && document.getElementById(id))
      document.getElementById(id).innerHTML = txt;
    else if(document.all && document.all[id])
      document.all[id].innerHTML = txt;
    // Can only write to document.layers (Netscape 4), so
    else if(document.layers && document.layers[id])
      with(document.layers[id].document){ open(); write(txt); close(); }
    return;
  }

  // Take a hierarchical unordered list,
  // contained within an <element id=menu_id>,
  // and only show submenus if they contain,
  // or are immediately preceded by,
  // the name of the current file being displayed.
  function modifyMenuHTML(menu_id) {

    // Where are we?
    var url = document.URL;

    // Where does the page name start? (after last "/")
    var fn_start = url.lastIndexOf("\/") + 1;

    // Where does the page name end? (before #anchor, ?skin=, etc.)
    // i.e. what is its length?
    var fn_terminator = '[".#?]';
    var fn_len = url.substring(fn_start).search(fn_terminator);

    // Extract page name
    if (fn_len > -1) {
      var fn = url.substr(fn_start, fn_len);
    } else {
      var fn = url.substr(fn_start);
    }
    
    //alert("url = "+url+", fn_start = "+fn_start+", fn_len = "+fn_len);

    // If fn == (blank || INCLUDINGWEB)
    // ie URL ends in "/Web" where Web/WebHome is implied
    if ((fn.length == 0) || (fn == includingweb)) {
      // Spell it out
      fn = "WebHome";
    }

    //Filename will be followed by #, ?, " etc.
    fn += fn_terminator;

    // Get the menu we're going to modify
    var menu = getMenuHTML(menu_id);

    // If the menu is blank, don't bother
    if (menu.length == 0) {
      return false;
    }

    // Turn the menu into a single line
    menu = menu.replace(/[\r\n]/g, '\t');

    // Cut to the first unordered list
    menu = menu.substring(menu.search(/<ul>/i));

    // Replace topicparent parameters in links to uncreated topics
    menu = menu.replace(/topicparent=[^"]*/gi, "topicparent="+web+"."+primarynav);

    // Pull out "<!-- comments -->" 
    menu = menu.replace(/<!--.*?-->/g, '');

    // Pull out "-- explanatory phrases" that follow menu entries
    menu = menu.replace(/-- [^<]*/g, '');

    // Convert complex <li>'s to plain ones.
    menu = menu.replace(/<li[^>]*>/gi, "<li>");

    // Now assign ids to the <li>'s
    var i = 0;
    while (menu.search(/<li>/i) != -1) {

      // Make up a label unique to the menu_id
      li_id = menu_id+"_"+i;

      // Assign the id to the first <li>
      menu = menu.replace(/<li>/i, "<li id='"+li_id+"'>");

      // For testing
      //alert(i+" "+menu);

      // On to the next
      i++;

    }    

    // Add CLF id's to <ul>'s.
    menu = menu.replace(/<ul>/i, "<ul id='primarynav'>");
    menu = menu.replace(/<ul>/gi, "<ul id='primarysubnav'>");

    // Put them in place, so you can refer to them
    setMenuHTML(menu_id, menu);
  
    // Now hide or show individual <li><ul>'s as necessary
    for (j=i-1;j>=0;j--) {

      // Make up the same id
      li_id = menu_id+"_"+j;

      // Get the line associated with it
      li_found = getMenuHTML(li_id);
      //alert(li_found+" "+li_id);

      // If the filename isn't in it, don't show submenus
      if ((li_found.search(fn) == -1 && li_found.search("twikiNewLink") == -1) || skin.search('uw.home') != -1) {
      //if (li_found.search(fn) == -1 || skin == 'uw2home' || skin == 'uw3home') {
      //if (li_found.search(fn) == -1 || skin.search('uw.home') != -1) {
      //if (li_found.search(fn) == -1) {

        // Set the style to "display: none"
        //li_found = li_found.replace(/<ul>/gi, "<ul style='display: none;'>");
        li_found = li_found.replace(/<ul/gi, "<ul style='display: none;' ");

        // For testing
        //alert(li_found);
        //alert(fn+" is not in "+li_id);

        // Replace the line
        setMenuHTML(li_id, li_found);

      } else {

        // For testing
        //alert(fn+" is in "+li_id);

      }
    }

    // Reload the completed menu
    menu = getMenuHTML(menu_id);
    //alert(menu);

    // Add CLF id's to <ul>'s.
    // Now done earlier
    //menu = menu.replace(/<ul>/i, "<ul id='primarynav'>");
    //menu = menu.replace(/<ul>/gi, "<ul id='primarysubnav'>");

    // Turn it back into multiple lines, more for style than any other reason
    menu = menu.replace(/\t/g, '\r\n');
     
    // For testing
    //alert(menu);

    // Set the modified menu in place
    setMenuHTML(menu_id, menu);
    //alert(getMenuHTML(menu_id));

    return true;
  }

  function initPage() {

    // Call the function to modify the primarynav menu
    // the call is now made in twiki.uwclf.tmpl,
    // immediately after the primarynavarea is displayed
    //var menu_id = 'primarynavarea';
    //modifyMenuHTML(menu_id);

  }
