Exemple #1
0
     }
 }
 $laarr = explode('##', $lastanswers[$i]);
 if ($attempts[$i] != count($laarr)) {
     //echo " (clicked \"Jump to answer\")";
 }
 if (count($laarr) > 1) {
     echo "<br/>Previous Attempts:";
     $cnt = 1;
     for ($k = 0; $k < count($laarr) - 1; $k++) {
         if ($laarr[$k] == "ReGen") {
             echo ' ReGen ';
         } else {
             echo "  <b>{$cnt}:</b> ";
             if (preg_match('/@FILE:(.+?)@/', $laarr[$k], $match)) {
                 $url = getasidfileurl($match[1]);
                 echo "<a href=\"{$url}\" target=\"_new\">" . basename($match[1]) . "</a>";
             } else {
                 if (strpos($laarr[$k], '$f$')) {
                     if (strpos($laarr[$k], '&')) {
                         //is multipart q
                         $laparr = explode('&', $laarr[$k]);
                         foreach ($laparr as $lk => $v) {
                             if (strpos($v, '$f$')) {
                                 $tmp = explode('$f$', $v);
                                 $laparr[$lk] = $tmp[0];
                             }
                         }
                         $laarr[$k] = implode('&', $laparr);
                     } else {
                         $tmp = explode('$f$', $laarr[$k]);
Exemple #2
0
function makeanswerbox($anstype, $qn, $la, $options, $multi, $colorbox = '')
{
    global $myrights, $useeqnhelper, $showtips, $imasroot;
    $out = '';
    $tip = '';
    $sa = '';
    $preview = '';
    $la = str_replace('"', '&quot;', $la);
    if ($anstype == "number") {
        if (isset($options['ansprompt'])) {
            if (is_array($options['ansprompt'])) {
                $ansprompt = $options['ansprompt'][$qn];
            } else {
                $ansprompt = $options['ansprompt'];
            }
        }
        if (isset($options['answerboxsize'])) {
            if (is_array($options['answerboxsize'])) {
                $sz = $options['answerboxsize'][$qn];
            } else {
                $sz = $options['answerboxsize'];
            }
        }
        if (isset($options['answerformat'])) {
            if (is_array($options['answerformat'])) {
                $answerformat = $options['answerformat'][$qn];
            } else {
                $answerformat = $options['answerformat'];
            }
        }
        if (isset($options['answer'])) {
            if (is_array($options['answer'])) {
                $answer = $options['answer'][$qn];
            } else {
                $answer = $options['answer'];
            }
        }
        if (isset($options['reqdecimals'])) {
            if (is_array($options['reqdecimals'])) {
                $reqdecimals = $options['reqdecimals'][$qn];
            } else {
                $reqdecimals = $options['reqdecimals'];
            }
        }
        if (isset($options['reqsigfigs'])) {
            if (is_array($options['reqsigfigs'])) {
                $reqsigfigs = $options['reqsigfigs'][$qn];
            } else {
                $reqsigfigs = $options['reqsigfigs'];
            }
        }
        if (isset($options['displayformat'])) {
            if (is_array($options['displayformat'])) {
                $displayformat = $options['displayformat'][$qn];
            } else {
                $displayformat = $options['displayformat'];
            }
        } else {
            $displayformat = '';
        }
        if (isset($options['scoremethod'])) {
            if (is_array($options['scoremethod'])) {
                $scoremethod = $options['scoremethod'][$qn];
            } else {
                $scoremethod = $options['scoremethod'];
            }
        }
        if (!isset($sz)) {
            $sz = 20;
        }
        if (isset($ansprompt)) {
            $out .= "<label for=\"qn{$qn}\">{$ansprompt}</label>";
        }
        if ($multi > 0) {
            $qn = $multi * 1000 + $qn;
            $qstr = $multi - 1 . '-' . $qn;
        } else {
            $qstr = $qn;
        }
        if ($displayformat == "point") {
            $leftb = "(";
            $rightb = ")";
        } else {
            if ($displayformat == "vector") {
                $leftb = "&lt;";
                $rightb = "&gt;";
            } else {
                $leftb = '';
                $rightb = '';
            }
        }
        if ($answerformat == 'list' || $answerformat == 'exactlist' || $answerformat == 'orderedlist') {
            $tip = _('Enter your answer as a list of whole or decimal numbers separated with commas: Examples: -4, 3, 2.5') . "<br/>";
            $shorttip = _('Enter a list of whole or decimal numbers');
        } else {
            $tip = _('Enter your answer as a whole or decimal number.  Examples: 3, -4, 5.5') . "<br/>";
            $shorttip = _('Enter a whole or decimal number');
        }
        $tip .= _('Enter DNE for Does Not Exist, oo for Infinity');
        if (isset($reqdecimals)) {
            $tip .= "<br/>" . sprintf(_('Your answer should be accurate to %d decimal places.'), $reqdecimals);
            $shorttip .= sprintf(_(", accurate to %d decimal places"), $reqdecimals);
        }
        if (isset($reqsigfigs)) {
            if ($reqsigfigs[0] == '=') {
                $reqsigfigs = substr($reqsigfigs, 1);
                $answer = prettysigfig($answer, $reqsigfigs);
                $tip .= "<br/>" . sprintf(_('Your answer should have exactly %d significant figures.'), $reqsigfigs);
                $shorttip .= sprintf(_(', with exactly %d significant figures'), $reqsigfigs);
            } else {
                if ($answer != 0) {
                    $v = -1 * floor(-log10(abs($answer)) - 1.0E-12) - $reqsigfigs;
                }
                if ($answer != 0 && $v < 0 && strlen($answer) - strpos($answer, '.') - 1 + $v < 0) {
                    $answer = prettysigfig($answer, $reqsigfigs);
                }
                $tip .= "<br/>" . sprintf(_('Your answer should have at least %d significant figures.'), $reqsigfigs);
                $shorttip .= sprintf(_(', with at least %d significant figures'), $reqsigfigs);
            }
        }
        $out .= "{$leftb}<input ";
        $addlclass = '';
        if ($displayformat == 'alignright') {
            $out .= 'style="text-align: right;" ';
        } else {
            if ($displayformat == 'hidden') {
                $out .= 'style="position: absolute; visibility: hidden; left: -5000px;" ';
            } else {
                if ($displayformat == 'debit') {
                    $out .= 'onkeyup="editdebit(this)" style="text-align: right;" ';
                } else {
                    if ($displayformat == 'credit') {
                        $out .= 'onkeyup="editcredit(this)" style="text-align: right;" ';
                        $addlclass = ' creditbox';
                    }
                }
            }
        }
        /*if ($showtips==2) { //eqntips: work in progress
        			if ($multi==0) {
        				$qnref = "$qn-0";
        			} else {
        				$qnref = ($multi-1).'-'.($qn%1000);
        			}
        			$out .= "onfocus=\"showehdd('qn$qn','$shorttip','$qnref')\" onblur=\"hideeh()\" ";
        		}*/
        if ($showtips == 2) {
            //eqntips: work in progress
            if ($multi == 0) {
                $qnref = "{$qn}-0";
            } else {
                $qnref = $multi - 1 . '-' . $qn % 1000;
            }
            if ($useeqnhelper && $useeqnhelper > 2 && !(isset($scoremethod) && $scoremethod == 'acct')) {
                $out .= "onfocus=\"showeebasicdd('qn{$qn}',0);showehdd('qn{$qn}','{$shorttip}','{$qnref}');\" onblur=\"hideebasice();hideebasicedd();hideeh();\" onclick=\"reshrinkeh('qn{$qn}')\" ";
            } else {
                $out .= "onfocus=\"showehdd('qn{$qn}','{$shorttip}','{$qnref}')\" onblur=\"hideeh()\" onclick=\"reshrinkeh('qn{$qn}')\" ";
            }
        } else {
            if ($useeqnhelper && !(isset($scoremethod) && $scoremethod == 'acct')) {
                $out .= "onfocus=\"showeebasicdd('qn{$qn}',0)\" onblur=\"hideebasice();hideebasicedd();\" ";
            }
        }
        $out .= "class=\"text {$colorbox}{$addlclass}\" type=\"text\"  size=\"{$sz}\" name=qn{$qn} id=qn{$qn} value=\"{$la}\" autocomplete=\"off\" />{$rightb}";
        $out .= getcolormark($colorbox);
        if ($displayformat == 'hidden') {
            $out .= '<script type="text/javascript">imasprevans[' . $qstr . '] = "' . $la . '";</script>';
        }
        if (isset($answer)) {
            if ($answerformat == 'parenneg' && $answer < 0) {
                $sa = '(' . -1 * $answer . ')';
            } else {
                $sa = $answer;
            }
        }
    } else {
        if ($anstype == "choices") {
            if (isset($options['displayformat'])) {
                if (is_array($options['displayformat'])) {
                    $displayformat = $options['displayformat'][$qn];
                } else {
                    $displayformat = $options['displayformat'];
                }
            } else {
                $displayformat = "vert";
            }
            if (isset($options['answer'])) {
                if (is_array($options['answer'])) {
                    $answer = $options['answer'][$qn];
                } else {
                    $answer = $options['answer'];
                }
            }
            if (is_array($options['questions'][$qn])) {
                $questions = $options['questions'][$qn];
            } else {
                $questions = $options['questions'];
            }
            if (isset($options['noshuffle'])) {
                if (is_array($options['noshuffle'])) {
                    $noshuffle = $options['noshuffle'][$qn];
                } else {
                    $noshuffle = $options['noshuffle'];
                }
            } else {
                $noshuffle = "none";
            }
            if (!is_array($questions)) {
                echo _('Eeek!  $questions is not defined or needs to be an array');
                return false;
            }
            if ($multi > 0) {
                $qn = $multi * 1000 + $qn;
            }
            if ($noshuffle == "last") {
                $randkeys = array_rand(array_slice($questions, 0, count($questions) - 1), count($questions) - 1);
                shuffle($randkeys);
                array_push($randkeys, count($questions) - 1);
            } else {
                if ($noshuffle == "all") {
                    $randkeys = array_keys($questions);
                } else {
                    $randkeys = array_rand($questions, count($questions));
                    shuffle($randkeys);
                }
            }
            $_SESSION['choicemap'][$qn] = $randkeys;
            if (isset($GLOBALS['capturechoices'])) {
                if (!isset($GLOBALS['choicesdata'])) {
                    $GLOBALS['choicesdata'] = array();
                }
                $GLOBALS['choicesdata'][$qn] = array($anstype, $questions);
            }
            //trim out unshuffled showans
            $la = explode('$!$', $la);
            $la = $la[0];
            if (isset($GLOBALS['lastanspretty'])) {
                //generate nice display version of
                if ($multi > 0) {
                    $laarr = explode('##', $GLOBALS['lastanspretty'][$multi - 1]);
                    foreach ($laarr as $k => $v) {
                        if ($v == 'ReGen') {
                            continue;
                        }
                        $laparts = explode('&', $v);
                        $laparts[$qn % 1000] = str_replace(array('##', '&'), '', $questions[$randkeys[$laparts[$qn % 1000]]]);
                        $laarr[$k] = implode('&', $laparts);
                    }
                    $GLOBALS['lastanspretty'][$multi - 1] = implode('##', $laarr);
                } else {
                    $laarr = explode('##', $GLOBALS['lastanspretty'][$qn]);
                    foreach ($laarr as $k => $v) {
                        if ($v == 'ReGen') {
                            continue;
                        }
                        $laarr[$k] = str_replace(array('##', '&'), '', $questions[$randkeys[$v]]);
                    }
                    $GLOBALS['lastanspretty'][$qn] = implode('##', $laarr);
                }
            }
            if ($displayformat == 'column') {
                $displayformat = '2column';
            }
            if (substr($displayformat, 1) == 'column') {
                $ncol = $displayformat[0];
                $itempercol = ceil(count($randkeys) / $ncol);
                $displayformat = 'column';
            }
            if ($displayformat == 'inline') {
                if ($colorbox != '') {
                    $style .= ' class="' . $colorbox . '" ';
                } else {
                    $style = '';
                }
                $out .= "<span {$style} id=\"qnwrap{$qn}\">";
            } else {
                if ($displayformat != 'select') {
                    if ($colorbox != '') {
                        $style .= ' class="' . $colorbox . ' clearfix" ';
                    } else {
                        $style = ' class="clearfix" ';
                    }
                    $out .= "<div {$style} id=\"qnwrap{$qn}\" style=\"display:block\">";
                }
            }
            if ($displayformat == "select") {
                $msg = '?';
                foreach ($questions as $qv) {
                    if (strlen($qv) > 2 && !($qv[0] == '&' && $qv[strlen($qv) - 1] == ';')) {
                        $msg = _('Select an answer');
                        break;
                    }
                }
                if ($colorbox != '') {
                    $style .= ' class="' . $colorbox . '" ';
                } else {
                    $style = '';
                }
                $out = "<select name=\"qn{$qn}\" id=\"qn{$qn}\" {$style}><option value=\"NA\">{$msg}</option>\n";
            } else {
                if ($displayformat == "horiz") {
                } else {
                    if ($displayformat == "inline") {
                    } else {
                        if ($displayformat == 'column') {
                        } else {
                            $out .= "<ul class=nomark>";
                        }
                    }
                }
            }
            for ($i = 0; $i < count($randkeys); $i++) {
                if ($displayformat == "horiz") {
                    $out .= "<div class=choice ><label for=\"qn{$qn}-{$i}\">{$questions[$randkeys[$i]]}</label><br/><input type=radio id=\"qn{$qn}-{$i}\" name=qn{$qn} value={$i} ";
                    if ($la != '' && $la == $i) {
                        $out .= "CHECKED";
                    }
                    $out .= " /></div>\n";
                } else {
                    if ($displayformat == "select") {
                        $out .= "<option value={$i} ";
                        if ($la != '' && $la != 'NA' && $la == $i) {
                            $out .= "selected=1";
                        }
                        $out .= ">{$questions[$randkeys[$i]]}</option>\n";
                    } else {
                        if ($displayformat == "inline") {
                            $out .= "<input type=radio name=qn{$qn} value={$i} id=\"qn{$qn}-{$i}\" ";
                            if ($la != '' && $la == $i) {
                                $out .= "CHECKED";
                            }
                            $out .= " /><label for=\"qn{$qn}-{$i}\">{$questions[$randkeys[$i]]}</label>";
                        } else {
                            if ($displayformat == 'column') {
                                if ($i % $itempercol == 0) {
                                    if ($i > 0) {
                                        $out .= '</ul></div>';
                                    }
                                    $out .= '<div class="match"><ul class=nomark>';
                                }
                                $out .= "<li><input type=radio name=qn{$qn} value={$i} id=\"qn{$qn}-{$i}\" ";
                                if ($la != '' && $la == $i) {
                                    $out .= "CHECKED";
                                }
                                $out .= " /><label for=\"qn{$qn}-{$i}\">{$questions[$randkeys[$i]]}<label></li> \n";
                            } else {
                                $out .= "<li><input class=\"unind\" type=radio name=qn{$qn} value={$i} id=\"qn{$qn}-{$i}\" ";
                                if ($la != '' && $la == $i) {
                                    $out .= "CHECKED";
                                }
                                $out .= " /><label for=\"qn{$qn}-{$i}\">{$questions[$randkeys[$i]]}<label></li> \n";
                            }
                        }
                    }
                }
            }
            if ($displayformat == "horiz") {
                //$out .= "<div class=spacer>&nbsp;</div>\n";
            } else {
                if ($displayformat == "select") {
                    $out .= "</select>\n";
                } else {
                    if ($displayformat == 'column') {
                        $out .= "</ul></div>";
                        //<div class=spacer>&nbsp;</div>\n";
                    } else {
                        if ($displayformat == "inline") {
                        } else {
                            $out .= "</ul>\n";
                        }
                    }
                }
            }
            $out .= getcolormark($colorbox);
            if ($displayformat == 'inline') {
                $out .= "</span>";
            } else {
                if ($displayformat != 'select') {
                    $out .= "</div>";
                }
            }
            $tip = _('Select the best answer');
            if (isset($answer)) {
                $anss = explode(' or ', $answer);
                $sapt = array();
                foreach ($anss as $v) {
                    $sapt[] = $questions[intval($v)];
                }
                $sa = implode(' or ', $sapt);
                //$questions[$answer];
            }
        } else {
            if ($anstype == "multans") {
                if (is_array($options['questions'][$qn])) {
                    $questions = $options['questions'][$qn];
                } else {
                    $questions = $options['questions'];
                }
                if (isset($options['answers'])) {
                    if (is_array($options['answers'])) {
                        $answers = $options['answers'][$qn];
                    } else {
                        $answers = $options['answers'];
                    }
                } else {
                    if (isset($options['answer'])) {
                        if (is_array($options['answer'])) {
                            $answers = $options['answer'][$qn];
                        } else {
                            $answers = $options['answer'];
                        }
                    }
                }
                if (isset($options['noshuffle'])) {
                    if (is_array($options['noshuffle'])) {
                        $noshuffle = $options['noshuffle'][$qn];
                    } else {
                        $noshuffle = $options['noshuffle'];
                    }
                }
                if (isset($options['displayformat'])) {
                    if (is_array($options['displayformat'])) {
                        $displayformat = $options['displayformat'][$qn];
                    } else {
                        $displayformat = $options['displayformat'];
                    }
                }
                if (!is_array($questions)) {
                    echo _('Eeek!  $questions is not defined or needs to be an array');
                    return false;
                }
                if ($multi > 0) {
                    $qn = $multi * 1000 + $qn;
                }
                //trim out unshuffled showans
                $la = explode('$!$', $la);
                $la = $la[0];
                if ($noshuffle == "last") {
                    $randkeys = array_rand(array_slice($questions, 0, count($questions) - 1), count($questions) - 1);
                    shuffle($randkeys);
                    array_push($randkeys, count($questions) - 1);
                } else {
                    if ($noshuffle == "all") {
                        $randkeys = array_keys($questions);
                    } else {
                        $randkeys = array_rand($questions, count($questions));
                        shuffle($randkeys);
                    }
                }
                $_SESSION['choicemap'][$qn] = $randkeys;
                if (isset($GLOBALS['capturechoices'])) {
                    if (!isset($GLOBALS['choicesdata'])) {
                        $GLOBALS['choicesdata'] = array();
                    }
                    $GLOBALS['choicesdata'][$qn] = array($anstype, $questions);
                }
                $labits = explode('|', $la);
                if ($displayformat == 'column') {
                    $displayformat = '2column';
                }
                if (substr($displayformat, 1) == 'column') {
                    $ncol = $displayformat[0];
                    $itempercol = ceil(count($randkeys) / $ncol);
                    $displayformat = 'column';
                }
                if ($displayformat == 'inline') {
                    if ($colorbox != '') {
                        $style .= ' class="' . $colorbox . '" ';
                    } else {
                        $style = '';
                    }
                    $out .= "<span {$style} id=\"qnwrap{$qn}\">";
                } else {
                    if ($colorbox != '') {
                        $style .= ' class="' . $colorbox . ' clearfix" ';
                    } else {
                        $style = ' class="clearfix" ';
                    }
                    $out .= "<div {$style} id=\"qnwrap{$qn}\" style=\"display:block\">";
                }
                if ($displayformat == "horiz") {
                } else {
                    if ($displayformat == "inline") {
                    } else {
                        if ($displayformat == 'column') {
                        } else {
                            $out .= "<ul class=nomark>";
                        }
                    }
                }
                for ($i = 0; $i < count($randkeys); $i++) {
                    if ($displayformat == "horiz") {
                        $out .= "<div class=choice><label for=\"qn{$qn}-{$i}\">{$questions[$randkeys[$i]]}</label><br/>";
                        $out .= "<input type=checkbox name=\"qn{$qn}" . "[{$i}]\" value={$i} id=\"qn{$qn}-{$i}\" ";
                        if (isset($labits[$i]) && $labits[$i] != '' && $labits[$i] == $i) {
                            $out .= "CHECKED";
                        }
                        $out .= " /></div> \n";
                    } else {
                        if ($displayformat == "inline") {
                            $out .= "<input type=checkbox name=\"qn{$qn}" . "[{$i}]\" value={$i} id=\"qn{$qn}-{$i}\" ";
                            if (isset($labits[$i]) && $labits[$i] != '' && $labits[$i] == $i) {
                                $out .= "CHECKED";
                            }
                            $out .= " /><label for=\"qn{$qn}-{$i}\">{$questions[$randkeys[$i]]}</label> ";
                        } else {
                            if ($displayformat == 'column') {
                                if ($i % $itempercol == 0) {
                                    if ($i > 0) {
                                        $out .= '</ul></div>';
                                    }
                                    $out .= '<div class="match"><ul class=nomark>';
                                }
                                $out .= "<li><input type=checkbox name=\"qn{$qn}" . "[{$i}]\" value={$i} id=\"qn{$qn}-{$i}\" ";
                                if (isset($labits[$i]) && $labits[$i] != '' && $labits[$i] == $i) {
                                    $out .= "CHECKED";
                                }
                                $out .= " /><label for=\"qn{$qn}-{$i}\">{$questions[$randkeys[$i]]}</label></li> \n";
                            } else {
                                $out .= "<li><input class=\"unind\" type=checkbox name=\"qn{$qn}" . "[{$i}]\" value={$i} id=\"qn{$qn}-{$i}\" ";
                                if (isset($labits[$i]) && $labits[$i] != '' && $labits[$i] == $i) {
                                    $out .= "CHECKED";
                                }
                                $out .= " /><label for=\"qn{$qn}-{$i}\">{$questions[$randkeys[$i]]}</label></li> \n";
                            }
                        }
                    }
                }
                if ($displayformat == "horiz") {
                    //$out .= "<div class=spacer>&nbsp;</div>\n";
                } else {
                    if ($displayformat == "inline") {
                    } else {
                        if ($displayformat == 'column') {
                            $out .= "</ul></div>";
                            //<div class=spacer>&nbsp;</div>\n";
                        } else {
                            $out .= "</ul>\n";
                        }
                    }
                }
                $out .= getcolormark($colorbox);
                if ($displayformat == 'inline') {
                    $out .= "</span>";
                } else {
                    $out .= "</div>";
                }
                $tip = _('Select all correct answers');
                if (isset($answers)) {
                    $akeys = explode(',', $answers);
                    foreach ($akeys as $akey) {
                        $sa .= '<br/>' . $questions[$akey];
                    }
                }
            } else {
                if ($anstype == "matching") {
                    if (is_array($options['questions'][$qn])) {
                        $questions = $options['questions'][$qn];
                    } else {
                        $questions = $options['questions'];
                    }
                    if (isset($options['answers'])) {
                        if (is_array($options['answers'][$qn])) {
                            $answers = $options['answers'][$qn];
                        } else {
                            $answers = $options['answers'];
                        }
                    } else {
                        if (isset($options['answer'])) {
                            if (is_array($options['answer'][$qn])) {
                                $answers = $options['answer'][$qn];
                            } else {
                                $answers = $options['answer'];
                            }
                        }
                    }
                    if (isset($options['questiontitle'])) {
                        if (is_array($options['questiontitle'])) {
                            $questiontitle = $options['questiontitle'][$qn];
                        } else {
                            $questiontitle = $options['questiontitle'];
                        }
                    }
                    if (isset($options['answertitle'])) {
                        if (is_array($options['answertitle'])) {
                            $answertitle = $options['answertitle'][$qn];
                        } else {
                            $answertitle = $options['answertitle'];
                        }
                    }
                    if (isset($options['matchlist'])) {
                        if (is_array($options['matchlist'])) {
                            $matchlist = $options['matchlist'][$qn];
                        } else {
                            $matchlist = $options['matchlist'];
                        }
                    }
                    if (isset($options['noshuffle'])) {
                        if (is_array($options['noshuffle'])) {
                            $noshuffle = $options['noshuffle'][$qn];
                        } else {
                            $noshuffle = $options['noshuffle'];
                        }
                    }
                    if (isset($options['displayformat'])) {
                        if (is_array($options['displayformat'])) {
                            $displayformat = $options['displayformat'][$qn];
                        } else {
                            $displayformat = $options['displayformat'];
                        }
                    }
                    if ($multi > 0) {
                        $qn = $multi * 1000 + $qn;
                    }
                    //trim out unshuffled showans
                    $la = explode('$!$', $la);
                    $la = $la[0];
                    if (!is_array($questions) || !is_array($answers)) {
                        echo _('Eeek!  $questions or $answers is not defined or needs to be an array');
                        return false;
                    }
                    if (isset($matchlist)) {
                        $matchlist = explode(',', $matchlist);
                    }
                    if ($noshuffle == "questions" || $noshuffle == 'all') {
                        $randqkeys = array_keys($questions);
                    } else {
                        $randqkeys = array_rand($questions, count($questions));
                        shuffle($randqkeys);
                    }
                    if ($noshuffle == "answers" || $noshuffle == 'all') {
                        $randakeys = array_keys($answers);
                    } else {
                        $randakeys = array_rand($answers, count($answers));
                        shuffle($randakeys);
                    }
                    if (isset($GLOBALS['capturechoices'])) {
                        if (!isset($GLOBALS['choicesdata'])) {
                            $GLOBALS['choicesdata'] = array();
                        }
                        $GLOBALS['choicesdata'][$qn] = array($anstype, $randakeys);
                    }
                    $ncol = 1;
                    if (substr($displayformat, 1) == 'columnselect') {
                        $ncol = $displayformat[0];
                        $itempercol = ceil(count($randqkeys) / $ncol);
                        $displayformat = 'select';
                    }
                    if (substr($displayformat, 0, 8) == "limwidth") {
                        $divstyle = 'style="max-width:' . substr($displayformat, 8) . 'px;"';
                    } else {
                        $divstyle = '';
                    }
                    if ($colorbox != '') {
                        $out .= '<div class="' . $colorbox . '" id="qnwrap' . $qn . '" style="display:block">';
                    }
                    $out .= "<div class=\"match\" {$divstyle}>\n";
                    $out .= "<p class=\"centered\">{$questiontitle}</p>\n";
                    $out .= "<ul class=\"nomark\">\n";
                    $las = explode("|", $la);
                    $letters = array_slice(explode(',', 'a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z'), 0, count($answers));
                    for ($i = 0; $i < count($randqkeys); $i++) {
                        //$out .= "<li><input class=\"text\" type=\"text\"  size=3 name=\"qn$qn-$i\" value=\"{$las[$i]}\" /> {$questions[$randqkeys[$i]]}</li>\n";
                        if ($ncol > 1) {
                            if ($i > 0 && $i % $itempercol == 0) {
                                $out .= '</ul></div><div class="match"><ul class=nomark>';
                            }
                        }
                        if (strpos($questions[$randqkeys[$i]], ' ') === false || strlen($questions[$randqkeys[$i]]) < 12) {
                            $out .= '<li class="nowrap">';
                        } else {
                            $out .= '<li>';
                        }
                        $out .= "<select name=\"qn{$qn}-{$i}\">";
                        $out .= '<option value="-" ';
                        if ($las[$i] == '-' || $las[$i] == '') {
                            $out .= 'selected="1"';
                        }
                        $out .= '>-</option>';
                        if ($displayformat == "select") {
                            for ($j = 0; $j < count($randakeys); $j++) {
                                $out .= "<option value=\"" . $letters[$j] . "\" ";
                                if ($las[$i] == $letters[$j]) {
                                    $out .= 'selected="1"';
                                }
                                $out .= ">{$answers[$randakeys[$j]]}</option>\n";
                            }
                        } else {
                            foreach ($letters as $v) {
                                $out .= "<option value=\"{$v}\" ";
                                if ($las[$i] == $v) {
                                    $out .= 'selected="1"';
                                }
                                $out .= ">{$v}</option>";
                            }
                        }
                        $out .= "</select>&nbsp;{$questions[$randqkeys[$i]]}</li>\n";
                    }
                    $out .= "</ul>\n";
                    $out .= "</div>";
                    if (!isset($displayformat) || $displayformat != "select") {
                        $out .= "<div class=\"match\" {$divstyle}>\n";
                        $out .= "<p class=centered>{$answertitle}</p>\n";
                        $out .= "<ol class=lalpha>\n";
                        for ($i = 0; $i < count($randakeys); $i++) {
                            $out .= "<li>{$answers[$randakeys[$i]]}</li>\n";
                        }
                        $out .= "</ol>";
                        $out .= "</div>";
                    }
                    $out .= "<input type=hidden name=\"qn{$qn}\" value=\"done\" /><div class=spacer>&nbsp;</div>";
                    $out .= getcolormark($colorbox);
                    if ($colorbox != '') {
                        $out .= '</div>';
                    }
                    //$tip = "In each box provided, type the letter (a, b, c, etc.) of the matching answer in the right-hand column";
                    if ($displayformat == "select") {
                        $tip = _('In each pull-down, select the item that matches with the displayed item');
                    } else {
                        $tip = _('In each pull-down on the left, select the letter (a, b, c, etc.) of the matching answer in the right-hand column');
                    }
                    for ($i = 0; $i < count($randqkeys); $i++) {
                        if (isset($matchlist)) {
                            $akey = array_search($matchlist[$randqkeys[$i]], $randakeys);
                        } else {
                            $akey = array_search($randqkeys[$i], $randakeys);
                        }
                        if ($displayformat == "select") {
                            $sa .= $answers[$randakeys[$akey]] . ' ';
                        } else {
                            $sa .= chr($akey + 97) . " ";
                        }
                    }
                } else {
                    if ($anstype == "calculated") {
                        if (isset($options['ansprompt'])) {
                            if (is_array($options['ansprompt'])) {
                                $ansprompt = $options['ansprompt'][$qn];
                            } else {
                                $ansprompt = $options['ansprompt'];
                            }
                        }
                        if (isset($options['answerboxsize'])) {
                            if (is_array($options['answerboxsize'])) {
                                $sz = $options['answerboxsize'][$qn];
                            } else {
                                $sz = $options['answerboxsize'];
                            }
                        }
                        if (isset($options['hidepreview'])) {
                            if (is_array($options['hidepreview'])) {
                                $hidepreview = $options['hidepreview'][$qn];
                            } else {
                                $hidepreview = $options['hidepreview'];
                            }
                        }
                        if (isset($options['answerformat'])) {
                            if (is_array($options['answerformat'])) {
                                $answerformat = $options['answerformat'][$qn];
                            } else {
                                $answerformat = $options['answerformat'];
                            }
                        }
                        if (isset($options['answer'])) {
                            if (is_array($options['answer'])) {
                                $answer = $options['answer'][$qn];
                            } else {
                                $answer = $options['answer'];
                            }
                        }
                        if (isset($options['reqdecimals'])) {
                            if (is_array($options['reqdecimals'])) {
                                $reqdecimals = $options['reqdecimals'][$qn];
                            } else {
                                $reqdecimals = $options['reqdecimals'];
                            }
                        }
                        if (isset($options['displayformat'])) {
                            if (is_array($options['displayformat'])) {
                                $displayformat = $options['displayformat'][$qn];
                            } else {
                                $displayformat = $options['displayformat'];
                            }
                        }
                        if (!isset($sz)) {
                            $sz = 20;
                        }
                        if ($multi > 0) {
                            $qn = $multi * 1000 + $qn;
                        }
                        $lap = explode('$f$', $la);
                        if (isset($lap[1]) && (!isset($GLOBALS['noformatfeedback']) || $GLOBALS['noformatfeedback'] == false)) {
                            $rightanswrongformat = true;
                            if ($colorbox == 'ansred') {
                                $colorbox = 'ansorg';
                            }
                        }
                        $la = $lap[0];
                        $la = explode('$#$', $la);
                        $la = $la[0];
                        if (!isset($answerformat)) {
                            $answerformat = '';
                        }
                        if (isset($ansprompt)) {
                            $out .= "<label for=\"tc{$qn}\">{$ansprompt}</label>";
                        }
                        if ($displayformat == "point") {
                            $leftb = "(";
                            $rightb = ")";
                        } else {
                            if ($displayformat == "vector") {
                                $leftb = "&lt;";
                                $rightb = "&gt;";
                            } else {
                                $leftb = '';
                                $rightb = '';
                            }
                        }
                        $out .= "{$leftb}<input class=\"text {$colorbox}\" type=\"text\"  size=\"{$sz}\" name=tc{$qn} id=tc{$qn} value=\"{$la}\" autocomplete=\"off\" ";
                        $ansformats = explode(',', $answerformat);
                        if (in_array('list', $ansformats) || in_array('exactlist', $ansformats) || in_array('orderedlist', $ansformats)) {
                            $tip = _('Enter your answer as a list of values separated by commas: Example: -4, 3, 2') . "<br/>";
                            $eword = _('each value');
                        } else {
                            $tip = '';
                            $eword = _('your answer');
                        }
                        list($longtip, $shorttip) = formathint($eword, $ansformats, 'calculated', in_array('list', $ansformats) || in_array('exactlist', $ansformats) || in_array('orderedlist', $ansformats), 1);
                        $tip .= $longtip;
                        if ($showtips == 2) {
                            //eqntips: work in progress
                            if ($multi == 0) {
                                $qnref = "{$qn}-0";
                            } else {
                                $qnref = $multi - 1 . '-' . $qn % 1000;
                            }
                            if ($useeqnhelper) {
                                $out .= "onfocus=\"showeedd('tc{$qn}',{$useeqnhelper});showehdd('tc{$qn}','{$shorttip}','{$qnref}');\" onblur=\"hideee();hideeedd();hideeh();\" onclick=\"reshrinkeh('tc{$qn}')\" ";
                            } else {
                                $out .= "onfocus=\"showehdd('tc{$qn}','{$shorttip}','{$qnref}')\" onblur=\"hideeh()\" onclick=\"reshrinkeh('tc{$qn}')\" ";
                            }
                        } else {
                            if ($useeqnhelper) {
                                $out .= "onfocus=\"showeedd('tc{$qn}',{$useeqnhelper})\" onblur=\"hideee();hideeedd();\" ";
                            }
                        }
                        $out .= "/>{$rightb}";
                        $out .= getcolormark($colorbox);
                        if (!isset($GLOBALS['nocolormark']) && isset($rightanswrongformat) && (!isset($GLOBALS['noformatfeedback']) || $GLOBALS['noformatfeedback'] == false)) {
                            if (in_array('list', $ansformats) || in_array('exactlist', $ansformats) || in_array('orderedlist', $ansformats)) {
                                $out .= ' ' . formhoverover('<span style="color:#f60;font-size:80%">(Format)</span>', 'One or more of your answers is equivalent to the correct answer, but is not simplified or is in the wrong format');
                            } else {
                                $out .= ' ' . formhoverover('<span style="color:#f60;font-size:80%">(Format)</span>', 'Your answer is equivalent to the correct answer, but is not simplified or is in the wrong format');
                            }
                        }
                        $out .= "<input type=\"hidden\" id=\"qn{$qn}\" name=\"qn{$qn}\" />\n";
                        if (!isset($hidepreview)) {
                            $preview .= "<input type=button class=btn value=\"" . _('Preview') . "\" onclick=\"calculate('tc{$qn}','p{$qn}','{$answerformat}')\" /> &nbsp;\n";
                        }
                        $preview .= "{$leftb}<span id=p{$qn}></span>{$rightb} ";
                        $out .= "<script type=\"text/javascript\">calctoproc[{$qn}] = 1; calcformat[{$qn}] = '{$answerformat}';</script>\n";
                        if (isset($answer)) {
                            if (!is_numeric($answer)) {
                                $sa = '`' . $answer . '`';
                            } else {
                                if (in_array('mixednumber', $ansformats) || in_array("sloppymixednumber", $ansformats) || in_array("mixednumberorimproper", $ansformats)) {
                                    $sa = '`' . decimaltofraction($answer, "mixednumber") . '`';
                                } else {
                                    if (in_array("fraction", $ansformats) || in_array("reducedfraction", $ansformats)) {
                                        $sa = '`' . decimaltofraction($answer) . '`';
                                    } else {
                                        if (in_array("scinot", $ansformats)) {
                                            $sa = '`' . makescinot($answer, -1, '*') . '`';
                                        } else {
                                            $sa = $answer;
                                        }
                                    }
                                }
                            }
                        }
                    } else {
                        if ($anstype == "matrix") {
                            if (isset($options['ansprompt'])) {
                                if (is_array($options['ansprompt'])) {
                                    $ansprompt = $options['ansprompt'][$qn];
                                } else {
                                    $ansprompt = $options['ansprompt'];
                                }
                            }
                            if (isset($options['answersize'])) {
                                if (is_array($options['answersize'])) {
                                    $answersize = $options['answersize'][$qn];
                                } else {
                                    $answersize = $options['answersize'];
                                }
                            }
                            if (isset($options['answerboxsize'])) {
                                if (is_array($options['answerboxsize'])) {
                                    $sz = $options['answerboxsize'][$qn];
                                } else {
                                    $sz = $options['answerboxsize'];
                                }
                            }
                            if (isset($options['answer'])) {
                                if (is_array($options['answer'])) {
                                    $answer = $options['answer'][$qn];
                                } else {
                                    $answer = $options['answer'];
                                }
                            }
                            if (!isset($sz)) {
                                $sz = 20;
                            }
                            if ($multi > 0) {
                                $qn = $multi * 1000 + $qn;
                            }
                            if (isset($ansprompt)) {
                                $out .= $ansprompt;
                            }
                            if (isset($answersize)) {
                                if ($colorbox == '') {
                                    $out .= '<table id="qnwrap' . $qn . '">';
                                } else {
                                    $out .= '<table class="' . $colorbox . '" id="qnwrap' . $qn . '">';
                                }
                                $out .= '<tr><td class="matrixleft">&nbsp;</td><td>';
                                $answersize = explode(",", $answersize);
                                $out .= "<table>";
                                $count = 0;
                                $las = explode("|", $la);
                                for ($row = 0; $row < $answersize[0]; $row++) {
                                    $out .= "<tr>";
                                    for ($col = 0; $col < $answersize[1]; $col++) {
                                        $out .= "<td><input class=\"text\" type=\"text\"  size=3 name=\"qn{$qn}-{$count}\" value=\"{$las[$count]}\"  autocomplete=\"off\" /></td>\n";
                                        $count++;
                                    }
                                    $out .= "</tr>";
                                }
                                $out .= "</table>\n";
                                $out .= getcolormark($colorbox);
                                $out .= '</td><td class="matrixright">&nbsp;</td></tr></table>';
                                $tip = _('Enter each element of the matrix as  number (like 5, -3, 2.2)');
                            } else {
                                $out .= "<input class=\"text {$colorbox}\" type=\"text\"  size=\"{$sz}\" name=qn{$qn} id=qn{$qn} value=\"{$la}\" autocomplete=\"off\" />\n";
                                $out .= getcolormark($colorbox);
                                $out .= "<input type=button class=btn value=\"" . _('Preview') . "\" onclick=\"AMmathpreview('qn{$qn}','p{$qn}')\" /> &nbsp;\n";
                                $out .= "<span id=p{$qn}></span> ";
                                $tip = _('Enter your answer as a matrix filled with numbers, like ((2,3,4),(3,4,5))');
                            }
                            if (isset($answer)) {
                                $sa = '`' . $answer . '`';
                            }
                        } else {
                            if ($anstype == "calcmatrix") {
                                if (isset($options['ansprompt'])) {
                                    if (is_array($options['ansprompt'])) {
                                        $ansprompt = $options['ansprompt'][$qn];
                                    } else {
                                        $ansprompt = $options['ansprompt'];
                                    }
                                }
                                if (isset($options['answersize'])) {
                                    if (is_array($options['answersize'])) {
                                        $answersize = $options['answersize'][$qn];
                                    } else {
                                        $answersize = $options['answersize'];
                                    }
                                }
                                if (isset($options['answerboxsize'])) {
                                    if (is_array($options['answerboxsize'])) {
                                        $sz = $options['answerboxsize'][$qn];
                                    } else {
                                        $sz = $options['answerboxsize'];
                                    }
                                }
                                if (isset($options['hidepreview'])) {
                                    if (is_array($options['hidepreview'])) {
                                        $hidepreview = $options['hidepreview'][$qn];
                                    } else {
                                        $hidepreview = $options['hidepreview'];
                                    }
                                }
                                if (isset($options['answer'])) {
                                    if (is_array($options['answer'])) {
                                        $answer = $options['answer'][$qn];
                                    } else {
                                        $answer = $options['answer'];
                                    }
                                }
                                if (isset($options['answerformat'])) {
                                    if (is_array($options['answerformat'])) {
                                        $answerformat = $options['answerformat'][$qn];
                                    } else {
                                        $answerformat = $options['answerformat'];
                                    }
                                }
                                if (!isset($answerformat)) {
                                    $answerformat = '';
                                }
                                $ansformats = explode(',', $answerformat);
                                if (!isset($sz)) {
                                    $sz = 20;
                                }
                                $la = explode('$#$', $la);
                                $la = $la[0];
                                if ($multi > 0) {
                                    $qn = $multi * 1000 + $qn;
                                }
                                if (isset($ansprompt)) {
                                    $out .= $ansprompt;
                                }
                                if (isset($answersize)) {
                                    $answersize = explode(",", $answersize);
                                    if ($colorbox == '') {
                                        $out .= '<table id="qnwrap' . $qn . '">';
                                    } else {
                                        $out .= '<table class="' . $colorbox . '" id="qnwrap' . $qn . '">';
                                    }
                                    $out .= '<tr><td class="matrixleft">&nbsp;</td><td>';
                                    $out .= "<table>";
                                    $count = 0;
                                    $las = explode("|", $la);
                                    for ($row = 0; $row < $answersize[0]; $row++) {
                                        $out .= "<tr>";
                                        for ($col = 0; $col < $answersize[1]; $col++) {
                                            $out .= "<td><input class=\"text\" type=\"text\"  size=3 name=\"qn{$qn}-{$count}\" id=\"qn{$qn}-{$count}\" value=\"{$las[$count]}\" autocomplete=\"off\" /></td>\n";
                                            $count++;
                                        }
                                        $out .= "</tr>";
                                    }
                                    $out .= "</table>\n";
                                    $out .= '</td><td class="matrixright">&nbsp;</td></tr></table>';
                                    $out .= getcolormark($colorbox);
                                    if (!isset($hidepreview)) {
                                        $preview .= "<input type=button class=btn value=\"" . _('Preview') . "\" onclick=\"matrixcalc('qn{$qn}','p{$qn}',{$answersize[0]},{$answersize[1]})\" /> &nbsp;\n";
                                    }
                                    $preview .= "<span id=p{$qn}></span>\n";
                                    $out .= "<script type=\"text/javascript\">matcalctoproc[{$qn}] = 1; matsize[{$qn}]='{$answersize[0]},{$answersize[1]}';</script>\n";
                                    $tip .= formathint(_('each element of the matrix'), $ansformats, 'calcmatrix');
                                    //$tip = "Enter each element of the matrix as  number (like 5, -3, 2.2) or as a calculation (like 5/3, 2^3, 5+4)";
                                } else {
                                    $out .= "<input class=\"text {$colorbox}\" type=\"text\"  size=\"{$sz}\" name=tc{$qn} id=tc{$qn} value=\"{$la}\" autocomplete=\"off\" />\n";
                                    $out .= getcolormark($colorbox);
                                    $out .= "<input type=button value=\"" . _('Preview') . "\" onclick=\"matrixcalc('tc{$qn}','p{$qn}')\" /> &nbsp;\n";
                                    $out .= "<span id=p{$qn}></span> \n";
                                    $out .= "<script type=\"text/javascript\">matcalctoproc[{$qn}] = 1;</script>\n";
                                    $tip = _('Enter your answer as a matrix, like ((2,3,4),(1,4,5))');
                                    $tip .= '<br/>' . formathint(_('each element of the matrix'), $ansformats, 'calcmatrix');
                                }
                                $out .= "<input type=\"hidden\" id=\"qn{$qn}\" name=\"qn{$qn}\" />";
                                if (isset($answer)) {
                                    $sa = '`' . $answer . '`';
                                }
                            } else {
                                if ($anstype == "numfunc") {
                                    if (isset($options['ansprompt'])) {
                                        if (is_array($options['ansprompt'])) {
                                            $ansprompt = $options['ansprompt'][$qn];
                                        } else {
                                            $ansprompt = $options['ansprompt'];
                                        }
                                    }
                                    if (isset($options['variables'])) {
                                        if (is_array($options['variables'])) {
                                            $variables = $options['variables'][$qn];
                                        } else {
                                            $variables = $options['variables'];
                                        }
                                    }
                                    if (isset($options['domain'])) {
                                        if (is_array($options['domain'])) {
                                            $domain = $options['domain'][$qn];
                                        } else {
                                            $domain = $options['domain'];
                                        }
                                    }
                                    if (isset($options['answerboxsize'])) {
                                        if (is_array($options['answerboxsize'])) {
                                            $sz = $options['answerboxsize'][$qn];
                                        } else {
                                            $sz = $options['answerboxsize'];
                                        }
                                    }
                                    if (isset($options['hidepreview'])) {
                                        if (is_array($options['hidepreview'])) {
                                            $hidepreview = $options['hidepreview'][$qn];
                                        } else {
                                            $hidepreview = $options['hidepreview'];
                                        }
                                    }
                                    if (isset($options['answerformat'])) {
                                        if (is_array($options['answerformat'])) {
                                            $answerformat = $options['answerformat'][$qn];
                                        } else {
                                            $answerformat = $options['answerformat'];
                                        }
                                    }
                                    if (isset($options['answer'])) {
                                        if (is_array($options['answer'])) {
                                            $answer = $options['answer'][$qn];
                                        } else {
                                            $answer = $options['answer'];
                                        }
                                    }
                                    if (!isset($sz)) {
                                        $sz = 20;
                                    }
                                    if ($multi > 0) {
                                        $qn = $multi * 1000 + $qn;
                                    }
                                    $lap = explode('$f$', $la);
                                    if (isset($lap[1]) && (!isset($GLOBALS['noformatfeedback']) || $GLOBALS['noformatfeedback'] == false)) {
                                        $rightanswrongformat = true;
                                        if ($colorbox == 'ansred') {
                                            $colorbox = 'ansorg';
                                        }
                                    }
                                    $la = $lap[0];
                                    if (isset($ansprompt)) {
                                        $out .= "<label for=\"tc{$qn}\">{$ansprompt}</label>";
                                    }
                                    if ($answerformat == "equation") {
                                        $shorttip = _('Enter an algebraic equation');
                                    } else {
                                        $shorttip = _('Enter an algebraic expression');
                                    }
                                    $out .= "<input class=\"text {$colorbox}\" type=\"text\"  size=\"{$sz}\" name=\"tc{$qn}\" id=\"tc{$qn}\" value=\"{$la}\" autocomplete=\"off\" ";
                                    if ($showtips == 2) {
                                        //eqntips: work in progress
                                        if ($multi == 0) {
                                            $qnref = "{$qn}-0";
                                        } else {
                                            $qnref = $multi - 1 . '-' . $qn % 1000;
                                        }
                                        if ($useeqnhelper) {
                                            $out .= "onfocus=\"showeedd('tc{$qn}',{$useeqnhelper});showehdd('tc{$qn}','{$shorttip}','{$qnref}');\" onblur=\"hideee();hideeedd();hideeh();\" onclick=\"reshrinkeh('tc{$qn}')\" ";
                                        } else {
                                            $out .= "onfocus=\"showehdd('tc{$qn}','{$shorttip}','{$qnref}')\" onblur=\"hideeh()\" onclick=\"reshrinkeh('tc{$qn}')\" ";
                                        }
                                    } else {
                                        if ($useeqnhelper) {
                                            $out .= "onfocus=\"showeedd('tc{$qn}',{$useeqnhelper})\" onblur=\"hideee();hideeedd();\" ";
                                        }
                                    }
                                    $out .= "/>\n";
                                    $out .= "<input type=\"hidden\" id=\"qn{$qn}\" name=\"qn{$qn}\" />";
                                    $out .= "<input type=\"hidden\" id=\"qn{$qn}-vals\" name=\"qn{$qn}-vals\" />";
                                    $out .= getcolormark($colorbox);
                                    if (!isset($GLOBALS['nocolormark']) && isset($rightanswrongformat) && (!isset($GLOBALS['noformatfeedback']) || $GLOBALS['noformatfeedback'] == false)) {
                                        $out .= ' ' . formhoverover('<span style="color:#f60;font-size:80%">(Format)</span>', 'Your answer is equivalent to the correct answer, but is not simplified or is in the wrong format');
                                    }
                                    if (!isset($hidepreview)) {
                                        $preview .= "<input type=button class=btn value=\"" . _('Preview') . "\" onclick=\"AMpreview('tc{$qn}','p{$qn}')\" /> &nbsp;\n";
                                    }
                                    $preview .= "<span id=p{$qn}></span>\n";
                                    if (!isset($variables)) {
                                        $variables = "x";
                                    }
                                    $variables = explode(",", $variables);
                                    $ovar = array();
                                    $ofunc = array();
                                    for ($i = 0; $i < count($variables); $i++) {
                                        $variables[$i] = trim($variables[$i]);
                                        if (strpos($variables[$i], '(') === false) {
                                            $ovar[] = $variables[$i];
                                        } else {
                                            $ofunc[] = substr($variables[$i], 0, strpos($variables[$i], '('));
                                            $variables[$i] = substr($variables[$i], 0, strpos($variables[$i], '('));
                                        }
                                    }
                                    if (($v = array_search('E', $variables)) !== false) {
                                        $variables[$v] = 'varE';
                                    }
                                    if (count($ovar) == 0) {
                                        $ovar[] = "x";
                                    }
                                    $vlist = implode("|", $variables);
                                    $flist = implode('|', $ofunc);
                                    $out .= "<script type=\"text/javascript\">functoproc[{$qn}] = 1; vlist[{$qn}]=\"{$vlist}\"; flist[{$qn}]=\"{$flist}\";</script>\n";
                                    if (isset($domain)) {
                                        $fromto = explode(",", $domain);
                                    } else {
                                        $fromto[0] = -10;
                                        $fromto[1] = 10;
                                    }
                                    for ($i = 0; $i < 20; $i++) {
                                        for ($j = 0; $j < count($variables); $j++) {
                                            if (isset($fromto[2]) && $fromto[2] == "integers") {
                                                $tp[$j] = rand($fromto[0], $fromto[1]);
                                            } else {
                                                if (isset($fromto[2 * $j + 1])) {
                                                    $tp[$j] = $fromto[2 * $j] + ($fromto[2 * $j + 1] - $fromto[2 * $j]) * rand(0, 499) / 500.0 + 0.001;
                                                } else {
                                                    $tp[$j] = $fromto[0] + ($fromto[1] - $fromto[0]) * rand(0, 499) / 500.0 + 0.001;
                                                }
                                            }
                                        }
                                        $pts[$i] = implode("~", $tp);
                                    }
                                    $points = implode(",", $pts);
                                    $out .= "<script type=\"text/javascript\">pts[{$qn}]=\"{$points}\";</script>\n";
                                    if ($answerformat == "equation") {
                                        $out .= "<script type=\"text/javascript\">iseqn[{$qn}] = 1;</script>\n";
                                        $tip = _('Enter your answer as an equation.  Example: y=3x^2+1, 2+x+y=3') . "\n<br/>" . _('Be sure your variables match those in the question');
                                    } else {
                                        $tip = _('Enter your answer as an expression.  Example: 3x^2+1, x/5, (a+b)/c') . "\n<br/>" . _('Be sure your variables match those in the question');
                                    }
                                    if (isset($answer)) {
                                        $sa = makeprettydisp($answer);
                                    }
                                } else {
                                    if ($anstype == "ntuple") {
                                        if (isset($options['answerboxsize'])) {
                                            if (is_array($options['answerboxsize'])) {
                                                $sz = $options['answerboxsize'][$qn];
                                            } else {
                                                $sz = $options['answerboxsize'];
                                            }
                                        }
                                        if (isset($options['answerformat'])) {
                                            if (is_array($options['answerformat'])) {
                                                $answerformat = $options['answerformat'][$qn];
                                            } else {
                                                $answerformat = $options['answerformat'];
                                            }
                                        }
                                        if (isset($options['answer'])) {
                                            if (is_array($options['answer'])) {
                                                $answer = $options['answer'][$qn];
                                            } else {
                                                $answer = $options['answer'];
                                            }
                                        }
                                        if (isset($options['displayformat'])) {
                                            if (is_array($options['displayformat'])) {
                                                $displayformat = $options['displayformat'][$qn];
                                            } else {
                                                $displayformat = $options['displayformat'];
                                            }
                                        }
                                        if (!isset($sz)) {
                                            $sz = 20;
                                        }
                                        if ($multi > 0) {
                                            $qn = $multi * 1000 + $qn;
                                        }
                                        if ($displayformat == 'point') {
                                            $tip = _('Enter your answer as a point.  Example: (2,5.5)') . "<br/>";
                                            $shorttip = _('Enter a point');
                                        } else {
                                            if ($displayformat == 'pointlist') {
                                                $tip = _('Enter your answer a list of points separated with commas.  Example: (1,2), (3.5,5)') . "<br/>";
                                                $shorttip = _('Enter a list of points');
                                            } else {
                                                if ($displayformat == 'vector') {
                                                    $tip = _('Enter your answer as a vector.  Example: <2,5.5>') . "<br/>";
                                                    $shorttip = _('Enter a vector');
                                                } else {
                                                    if ($displayformat == 'vectorlist') {
                                                        $tip = _('Enter your answer a list of vectors separated with commas.  Example: <1,2>, <3.5,5>') . "<br/>";
                                                        $shorttip = _('Enter a list of vectors');
                                                    } else {
                                                        if ($displayformat == 'set') {
                                                            $tip = _('Enter your answer as a set of numbers.  Example: {1,2,3}') . "<br/>";
                                                            $shorttip = _('Enter a set');
                                                        } else {
                                                            if ($displayformat == 'list') {
                                                                $tip = _('Enter your answer as a list of n-tuples of numbers separated with commas: Example: (1,2),(3.5,4)') . "<br/>";
                                                                $shorttip = _('Enter a list of n-tuples');
                                                            } else {
                                                                $tip = _('Enter your answer as an n-tuple of numbers.  Example: (2,5.5)') . "<br/>";
                                                                $shorttip = _('Enter an n-tuple');
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                        $tip .= _('Enter DNE for Does Not Exist');
                                        $out .= "<input class=\"text {$colorbox}\" type=\"text\"  size=\"{$sz}\" name=qn{$qn} id=qn{$qn} value=\"{$la}\" autocomplete=\"off\" ";
                                        if ($showtips == 2) {
                                            //eqntips: work in progress
                                            if ($multi == 0) {
                                                $qnref = "{$qn}-0";
                                            } else {
                                                $qnref = $multi - 1 . '-' . $qn % 1000;
                                            }
                                            $out .= "onfocus=\"showehdd('qn{$qn}','{$shorttip}','{$qnref}')\" onblur=\"hideeh()\" ";
                                        }
                                        $out .= '/>';
                                        $out .= getcolormark($colorbox);
                                        if (isset($answer)) {
                                            $sa = $answer;
                                        }
                                    } else {
                                        if ($anstype == "calcntuple") {
                                            if (isset($options['answerboxsize'])) {
                                                if (is_array($options['answerboxsize'])) {
                                                    $sz = $options['answerboxsize'][$qn];
                                                } else {
                                                    $sz = $options['answerboxsize'];
                                                }
                                            }
                                            if (isset($options['answerformat'])) {
                                                if (is_array($options['answerformat'])) {
                                                    $answerformat = $options['answerformat'][$qn];
                                                } else {
                                                    $answerformat = $options['answerformat'];
                                                }
                                            }
                                            if (isset($options['answer'])) {
                                                if (is_array($options['answer'])) {
                                                    $answer = $options['answer'][$qn];
                                                } else {
                                                    $answer = $options['answer'];
                                                }
                                            }
                                            if (isset($options['displayformat'])) {
                                                if (is_array($options['displayformat'])) {
                                                    $displayformat = $options['displayformat'][$qn];
                                                } else {
                                                    $displayformat = $options['displayformat'];
                                                }
                                            }
                                            if (isset($options['answerformat'])) {
                                                if (is_array($options['answerformat'])) {
                                                    $answerformat = $options['answerformat'][$qn];
                                                } else {
                                                    $answerformat = $options['answerformat'];
                                                }
                                            }
                                            if (!isset($answerformat)) {
                                                $answerformat = '';
                                            }
                                            $ansformats = explode(',', $answerformat);
                                            $la = explode('$#$', $la);
                                            $la = $la[0];
                                            if (!isset($sz)) {
                                                $sz = 20;
                                            }
                                            if ($multi > 0) {
                                                $qn = $multi * 1000 + $qn;
                                            }
                                            if ($displayformat == 'point') {
                                                $tip = _('Enter your answer as a point.  Example: (2,5.5)') . "<br/>";
                                                $shorttip = _('Enter a point');
                                            } else {
                                                if ($displayformat == 'pointlist') {
                                                    $tip = _('Enter your answer a list of points separated with commas.  Example: (1,2), (3.5,5)') . "<br/>";
                                                    $shorttip = _('Enter a list of points');
                                                } else {
                                                    if ($displayformat == 'vector') {
                                                        $tip = _('Enter your answer as a vector.  Example: <2,5.5>') . "<br/>";
                                                        $shorttip = _('Enter a vector');
                                                    } else {
                                                        if ($displayformat == 'vectorlist') {
                                                            $tip = _('Enter your answer a list of vectors separated with commas.  Example: <1,2>, <3.5,5>') . "<br/>";
                                                            $shorttip = _('Enter a list of vectors');
                                                        } else {
                                                            if ($displayformat == 'set') {
                                                                $tip = _('Enter your answer as a set of numbers.  Example: {1,2,3}') . "<br/>";
                                                                $shorttip = _('Enter a set');
                                                            } else {
                                                                if ($displayformat == 'list') {
                                                                    $tip = _('Enter your answer as a list of n-tuples of numbers separated with commas: Example: (1,2),(3.5,4)') . "<br/>";
                                                                    $shorttip = _('Enter a list of n-tuples');
                                                                } else {
                                                                    $tip = _('Enter your answer as an n-tuple of numbers.  Example: (2,5.5)') . "<br/>";
                                                                    $shorttip = _('Enter an n-tuple');
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                            $tip .= formathint('each value', $ansformats, 'calcntuple');
                                            $out .= "<input class=\"text {$colorbox}\" type=\"text\"  size=\"{$sz}\" name=tc{$qn} id=tc{$qn} value=\"{$la}\" autocomplete=\"off\" ";
                                            if ($showtips == 2) {
                                                //eqntips: work in progress
                                                if ($multi == 0) {
                                                    $qnref = "{$qn}-0";
                                                } else {
                                                    $qnref = $multi - 1 . '-' . $qn % 1000;
                                                }
                                                if ($useeqnhelper) {
                                                    $out .= "onfocus=\"showeedd('tc{$qn}',{$useeqnhelper});showehdd('tc{$qn}','{$shorttip}','{$qnref}');\" onblur=\"hideee();hideeedd();hideeh();\" onclick=\"reshrinkeh('tc{$qn}')\" ";
                                                } else {
                                                    $out .= "onfocus=\"showehdd('tc{$qn}','{$shorttip}','{$qnref}')\" onblur=\"hideeh()\" onclick=\"reshrinkeh('tc{$qn}')\" ";
                                                }
                                            } else {
                                                if ($useeqnhelper) {
                                                    $out .= "onfocus=\"showeedd('tc{$qn}',{$useeqnhelper})\" onblur=\"hideee();hideeedd();\" ";
                                                }
                                            }
                                            $out .= "/>";
                                            $out .= "<input type=\"hidden\" id=\"qn{$qn}\" name=\"qn{$qn}\" />";
                                            $out .= getcolormark($colorbox);
                                            if (!isset($hidepreview)) {
                                                $preview .= "<input type=button class=btn value=\"" . _('Preview') . "\" onclick=\"ntuplecalc('tc{$qn}','p{$qn}','{$answerformat}')\" /> &nbsp;\n";
                                            }
                                            $preview .= "<span id=p{$qn}></span> ";
                                            $out .= "<script type=\"text/javascript\">ntupletoproc[{$qn}] = 1; calcformat[{$qn}] = '{$answerformat}';</script>\n";
                                            //$tip .= "Enter DNE for Does Not Exist";
                                            if (isset($answer)) {
                                                $sa = makeprettydisp($answer);
                                            }
                                        } else {
                                            if ($anstype == "complex") {
                                                if (isset($options['answerboxsize'])) {
                                                    if (is_array($options['answerboxsize'])) {
                                                        $sz = $options['answerboxsize'][$qn];
                                                    } else {
                                                        $sz = $options['answerboxsize'];
                                                    }
                                                }
                                                if (isset($options['answerformat'])) {
                                                    if (is_array($options['answerformat'])) {
                                                        $answerformat = $options['answerformat'][$qn];
                                                    } else {
                                                        $answerformat = $options['answerformat'];
                                                    }
                                                }
                                                if (isset($options['answer'])) {
                                                    if (is_array($options['answer'])) {
                                                        $answer = $options['answer'][$qn];
                                                    } else {
                                                        $answer = $options['answer'];
                                                    }
                                                }
                                                if (isset($options['displayformat'])) {
                                                    if (is_array($options['displayformat'])) {
                                                        $displayformat = $options['displayformat'][$qn];
                                                    } else {
                                                        $displayformat = $options['displayformat'];
                                                    }
                                                }
                                                if (!isset($sz)) {
                                                    $sz = 20;
                                                }
                                                if ($multi > 0) {
                                                    $qn = $multi * 1000 + $qn;
                                                }
                                                if ($answerformat == "list") {
                                                    $tip = _('Enter your answer as a list of complex numbers in a+bi form separated with commas.  Example: 2+5.5i,-3-4i') . "<br/>";
                                                    $shorttip = _('Enter a list of complex numbers');
                                                } else {
                                                    $tip = _('Enter your answer as a complex number in a+bi form.  Example: 2+5.5i') . "<br/>";
                                                    $shorttip = _('Enter a complex number');
                                                }
                                                $tip .= _('Enter DNE for Does Not Exist');
                                                $out .= "<input class=\"text {$colorbox}\" type=\"text\"  size=\"{$sz}\" name=qn{$qn} id=qn{$qn} value=\"{$la}\" autocomplete=\"off\"  ";
                                                if ($showtips == 2) {
                                                    //eqntips: work in progress
                                                    if ($multi == 0) {
                                                        $qnref = "{$qn}-0";
                                                    } else {
                                                        $qnref = $multi - 1 . '-' . $qn % 1000;
                                                    }
                                                    $out .= "onfocus=\"showehdd('qn{$qn}','{$shorttip}','{$qnref}')\" onblur=\"hideeh()\" ";
                                                }
                                                $out .= '/>';
                                                $out .= getcolormark($colorbox);
                                                if (isset($answer)) {
                                                    $sa = makepretty($answer);
                                                }
                                            } else {
                                                if ($anstype == "calccomplex") {
                                                    if (isset($options['answerboxsize'])) {
                                                        if (is_array($options['answerboxsize'])) {
                                                            $sz = $options['answerboxsize'][$qn];
                                                        } else {
                                                            $sz = $options['answerboxsize'];
                                                        }
                                                    }
                                                    if (isset($options['answerformat'])) {
                                                        if (is_array($options['answerformat'])) {
                                                            $answerformat = $options['answerformat'][$qn];
                                                        } else {
                                                            $answerformat = $options['answerformat'];
                                                        }
                                                    }
                                                    if (isset($options['answer'])) {
                                                        if (is_array($options['answer'])) {
                                                            $answer = $options['answer'][$qn];
                                                        } else {
                                                            $answer = $options['answer'];
                                                        }
                                                    }
                                                    if (isset($options['displayformat'])) {
                                                        if (is_array($options['displayformat'])) {
                                                            $displayformat = $options['displayformat'][$qn];
                                                        } else {
                                                            $displayformat = $options['displayformat'];
                                                        }
                                                    }
                                                    if (isset($options['answerformat'])) {
                                                        if (is_array($options['answerformat'])) {
                                                            $answerformat = $options['answerformat'][$qn];
                                                        } else {
                                                            $answerformat = $options['answerformat'];
                                                        }
                                                    }
                                                    if (!isset($answerformat)) {
                                                        $answerformat = '';
                                                    }
                                                    $ansformats = explode(',', $answerformat);
                                                    if (!isset($sz)) {
                                                        $sz = 20;
                                                    }
                                                    if ($multi > 0) {
                                                        $qn = $multi * 1000 + $qn;
                                                    }
                                                    if (!isset($answerformat)) {
                                                        $answerformat = '';
                                                    }
                                                    $ansformats = explode(',', $answerformat);
                                                    if (in_array('list', $ansformats)) {
                                                        $tip = _('Enter your answer as a list of complex numbers in a+bi form separated with commas.  Example: 2+5i,-3-4i') . "<br/>";
                                                        $shorttip = _('Enter a list of complex numbers');
                                                    } else {
                                                        $tip = _('Enter your answer as a complex number in a+bi form.  Example: 2+5i') . "<br/>";
                                                        $shorttip = _('Enter a complex number');
                                                    }
                                                    $tip .= formathint('each value', $ansformats, 'calccomplex');
                                                    $out .= "<input class=\"text {$colorbox}\" type=\"text\"  size=\"{$sz}\" name=tc{$qn} id=tc{$qn} value=\"{$la}\" autocomplete=\"off\"  ";
                                                    if ($showtips == 2) {
                                                        //eqntips: work in progress
                                                        if ($multi == 0) {
                                                            $qnref = "{$qn}-0";
                                                        } else {
                                                            $qnref = $multi - 1 . '-' . $qn % 1000;
                                                        }
                                                        if ($useeqnhelper) {
                                                            $out .= "onfocus=\"showeedd('tc{$qn}',{$useeqnhelper});showehdd('tc{$qn}','{$shorttip}','{$qnref}');\" onblur=\"hideee();hideeedd();hideeh();\" onclick=\"reshrinkeh('tc{$qn}')\" ";
                                                        } else {
                                                            $out .= "onfocus=\"showehdd('tc{$qn}','{$shorttip}','{$qnref}')\" onblur=\"hideeh()\" onclick=\"reshrinkeh('tc{$qn}')\" ";
                                                        }
                                                    } else {
                                                        if ($useeqnhelper) {
                                                            $out .= "onfocus=\"showeedd('tc{$qn}',{$useeqnhelper})\" onblur=\"hideee();hideeedd();\" ";
                                                        }
                                                    }
                                                    $out .= "/>";
                                                    $out .= "<input type=\"hidden\" id=\"qn{$qn}\" name=\"qn{$qn}\" />";
                                                    $out .= getcolormark($colorbox);
                                                    if (!isset($hidepreview)) {
                                                        $preview .= "<input type=button class=btn value=\"" . _('Preview') . "\" onclick=\"complexcalc('tc{$qn}','p{$qn}','{$answerformat}')\" /> &nbsp;\n";
                                                    }
                                                    $preview .= "<span id=p{$qn}></span> ";
                                                    $out .= "<script type=\"text/javascript\">complextoproc[{$qn}] = 1;</script>\n";
                                                    //$tip .= "Enter DNE for Does Not Exist";
                                                    if (isset($answer)) {
                                                        $sa = makeprettydisp($answer);
                                                    }
                                                } else {
                                                    if ($anstype == "string") {
                                                        if (isset($options['ansprompt'])) {
                                                            if (is_array($options['ansprompt'])) {
                                                                $ansprompt = $options['ansprompt'][$qn];
                                                            } else {
                                                                $ansprompt = $options['ansprompt'];
                                                            }
                                                        }
                                                        if (isset($options['answerboxsize'])) {
                                                            if (is_array($options['answerboxsize'])) {
                                                                $sz = $options['answerboxsize'][$qn];
                                                            } else {
                                                                $sz = $options['answerboxsize'];
                                                            }
                                                        }
                                                        if (isset($options['answer'])) {
                                                            if (is_array($options['answer'])) {
                                                                $answer = $options['answer'][$qn];
                                                            } else {
                                                                $answer = $options['answer'];
                                                            }
                                                        }
                                                        if (isset($options['displayformat'])) {
                                                            if (is_array($options['displayformat'])) {
                                                                $displayformat = $options['displayformat'][$qn];
                                                            } else {
                                                                $displayformat = $options['displayformat'];
                                                            }
                                                        }
                                                        if (isset($options['answerformat'])) {
                                                            if (is_array($options['answerformat'])) {
                                                                $answerformat = $options['answerformat'][$qn];
                                                            } else {
                                                                $answerformat = $options['answerformat'];
                                                            }
                                                        }
                                                        if (is_array($options['questions'][$qn])) {
                                                            $questions = $options['questions'][$qn];
                                                        } else {
                                                            $questions = $options['questions'];
                                                        }
                                                        if (!isset($answerformat)) {
                                                            $answerformat = '';
                                                        }
                                                        if ($multi > 0) {
                                                            $qn = $multi * 1000 + $qn;
                                                        }
                                                        if (!isset($sz)) {
                                                            $sz = 20;
                                                        }
                                                        if (isset($ansprompt)) {
                                                            $out .= "<label for=\"qn{$qn}\">{$ansprompt}</label>";
                                                        }
                                                        if ($answerformat == 'list') {
                                                            $tip = _('Enter your answer as a list of text separated by commas.  Example:  dog, cat, rabbit.') . "<br/>";
                                                            $shorttip = _('Enter a list of text');
                                                        } else {
                                                            $tip .= _('Enter your answer as letters.  Examples: A B C, linear, a cat');
                                                            $shorttip = _('Enter text');
                                                        }
                                                        if ($displayformat == 'select') {
                                                            $out .= "<select name=\"qn{$qn}\" id=\"qn{$qn}\" style=\"margin-right:20px\" class=\"{$colorbox}\"><option value=\"\"> </option>";
                                                            foreach ($questions as $i => $v) {
                                                                $out .= '<option value="' . htmlentities($v) . '"';
                                                                //This is a hack.  Need to figure a better way to deal with & in answers
                                                                if (str_replace('&', '', $v) == $la) {
                                                                    $out .= ' selected="selected"';
                                                                }
                                                                $out .= '>' . htmlentities($v) . '</option>';
                                                            }
                                                            $out .= '</select>';
                                                            $out .= getcolormark($colorbox);
                                                        } else {
                                                            $out .= "<input type=\"text\"  size=\"{$sz}\" name=\"qn{$qn}\" id=\"qn{$qn}\" value=\"{$la}\" autocomplete=\"off\"  ";
                                                            if ($showtips == 2) {
                                                                //eqntips: work in progress
                                                                if ($multi == 0) {
                                                                    $qnref = "{$qn}-0";
                                                                } else {
                                                                    $qnref = $multi - 1 . '-' . $qn % 1000;
                                                                }
                                                                if ($useeqnhelper && $displayformat == 'usepreview') {
                                                                    $out .= "onfocus=\"showeedd('qn{$qn}',{$useeqnhelper});showehdd('qn{$qn}','{$shorttip}','{$qnref}');\" onblur=\"hideee();hideeedd();hideeh();\" onclick=\"reshrinkeh('qn{$qn}')\" ";
                                                                } else {
                                                                    $out .= "onfocus=\"showehdd('qn{$qn}','{$shorttip}','{$qnref}')\" onblur=\"hideeh()\" onclick=\"reshrinkeh('qn{$qn}')\" ";
                                                                }
                                                            } else {
                                                                if ($useeqnhelper && $displayformat == 'usepreview') {
                                                                    $out .= "onfocus=\"showeedd('qn{$qn}',{$useeqnhelper})\" onblur=\"hideee();hideeedd();\" ";
                                                                }
                                                            }
                                                            $addlclass = '';
                                                            if ($displayformat == 'debit') {
                                                                $out .= 'onkeyup="editdebit(this)" style="text-align: right;" ';
                                                            } else {
                                                                if ($displayformat == 'credit') {
                                                                    $out .= 'onkeyup="editcredit(this)" style="text-align: right;" ';
                                                                    $addlclass = ' creditbox';
                                                                } else {
                                                                    if ($displayformat == 'alignright') {
                                                                        $out .= 'style="text-align: right;" ';
                                                                    }
                                                                }
                                                            }
                                                            $out .= "class=\"text {$colorbox}{$addlclass}\"";
                                                            $out .= '/>';
                                                            $out .= getcolormark($colorbox);
                                                            if ($displayformat == 'usepreview') {
                                                                $preview .= "<input type=button class=btn value=\"" . _('Preview') . "\" onclick=\"stringqpreview('qn{$qn}','p{$qn}','{$answerformat}')\" /> &nbsp;\n";
                                                                $preview .= "<span id=p{$qn}></span> ";
                                                            } else {
                                                                if ($displayformat == 'typeahead') {
                                                                    if (!is_array($questions)) {
                                                                        echo _('Eeek!  $questions is not defined or needs to be an array');
                                                                    } else {
                                                                        foreach ($questions as $i => $v) {
                                                                            $questions[$i] = htmlentities(trim($v));
                                                                        }
                                                                        $out .= '<script type="text/javascript">';
                                                                        $autosugglist = '["' . implode('","', $questions) . '"]';
                                                                        if (!isset($GLOBALS['autosuggestlists'])) {
                                                                            $GLOBALS['autosuggestlists'] = array();
                                                                        }
                                                                        if (($k = array_search($autosugglist, $GLOBALS['autosuggestlists'])) !== false) {
                                                                            $asvar = 'autosuggestlist' . $k;
                                                                        } else {
                                                                            $GLOBALS['autosuggestlists'][] = $autosugglist;
                                                                            $ascnt = count($GLOBALS['autosuggestlists']) - 1;
                                                                            $out .= 'var autosuggestlist' . $ascnt . ' = ' . $autosugglist . ';';
                                                                            $asvar = 'autosuggestlist' . $ascnt;
                                                                        }
                                                                        $out .= 'initstack.push(function(){ autosugg' . $qn . ' = new AutoSuggest(document.getElementById("qn' . $qn . '"),' . $asvar . ');});</script>';
                                                                    }
                                                                }
                                                            }
                                                        }
                                                        $sa .= $answer;
                                                    } else {
                                                        if ($anstype == "essay") {
                                                            if (isset($options['answerboxsize'])) {
                                                                if (is_array($options['answerboxsize'])) {
                                                                    $sz = $options['answerboxsize'][$qn];
                                                                } else {
                                                                    $sz = $options['answerboxsize'];
                                                                }
                                                            }
                                                            if (isset($options['displayformat'])) {
                                                                if (is_array($options['displayformat'])) {
                                                                    $displayformat = $options['displayformat'][$qn];
                                                                } else {
                                                                    $displayformat = $options['displayformat'];
                                                                }
                                                            }
                                                            if (isset($options['answer'])) {
                                                                if (is_array($options['answer'])) {
                                                                    $answer = $options['answer'][$qn];
                                                                } else {
                                                                    $answer = $options['answer'];
                                                                }
                                                            }
                                                            if ($multi > 0) {
                                                                $qn = $multi * 1000 + $qn;
                                                            }
                                                            if (!isset($sz)) {
                                                                $rows = 5;
                                                                $cols = 50;
                                                            } else {
                                                                if (strpos($sz, ',') > 0) {
                                                                    list($rows, $cols) = explode(',', $sz);
                                                                } else {
                                                                    $cols = 50;
                                                                    $rows = $sz;
                                                                }
                                                            }
                                                            if ($displayformat == 'editor') {
                                                                $rows += 5;
                                                            }
                                                            if ($GLOBALS['useeditor'] == 'review' || $GLOBALS['useeditor'] == 'reviewifneeded' && trim($la) == '') {
                                                                $la = str_replace('&quot;', '"', $la);
                                                                $la = preg_replace('/%(\\w+;)/', "&\$1", $la);
                                                                //$la = str_replace('nbsp;','&nbsp;',$la);
                                                                if ($displayformat != 'editor') {
                                                                    $la = preg_replace('/\\n/', '<br/>', $la);
                                                                }
                                                                if ($colorbox == '') {
                                                                    $out .= '<div class="intro" id="qnwrap' . $qn . '">';
                                                                } else {
                                                                    $out .= '<div class="intro ' . $colorbox . '" id="qnwrap' . $qn . '">';
                                                                }
                                                                if (isset($GLOBALS['questionscoreref'])) {
                                                                    if ($multi == 0) {
                                                                        $el = $GLOBALS['questionscoreref'][0];
                                                                        $sc = $GLOBALS['questionscoreref'][1];
                                                                    } else {
                                                                        $el = $GLOBALS['questionscoreref'][0] . '-' . $qn % 1000;
                                                                        $sc = $GLOBALS['questionscoreref'][1][$qn % 1000];
                                                                    }
                                                                    $out .= '<span style="float:right;">';
                                                                    $out .= '<img class="scoreicon" src="' . $imasroot . '/img/q_fullbox.gif" ';
                                                                    $out .= "onclick=\"quicksetscore('{$el}',{$sc})\" />";
                                                                    $out .= '<img class="scoreicon" src="' . $imasroot . '/img/q_halfbox.gif" ';
                                                                    $out .= "onclick=\"quicksetscore('{$el}',.5*{$sc})\" />";
                                                                    $out .= '<img class="scoreicon" src="' . $imasroot . '/img/q_emptybox.gif" ';
                                                                    $out .= "onclick=\"quicksetscore('{$el}',0)\" /></span>";
                                                                }
                                                                $out .= filter($la);
                                                                $out .= getcolormark($colorbox);
                                                                $out .= "</div>";
                                                            } else {
                                                                $la = stripslashes($la);
                                                                $la = preg_replace('/%(\\w+;)/', "&\$1", $la);
                                                                if ($displayformat == 'editor' && $GLOBALS['useeditor'] == 1) {
                                                                    $la = str_replace('&quot;', '"', $la);
                                                                    $la = htmlentities($la);
                                                                }
                                                                if ($rows < 2) {
                                                                    $out .= "<input type=\"text {$colorbox}\" size=\"{$cols}\" name=\"qn{$qn}\" id=\"qn{$qn}\" value=\"{$la}\" /> ";
                                                                    $out .= getcolormark($colorbox);
                                                                } else {
                                                                    if ($colorbox != '') {
                                                                        $out .= '<div class="' . $colorbox . '">';
                                                                    }
                                                                    $out .= "<textarea rows=\"{$rows}\" name=\"qn{$qn}\" id=\"qn{$qn}\" ";
                                                                    if ($displayformat == 'editor' && $GLOBALS['useeditor'] == 1) {
                                                                        $out .= "style=\"width:98%;\" class=\"mceEditor\" ";
                                                                    } else {
                                                                        $out .= "cols=\"{$cols}\" ";
                                                                    }
                                                                    $out .= ">{$la}</textarea>\n";
                                                                    $out .= getcolormark($colorbox);
                                                                    if ($colorbox != '') {
                                                                        $out .= '</div>';
                                                                    }
                                                                }
                                                                if ($displayformat == 'editor' && $GLOBALS['useeditor'] == 1) {
                                                                    //$out .= "<script type=\"text/javascript\">editornames[editornames.length] = \"qn$qn\";</script>";
                                                                }
                                                            }
                                                            $tip .= _('Enter your answer as text.  This question is not automatically graded.');
                                                            $sa .= $answer;
                                                        } else {
                                                            if ($anstype == 'interval') {
                                                                if (isset($options['ansprompt'])) {
                                                                    if (is_array($options['ansprompt'])) {
                                                                        $ansprompt = $options['ansprompt'][$qn];
                                                                    } else {
                                                                        $ansprompt = $options['ansprompt'];
                                                                    }
                                                                }
                                                                if (isset($options['answerboxsize'])) {
                                                                    if (is_array($options['answerboxsize'])) {
                                                                        $sz = $options['answerboxsize'][$qn];
                                                                    } else {
                                                                        $sz = $options['answerboxsize'];
                                                                    }
                                                                }
                                                                if (isset($options['answer'])) {
                                                                    if (is_array($options['answer'])) {
                                                                        $answer = $options['answer'][$qn];
                                                                    } else {
                                                                        $answer = $options['answer'];
                                                                    }
                                                                }
                                                                if (isset($options['reqdecimals'])) {
                                                                    if (is_array($options['reqdecimals'])) {
                                                                        $reqdecimals = $options['reqdecimals'][$qn];
                                                                    } else {
                                                                        $reqdecimals = $options['reqdecimals'];
                                                                    }
                                                                }
                                                                if (isset($options['answerformat'])) {
                                                                    if (is_array($options['answerformat'])) {
                                                                        $answerformat = $options['answerformat'][$qn];
                                                                    } else {
                                                                        $answerformat = $options['answerformat'];
                                                                    }
                                                                }
                                                                if (!isset($sz)) {
                                                                    $sz = 20;
                                                                }
                                                                if (isset($ansprompt)) {
                                                                    $out .= "<label for=\"qn{$qn}\">{$ansprompt}</label>";
                                                                }
                                                                if ($multi > 0) {
                                                                    $qn = $multi * 1000 + $qn;
                                                                }
                                                                if ($answerformat == 'normalcurve' && $GLOBALS['sessiondata']['graphdisp'] != 0) {
                                                                    $top = _('Enter your answer by selecting the shade type, and by clicking and dragging the sliders on the normal curve');
                                                                    $shorttip = _('Adjust the sliders');
                                                                } else {
                                                                    $tip = _('Enter your answer using interval notation.  Example: [2.1,5.6)') . " <br/>";
                                                                    $tip .= _('Use U for union to combine intervals.  Example: (-oo,2] U [4,oo)') . "<br/>";
                                                                    $tip .= _('Enter DNE for an empty set, oo for Infinity');
                                                                    if (isset($reqdecimals)) {
                                                                        $tip .= "<br/>" . sprintf(_('Your numbers should be accurate to %d decimal places.'), $reqdecimals);
                                                                    }
                                                                    $shorttip = _('Enter an interval using interval notation');
                                                                }
                                                                if ($answerformat == 'normalcurve' && $GLOBALS['sessiondata']['graphdisp'] != 0) {
                                                                    $out .= '<div style="background:#fff;padding:10px;">';
                                                                    $out .= '<p style="margin:0px";>Shade: <select id="shaderegions' . $qn . '" onchange="chgnormtype(this.id.substring(12));"><option value="1L">' . _('Left of a value') . '</option><option value="1R">' . _('Right of a value') . '</option>';
                                                                    $out .= '<option value="2B">' . _('Between two values') . '</option><option value="2O">' . _('2 regions') . '</option></select>. ' . _('Click and drag and arrows to adjust the values.');
                                                                    $out .= '<div style="position: relative; width: 500px; height:200px;padding:0px;">';
                                                                    //for future development of non-standard normal
                                                                    //for ($i=0;$i<9;$i++) {
                                                                    //	$out .= '<div style="position: absolute; left:'.(60*$i).'px; top:150px; height:20px; width:20px; background:#fff;z-index:2;text-align:center">'.($mu+($i-4)*$sig).'</div>';
                                                                    //}
                                                                    $out .= '<div style="position: absolute; left:0; top:0; height:200px; width:0px; background:#00f;" id="normleft' . $qn . '">&nbsp;</div>';
                                                                    $out .= '<div style="position: absolute; right:0; top:0; height:200px; width:0px; background:#00f;" id="normright' . $qn . '">&nbsp;</div>';
                                                                    $out .= '<img style="position: absolute; left:0; top:0;z-index:1;width:100%;max-width:100%" src="' . $imasroot . '/img/normalcurve.gif"/>';
                                                                    $out .= '<img style="position: absolute; top:142px;left:0px;cursor:pointer;z-index:3;" id="slid1' . $qn . '" src="' . $imasroot . '/img/uppointer.gif"/>';
                                                                    $out .= '<img style="position: absolute; top:142px;left:0px;cursor:pointer;z-index:3;" id="slid2' . $qn . '" src="' . $imasroot . '/img/uppointer.gif"/>';
                                                                    $out .= '<div style="position: absolute; top:170px;left:0px;z-index:3;" id="slid1txt' . $qn . '"></div>';
                                                                    $out .= '<div style="position: absolute; top:170px;left:0px;z-index:3;" id="slid2txt' . $qn . '"></div>';
                                                                    $out .= '</div></div>';
                                                                    $out .= '<script type="text/javascript">addnormslider(' . $qn . ');</script>';
                                                                } else {
                                                                    if ($answerformat == 'normalcurve') {
                                                                        $out .= _('Enter an interval corresponding to the region to be shaded');
                                                                    }
                                                                }
                                                                $out .= "<input class=\"text {$colorbox}\" type=\"text\"  size=\"{$sz}\" name=qn{$qn} id=qn{$qn} value=\"{$la}\" autocomplete=\"off\"  ";
                                                                if ($answerformat == 'normalcurve' && $GLOBALS['sessiondata']['graphdisp'] != 0) {
                                                                    $out .= 'style="position:absolute;visibility:hidden;" ';
                                                                }
                                                                /*if ($showtips==2) { //eqntips: work in progress
                                                                			if ($multi==0) {
                                                                				$qnref = "$qn-0";
                                                                			} else {
                                                                				$qnref = ($multi-1).'-'.($qn%1000);
                                                                			}
                                                                			$out .= "onfocus=\"showehdd('qn$qn','$shorttip','$qnref')\" onblur=\"hideeh()\" ";
                                                                		}*/
                                                                if ($showtips == 2) {
                                                                    //eqntips: work in progress
                                                                    if ($multi == 0) {
                                                                        $qnref = "{$qn}-0";
                                                                    } else {
                                                                        $qnref = $multi - 1 . '-' . $qn % 1000;
                                                                    }
                                                                    if ($useeqnhelper && $useeqnhelper > 2) {
                                                                        $out .= "onfocus=\"showeebasicdd('qn{$qn}',1);showehdd('qn{$qn}','{$shorttip}','{$qnref}');\" onblur=\"hideebasice();hideebasicedd();hideeh();\" onclick=\"reshrinkeh('qn{$qn}')\" ";
                                                                    } else {
                                                                        $out .= "onfocus=\"showehdd('qn{$qn}','{$shorttip}','{$qnref}')\" onblur=\"hideeh()\" onclick=\"reshrinkeh('qn{$qn}')\" ";
                                                                    }
                                                                } else {
                                                                    if ($useeqnhelper) {
                                                                        $out .= "onfocus=\"showeebasicdd('qn{$qn}',1)\" onblur=\"hideebasice();hideebasicedd();\" ";
                                                                    }
                                                                }
                                                                $out .= '/>';
                                                                $out .= getcolormark($colorbox);
                                                                if (isset($answer)) {
                                                                    if ($answerformat == 'normalcurve' && $GLOBALS['sessiondata']['graphdisp'] != 0) {
                                                                        $sa .= '<div style="position: relative; width: 500px; height:200px;padding:0px;background:#fff;">';
                                                                        if (preg_match('/\\(-oo,([\\-\\d\\.]+)\\)U\\(([\\-\\d\\.]+),oo\\)/', $answer, $matches)) {
                                                                            $sa .= '<div style="position: absolute; left:0; top:0; height:200px; width:' . (250 + 60 * $matches[1] + 1) . 'px; background:#00f;">&nbsp;</div>';
                                                                            $sa .= '<div style="position: absolute; right:0; top:0; height:200px; width:' . (250 - 60 * $matches[2]) . 'px; background:#00f;">&nbsp;</div>';
                                                                        } else {
                                                                            if (preg_match('/\\(-oo,([\\-\\d\\.]+)\\)/', $answer, $matches)) {
                                                                                $sa .= '<div style="position: absolute; left:0; top:0; height:200px; width:' . (250 + 60 * $matches[1] + 1) . 'px; background:#00f;">&nbsp;</div>';
                                                                            } else {
                                                                                if (preg_match('/\\(([\\-\\d\\.]+),oo\\)/', $answer, $matches)) {
                                                                                    $sa .= '<div style="position: absolute; right:0; top:0; height:200px; width:' . (250 - 60 * $matches[1]) . 'px; background:#00f;">&nbsp;</div>';
                                                                                } else {
                                                                                    if (preg_match('/\\(([\\-\\d\\.]+),([\\-\\d\\.]+)\\)/', $answer, $matches)) {
                                                                                        $sa .= '<div style="position: absolute; left:' . (250 + 60 * $matches[1]) . 'px; top:0; height:200px; width:' . (60 * ($matches[2] - $matches[1]) + 1) . 'px; background:#00f;">&nbsp;</div>';
                                                                                    }
                                                                                }
                                                                            }
                                                                        }
                                                                        $sa .= '<img style="position: absolute; left:0; top:0;z-index:1;width:100%;max-width:100%" src="' . $imasroot . '/img/normalcurve.gif"/>';
                                                                        $sa .= '</div>';
                                                                    } else {
                                                                        $sa = $answer;
                                                                    }
                                                                }
                                                            } else {
                                                                if ($anstype == 'calcinterval') {
                                                                    if (isset($options['ansprompt'])) {
                                                                        if (is_array($options['ansprompt'])) {
                                                                            $ansprompt = $options['ansprompt'][$qn];
                                                                        } else {
                                                                            $ansprompt = $options['ansprompt'];
                                                                        }
                                                                    }
                                                                    if (isset($options['answerboxsize'])) {
                                                                        if (is_array($options['answerboxsize'])) {
                                                                            $sz = $options['answerboxsize'][$qn];
                                                                        } else {
                                                                            $sz = $options['answerboxsize'];
                                                                        }
                                                                    }
                                                                    if (isset($options['hidepreview'])) {
                                                                        if (is_array($options['hidepreview'])) {
                                                                            $hidepreview = $options['hidepreview'][$qn];
                                                                        } else {
                                                                            $hidepreview = $options['hidepreview'];
                                                                        }
                                                                    }
                                                                    if (isset($options['answer'])) {
                                                                        if (is_array($options['answer'])) {
                                                                            $answer = $options['answer'][$qn];
                                                                        } else {
                                                                            $answer = $options['answer'];
                                                                        }
                                                                    }
                                                                    if (isset($options['reqdecimals'])) {
                                                                        if (is_array($options['reqdecimals'])) {
                                                                            $reqdecimals = $options['reqdecimals'][$qn];
                                                                        } else {
                                                                            $reqdecimals = $options['reqdecimals'];
                                                                        }
                                                                    }
                                                                    if (isset($options['answerformat'])) {
                                                                        if (is_array($options['answerformat'])) {
                                                                            $answerformat = $options['answerformat'][$qn];
                                                                        } else {
                                                                            $answerformat = $options['answerformat'];
                                                                        }
                                                                    }
                                                                    if (isset($options['variables'])) {
                                                                        if (is_array($options['variables'])) {
                                                                            $variables = $options['variables'][$qn];
                                                                        } else {
                                                                            $variables = $options['variables'];
                                                                        }
                                                                    }
                                                                    if (!isset($answerformat)) {
                                                                        $answerformat = '';
                                                                    }
                                                                    if (!isset($variables)) {
                                                                        $variables = 'x';
                                                                    }
                                                                    $ansformats = explode(',', $answerformat);
                                                                    if (!isset($sz)) {
                                                                        $sz = 20;
                                                                    }
                                                                    if (isset($ansprompt)) {
                                                                        $out .= "<label for=\"qn{$qn}\">{$ansprompt}</label>";
                                                                    }
                                                                    if ($multi > 0) {
                                                                        $qn = $multi * 1000 + $qn;
                                                                    }
                                                                    if (in_array('inequality', $ansformats)) {
                                                                        $tip = sprintf(_('Enter your answer using inequality notation.  Example: 3 <= %s < 4'), $variables) . " <br/>";
                                                                        $tip .= sprintf(_('Use or to combine intervals.  Example: %s < 2 or %s >= 3'), $variables, $variables) . "<br/>";
                                                                        $tip .= _('Enter <i>all real numbers</i> for solutions of that type') . "<br/>";
                                                                        $shorttip = _('Enter an interval using inequalities');
                                                                    } else {
                                                                        $tip = _('Enter your answer using interval notation.  Example: [2.1,5.6)') . " <br/>";
                                                                        if (in_array('list', $ansformats)) {
                                                                            $tip .= _('Separate intervals by a comma.  Example: (-oo,2],[4,oo)') . "<br/>";
                                                                            $shorttip = _('Enter a list of intervals using interval notation');
                                                                        } else {
                                                                            $tip .= _('Use U for union to combine intervals.  Example: (-oo,2] U [4,oo)') . "<br/>";
                                                                            $shorttip = _('Enter an interval using interval notation');
                                                                        }
                                                                    }
                                                                    //$tip .= "Enter values as numbers (like 5, -3, 2.2) or as calculations (like 5/3, 2^3, 5+4)<br/>";
                                                                    //$tip .= "Enter DNE for an empty set, oo for Infinity";
                                                                    $tip .= formathint(_('each value'), $ansformats, 'calcinterval');
                                                                    if (isset($reqdecimals)) {
                                                                        $tip .= "<br/>" . sprintf(_('Your numbers should be accurate to %d decimal places.'), $reqdecimals);
                                                                    }
                                                                    $out .= "<input class=\"text {$colorbox}\" type=\"text\"  size=\"{$sz}\" name=tc{$qn} id=tc{$qn} value=\"{$la}\" autocomplete=\"off\"  ";
                                                                    if ($showtips == 2) {
                                                                        //eqntips: work in progress
                                                                        if ($multi == 0) {
                                                                            $qnref = "{$qn}-0";
                                                                        } else {
                                                                            $qnref = $multi - 1 . '-' . $qn % 1000;
                                                                        }
                                                                        if ($useeqnhelper) {
                                                                            $out .= "onfocus=\"showeedd('tc{$qn}',{$useeqnhelper}," . (in_array('inequality', $ansformats) ? "'ineq'" : "'int'") . ");showehdd('tc{$qn}','{$shorttip}','{$qnref}');\" onblur=\"hideee();hideeedd();hideeh();\" onclick=\"reshrinkeh('tc{$qn}')\" ";
                                                                        } else {
                                                                            $out .= "onfocus=\"showehdd('tc{$qn}','{$shorttip}','{$qnref}')\" onblur=\"hideeh()\" onclick=\"reshrinkeh('tc{$qn}')\" ";
                                                                        }
                                                                    } else {
                                                                        if ($useeqnhelper) {
                                                                            $out .= "onfocus=\"showeedd('tc{$qn}',{$useeqnhelper}," . (in_array('inequality', $ansformats) ? "'ineq'" : "'int'") . ")\" onblur=\"hideee();hideeedd();\" ";
                                                                        }
                                                                    }
                                                                    $out .= '/>';
                                                                    $out .= "<input type=\"hidden\" id=\"qn{$qn}\" name=\"qn{$qn}\" />";
                                                                    $out .= getcolormark($colorbox);
                                                                    if (!isset($hidepreview)) {
                                                                        $preview .= "<input type=button class=btn value=\"" . _('Preview') . "\" onclick=\"intcalculate('tc{$qn}','p{$qn}','{$answerformat}')\" /> &nbsp;\n";
                                                                    }
                                                                    $preview .= "<span id=p{$qn}></span> ";
                                                                    $out .= "<script type=\"text/javascript\">intcalctoproc[{$qn}] = 1 ; calcformat[{$qn}] = '{$answerformat}';</script>\n";
                                                                    if (isset($answer)) {
                                                                        if (in_array('inequality', $ansformats)) {
                                                                            $sa = '`' . intervaltoineq($answer, $variables) . '`';
                                                                        } else {
                                                                            $sa = '`' . str_replace('U', 'uu', $answer) . '`';
                                                                        }
                                                                    }
                                                                } else {
                                                                    if ($anstype == 'draw') {
                                                                        if ($multi > 0) {
                                                                            if (isset($options['grid'][$qn])) {
                                                                                $grid = $options['grid'][$qn];
                                                                            }
                                                                            if (isset($options['snaptogrid'][$qn])) {
                                                                                $snaptogrid = $options['snaptogrid'][$qn];
                                                                            }
                                                                            if (isset($options['background'][$qn])) {
                                                                                $backg = $options['background'][$qn];
                                                                            }
                                                                            if (isset($options['answers'][$qn])) {
                                                                                $answers = $options['answers'][$qn];
                                                                            } else {
                                                                                if (isset($options['answer'][$qn])) {
                                                                                    $answers = $options['answer'][$qn];
                                                                                }
                                                                            }
                                                                        } else {
                                                                            if (isset($options['grid'])) {
                                                                                $grid = $options['grid'];
                                                                            }
                                                                            if (isset($options['snaptogrid'])) {
                                                                                $snaptogrid = $options['snaptogrid'];
                                                                            }
                                                                            if (isset($options['background'])) {
                                                                                $backg = $options['background'];
                                                                            }
                                                                            if (isset($options['answers'])) {
                                                                                $answers = $options['answers'];
                                                                            } else {
                                                                                if (isset($options['answer'])) {
                                                                                    $answers = $options['answer'];
                                                                                }
                                                                            }
                                                                        }
                                                                        if (isset($options['answerformat'])) {
                                                                            if (is_array($options['answerformat'])) {
                                                                                $answerformat = $options['answerformat'][$qn];
                                                                            } else {
                                                                                $answerformat = $options['answerformat'];
                                                                            }
                                                                        }
                                                                        if (!is_array($answers)) {
                                                                            settype($answers, "array");
                                                                        }
                                                                        if (!isset($snaptogrid)) {
                                                                            $snaptogrid = 0;
                                                                        }
                                                                        if ($multi > 0) {
                                                                            $qn = $multi * 1000 + $qn;
                                                                        }
                                                                        $imgborder = 5;
                                                                        if (!isset($answerformat)) {
                                                                            $answerformat = array('line', 'dot', 'opendot');
                                                                        } else {
                                                                            if (!is_array($answerformat)) {
                                                                                $answerformat = explode(',', $answerformat);
                                                                            }
                                                                        }
                                                                        if ($answerformat[0] == 'numberline') {
                                                                            $settings = array(-5, 5, 0, 0, 1, 0, 300, 50, "", "");
                                                                            $locky = 1;
                                                                            if (count($answerformat) == 1) {
                                                                                $answerformat[] = "lineseg";
                                                                                $answerformat[] = "dot";
                                                                                $answerformat[] = "opendot";
                                                                            }
                                                                        } else {
                                                                            $settings = array(-5, 5, -5, 5, 1, 1, 300, 300, "", "");
                                                                            $locky = 0;
                                                                        }
                                                                        if (isset($grid)) {
                                                                            if (!is_array($grid)) {
                                                                                $grid = explode(',', $grid);
                                                                            } else {
                                                                                if (strpos($grid[0], ',') !== false) {
                                                                                    //forgot to set as multipart?
                                                                                    $grid = array();
                                                                                }
                                                                            }
                                                                            for ($i = 0; $i < count($grid); $i++) {
                                                                                if ($grid[$i] != '') {
                                                                                    if (strpos($grid[$i], ':') !== false) {
                                                                                        $pts = explode(':', $grid[$i]);
                                                                                        foreach ($pts as $k => $v) {
                                                                                            $pts[$k] = evalbasic($v);
                                                                                        }
                                                                                        $settings[$i] = implode(':', $pts);
                                                                                    } else {
                                                                                        $settings[$i] = evalbasic($grid[$i]);
                                                                                    }
                                                                                }
                                                                            }
                                                                            if (strpos($grid[4], 'pi') !== false) {
                                                                                $settings[4] = 2 * ($settings[1] - $settings[0]) . ':' . $settings[4];
                                                                            }
                                                                        }
                                                                        if (!isset($backg)) {
                                                                            $backg = '';
                                                                        }
                                                                        if ($answerformat[0] == 'numberline') {
                                                                            $settings[2] = 0;
                                                                            $settings[3] = 0;
                                                                            if (strpos($settings[4], ':') !== false) {
                                                                                $settings[4] = explode(':', $settings[4]);
                                                                                $sclinglbl = $settings[4][0];
                                                                                $sclinggrid = $settings[4][1];
                                                                            } else {
                                                                                $sclinglbl = $settings[4];
                                                                                $sclinggrid = 0;
                                                                            }
                                                                        } else {
                                                                            if (strpos($settings[4], ':') !== false) {
                                                                                $settings[4] = explode(':', $settings[4]);
                                                                                $xlbl = $settings[4][0];
                                                                                $xgrid = $settings[4][1];
                                                                            } else {
                                                                                $xlbl = $settings[4];
                                                                                $xgrid = $settings[4];
                                                                            }
                                                                            if (strpos($settings[5], ':') !== false) {
                                                                                $settings[5] = explode(':', $settings[5]);
                                                                                $ylbl = $settings[5][0];
                                                                                $ygrid = $settings[5][1];
                                                                            } else {
                                                                                $ylbl = $settings[5];
                                                                                $ygrid = $settings[5];
                                                                            }
                                                                            $sclinglbl = "{$xlbl}:{$ylbl}";
                                                                            $sclinggrid = "{$xgrid}:{$ygrid}";
                                                                        }
                                                                        if ($snaptogrid > 0) {
                                                                            list($newwidth, $newheight) = getsnapwidthheight($settings[0], $settings[1], $settings[2], $settings[3], $settings[6], $settings[7], $snaptogrid);
                                                                            if (($newwidth - $settings[6]) / $settings[6] < 0.1) {
                                                                                $settings[6] = $newwidth;
                                                                            }
                                                                            if (($newheight - $settings[7]) / $settings[7] < 0.1) {
                                                                                $settings[7] = $newheight;
                                                                            }
                                                                        }
                                                                        if (!is_array($backg) && substr($backg, 0, 5) == "draw:") {
                                                                            $plot = showplot("", $settings[0], $settings[1], $settings[2], $settings[3], $sclinglbl, $sclinggrid, $settings[6], $settings[7]);
                                                                            $insat = strpos($plot, ');', strpos($plot, 'axes')) + 2;
                                                                            $plot = substr($plot, 0, $insat) . str_replace("'", '"', substr($backg, 5)) . substr($plot, $insat);
                                                                        } else {
                                                                            $plot = showplot($backg, $settings[0], $settings[1], $settings[2], $settings[3], $sclinglbl, $sclinggrid, $settings[6], $settings[7]);
                                                                        }
                                                                        if (is_array($settings[4]) && count($settings[4] > 2)) {
                                                                            $plot = addlabel($plot, $settings[1], 0, $settings[4][2], "black", "aboveleft");
                                                                        }
                                                                        if (is_array($settings[5]) && count($settings[5] > 2)) {
                                                                            $plot = addlabel($plot, 0, $settings[3], $settings[5][2], "black", "belowright");
                                                                        }
                                                                        if (isset($grid) && strpos($grid[4], 'pi') !== false) {
                                                                            $plot = addfractionaxislabels($plot, $grid[4]);
                                                                        }
                                                                        if ($settings[8] != "") {
                                                                        }
                                                                        $bg = getgraphfilename($plot);
                                                                        $dotline = 0;
                                                                        if ($colorbox != '') {
                                                                            $out .= '<div class="' . $colorbox . '" id="qnwrap' . $qn . '">';
                                                                        }
                                                                        $out .= "<canvas class=\"drawcanvas\" id=\"canvas{$qn}\" width=\"{$settings[6]}\" height=\"{$settings[7]}\"></canvas>";
                                                                        $out .= "<div><span id=\"drawtools{$qn}\" class=\"drawtools\">";
                                                                        $out .= "<span onclick=\"clearcanvas({$qn})\">" . _('Clear All') . "</span> " . _('Draw:') . " ";
                                                                        if ($answerformat[0] == 'inequality') {
                                                                            if (in_array('both', $answerformat)) {
                                                                                $out .= "<img src=\"{$imasroot}/img/tpineq.gif\" onclick=\"settool(this,{$qn},10)\" class=\"sel\"/>";
                                                                                $out .= "<img src=\"{$imasroot}/img/tpineqdash.gif\" onclick=\"settool(this,{$qn},10.2)\"/>";
                                                                                $out .= "<img src=\"{$imasroot}/img/tpineqparab.gif\" onclick=\"settool(this,{$qn},10.3)\"/>";
                                                                                $out .= "<img src=\"{$imasroot}/img/tpineqparabdash.gif\" onclick=\"settool(this,{$qn},10.4)\"/>";
                                                                                $def = 10;
                                                                            } else {
                                                                                if (in_array('parab', $answerformat)) {
                                                                                    $out .= "<img src=\"{$imasroot}/img/tpineqparab.gif\" onclick=\"settool(this,{$qn},10.3)\" class=\"sel\"/>";
                                                                                    $out .= "<img src=\"{$imasroot}/img/tpineqparabdash.gif\" onclick=\"settool(this,{$qn},10.4)\"/>";
                                                                                    $def = 10.3;
                                                                                } else {
                                                                                    $out .= "<img src=\"{$imasroot}/img/tpineq.gif\" onclick=\"settool(this,{$qn},10)\" class=\"sel\"/>";
                                                                                    $out .= "<img src=\"{$imasroot}/img/tpineqdash.gif\" onclick=\"settool(this,{$qn},10.2)\"/>";
                                                                                    $def = 10;
                                                                                }
                                                                            }
                                                                        } else {
                                                                            if ($answerformat[0] == 'twopoint') {
                                                                                if (count($answerformat) == 1 || in_array('line', $answerformat)) {
                                                                                    $out .= "<img src=\"{$imasroot}/img/tpline.gif\" onclick=\"settool(this,{$qn},5)\" ";
                                                                                    if (count($answerformat) == 1 || $answerformat[1] == 'line') {
                                                                                        $out .= 'class="sel" ';
                                                                                        $def = 5;
                                                                                    }
                                                                                    $out .= '/>';
                                                                                }
                                                                                //$out .= "<img src=\"$imasroot/img/tpline2.gif\" onclick=\"settool(this,$qn,5.2)\"/>";
                                                                                if (in_array('lineseg', $answerformat)) {
                                                                                    $out .= "<img src=\"{$imasroot}/img/tpline3.gif\" onclick=\"settool(this,{$qn},5.3)\" ";
                                                                                    if (count($answerformat) > 1 && $answerformat[1] == 'lineseg') {
                                                                                        $out .= 'class="sel" ';
                                                                                        $def = 5.3;
                                                                                    }
                                                                                    $out .= "/>";
                                                                                }
                                                                                if (in_array('ray', $answerformat)) {
                                                                                    $out .= "<img src=\"{$imasroot}/img/tpline2.gif\" onclick=\"settool(this,{$qn},5.2)\" ";
                                                                                    if (count($answerformat) > 1 && $answerformat[1] == 'ray') {
                                                                                        $out .= 'class="sel" ';
                                                                                        $def = 5.2;
                                                                                    }
                                                                                    $out .= "/>";
                                                                                }
                                                                                if (count($answerformat) == 1 || in_array('parab', $answerformat)) {
                                                                                    $out .= "<img src=\"{$imasroot}/img/tpparab.png\" onclick=\"settool(this,{$qn},6)\" ";
                                                                                    if (count($answerformat) > 1 && $answerformat[1] == 'parab') {
                                                                                        $out .= 'class="sel" ';
                                                                                        $def = 6;
                                                                                    }
                                                                                    $out .= '/>';
                                                                                }
                                                                                if (in_array('sqrt', $answerformat)) {
                                                                                    $out .= "<img src=\"{$imasroot}/img/tpsqrt.png\" onclick=\"settool(this,{$qn},6.5)\" ";
                                                                                    if (count($answerformat) > 1 && $answerformat[1] == 'sqrt') {
                                                                                        $out .= 'class="sel" ';
                                                                                        $def = 6.5;
                                                                                    }
                                                                                    $out .= '/>';
                                                                                }
                                                                                if (count($answerformat) == 1 || in_array('abs', $answerformat)) {
                                                                                    $out .= "<img src=\"{$imasroot}/img/tpabs.gif\" onclick=\"settool(this,{$qn},8)\" ";
                                                                                    if (count($answerformat) > 1 && $answerformat[1] == 'abs') {
                                                                                        $out .= 'class="sel" ';
                                                                                        $def = 8;
                                                                                    }
                                                                                    $out .= '/>';
                                                                                }
                                                                                if (in_array('exp', $answerformat)) {
                                                                                    $out .= "<img src=\"{$imasroot}/img/tpexp.png\" onclick=\"settool(this,{$qn},8.3)\" ";
                                                                                    if (count($answerformat) > 1 && $answerformat[1] == 'exp') {
                                                                                        $out .= 'class="sel" ';
                                                                                        $def = 8.300000000000001;
                                                                                    }
                                                                                    $out .= '/>';
                                                                                }
                                                                                if ($settings[6] * ($settings[3] - $settings[2]) == $settings[7] * ($settings[1] - $settings[0])) {
                                                                                    //only circles if equal spacing in x and y
                                                                                    if (count($answerformat) == 1 || in_array('circle', $answerformat)) {
                                                                                        $out .= "<img src=\"{$imasroot}/img/tpcirc.png\" onclick=\"settool(this,{$qn},7)\" ";
                                                                                        if (count($answerformat) > 1 && $answerformat[1] == 'circle') {
                                                                                            $out .= 'class="sel" ';
                                                                                            $def = 7;
                                                                                        }
                                                                                        $out .= '/>';
                                                                                    }
                                                                                }
                                                                                if (count($answerformat) == 1 || in_array('dot', $answerformat)) {
                                                                                    $out .= "<img src=\"{$imasroot}/img/tpdot.gif\" onclick=\"settool(this,{$qn},1)\" ";
                                                                                    if (count($answerformat) > 1 && $answerformat[1] == 'dot') {
                                                                                        $out .= 'class="sel" ';
                                                                                        $def = 1;
                                                                                    }
                                                                                    $out .= '/>';
                                                                                }
                                                                                if (in_array('opendot', $answerformat)) {
                                                                                    $out .= "<img src=\"{$imasroot}/img/tpodot.gif\" onclick=\"settool(this,{$qn},2)\" ";
                                                                                    if (count($answerformat) > 1 && $answerformat[1] == 'opendot') {
                                                                                        $out .= 'class="sel" ';
                                                                                        $def = 2;
                                                                                    }
                                                                                    $out .= '/>';
                                                                                }
                                                                                if (in_array('trig', $answerformat)) {
                                                                                    $out .= "<img src=\"{$imasroot}/img/tpcos.png\" onclick=\"settool(this,{$qn},9)\" ";
                                                                                    if (count($answerformat) > 1 && $answerformat[1] == 'trig') {
                                                                                        $out .= 'class="sel" ';
                                                                                        $def = 9;
                                                                                    }
                                                                                    $out .= '/>';
                                                                                    $out .= "<img src=\"{$imasroot}/img/tpsin.png\" onclick=\"settool(this,{$qn},9.1)\"/>";
                                                                                }
                                                                                if (in_array('vector', $answerformat)) {
                                                                                    $out .= "<img src=\"{$imasroot}/img/tpvec.gif\" onclick=\"settool(this,{$qn},5.4)\" ";
                                                                                    if (count($answerformat) > 1 && $answerformat[1] == 'vector') {
                                                                                        $out .= 'class="sel" ';
                                                                                        $def = 5.4;
                                                                                    }
                                                                                    $out .= '/>';
                                                                                }
                                                                            } else {
                                                                                if ($answerformat[0] == 'numberline') {
                                                                                    array_shift($answerformat);
                                                                                }
                                                                                for ($i = 0; $i < count($answerformat); $i++) {
                                                                                    if ($i == 0) {
                                                                                        $out .= '<span class="sel" ';
                                                                                    } else {
                                                                                        $out .= '<span ';
                                                                                    }
                                                                                    if ($answerformat[$i] == 'line') {
                                                                                        $out .= "onclick=\"settool(this,{$qn},0)\">" . _('Line') . "</span>";
                                                                                    } else {
                                                                                        if ($answerformat[$i] == 'lineseg') {
                                                                                            $out .= "onclick=\"settool(this,{$qn},0.5)\">" . _('Line Segment') . "</span>";
                                                                                        } else {
                                                                                            if ($answerformat[$i] == 'dot') {
                                                                                                $out .= "onclick=\"settool(this,{$qn},1)\">" . _('Dot') . "</span>";
                                                                                            } else {
                                                                                                if ($answerformat[$i] == 'opendot') {
                                                                                                    $out .= "onclick=\"settool(this,{$qn},2)\">" . _('Open Dot') . "</span>";
                                                                                                } else {
                                                                                                    if ($answerformat[$i] == 'polygon') {
                                                                                                        $out .= "onclick=\"settool(this,{$qn},0)\">" . _('Polygon') . "</span>";
                                                                                                        $dotline = 1;
                                                                                                    } else {
                                                                                                        if ($answerformat[$i] == 'closedpolygon') {
                                                                                                            $out .= "onclick=\"settool(this,{$qn},0)\">" . _('Polygon') . "</span>";
                                                                                                            $dotline = 2;
                                                                                                            $answerformat[$i] = 'polygon';
                                                                                                        }
                                                                                                    }
                                                                                                }
                                                                                            }
                                                                                        }
                                                                                    }
                                                                                }
                                                                                if ($answerformat[0] == 'line') {
                                                                                    $def = 0;
                                                                                } else {
                                                                                    if ($answerformat[0] == 'lineseg') {
                                                                                        $def = 0.5;
                                                                                    } else {
                                                                                        if ($answerformat[0] == 'dot') {
                                                                                            $def = 1;
                                                                                        } else {
                                                                                            if ($answerformat[0] == 'opendot') {
                                                                                                $def = 2;
                                                                                            } else {
                                                                                                if ($answerformat[0] == 'polygon') {
                                                                                                    $def = 0;
                                                                                                }
                                                                                            }
                                                                                        }
                                                                                    }
                                                                                }
                                                                            }
                                                                        }
                                                                        if (strpos($snaptogrid, ':') !== false) {
                                                                            $snaptogrid = "'{$snaptogrid}'";
                                                                        }
                                                                        $out .= '</span></div>';
                                                                        $out .= getcolormark($colorbox);
                                                                        if ($colorbox != '') {
                                                                            $out .= '</div>';
                                                                        }
                                                                        $out .= "<input type=\"hidden\" name=\"qn{$qn}\" id=\"qn{$qn}\" value=\"{$la}\" />";
                                                                        $out .= "<script type=\"text/javascript\">canvases[{$qn}] = [{$qn},'{$bg}',{$settings[0]},{$settings[1]},{$settings[2]},{$settings[3]},5,{$settings[6]},{$settings[7]},{$def},{$dotline},{$locky},{$snaptogrid}];";
                                                                        $la = str_replace(array('(', ')'), array('[', ']'), $la);
                                                                        $la = explode(';;', $la);
                                                                        if ($la[0] != '') {
                                                                            $la[0] = '[' . str_replace(';', '],[', $la[0]) . ']';
                                                                        }
                                                                        $la = '[[' . implode('],[', $la) . ']]';
                                                                        $out .= "drawla[{$qn}] = {$la};</script>";
                                                                        $tip = _('Enter your answer by drawing on the graph.');
                                                                        if (isset($answers)) {
                                                                            $saarr = array();
                                                                            $ineqcolors = array("blue", "red", "green");
                                                                            foreach ($answers as $k => $ans) {
                                                                                if (is_array($ans)) {
                                                                                    continue;
                                                                                }
                                                                                //shouldn't happen, unless user forgot to set question to multipart
                                                                                if ($ans == '') {
                                                                                    continue;
                                                                                }
                                                                                $function = explode(',', $ans);
                                                                                if ($answerformat[0] == 'inequality') {
                                                                                    if ($function[0][2] == '=') {
                                                                                        $type = 10;
                                                                                        $c = 3;
                                                                                    } else {
                                                                                        $type = 10.2;
                                                                                        $c = 2;
                                                                                    }
                                                                                    $dir = $function[0][1];
                                                                                    $saarr[$k] = makepretty($function[0]) . ',' . $ineqcolors[$k % 3];
                                                                                } else {
                                                                                    if (count($function) == 2 || count($function) == 3 && ($function[2] == 'open' || $function[2] == 'closed')) {
                                                                                        //is dot
                                                                                        $saarr[$k] = $function[1] . ',blue,' . $function[0] . ',' . $function[0];
                                                                                        if (count($function) == 2 || $function[2] == 'closed') {
                                                                                            $saarr[$k] .= ',closed';
                                                                                        } else {
                                                                                            $saarr[$k] .= ',open';
                                                                                        }
                                                                                        if ($locky == 1) {
                                                                                            $saarr[$k] .= ',,2';
                                                                                        }
                                                                                    } else {
                                                                                        if ($function[0] == 'vector') {
                                                                                            if (count($function) > 4) {
                                                                                                $dx = $function[3] - $function[1];
                                                                                                $dy = $function[4] - $function[2];
                                                                                                $xs = $function[1];
                                                                                                $ys = $function[2];
                                                                                            } else {
                                                                                                $dx = $function[1];
                                                                                                $dy = $function[2];
                                                                                                $xs = 0;
                                                                                                $ys = 0;
                                                                                            }
                                                                                            $saarr[$k] = "[{$xs} + ({$dx})*t, {$ys} + ({$dy})*t],blue,0,1,,arrow";
                                                                                        } else {
                                                                                            if ($function[0] == 'circle') {
                                                                                                //is circle
                                                                                                $saarr[$k] = "[{$function[3]}*cos(t)+{$function[1]},{$function[3]}*sin(t)+{$function[2]}],blue,0,6.31";
                                                                                            } else {
                                                                                                if (substr($function[0], 0, 2) == 'x=') {
                                                                                                    $saarr[$k] = '[' . substr($function[0], 2) . ',t],blue,' . ($settings[2] - 1) . ',' . ($settings[3] + 1);
                                                                                                } else {
                                                                                                    //is function
                                                                                                    $saarr[$k] = $function[0] . ',blue';
                                                                                                    if (count($function) > 2) {
                                                                                                        if ($function[1] == '-oo') {
                                                                                                            $function[1] = $settings[0] - 0.1 * ($settings[1] - $settings[0]);
                                                                                                        }
                                                                                                        if ($function[2] == 'oo') {
                                                                                                            $function[2] = $settings[1] + 0.1 * ($settings[1] - $settings[0]);
                                                                                                        }
                                                                                                        $saarr[$k] .= ',' . $function[1] . ',' . $function[2];
                                                                                                        if ($locky == 1) {
                                                                                                            $saarr[$k] .= ',,,3';
                                                                                                        }
                                                                                                    }
                                                                                                    if ($locky == 1) {
                                                                                                        $saarr[$k] .= ',,,,,3';
                                                                                                    }
                                                                                                }
                                                                                            }
                                                                                        }
                                                                                    }
                                                                                }
                                                                            }
                                                                            if ($backg != '') {
                                                                                if (!is_array($backg) && substr($backg, 0, 5) == "draw:") {
                                                                                    $sa = showplot($saarr, $settings[0], $settings[1], $settings[2], $settings[3], $sclinglbl, $sclinggrid, $settings[6], $settings[7]);
                                                                                    $insat = strpos($sa, ');', strpos($sa, 'axes')) + 2;
                                                                                    $sa = substr($sa, 0, $insat) . str_replace("'", '"', substr($backg, 5)) . substr($sa, $insat);
                                                                                } else {
                                                                                    if (!is_array($backg)) {
                                                                                        settype($backg, "array");
                                                                                    }
                                                                                    $saarr = array_merge($saarr, $backg);
                                                                                    $sa = showplot($saarr, $settings[0], $settings[1], $settings[2], $settings[3], $sclinglbl, $sclinggrid, $settings[6], $settings[7]);
                                                                                    if (isset($grid) && strpos($grid[4], 'pi') !== false) {
                                                                                        $sa = addfractionaxislabels($sa, $grid[4]);
                                                                                    }
                                                                                }
                                                                            } else {
                                                                                $sa = showplot($saarr, $settings[0], $settings[1], $settings[2], $settings[3], $sclinglbl, $sclinggrid, $settings[6], $settings[7]);
                                                                                if (isset($grid) && strpos($grid[4], 'pi') !== false) {
                                                                                    $sa = addfractionaxislabels($sa, $grid[4]);
                                                                                }
                                                                            }
                                                                            if ($answerformat[0] == "polygon") {
                                                                                if ($dotline == 2) {
                                                                                    $cmd = 'fill="transblue";path([[' . implode('],[', $answers) . ']]);fill="blue";';
                                                                                } else {
                                                                                    $cmd = 'stroke="blue";path([[' . implode('],[', $answers) . ']]);';
                                                                                }
                                                                                for ($i = 0; $i < count($answers) - 1; $i++) {
                                                                                    $cmd .= 'dot([' . $answers[$i] . ']);';
                                                                                }
                                                                                $sa = adddrawcommand($sa, $cmd);
                                                                            }
                                                                        }
                                                                    } else {
                                                                        if ($anstype == "file") {
                                                                            if (isset($options['ansprompt'])) {
                                                                                if (is_array($options['ansprompt'])) {
                                                                                    $ansprompt = $options['ansprompt'][$qn];
                                                                                } else {
                                                                                    $ansprompt = $options['ansprompt'];
                                                                                }
                                                                            }
                                                                            if (isset($options['answer'])) {
                                                                                if (is_array($options['answer'])) {
                                                                                    $answer = $options['answer'][$qn];
                                                                                } else {
                                                                                    $answer = $options['answer'];
                                                                                }
                                                                            }
                                                                            if ($multi > 0) {
                                                                                $qn = $multi * 1000 + $qn;
                                                                            }
                                                                            if (isset($ansprompt)) {
                                                                                $out .= "<label for=\"qn{$qn}\">{$ansprompt}</label>";
                                                                            }
                                                                            if ($colorbox != '') {
                                                                                $out .= '<span class="' . $colorbox . '">';
                                                                            }
                                                                            $out .= "<input type=\"file\" name=\"qn{$qn}\" id=\"qn{$qn}\" />\n";
                                                                            $out .= getcolormark($colorbox);
                                                                            if ($colorbox != '') {
                                                                                $out .= '</span>';
                                                                            }
                                                                            if ($la != '') {
                                                                                if (isset($GLOBALS['testsettings']) && isset($GLOBALS['sessiondata']['groupid']) && $GLOBALS['testsettings'] > 0 && $GLOBALS['sessiondata']['groupid'] > 0) {
                                                                                    $s3asid = 'grp' . $GLOBALS['sessiondata']['groupid'] . '/' . $GLOBALS['testsettings']['id'];
                                                                                } else {
                                                                                    if (isset($GLOBALS['asid'])) {
                                                                                        $s3asid = $GLOBALS['asid'];
                                                                                    }
                                                                                }
                                                                                if (isset($GLOBALS['questionscoreref'])) {
                                                                                    if ($multi == 0) {
                                                                                        $el = $GLOBALS['questionscoreref'][0];
                                                                                        $sc = $GLOBALS['questionscoreref'][1];
                                                                                    } else {
                                                                                        $el = $GLOBALS['questionscoreref'][0] . '-' . $qn % 1000;
                                                                                        $sc = $GLOBALS['questionscoreref'][1][$qn % 1000];
                                                                                    }
                                                                                    $out .= '<span style="float:right;">';
                                                                                    $out .= '<img class="scoreicon" src="' . $imasroot . '/img/q_fullbox.gif" ';
                                                                                    $out .= "onclick=\"quicksetscore('{$el}',{$sc})\" />";
                                                                                    $out .= '<img class="scoreicon" src="' . $imasroot . '/img/q_halfbox.gif" ';
                                                                                    $out .= "onclick=\"quicksetscore('{$el}',.5*{$sc})\" />";
                                                                                    $out .= '<img class="scoreicon" src="' . $imasroot . '/img/q_emptybox.gif" ';
                                                                                    $out .= "onclick=\"quicksetscore('{$el}',0)\" /></span>";
                                                                                }
                                                                                if (!empty($s3asid)) {
                                                                                    require_once "../includes/filehandler.php";
                                                                                    if (substr($la, 0, 5) == "Error") {
                                                                                        $out .= "<br/>{$la}";
                                                                                    } else {
                                                                                        $file = preg_replace('/@FILE:(.+?)@/', "\$1", $la);
                                                                                        $url = getasidfileurl($file);
                                                                                        $extension = substr($url, strrpos($url, '.') + 1, 3);
                                                                                        $filename = basename($file);
                                                                                        $out .= "<br/>" . _('Last file uploaded:') . " <a href=\"{$url}\" target=\"_new\">{$filename}</a>";
                                                                                        $out .= "<input type=\"hidden\" name=\"lf{$qn}\" value=\"{$file}\"/>";
                                                                                        if (in_array(strtolower($extension), array('jpg', 'gif', 'png', 'bmp', 'jpe'))) {
                                                                                            $out .= " <span class=\"clickable\" id=\"filetog{$qn}\" onclick=\"toggleinlinebtn('img{$qn}','filetog{$qn}');\">[+]</span>";
                                                                                            $out .= " <br/><img id=\"img{$qn}\" style=\"display:none;max-width:80%;\" src=\"{$url}\" />";
                                                                                        } else {
                                                                                            if (in_array(strtolower($extension), array('doc', 'docx', 'pdf', 'xls', 'xlsx', 'ppt', 'pptx'))) {
                                                                                                $out .= " <span class=\"clickable\" id=\"filetog{$qn}\" onclick=\"toggleinlinebtn('fileprev{$qn}','filetog{$qn}');\">[+]</span>";
                                                                                                $out .= " <br/><iframe id=\"fileprev{$qn}\" style=\"display:none;\" src=\"http://docs.google.com/viewer?url=" . urlencode($url) . "&embedded=true\" width=\"80%\" height=\"600px\"></iframe>";
                                                                                            }
                                                                                        }
                                                                                    }
                                                                                } else {
                                                                                    $out .= "<br/>{$la}";
                                                                                }
                                                                            }
                                                                            $tip .= _('Select a file to upload');
                                                                            $sa .= $answer;
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return array($out, $tip, $sa, $preview);
}