scores[1] = scores[1].replace('. ', '.\n\n');

function insertScore( qnr ) {
  if ( document.getElementById( 'negqstweight' ) == null ) return;
  var qw = document.getElementById( 'Q_WEIGHT_' + qnr );
  if ( qw ) {
    var qwval = qw.value;
    var qwvalstrip = qwval.replace(/^ +/, '').replace(/ +$/, '');
    if ( qwvalstrip == '\t' ) return; // weight not to be specified
    if ( qwvalstrip ) {
      if ( qwval.length > 3 && qwval.slice(-3) == '   ' ) {
        var pw = prompt(scores[3], '').replace(/^ +/, '').replace(/ +$/, '');
        while ( pw == '' )
          pw = prompt(scores[3], '').replace(/^ +/, '').replace(/ +$/, '');
        qw.value = qwvalstrip + ' ' + pw + '   ';
      }
      return;
    }
    else { // prompt 1. spec.: total or 1. part weight
      var tw = prompt(scores[1], '');
      tw = tw.replace(/^ +/, '').replace(/ +$/, '');
      if ( tw ) {
        qw.value = tw;
        return;
      }
      else { // prompt 1. part
        var pw = prompt(scores[2], '');
        pw = pw.replace(/^ +/, '').replace(/ +$/, '');
        if ( pw ) { // 1. part specified
          qw.value = pw + '   ';
          return;
        }
        else {
          qw.value = '\t';
          return;
        }
      }
    }
  }
}

function onLoad( ) {
  var qnr = 1;
  while ( 1 ) {
    var qnrStr = qnr.toString();
    qnr = qnr + 1;
    var qsttypeName = 'qsttype_' + qnrStr;
    if ( document.getElementById(qsttypeName) == null ) return;
    var correctName = 'q_correct_' + qnrStr;
    var correctValue = document.getElementById(correctName).value;
    if ( correctValue == '' ) return;
    var qsttype = document.getElementById(qsttypeName).value;
    var qsttypeLength = qsttype.length;
    if ( qsttypeLength == 0 ) continue;
    var which = 'insrtslct0_' + qnrStr;
    var last = qsttype.slice(-1);
    if ( last == '$' ) {
      document.getElementById(which).options[25] = null;
      for ( var i = 0; i <=16; ++ i )
        document.getElementById(which).options[1] = null;
      continue;
    }
    if ( qsttypeLength < 2 ) continue;
    var last2 = qsttype.slice(-2);
    if ( last2 == '$}' ) {
      document.getElementById(which).options[25] = null;
      for ( var i = 1; i <=11; ++ i )
        document.getElementById(which).options[1] = null;
      document.getElementById(which).options[4] = null;
      document.getElementById(which).options[4] = null;
    }
    else if ( last == '}' ) {
      document.getElementById(which).options[25] = null;
      document.getElementById(which).options[2] = null;
      for ( var i = 0; i <=16; ++ i )
        document.getElementById(which).options[3] = null;
    }
    else if ( last2 == '$]' || last2 == '}]' ) {
      document.getElementById(which).options[25] = null;
      for ( var i = 0; i <=5; ++ i )
        document.getElementById(which).options[1] = null;
      for ( var i = 0; i <=6; ++ i )
        document.getElementById(which).options[6] = null;
    }
    else if ( last == ']' ) {
      document.getElementById(which).options[25] = null;
      document.getElementById(which).options[1] = null;
      for ( var i = 0; i <=16; ++ i )
        document.getElementById(which).options[3] = null;
    }
    else if ( last2 == 'I3' ) {
      optionsLength = document.getElementById(which).options.length;
      var speccharOpt = document.getElementById(which).options[optionsLength-6];
      var delOpt = document.getElementById(which).options[optionsLength-1];
      document.getElementById(which).options.length = null;
      var opt = new Option(texts[27], '', '', '');
      document.getElementById(which).options[0] = opt;
      opt = new Option(texts[28], 'CBo', '', '');
      document.getElementById(which).options[1] = opt;
      opt = new Option(texts[29], 'CBu', '', '');
      document.getElementById(which).options[2] = opt;
      opt = new Option(texts[30], 'CBs', '', '');
      document.getElementById(which).options[3] = opt;
      document.getElementById(which).options[4] = speccharOpt;
      document.getElementById(which).options[5] = delOpt;
    }
    else if ( qsttype.slice(-2,-1) == 'B' ) {
      optionsLength = document.getElementById(which).options.length;
      var speccharOpt = document.getElementById(which).options[optionsLength-6];
      var delOpt = document.getElementById(which).options[optionsLength-1];
      document.getElementById(which).options.length = null;
      var opt = new Option(texts[27], '', '', '');
      document.getElementById(which).options[0] = opt;
      last = qsttype.slice(-1);
      if ( last == 'o' )
        opt = new Option(texts[28], 'CBo', '', '');
      else if ( last == 'u' )
        opt = new Option(texts[29], 'CBu', '', '');
      else
        opt = new Option(texts[30], 'CBs', '', '');
      document.getElementById(which).options[1] = opt;
      document.getElementById(which).options[2] = speccharOpt;
      document.getElementById(which).options[3] = delOpt;
    }
  }
}

