function viewcommentary($section, $message = "Interject your own commentary?", $limit = 25, $talkline = "says", $schema = false, $skipfooter = false, $customsql = false, $skiprecentupdate = false, $overridemod = false)
{
    global $session, $REQUEST_URI, $doublepost, $translation_namespace;
    global $emptypost;
    global $chatloc, $afk, $dni, $moderating;
    if (!array_key_exists('commentary_auto_update', $session['user']['prefs'])) {
        $session['user']['prefs']['commentary_auto_update'] = 1;
    }
    if (httpget("disable_auto_update")) {
        $session['user']['prefs']['commentary_auto_update'] = 0;
    }
    if (httpget("enable_auto_update")) {
        $session['user']['prefs']['commentary_auto_update'] = 1;
    }
    if (!$returnlink) {
        $returnlink = URLEncode($_SERVER['REQUEST_URI']);
    }
    if ($session['user']['superuser'] & SU_EDIT_COMMENTS || $overridemod) {
        $showmodlink = true;
    } else {
        $showmodlink = false;
    }
    if (!$skiprecentupdate) {
        $session['recentcomments'] = date("Y-m-d H:i:s");
    }
    rawoutput("<!--start of commentary area--><a name=\"commentarystart\"></a>");
    global $fiveminuteload;
    if ($session['user']['prefs']['commentary_auto_update'] && !httpget('comscroll') && $fiveminuteload < 8) {
        $jsec = strtolower($section);
        $jsec = str_replace("_", "", $jsec);
        $jsec = str_replace("-", "", $jsec);
        $jsec = str_replace(",", "0", $jsec);
        //this javascript code auto-updates the chat automatically, based on server load.
        //The current five-minute average as last reported to the game (reported once per minute) is stored in the global $fiveminuteload.
        // $refreshbase = ceil($fiveminuteload);
        // if ($refreshbase < 1) $refreshbase = 1;
        // $refreshrate = $refreshbase*1000;
        //or, just go with every six seconds
        $refreshrate = 6000;
        rawoutput("<script type=\"text/javascript\">\r\n\t\t\tvar limit" . $jsec . " = 0;\r\n\t\t\ttimer" . $jsec . "=setTimeout(\"loadnewchat" . $jsec . "()\"," . $refreshrate . ");\r\n\t\t\tfunction loadnewchat" . $jsec . "() {\r\n\t\t\t\tlimit" . $jsec . " ++;\r\n\t\t\t\tif (window.XMLHttpRequest){\r\n\t\t\t\t\t// code for IE7+, Firefox, Chrome, Opera, Safari\r\n\t\t\t\t\txmlhttp" . $jsec . "=new XMLHttpRequest();\r\n\t\t\t\t} else {\r\n\t\t\t\t\t// code for IE6, IE5\r\n\t\t\t\t\txmlhttp" . $jsec . "=new ActiveXObject(\"Microsoft.XMLHTTP\");\r\n\t\t\t\t}\r\n\t\t\t\txmlhttp" . $jsec . ".onreadystatechange=function(){\r\n\t\t\t\t\tif (xmlhttp" . $jsec . ".readyState==4 && xmlhttp" . $jsec . ".status==200 && limit" . $jsec . " < 100){\r\n\t\t\t\t\t\tdocument.getElementById(\"ajaxcommentarydiv" . $jsec . "\").innerHTML = xmlhttp" . $jsec . ".responseText;\r\n\t\t\t\t\t\ttimer" . $jsec . "=setTimeout(\"loadnewchat" . $jsec . "()\"," . $refreshrate . ");\r\n\t\t\t\t\t} else if (limit" . $jsec . " >= 100){\r\n\t\t\t\t\t\tdocument.getElementById(\"ajaxcommentarynoticediv" . $jsec . "\").innerHTML = 'Auto-update has timed out.  Click any link to restart the clock.';\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t\txmlhttp" . $jsec . ".open(\"GET\",\"ajaxcommentary.php?section=" . $section . "&message=" . $message . "&limit=" . $limit . "&talkline=" . $talkline . "&returnlink=" . $returnlink . "\",true);\r\n\t\t\t\txmlhttp" . $jsec . ".send();\r\n\t\t\t}\r\n\t\t</script>");
        rawoutput("<div id=\"ajaxcommentarydiv" . $jsec . "\">");
    }
    output_notl("`n");
    $out = preparecommentaryblock($section, $message, $limit, $talkline, $schema, $skipfooter, $customsql, $skiprecentupdate, $overridemod, $returnlink);
    $commentary = appoencode($out, true);
    rawoutput($commentary);
    //output_notl("%s",$commentary,true);
    if ($session['user']['prefs']['commentary_auto_update']) {
        rawoutput("</div>");
    }
    if ($showmodlink) {
        //moderation link mass delete button
        rawoutput("<input type=\"submit\" value=\"Mass Delete\"></form>");
    }
    if (!$skipfooter) {
        commentaryfooter($section, $message, $limit, $talkline, $schema);
    }
    rawoutput("<!--end of commentary area-->");
}
function viewcommentary($section, $message = "Interject your own commentary?", $limit = 25, $talkline = "says", $schema = false, $skipfooter = false, $customsql = false, $skiprecentupdate = false, $overridemod = false)
{
    global $session, $REQUEST_URI, $doublepost, $translation_namespace;
    global $emptypost;
    global $chatloc, $chatsonpage, $afk, $dni, $moderating;
    if (httpget('comment')) {
        invalidatedatacache("latestcommentary_" . $section);
    }
    if ($session['user']['superuser'] & SU_EDIT_COMMENTS || $overridemod) {
        $showmodlink = true;
    } else {
        $showmodlink = false;
    }
    output_notl("`n");
    //skip assigning chatloc if this chatloc's id ends with "_aux" - this way we can have dual chat areas
    if (!$afk && $session['user']['chatloc'] != "DNI") {
        if (substr($section, strlen($section) - 4, strlen($section)) != "_aux") {
            $chatloc = $section;
        } else {
            $chatloc = substr($section, 0, -4);
        }
        $session['user']['chatloc'] = $chatloc;
    } else {
        $chatloc = "AFK";
    }
    if ($section) {
        rawoutput("<a name='{$section}'></a>");
        // Let's add a hook for modules to block commentary sections
        $args = modulehook("blockcommentarea", array("section" => $section));
        if (isset($args['block']) && $args['block'] == "yes") {
            return;
        }
    }
    $commentbuffer = getcommentary($section, $limit, $talkline, $customsql, $showmodlink);
    $rowcount = count($commentbuffer);
    if ($doublepost) {
        output("`\$`bDouble post?`b`0`n");
    }
    if ($emptypost) {
        output("`\$`bWell, they say silence is a virtue.`b`0`n");
    }
    //output the comments!
    if ($moderating && $rowcount || !$moderating) {
        $new = 0;
        if (!isset($session['user']['prefs']['commentary_recentline'])) {
            $session['user']['prefs']['commentary_recentline'] = 1;
        }
        if (!$session['user']['prefs']['commentary_reverse']) {
            for ($i = $rowcount - 1; $i >= 0; $i--) {
                $line = preparecommentaryline($commentbuffer[$i]);
                //highlight new commentary lines
                if ($commentbuffer[$i]['postdate'] > $session['user']['recentcomments'] && !$new && $session['user']['prefs']['commentary_recentline']) {
                    rawoutput("<hr style=\"border-bottom: 1px dotted #333333; border-top: 0; border-left: 0; border-right: 0;\" />");
                    $new = 1;
                }
                output_notl("{$line}`n", true);
            }
        } else {
            for ($i = 0; $i <= $rowcount - 1; $i++) {
                $line = preparecommentaryline($commentbuffer[$i]);
                output_notl("{$line}`n", true);
                if ($commentbuffer[$i]['postdate'] > $session['user']['recentcomments'] && !$new && $session['user']['prefs']['commentary_recentline']) {
                    rawoutput("<hr style=\"border-bottom: 1px dotted #333333; border-top: 0; border-left: 0; border-right: 0;\" />");
                    $new = 1;
                }
            }
        }
        if ($showmodlink) {
            //moderation link mass delete button
            rawoutput("<input type=\"submit\" value=\"Mass Delete\"></form>");
        }
        if (!$skipfooter) {
            commentaryfooter($section, $message, $limit, $talkline, $schema);
        }
    } else {
        //		output("There are no comments in this area.");
    }
    if (!$skiprecentupdate) {
        $session['user']['recentcomments'] = date("Y-m-d H:i:s");
    }
}