function nestPages($id, $level)
{
    $qry = "SELECT DISTINCT D.pagename AS parentpagename, A.*, B.* FROM pagenavigation A " . "INNER JOIN pages B " . "ON A.childpageid = B.pageid " . "INNER JOIN pageroles C " . "ON C.pageid = B.pageid " . "INNER JOIN pages D " . "ON D.pageid = A.pageid " . "WHERE A.pageid = " . $id . " " . "AND C.roleid IN (" . ArrayToInClause($_SESSION['ROLES']) . ")" . "ORDER BY A.sequence";
    $result = mysql_query($qry);
    //Check whether the query was successful or not
    if ($result) {
        if (mysql_num_rows($result) == 0) {
            if ($level == 2) {
                nestPages(1, 1);
            } else {
                $qry = "SELECT pageid FROM pagenavigation " . "WHERE childpageid = {$id}";
                $result = mysql_query($qry);
                //Check whether the query was successful or not
                if ($result) {
                    if (mysql_num_rows($result) == 1) {
                        $member = mysql_fetch_assoc($result);
                        nestPages($member['pageid'], 2);
                    }
                }
            }
        } else {
            echo "<ul>";
            /* Show children. */
            while ($member = mysql_fetch_assoc($result)) {
                $_SESSION['parentpagename'] = $member['parentpagename'];
                if ($member['pagename'] == $_SESSION['pagename']) {
                    echo "<li class='selected'>";
                } else {
                    echo "<li>";
                }
                echo "<a href='" . $member['pagename'] . "'>" . $member['label'] . "</a></li>";
            }
            echo "</ul>";
        }
    }
}
function nestPages($id, $ancestors)
{
    $menuHTML = "";
    $qry = "SELECT DISTINCT A.*, B.* FROM {$_SESSION['DB_PREFIX']}pagenavigation A " . "INNER JOIN {$_SESSION['DB_PREFIX']}pages B " . "ON A.childpageid = B.pageid " . "INNER JOIN {$_SESSION['DB_PREFIX']}pageroles C " . "ON C.pageid = B.pageid " . "WHERE A.pageid = " . $id . " " . "AND A.pagetype = 'P' " . "AND C.roleid IN (" . ArrayToInClause($_SESSION['ROLES']) . ") " . "ORDER BY A.sequence";
    $result = mysql_query($qry);
    //Check whether the query was successful or not
    if ($result) {
        if (mysql_num_rows($result) == 0) {
            if (isAuthenticated()) {
                $ancestors = findParentMenu($id, $ancestors);
                $menuHTML = $menuHTML . nestPages($ancestors[count($ancestors) - 1], $ancestors);
            }
        } else {
            $result = mysql_query($qry);
            $highestPage = 0;
            while ($member = mysql_fetch_assoc($result)) {
                for ($index = 0; $index < count($ancestors); $index++) {
                    if ($ancestors[$index] == $member['pageid']) {
                        if ($highestPage < $member['pageid']) {
                            $highestPage = $member['pageid'];
                        }
                    }
                }
            }
            $result = mysql_query($qry);
            $first = true;
            $counter = 0;
            $menuHTML = $menuHTML . "<div class='red'><ul class='mega-menu'>\n";
            /* Show children. */
            while ($member = mysql_fetch_assoc($result)) {
                $anchorClass = "";
                $menuHTML = $menuHTML . "<li class='";
                $counter++;
                if ($counter == 6) {
                    $anchorClass = "last ";
                }
                if ($first) {
                    $first = false;
                    $anchorClass = $anchorClass . "first ";
                }
                if ($highestPage == $member['pageid']) {
                    $menuHTML = $menuHTML . "current ";
                }
                $menuHTML = $menuHTML . "' onclick='window.location.href = \"" . $member['pagename'] . "\"'>";
                $target = "";
                if ($member['target'] != null) {
                    $target = " target='" . $member['target'] . "' ";
                }
                $menuHTML = $menuHTML . "<a {$target} class='{$anchorClass}' href='" . $member['pagename'] . "'><em><b>" . $member['label'] . "</b></em></a>\n";
                $menuHTML = $menuHTML . showSubMenu($member['childpageid']);
                $menuHTML = $menuHTML . "</li>\n";
                if ($member['divider'] == 1) {
                    $menuHTML = $menuHTML . "<div class='divider'>&nbsp;</div>\n";
                }
            }
            $menuHTML = $menuHTML . "</ul></div>\n";
        }
    }
    return $menuHTML;
}