//myField accepts an object reference, myValue accepts the text strint to add
function insertAtCursor(myField, myValue) {
  //IE support
  if (document.selection) {
    myField.focus();
    sel = document.selection.createRange();
    sel.text = myValue;
    if ( myValue.slice(0,1) == '<' ) {
      var p = - myValue.length + myValue.search(/>/) + 1;
      sel.moveStart('character', p);
      sel.moveEnd('character', p);
      sel.select();
    }
  }

  //Mozilla/Firefox/Netscape 7+ support
  else {
    var startPos = myField.selectionStart;
    if (myField.selectionStart || myField.selectionStart == '0') {
      var endPos = myField.selectionEnd;
      myField.value = myField.value.substring(0, startPos)+ myValue+ myField.value.substring(endPos, myField.value.length);
      }
    else {
      myField.value += myValue;
    }
    if ( myValue.slice(0,1) == '<' ) {
      myField.selectionStart =
      myField.selectionEnd = startPos + myValue.search(/>/) + 1;
      myField.focus();
    }
  }
}

function reEstablishSelect( where ) {
  var nr = where.replace( 'q_phrase_', '' );
  var txt = document.getElementById('qcmarkup').value;
  txt = txt.replace(/<option value="/g,'');
  txt = txt.replace(/<\/option>/g,'');
  splittxt = txt.split(/\r?\n/);
  slctobj = document.getElementById('insrtslct0_'+nr);
  slctobj.length = 0;
  splittxtLength = splittxt.length;
  for ( var i = 0 ; i < splittxtLength; ++i ) {
    line = splittxt[i];
    parts = line.split(/">/);
    if ( parts[1] === undefined ) { // MSIE
      parts[1] = parts[0];
      parts[0] = '';
    }
    x = new Option( parts[1], parts[0], '', '' );
    slctobj.options[slctobj.options.length] = x;
  }
}

function mismatch(expr) {
  if ( expr ) {
    alert(texts[0]);
    return true;
  }
  else return false;  
}

function display(where) {
  newWin = window.open('','entry','width=600,height=200,scrollbars=yes');
  newWin.document.write('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"\n"http://www.w3.org/TR/html40/loose.dtd">\n<html><head><title></title></head><body style="background-color:rgb(230, 255, 255); background-image:none; color:rgb(8,42,8);">');
  txt = document.getElementById(where).value;
  txt = txt.replace(/&\n\r*/g,'');
  txt = txt.replace(/&./g,
   function(x){y='\t'+x.substr(1,1).charCodeAt(0)+';'+x.substr(2);return(y)}); 
  txt = txt.replace(/\t/g,'&#');
  txt = txt.replace(/\{[a-z]\}/g,'<input type="radio" value="a" />');
  txt = txt.replace(/\[[a-z]\]/g,'<input type="checkbox" size="a" />');
  txt = txt.replace(/\[(\d*)\]/g,
   function(x){return('<input type="text" size="'+x.substring(1,x.length-1)+'" />')});
  txt = txt.replace(/(\[\w+\])/g,
   function(x){return('<input type="text" value="'+x.substring(1,x.length-1)+'"  size="'+x.substring(1,x.length-1).length.toString()+'"/>')});
  txt = txt.replace(/\n/g,'<br />');
  re0 = /([^\{]*)\{([^\}]+)\}(.*)/
  while (1) {
    if (txt.search(/\{[^\}]+\?[^\}]+\}/) == -1) break;
    result = txt.match(re0);
    txt = result[1]+ '<select><option>' + result[2].replace(/\?/g,'</option><option>') + '</option></select>' + result[3];
  }
  newWin.document.write(txt);
  newWin.document.write('</body></html>');
  newWin.document.close();
  return;
}

