/**
 * Starts page output.  Inits the template and translator modules.
 *
 * @param array|string $title
 * Hooks provided:
 *		everyheader
 *		header-{scriptname}
 */
function page_header()
{
    global $header, $SCRIPT_NAME, $session, $template, $runheaders, $nopopups;
    $nopopups["login.php"] = 1;
    $nopopups["motd.php"] = 1;
    $nopopups["index.php"] = 1;
    $nopopups["create.php"] = 1;
    $nopopups["about.php"] = 1;
    $nopopups["mail.php"] = 1;
    //in case this didn't already get called (such as on a database error)
    translator_setup();
    prepare_template();
    $script = substr($SCRIPT_NAME, 0, strrpos($SCRIPT_NAME, "."));
    if ($script) {
        if (!array_key_exists($script, $runheaders)) {
            $runheaders[$script] = false;
        }
        if (!$runheaders[$script]) {
            modulehook("everyheader", array('script' => $script));
            if ($session['user']['loggedin']) {
                modulehook("everyheader-loggedin", array('script' => $script));
            }
            $runheaders[$script] = true;
            modulehook("header-{$script}");
        }
    }
    $arguments = func_get_args();
    if (!$arguments || count($arguments) == 0) {
        $arguments = array("Legend of the Green Dragon");
    }
    $title = call_user_func_array("sprintf_translate", $arguments);
    // $title = holidayize($title,'title');
    $title = sanitize($title);
    calculate_buff_fields();
    $header = $template['header'];
    $header = str_replace("{title}", $title, $header);
    $header .= tlbutton_pop();
    // $session['testhits']+=1;
    // $session['totalpost'][]=$_POST;
    // debug($session);
    // debug($_POST,true);
    // debug($_GET,true);
}
function tlbutton_clear()
{
    global $translatorbuttons, $session;
    if ($session['user']['superuser'] & SU_IS_TRANSLATOR) {
        $return = tlbutton_pop() . join("", $translatorbuttons);
        $translatorbuttons = array();
        return $return;
    } else {
        return "";
    }
}
/**
 * Private functions (Undocumented)
 *
 * @param string $text
 * @param string $link
 * @param bool $priv
 * @param bool $pop
 * @param bool $popsize
 * @return mixed
 */