function insertHTML( where, which ) {
  var idx = document.getElementById(which).selectedIndex;
  var selected = document.getElementById(which).options[idx].value;
  if ( ! selected ) {
    idx = document.all[which].selectedIndex;
    selected = document.all[which].options[idx].value;
  }
  var markup = selected;
  document.getElementById(which).selectedIndex = 0;
  if ( markup == '' ) return;
  var markup0 = markup.charAt(0);
  if ( markup0 == '<' ) {
    insertAtCursor(document.getElementById(where), markup);
    return;
  }
  if ( markup0 == 'i' ) {
    if ( markup.charAt(1) == '0' ) {
      var lst = document.getElementsByName('htmlfile');
      if ( lst.length == 0 ) return;
      quizname = lst[0].value;
      quiz = prompt(texts[15], quizname);
      if ( ! quiz ) return;
      img = prompt(texts[16], '');
      if ( ! img ) return;
      markup = '<img src="' + quiz + '/' + img + '" /> ';
      insertAtCursor(document.getElementById(where), markup);
      return;
    }
    else {
      loc = prompt(texts[17], 'http://');
      if ( ! loc ) return;
      markup = '<img src="' + loc + '" /> ';
      insertAtCursor(document.getElementById(where), markup);
      return;
    }
  }
  if ( markup0 == 'a' ) {
    loc = prompt(texts[18], 'http://');
    if ( ! loc ) return;
    txt = prompt(texts[19], '');
    if ( ! txt ) return;
    markup = '<a href="' + loc + '">' + txt + '</a> ';
    insertAtCursor(document.getElementById(where), markup);
    return;
  }
  if ( markup == 't' ) {
    markup = '<table>\n<tr><td>u</td><td>v</td><td>w</td></tr>\n<tr><td>x</td><td>y</td><td>z</td></tr>\n</table> ';
    insertAtCursor(document.getElementById(where), markup);
    return;
  }
  if ( markup == 'T' ) {
    var border = prompt(texts[35], '');
    markup = '<table border="'+border+'">\n</table>';
    insertAtCursor(document.getElementById(where), markup);
    return;
  }
  if ( markup == 'D' ) {
    markup = '<td></td>';
    insertAtCursor(document.getElementById(where), markup);
    return;
  }
  if ( markup == 'de' ) display(where);
  if ( markup == 'sf' ) {
    document.getElementsByName('myForm')[0].submit();
    return;
  }
}

function coordBox( corrField, selList, markup, texts ) {
  corr = corrField.value;
  if ( corr != '' && corr.match(/^ *[\d\.eE-]+:[\d\.eE-]+_/) ) {
    if ( markup == 'CBo' ) {
      var b1 = '[';
      var b2 = ']';
      corr = b1 + corr + b2;
      selList[2] = null;
      selList[2] = null;
    }
    else if ( markup == 'CBu' ) {
      var b1 = '{';
      var b2 = '}';
      corr = b1 + corr + b2;
      selList[1] = null;
      selList[2] = null;
    }
    else if ( markup == 'CBs' ) {
      N = prompt(texts[9],'');
      if ( ! N ) return;
      corr = '{.' + N + ' ' + corr + '}';
      var b1 = '{';
      var b2 = '}';
      selList[1] = null;
      selList[1] = null;
    }
  }
  else {
    if ( markup == 'CBo' ) {
      var b1 = '[';
      var b2 = ']';
    }
    else {
      var b1 = '{';
      var b2 = '}';
    }
  }
  corrField.value = corr;
  midcoord = prompt(texts[31],'');
  if ( ! midcoord ) return;
  halfintv = prompt(texts[32],'');
  if ( ! halfintv ) return;
  intv = midcoord + '+-' + halfintv;
  midcoord = prompt(texts[33],'');
  if ( ! midcoord ) return;
  halfintv = prompt(texts[32],'');
  if ( ! halfintv ) return;
  box = b1 + intv + '_' + midcoord + '+-' + halfintv + b2;
  corrField.value += box;
}

function insertCode( where, which ) {
  var idx = document.getElementById(which).selectedIndex;
  var selected = document.getElementById(which).options[idx].value;
  if ( ! selected ) {
    idx = document.all[which].selectedIndex;
    selected = document.all[which].options[idx].value;
  }
  var markup = selected;
  if ( markup == '' ) return;
  document.getElementById(which).selectedIndex = 0;
  var i = which.indexOf('_') + 1;
  var qnr = which.slice(i);
  var qsttypename = 'qsttype_' + qnr;
  var qsttype = document.getElementById(qsttypename).value;
  var q_correct_qnr = 'q_correct_' + qnr;
  if ( qsttype == '' ) {
    if ( markup == '{}' ) {
      markup = '{a}';
      insertAtCursor(document.getElementById(where), markup);
      if ( confirm(texts[1]) )
        document.getElementById(q_correct_qnr).value = '{a}';
      insertScore( qnr );
      document.getElementById(which).options[25] = null;
      document.getElementById(which).options[2] = null;  // TODO:?
      for ( var i = 0; i <=16; ++ i )
        document.getElementById(which).options[3] = null;
    }
    else if ( markup == '[]' ) {
      markup = '[a]';
      insertAtCursor(document.getElementById(where), markup);
      if ( confirm(texts[2]) )
        document.getElementById(q_correct_qnr).value = '[a]';
      insertScore( qnr );
      document.getElementById(which).options[25] = null;
      document.getElementById(which).options[1] = null;
      for ( var i = 0; i <=16; ++ i )
        document.getElementById(which).options[3] = null;
    }
    else if ( markup == '$' ) {
      corr = prompt(texts[34],'');
      if ( ! corr ) return;
      document.getElementById(q_correct_qnr).value = corr;
      loif = prompt(texts[4],'10');
      insertAtCursor(document.getElementById(where), '['+loif+']');
      insertScore( qnr );
      document.getElementById(which).options[25] = null;
      for ( var i = 0; i <=16; ++ i )
        document.getElementById(which).options[1] = null;
    }
    else if ( markup == '[$]' || markup.substr(0,2) == '[!' || markup.substr(0,2) == '[~' ) {
      corr = prompt(texts[3],'');
      if ( ! corr ) return;
      loif = prompt(texts[4],'10');
      if ( ! loif ) return;
      document.getElementById(q_correct_qnr).value += markup.replace('$', corr + ' ');
      if ( loif.match(/^[a-z]$/) ) loif = loif + ' ';
      insertAtCursor(document.getElementById(where),'['+loif+']');
      insertScore( qnr );
      document.getElementById(which).options[25] = null;
      for ( var i = 0; i <=5; ++ i )
        document.getElementById(which).options[0] = null;
      for ( var i = 0; i <=6; ++ i )
        document.getElementById(which).options[6] = null;
    }
    else if ( markup == '[{$?$?$}]' ) { // TODO: merge with above?
      corr = prompt(texts[14],'');
      if ( ! corr ) return;
      loif = prompt(texts[4],'10');
      if ( ! loif ) return;
      document.getElementById(q_correct_qnr).value += markup.replace('$?$?$', corr);
      markup = '[$]'; // for setting qsttype
      insertAtCursor(document.getElementById(where),'['+loif+']');
      insertScore( qnr );
      document.getElementById(which).options[25] = null;
      for ( var i = 0; i <=5; ++ i )
        document.getElementById(which).options[0] = null;
      for ( var i = 0; i <=6; ++ i )
        document.getElementById(which).options[6] = null;
    }
    else if ( markup == '{$?$?$}' ) {
      corr = prompt(texts[5],'');
      insertAtCursor(document.getElementById(where),markup.replace('$?$?$', corr));
      return;
    }
    else if ( markup == '{$}' || markup.substr(0,2) == '{!' || markup.substr(0,2) == '{~' ) {
      N = prompt(texts[6],'');
      if ( ! N ) return;
      loif = prompt(texts[8],'10');
      if ( ! loif ) return;
      corr = prompt(texts[7],''); // maybe corr is lower latin char
      if ( ! corr ) return;
      for ( var i = 1; i <= N; i++ )
        insertAtCursor(document.getElementById(where),'['+loif+']');
      document.getElementById(q_correct_qnr).value += markup.replace('$', corr + ' ');
      insertScore( qnr );
      document.getElementById(which).options[25] = null;
      for ( var i = 0; i <=11; ++ i )
        document.getElementById(which).options[0] = null;
      document.getElementById(which).options[4] = null;
      document.getElementById(which).options[4] = null;
    }
    else if ( markup.substr(0,2) == '{.' ) {
      N = prompt(texts[9],'');
      if ( ! N ) return;
      loif = prompt(texts[11],'10');
      if ( ! loif ) return;
      corr = prompt(texts[10],'');
      if ( ! corr ) return;
      for ( var i = 1; i <= N; i++ )
        insertAtCursor(document.getElementById(where),'['+loif+']');
      markup = markup.replace('N', N);
      document.getElementById(q_correct_qnr).value += markup.replace('$', corr + ' ');
      insertScore( qnr );
      document.getElementById(which).options[25] = null;
      for ( var i = 0; i <=11; ++ i )
        document.getElementById(which).options[0] = null;
      document.getElementById(which).options[4] = null;
      document.getElementById(which).options[4] = null;
    }
    else if ( markup == '&' ) {
      char = prompt(texts[12],'');
      if ( ! char ) return;
      markup = char.replace(/(.)/g,'&$1');
      insertAtCursor(document.getElementById(where), markup);
      return;
    }
    else if ( markup == 'I' ) {
      img = prompt(texts[20],'');
      if ( ! img ) return;
      insertAtCursor(document.getElementById(where), img + ' ');
      return;
    }
    else if ( markup == 'I2' ) {
      img = prompt(texts[20],'');
      if ( ! img ) return;
      width = prompt(texts[21],'');
      if ( ! width ) return;
      height = prompt(texts[22],'');
      if ( ! height ) return;
      img = img + '_' + width + '_' + height + ' ';
      insertAtCursor(document.getElementById(where), img);
      return;
    }
    else if ( markup == 'I3' ) {
      img = prompt(texts[20],'');
      if ( ! img ) return;
      width = prompt(texts[21],'');
      if ( ! width ) return;
      height = prompt(texts[22],'');
      if ( ! height ) return;
      img = img + '_' + width + '_' + height + ' ';
      insertAtCursor(document.getElementById(where), img);
      xmin = prompt(texts[23],'');
      if ( ! xmin ) return;
      xmax = prompt(texts[24],'');
      if ( ! xmax ) return;
      ymin = prompt(texts[25],'');
      if ( ! ymin ) return;
      ymax = prompt(texts[26],'');
      if ( ! ymax ) return;
      spanbox = xmin + ':' + xmax + '_' + ymin + ':' + ymax;
      document.getElementById(q_correct_qnr).value = spanbox;
      optionsLength = document.getElementById(which).options.length;
      var speccharOpt = document.getElementById(which).options[optionsLength-6];
      var delOpt = document.getElementById(which).options[optionsLength-1];
      document.getElementById(which).options.length = null;
      var opt = new Option(texts[27], '', '', '');
      document.getElementById(which).options[0] = opt;
      opt = new Option(texts[28], 'CBo', '', '');
      document.getElementById(which).options[1] = opt;
      opt = new Option(texts[29], 'CBu', '', '');
      document.getElementById(which).options[2] = opt;
      opt = new Option(texts[30], 'CBs', '', '');
      document.getElementById(which).options[3] = opt;
      document.getElementById(which).options[4] = speccharOpt;
      document.getElementById(which).options[5] = delOpt;
    }
    else if ( markup.slice(0,2) == 'CB' ) {
      coordBox( document.getElementById(q_correct_qnr), document.getElementById(which).options, markup, texts );
      insertScore( qnr );
    }
    else if ( markup == '!!' ) {
      if ( confirm(texts[13]) ) {
        txt = document.getElementById(where).value.replace(/\{[^\}]+\} */g,'');
        txt = txt.replace(/\[[^\&]+\] */g,'');
        txt = txt.replace(/\w+\.\w\w\w(_\d+)* */g,'');
        document.getElementById(where).value = txt;
        document.getElementById(q_correct_qnr).value = '';
        if ( document.getElementById( 'negqstweight' ) ) {
          document.getElementById( 'Q_WEIGHT_' + qnr ).value = '';
        }
        // TODO: hints
        document.getElementById(qsttypename).value = '';
        reEstablishSelect( where );
      }
      return;
    }
    if ( markup == 'de' ) display(where);
    if ( markup == 'sf' ) {
      document.getElementsByName('myForm')[0].submit();
      return;
    }
  } //----------------------------------- if ( qsttype == '' )

  else if ( markup == '{}' ) {
    if ( mismatch( qsttype.charAt(1) == ']' || qsttype.charAt(0) == '$') ) return;
    markup = '{' + String.fromCharCode( qsttype.charCodeAt(0) + 1 ) + '}';
    insertAtCursor(document.getElementById(where), markup);
    if ( document.getElementById(q_correct_qnr).value == '' )
      if ( confirm(texts[1]) )
        document.getElementById(q_correct_qnr).value = markup;
    insertScore( qnr );
  }
  else if ( markup == '[]' ) {
    var x = qsttype.charAt(0);
    if ( mismatch( qsttype.charAt(1) == '}' || qsttype.charAt(0) == '$') ) return;
    markup = '[' + String.fromCharCode( qsttype.charCodeAt(0) + 1 ) + ']';
    insertAtCursor(document.getElementById(where), markup);
    if ( confirm(texts[2]) ) {
      document.getElementById(q_correct_qnr).value += markup;
    }
    insertScore( qnr );
  }
  else if ( markup == '[$]' || markup.substr(0,2) == '[!' || markup.substr(0,2) == '[~' ) {
    corr = document.getElementById(q_correct_qnr).value;
    if ( mismatch( qsttype != '$]' ) ) return;
    corr = prompt(texts[3],'');
    if ( ! corr ) return;
    loif = prompt(texts[4],'10');
    if ( ! loif ) return;
    document.getElementById(q_correct_qnr).value += markup.replace('$', corr + ' ');
    if ( loif.match(/^[a-z]$/) ) loif = loif + ' ';
    insertAtCursor(document.getElementById(where),'['+loif+']');
    insertScore( qnr );
  }
  else if ( markup == '[{$?$?$}]' ) {
    corr = prompt(texts[14],'');
    if ( ! corr ) return;
    loif = prompt(texts[4],'10');
    if ( ! loif ) return;
    document.getElementById(q_correct_qnr).value += markup.replace('$?$?$', corr);
    insertScore( qnr );
    markup = '[$]'; // for setting qsttype
    insertAtCursor(document.getElementById(where),'['+loif+']');
  }
  else if ( markup == '{$?$?$}' ) {
    corr = prompt(texts[5],'');
    if ( ! corr ) return;
    insertAtCursor(document.getElementById(where),markup.replace('$?$?$', corr));
    return;
  }
  else if ( markup == '{$}' || markup.substr(0,2) == '{!' || markup.substr(0,2) == '{~' ) {
    if ( mismatch( qsttype != '$}' ) ) return;
    corr = prompt(texts[7],'');
    document.getElementById(q_correct_qnr).value += markup.replace('$', corr + ' ');
    insertScore( qnr );
  }
  else if ( markup == '&' ) {
    char = prompt(texts[12],'');
    if ( ! char ) return;
    markup = char.replace(/(.)/g,'&'+'$1');
    insertAtCursor(document.getElementById(where), markup);
    return;
  }
  else if ( markup == 'I' ) {
    img = prompt(texts[20],'');
    if ( ! img ) return;
    insertAtCursor(document.getElementById(where), img + ' ');
    return;
  }
  else if ( markup == 'I2' ) {
    img = prompt(texts[20],'');
    if ( ! img ) return;
    width = prompt(texts[21],'');
    if ( ! width ) return;
    height = prompt(texts[22],'');
    if ( ! height ) return;
    img = img + '_' + width + '_' + height + ' ';
    insertAtCursor(document.getElementById(where), img);
    return;
  }
  else if ( markup.slice(0,2) == 'CB' ) {
    coordBox( document.getElementById(q_correct_qnr), document.getElementById(which).options, markup, texts );
    insertScore( qnr );
  }
  else if ( markup == '!!' ) {
    if ( confirm(texts[13]) ) {
      txt = document.getElementById(where).value.replace(/\{[^\}]+\} */g,'');
      txt = txt.replace(/\[[^\&]+\] */g,'');
      txt = txt.replace(/\w+\.\w\w\w(_\d+)* */g,'');
      document.getElementById(where).value = txt;
      document.getElementById(qsttypename).value = '';
      document.getElementById(q_correct_qnr).value = '';
      if ( document.getElementById( 'negqstweight' ) ) {
        document.getElementById( 'Q_WEIGHT_' + qnr ).value = '';
      }
      // TODO: hints
      reEstablishSelect( where );
    }
    return;
  }
  if ( markup == 'de' ) display(where);
  if ( markup == 'sf' ) {
    document.getElementsByName('myForm')[0].submit();
    return;
  }
  if ( markup != '' ) {
    qsttype =  markup.substr(markup.length-2); // length-2==-1 for markup=='$'
  }
  document.getElementById(qsttypename).value = qsttype;
}