function private_addnav($text, $link = false, $priv = false, $pop = false, $popsize = "500x300")
{
    //don't call this directly please.  I'll break your thumbs if you do.
    global $nav, $session, $accesskeys, $REQUEST_URI, $quickkeys, $navschema, $notranslate;
    if (is_blocked($link)) {
        return false;
    }
    $thisnav = "";
    $unschema = 0;
    $translate = true;
    if (isset($notranslate)) {
        if (in_array(array($text, $link), $notranslate)) {
            $translate = false;
        }
    }
    if (is_array($text)) {
        if ($text[0] && $session['loggedin']) {
            if ($link === false) {
                $schema = "!array!" . serialize($text);
            } else {
                $schema = $text[0];
            }
            if ($translate) {
                tlschema($navschema[$schema]);
                $unschema = 1;
            }
        }
        if ($link != "!!!addraw!!!") {
            if ($translate) {
                $text[0] = translate($text[0]);
            }
            $text = call_user_func_array("sprintf", $text);
        } else {
            $text = call_user_func_array("sprintf", $text);
        }
    } else {
        if ($text && $session['loggedin'] && $translate) {
            tlschema($navschema[$text]);
            $unschema = 1;
        }
        if ($link != "!!!addraw!!!" && $text > "" && $translate) {
            $text = translate($text);
        }
        //leave the hack in here for now, use addnav_notl please
    }
    $extra = "";
    $ignoreuntil = "";
    if ($link === false) {
        //$text = holidayize($text,'nav');
        $thisnav .= tlbutton_pop() . templatereplace("navhead", array("title" => appoencode($text, $priv)));
    } elseif ($link === "") {
        //$text = holidayize($text,'nav');
        $thisnav .= tlbutton_pop() . templatereplace("navhelp", array("text" => appoencode($text, $priv)));
    } elseif ($link == "!!!addraw!!!") {
        $thisnav .= $text;
    } else {
        if ($text != "") {
            $extra = "";
            if (strpos($link, "?")) {
                $extra = "&c={$session['counter']}";
            } else {
                $extra = "?c={$session['counter']}";
            }
            $extra .= "-" . date("His");
            //hotkey for the link.
            $key = "";
            if ($text[1] == "?") {
                // check to see if a key was specified up front.
                $hchar = strtolower($text[0]);
                if ($hchar == ' ' || array_key_exists($hchar, $accesskeys) && $accesskeys[$hchar] == 1) {
                    $text = substr($text, 2);
                    //$text = holidayize($text,'nav');
                    if ($hchar == ' ') {
                        $key = " ";
                    }
                } else {
                    $key = $text[0];
                    $text = substr($text, 2);
                    //$text = holidayize($text,'nav');
                    $found = false;
                    $text_len = strlen($text);
                    for ($i = 0; $i < $text_len; ++$i) {
                        $char = $text[$i];
                        if ($ignoreuntil == $char) {
                            $ignoreuntil = "";
                        } else {
                            if ($ignoreuntil != "") {
                                if ($char == "<") {
                                    $ignoreuntil = ">";
                                }
                                if ($char == "&") {
                                    $ignoreuntil = ";";
                                }
                                if ($char == "`") {
                                    $ignoreuntil = $text[$i + 1];
                                }
                            } else {
                                if ($char == $key) {
                                    $found = true;
                                    break;
                                }
                            }
                        }
                    }
                    if ($found == false) {
                        //the hotkey for this link wasn't actually in the
                        //text, prepend it in parens.
                        if (strpos($text, "__") !== false) {
                            $text = str_replace("__", "(" . $key . ") ", $text);
                        } else {
                            $text = "(" . strtoupper($key) . ") " . $text;
                        }
                        $i = strpos($text, $key);
                    }
                }
            } else {
                //$text = holidayize($text,'nav');
            }
            if ($key == "") {
                //we have no previously defined key.  Look for a new one.
                $tlength = strlen($text);
                for ($i = 0; $i < $tlength; $i++) {
                    $char = substr($text, $i, 1);
                    if ($ignoreuntil == $char) {
                        $ignoreuntil = "";
                    } else {
                        if (isset($accesskeys[strtolower($char)]) && $accesskeys[strtolower($char)] == 1 || strpos("abcdefghijklmnopqrstuvwxyz0123456789", strtolower($char)) === false || $ignoreuntil != "") {
                            if ($char == "<") {
                                $ignoreuntil = ">";
                            }
                            if ($char == "&") {
                                $ignoreuntil = ";";
                            }
                            if ($char == "`") {
                                $ignoreuntil = substr($text, $i + 1, 1);
                            }
                        } else {
                            break;
                        }
                    }
                }
            }
            if (!isset($i)) {
                $i = 0;
            }
            if ($i < strlen($text) && $key != ' ') {
                $key = substr($text, $i, 1);
                $accesskeys[strtolower($key)] = 1;
                $keyrep = " accesskey=\"{$key}\" ";
            } else {
                $key = "";
                $keyrep = "";
            }
            if ($key == "" || $key == " ") {
            } else {
                $pattern1 = "/^" . preg_quote($key, "/") . "/";
                $pattern2 = "/([^`])" . preg_quote($key, "/") . "/";
                $rep1 = "`H{$key}`H";
                $rep2 = "\$1`H{$key}`H";
                $text = preg_replace($pattern1, $rep1, $text, 1);
                if (strpos($text, "`H") === false) {
                    $text = preg_replace($pattern2, $rep2, $text, 1);
                }
                if ($pop) {
                    if ($popsize == "") {
                        $quickkeys[$key] = "window.open('{$link}')";
                    } else {
                        $quickkeys[$key] = popup($link, $popsize);
                    }
                } else {
                    $quickkeys[$key] = "window.location='{$link}{$extra}'";
                }
            }
            $n = templatereplace("navitem", array("text" => appoencode($text, $priv), "link" => HTMLEntities($link . ($pop != true ? $extra : ""), ENT_COMPAT, getsetting("charset", "ISO-8859-1")), "accesskey" => $keyrep, "popup" => $pop == true ? "target='_blank'" . ($popsize > "" ? " onClick=\"" . popup($link, $popsize) . "; return false;\"" : "") : ""));
            $n = str_replace("<a ", tlbutton_pop() . "<a ", $n);
            $thisnav .= $n;
        }
        $session['allowednavs'][$link . $extra] = true;
        $session['allowednavs'][str_replace(" ", "%20", $link) . $extra] = true;
        $session['allowednavs'][str_replace(" ", "+", $link) . $extra] = true;
        if (($pos = strpos($link, "#")) !== false) {
            $sublink = substr($link, 0, $pos);
            $session['allowednavs'][$sublink . $extra] = true;
        }
    }
    if ($unschema) {
        tlschema();
    }
    $nav .= $thisnav;
    return $thisnav;
}
function showform($layout, $row, $nosave = false, $keypref = false)
{
    global $session;
    static $showform_id = 0;
    static $title_id = 0;
    $showform_id++;
    $formSections = array();
    $returnvalues = array();
    $extensions = array();
    $extensions = modulehook("showformextensions", $extensions);
    rawoutput("<table width='100%' cellpadding='0' cellspacing='0'><tr><td>");
    rawoutput("<div id='showFormSection{$showform_id}'></div>");
    rawoutput("</td></tr><tr><td>&nbsp;</td></tr><tr><td>");
    rawoutput("<table cellpadding='2' cellspacing='0'>");
    $i = 0;
    while (list($key, $val) = each($layout)) {
        $pretrans = 0;
        if ($keypref !== false) {
            $keyout = sprintf($keypref, $key);
        } else {
            $keyout = $key;
        }
        if (is_array($val)) {
            $v = $val[0];
            $info = split(",", $v);
            $val[0] = $info[0];
            $info[0] = $val;
        } else {
            $info = split(",", $val);
        }
        if (is_array($info[0])) {
            $info[0] = call_user_func_array("sprintf_translate", $info[0]);
        } else {
            $info[0] = translate($info[0]);
        }
        if (isset($info[1])) {
            $info[1] = trim($info[1]);
        } else {
            $info[1] = "";
        }
        if ($info[1] == "title") {
            $title_id++;
            rawoutput("</table>");
            $formSections[$title_id] = $info[0];
            rawoutput("<table id='showFormTable{$title_id}' cellpadding='2' cellspacing='0'>");
            rawoutput("<tr><td colspan='2' class='trhead'>");
            output_notl("`b%s`b", $info[0], true);
            rawoutput("</td></tr>", true);
            $i = 0;
        } elseif ($info[1] == "note") {
            rawoutput("<tr class='" . ($i % 2 ? 'trlight' : 'trdark') . "'><td colspan='2'>");
            output_notl("`i%s`i", $info[0], true);
            $i++;
        } elseif ($info[1] == "invisible") {
            // Don't show
        } else {
            if (isset($row[$key])) {
                $returnvalues[$key] = $row[$key];
            }
            rawoutput("<tr class='" . ($i % 2 ? 'trlight' : 'trdark') . "'><td valign='top'>");
            output_notl("%s", $info[0], true);
            rawoutput("</td><td valign='top'>");
            $i++;
        }
        switch ($info[1]) {
            case "title":
            case "note":
            case "invisible":
                break;
            case "theme":
                // A generic way of allowing a theme to be selected.
                $skins = array();
                $handle = @opendir("templates");
                // Template directory open failed
                if (!$handle) {
                    output("None available");
                    break;
                }
                while (false != ($file = @readdir($handle))) {
                    if (strpos($file, ".htm") > 0) {
                        array_push($skins, $file);
                    }
                }
                // No templates installed!
                if (count($skins) == 0) {
                    output("None available");
                    break;
                }
                natcasesort($skins);
                //sort them in natural order
                rawoutput("<select name='{$keyout}'>");
                foreach ($skins as $skin) {
                    if ($skin == $row[$key]) {
                        rawoutput("<option value='{$skin}' selected>" . htmlentities(substr($skin, 0, strpos($skin, ".htm")), ENT_COMPAT, getsetting("charset", "ISO-8859-1")) . "</option>");
                    } else {
                        rawoutput("<option value='{$skin}'>" . htmlentities(substr($skin, 0, strpos($skin, ".htm")), ENT_COMPAT, getsetting("charset", "ISO-8859-1")) . "</option>");
                    }
                }
                rawoutput("</select>");
                break;
            case "location":
                // A generic way of allowing the location to be specified for
                // things which only want to be in one place.  There are other
                // things which would be good to do as well of course, such
                // as making sure to handle village name changes in the module
                // that cares about this or what not, but this at least gives
                // some support.
                $vloc = array();
                $vname = getsetting("villagename", LOCATION_FIELDS);
                $vloc[$vname] = "village";
                $vloc['all'] = 1;
                $vloc = modulehook("validlocation", $vloc);
                unset($vloc['all']);
                reset($vloc);
                rawoutput("<select name='{$keyout}'>");
                foreach ($vloc as $loc => $val) {
                    if ($loc == $row[$key]) {
                        rawoutput("<option value='{$loc}' selected>" . htmlentities($loc, ENT_COMPAT, getsetting("charset", "ISO-8859-1")) . "</option>");
                    } else {
                        rawoutput("<option value='{$loc}'>" . htmlentities($loc, ENT_COMPAT, getsetting("charset", "ISO-8859-1")) . "</option>");
                    }
                }
                rawoutput("</select>");
                break;
            case "checkpretrans":
                $pretrans = 1;
                // FALLTHROUGH
            // FALLTHROUGH
            case "checklist":
                reset($info);
                list($k, $v) = each($info);
                list($k, $v) = each($info);
                $select = "";
                while (list($k, $v) = each($info)) {
                    $optval = $v;
                    list($k, $v) = each($info);
                    $optdis = $v;
                    if (!$pretrans) {
                        $optdis = translate_inline($optdis);
                    }
                    if (is_array($row[$key])) {
                        if ($row[$key][$optval]) {
                            $checked = true;
                        } else {
                            $checked = false;
                        }
                    } else {
                        //any other ways to represent this?
                        debug("You must pass an array as the value when using a checklist.");
                        $checked = false;
                    }
                    $select .= "<input type='checkbox' name='{$keyout}[{$optval}]' value='1'" . ($checked == $optval ? " checked" : "") . ">&nbsp;" . "{$optdis}" . "<br>";
                }
                rawoutput($select);
                break;
            case "radiopretrans":
                $pretrans = 1;
                // FALLTHROUGH
            // FALLTHROUGH
            case "radio":
                reset($info);
                list($k, $v) = each($info);
                list($k, $v) = each($info);
                $select = "";
                while (list($k, $v) = each($info)) {
                    $optval = $v;
                    list($k, $v) = each($info);
                    $optdis = $v;
                    if (!$pretrans) {
                        $optdis = translate_inline($optdis);
                    }
                    $select .= "<input type='radio' name='{$keyout}' value='{$optval}'" . ($row[$key] == $optval ? " checked" : "") . ">&nbsp;" . "{$optdis}" . "<br>";
                }
                rawoutput($select);
                break;
            case "dayrange":
                $start = strtotime(date("Y-m-d", strtotime("now")));
                $end = strtotime($info[2]);
                $step = $info[3];
                // we should really try to avoid an infinite loop here if
                // they define a time string which equates to 0 :/
                $cur = $row[$key];
                rawoutput("<select name='{$keyout}'>");
                if ($cur && $cur < date("Y-m-d H:i:s", $start)) {
                    rawoutput("<option value='{$cur}' selected>" . htmlentities($cur, ENT_COMPAT, getsetting("charset", "ISO-8859-1")) . "</option>");
                }
                for ($j = $start; $j < $end; $j = strtotime($step, $j)) {
                    $d = date("Y-m-d H:i:s", $j);
                    rawoutput("<option value='{$d}'" . ($cur == $d ? " selected" : "") . ">" . HTMLEntities("{$d}", ENT_COMPAT, getsetting("charset", "ISO-8859-1")) . "</option>");
                }
                if ($cur && $cur > date("Y-m-d H:i:s", $end)) {
                    rawoutput("<option value='{$cur}' selected>" . htmlentities($cur, ENT_COMPAT, getsetting("charset", "ISO-8859-1")) . "</option>");
                }
                rawoutput("</select>");
                break;
            case "range":
                $min = (int) $info[2];
                $max = (int) $info[3];
                $step = (int) (isset($info[4]) ? $info[4] : false);
                if ($step == 0) {
                    $step = 1;
                }
                rawoutput("<select name='{$keyout}'>");
                if ($min < $max && ($max - $min) / $step > 300) {
                    $step = max(1, (int) (($max - $min) / 300));
                }
                for ($j = $min; $j <= $max; $j += $step) {
                    rawoutput("<option value='{$j}'" . ($row[$key] == $j ? " selected" : "") . ">" . HTMLEntities("{$j}", ENT_COMPAT, getsetting("charset", "ISO-8859-1")) . "</option>");
                }
                rawoutput("</select>");
                break;
            case "floatrange":
                $min = round((double) $info[2], 2);
                $max = round((double) $info[3], 2);
                $step = round((double) $info[4], 2);
                if ($step == 0) {
                    $step = 1;
                }
                rawoutput("<select name='{$keyout}'>", true);
                $val = round((double) $row[$key], 2);
                for ($j = $min; $j <= $max; $j = round($j + $step, 2)) {
                    rawoutput("<option value='{$j}'" . ($val == $j ? " selected" : "") . ">" . HTMLEntities("{$j}", ENT_COMPAT, getsetting("charset", "ISO-8859-1")) . "</option>", true);
                }
                rawoutput("</select>", true);
                break;
            case "bitfieldpretrans":
                $pretrans = 1;
                // FALLTHROUGH
            // FALLTHROUGH
            case "bitfield":
                //format:
                //DisplayName,bitfield,disablemask,(highbit,display)+
                //1-26-03 added disablemask so this field type can be used
                // on bitfields other than superuser.
                reset($info);
                list($k, $v) = each($info);
                list($k, $v) = each($info);
                list($k, $disablemask) = each($info);
                rawoutput("<input type='hidden' name='{$keyout}" . "[0]' value='1'>", true);
                while (list($k, $v) = each($info)) {
                    rawoutput("<input type='checkbox' name='{$keyout}" . "[{$v}]'" . (isset($row[$key]) && (int) $row[$key] & (int) $v ? " checked" : "") . ($disablemask & (int) $v ? "" : " disabled") . " value='1'> ");
                    list($k, $v) = each($info);
                    if (!$pretrans) {
                        $v = translate_inline($v);
                    }
                    output_notl("%s`n", $v, true);
                }
                break;
            case "datelength":
                // However, there was a bug with your translation code wiping
                // the key name for the actual form.  It's now fixed.
                // ok, I see that, but 24 hours and 1 day are the same
                // aren't they?
                $vals = array("1 hour", "2 hours", "3 hours", "4 hours", "5 hours", "6 hours", "8 hours", "10 hours", "12 hours", "16 hours", "18 hours", "24 hours", "1 day", "2 days", "3 days", "4 days", "5 days", "6 days", "7 days", "1 week", "2 weeks", "3 weeks", "4 weeks", "1 month", "2 months", "3 months", "4 months", "6 months", "9 months", "12 months", "1 year");
                tlschema("showform");
                while (list($k, $v) = each($vals)) {
                    $vals[$k] = translate($v);
                    rawoutput(tlbutton_pop());
                }
                tlschema();
                reset($vals);
                rawoutput("<select name='{$keyout}'>");
                while (list($k, $v) = each($vals)) {
                    rawoutput("<option value=\"" . htmlentities($v, ENT_COMPAT, getsetting("charset", "ISO-8859-1")) . "\"" . ($row[$key] == $v ? " selected" : "") . ">" . htmlentities($v, ENT_COMPAT, getsetting("charset", "ISO-8859-1")) . "</option>");
                }
                rawoutput("</select>");
                break;
            case "enumpretrans":
                $pretrans = 1;
                // FALLTHROUGH
            // FALLTHROUGH
            case "enum":
                reset($info);
                list($k, $v) = each($info);
                list($k, $v) = each($info);
                $select = "";
                $select .= "<select name='{$keyout}'>";
                while (list($k, $v) = each($info)) {
                    $optval = $v;
                    list($k, $v) = each($info);
                    $optdis = $v;
                    if (!$pretrans) {
                        $optdis = translate_inline($optdis);
                    }
                    $selected = 0;
                    if (isset($row[$key]) && $row[$key] == $optval) {
                        $selected = 1;
                    }
                    $select .= "<option value='{$optval}'" . ($selected ? " selected" : "") . ">" . HTMLEntities("{$optdis}", ENT_COMPAT, getsetting("charset", "ISO-8859-1")) . "</option>";
                }
                $select .= "</select>";
                rawoutput($select);
                break;
            case "password":
                if (array_key_exists($key, $row)) {
                    $out = $row[$key];
                } else {
                    $out = "";
                }
                rawoutput("<input type='password' name='{$keyout}' value='" . HTMLEntities($out, ENT_COMPAT, getsetting("charset", "ISO-8859-1")) . "'>");
                break;
            case "bool":
                tlschema("showform");
                $yes = translate_inline("Yes");
                $no = translate_inline("No");
                tlschema();
                rawoutput("<select name='{$keyout}'>");
                rawoutput("<option value='0'" . ($row[$key] == 0 ? " selected" : "") . ">{$no}</option>");
                rawoutput("<option value='1'" . ($row[$key] == 1 ? " selected" : "") . ">{$yes}</option>");
                rawoutput("</select>", true);
                break;
            case "hidden":
                if (isset($row[$key])) {
                    rawoutput("<input type='hidden' name='{$keyout}' value=\"" . HTMLEntities($row[$key], ENT_COMPAT, getsetting("charset", "ISO-8859-1")) . "\">" . HTMLEntities($row[$key], ENT_COMPAT, getsetting("charset", "ISO-8859-1")));
                }
                break;
            case "viewonly":
                unset($returnvalues[$key]);
                if (isset($row[$key])) {
                    output_notl(dump_item($row[$key]), true);
                }
                break;
            case "textarearesizeable":
                $resize = true;
                //FALLTHROUGH
            //FALLTHROUGH
            case "textarea":
                $cols = 0;
                if (isset($info[2])) {
                    $cols = $info[2];
                }
                if (!$cols) {
                    $cols = 70;
                }
                $text = "";
                if (isset($row[$key])) {
                    $text = $row[$key];
                }
                if (isset($resize) && $resize) {
                    rawoutput("<script type=\"text/javascript\">function increase(target, value){  if (target.rows + value > 3 && target.rows + value < 50) target.rows = target.rows + value;}</script>");
                    rawoutput("<textarea id='textarea{$key}' class='input' name='{$keyout}' cols='{$cols}' rows='5'>" . htmlentities($text, ENT_COMPAT, getsetting("charset", "ISO-8859-1")) . "</textarea>");
                    rawoutput("<input type='button' onClick=\"increase(textarea{$key},1);\" value='+' accesskey='+'><input type='button' onClick=\"increase(textarea{$key},-1);\" value='-' accesskey='-'>");
                } else {
                    rawoutput("<textarea class='input' name='{$keyout}' cols='{$cols}' rows='5'>" . htmlentities($text, ENT_COMPAT, getsetting("charset", "ISO-8859-1")) . "</textarea>");
                }
                break;
            case "int":
                if (array_key_exists($key, $row)) {
                    $out = $row[$key];
                } else {
                    $out = 0;
                }
                rawoutput("<input name='{$keyout}' value=\"" . HTMLEntities($out, ENT_COMPAT, getsetting("charset", "ISO-8859-1")) . "\" size='5'>");
                break;
            case "float":
                rawoutput("<input name='{$keyout}' value=\"" . htmlentities($row[$key], ENT_COMPAT, getsetting("charset", "ISO-8859-1")) . "\" size='8'>");
                break;
            case "string":
                $len = 50;
                $minlen = 50;
                if (isset($info[2])) {
                    $len = (int) $info[2];
                }
                if ($len < $minlen) {
                    $minlen = $len;
                }
                if ($len > $minlen) {
                    $minlen = $len / 2;
                }
                if ($minlen > 70) {
                    $minlen = 70;
                }
                if (array_key_exists($key, $row)) {
                    $val = $row[$key];
                } else {
                    $val = "";
                }
                rawoutput("<input size='{$minlen}' maxlength='{$len}' name='{$keyout}' value=\"" . HTMLEntities($val, ENT_COMPAT, getsetting("charset", "ISO-8859-1")) . "\">");
                break;
            default:
                if (array_key_exists($info[1], $extensions)) {
                    $func = $extensions[$info[1]];
                    if (array_key_exists($key, $row)) {
                        $val = $row[$key];
                    } else {
                        $val = "";
                    }
                    call_user_func($func, $keyout, $val, $info);
                } else {
                    if (array_key_exists($key, $row)) {
                        $val = $row[$key];
                    } else {
                        $val = "";
                    }
                    rawoutput("<input size='50' name='{$keyout}' value=\"" . HTMLEntities($val, ENT_COMPAT, getsetting("charset", "ISO-8859-1")) . "\">");
                }
        }
        rawoutput("</td></tr>", true);
    }
    rawoutput("</table><br>", true);
    if ($showform_id == 1) {
        $startIndex = (int) httppost("showFormTabIndex");
        if ($startIndex == 0) {
            $startIndex = 1;
        }
        if (isset($session['user']['prefs']['tabconfig']) && $session['user']['prefs']['tabconfig'] == 0) {
        } else {
            rawoutput("\r\n\t\t \t<script language='JavaScript'>\r\n\t\t \tfunction prepare_form(id){\r\n\t\t \t\tvar theTable;\r\n\t\t \t\tvar theDivs='';\r\n\t\t \t\tvar x=0;\r\n\t\t \t\tvar weight='';\r\n\t\t \t\tfor (x in formSections[id]){\r\n\t\t \t\t\ttheTable = document.getElementById('showFormTable'+x);\r\n\t\t \t\t\tif (x != {$startIndex} ){\r\n\t\t\t \t\t\ttheTable.style.visibility='hidden';\r\n\t\t\t \t\t\ttheTable.style.display='none';\r\n\t\t\t \t\t\tweight='';\r\n\t\t\t \t\t}else{\r\n\t\t\t \t\t\ttheTable.style.visibility='visible';\r\n\t\t\t \t\t\ttheTable.style.display='inline';\r\n\t\t\t \t\t\tweight='color: yellow;';\r\n\t\t\t \t\t}\r\n\t\t\t \t\ttheDivs += \"<div id='showFormButton\"+x+\"' class='trhead' style='\"+weight+\"float: left; cursor: pointer; cursor: hand; padding: 5px; border: 1px solid #000000;' onClick='showFormTabClick(\"+id+\",\"+x+\");'>\"+formSections[id][x]+\"</div>\";\r\n\t\t \t\t}\r\n\t\t \t\ttheDivs += \"<div style='display: block;'>&nbsp;</div>\";\r\n\t\t\t\ttheDivs += \"<input type='hidden' name='showFormTabIndex' value='{$startIndex}' id='showFormTabIndex'>\";\r\n\t\t \t\tdocument.getElementById('showFormSection'+id).innerHTML = theDivs;\r\n\t\t \t}\r\n\t\t \tfunction showFormTabClick(formid,sectionid){\r\n\t\t \t\tvar theTable;\r\n\t\t \t\tvar theButton;\r\n\t\t \t\tfor (x in formSections[formid]){\r\n\t\t \t\t\ttheTable = document.getElementById('showFormTable'+x);\r\n\t\t \t\t\ttheButton = document.getElementById('showFormButton'+x);\r\n\t\t \t\t\tif (x == sectionid){\r\n\t\t \t\t\t\ttheTable.style.visibility='visible';\r\n\t\t \t\t\t\ttheTable.style.display='inline';\r\n\t\t \t\t\t\ttheButton.style.fontWeight='normal';\r\n\t\t \t\t\t\ttheButton.style.color='yellow';\r\n\t\t\t\t\t\tdocument.getElementById('showFormTabIndex').value = sectionid;\r\n\t\t \t\t\t}else{\r\n\t\t \t\t\t\ttheTable.style.visibility='hidden';\r\n\t\t \t\t\t\ttheTable.style.display='none';\r\n\t\t \t\t\t\ttheButton.style.fontWeight='normal';\r\n\t\t \t\t\t\ttheButton.style.color='';\r\n\t\t \t\t\t}\r\n\t\t \t\t}\r\n\t\t \t}\r\n\t\t \tformSections = new Array();\r\n\t\t\t</script>");
        }
    }
    if (isset($session['user']['prefs']['tabconfig']) && $session['user']['prefs']['tabconfig'] == 0) {
    } else {
        rawoutput("<script language='JavaScript'>");
        rawoutput("formSections[{$showform_id}] = new Array();");
        reset($formSections);
        while (list($key, $val) = each($formSections)) {
            rawoutput("formSections[{$showform_id}][{$key}] = '" . addslashes($val) . "';");
        }
        rawoutput("\r\n\t\tprepare_form({$showform_id});\r\n\t\t</script>");
    }
    rawoutput("</td></tr></table>");
    tlschema("showform");
    $save = translate_inline("Save");
    tlschema();
    if ($nosave) {
    } else {
        rawoutput("<input type='submit' class='button' value='{$save}'>");
    }
    return $returnvalues;
}