function insert( where, ins ) {
  if ( hyperquiz )
    txt = prompt(hints[2],'');
  else
    txt = prompt(hints[1],'');
  if ( ! txt ) return;
  txt = ':: ' + ins + ' : ' + txt + ' ';
  insertAtCursor(document.getElementById(where), txt);
}

function insertHint( where, which ) {
  var idx = document.getElementById(which).selectedIndex;
  var selected = document.getElementById(which).options[idx].value;
  if ( ! selected ) {
    idx = document.all[which].selectedIndex;
    selected = document.all[which].options[idx].value;
  }
  var markup = selected;
  if ( markup == '' ) return;
  markup = markup.substr(0,3);
  document.getElementById(which).selectedIndex = 0;
//var i = which.indexOf('_') + 1;
//var qnr = which.slice(i);
//var qsttypename = 'qsttype_' + qnr;
//var qsttype = document.getElementById(qsttypename).value;
  hyperquiz = document.getElementById("hyperquiz").checked;

  if ( markup == 'unc' ) { // uncond. hint text for curr. answer round
    if ( hyperquiz )
      txt = prompt(hints[2],'');
    else
      txt = prompt(hints[1],'');
    if ( ! txt ) return;
    insertAtCursor(document.getElementById(where), txt);
    return;
  }
  else if ( markup == 'but' ) { // conditional hint for question with buttons
    ins = prompt(hints[8] + hints[3],'');
    insert( where, ins );
    return;
  }
  else if ( markup == 'num' ) { // insert cond. hint for incorr. number answer
    ins = prompt(hints[8] + hints[4],'');
    insert( where, ins );
    return;
  }
  else if ( markup == 'cor' ) { // insert corrective
    ins = prompt(hints[8] + hints[5],'');
    insertAtCursor(document.getElementById(where), ins);
    return;
  }
  else if ( markup == 'reg' ) { // insert regular expression
    ins = prompt(hints[8] + hints[6],'');
    insert( where, ins );
    return;
  }
  else if ( markup == 'nar' ) { // cond. hint with number of answer round
    ins = prompt(hints[8] + hints[7],'');
    if ( ins ) ins = '#' + ins;
    insert( where, ins );
    return;
  }
  else if ( markup == 'esa' ) { // start hint for (first/next) element
    insertAtCursor(document.getElementById(where), '[ ]');
    return;
  }
  else if ( markup == 'pos' ) { // pos. cursor for hint for next answer round
    alert(hints[9]);
  }
}

function adjustRows( where ) {
  var e = document.getElementById(where);
  var rows = e.rows;
  var n = e.value.replace(/[^\n]/g,'').length;
  if ( n > rows ) e.rows = n;
}

