function onLoad() {
  var qnr = 1;
  while ( 1 ) {
    var qnrStr = qnr.toString();
    var qsttypeName = 'qsttype_' + qnrStr;
    if ( document.getElementById(qsttypeName) == null ) return;
    var phraseName = 'q_phrase_' + qnrStr;
    var phraseValue = document.getElementById(phraseName).value;
    if ( phraseValue == '' ) return;
    var qsttype = document.getElementById(qsttypeName).value;
    var which = 'insrtslct0_' + qnrStr;
    if ( qsttype.slice(-1) == '}' ) {
      document.getElementById(which).options[2] = null;
      document.getElementById(which).options[2] = null;
    }
    else if ( qsttype.slice(-2,2) == '$]' ) {
      document.getElementById(which).options[1] = null;
      document.getElementById(which).options[1] = null;
    }
    else if ( qsttype ) { // qsttype.slice(-1) == ']'
      document.getElementById(which).options[1] = null;
      document.getElementById(which).options[2] = null;
    }
    qnr = qnr + 1;
  }
}

//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[6], quizname);
      if ( ! quiz ) return;
      img = prompt(texts[7], '');
      if ( ! img ) return;
      markup = '<img src="' + quiz + '/' + img + '" /> ';
      insertAtCursor(document.getElementById(where), markup);
      return;
    }
    else {
      loc = prompt(texts[8], 'http://');
      if ( ! loc ) return;
      markup = '<img src="' + loc + '" /> ';
      insertAtCursor(document.getElementById(where), markup);
      return;
    }
  }
  if ( markup == 'a' ) {
    loc = prompt(texts[9], 'http://');
    if ( ! loc ) return;
    txt = prompt(texts[10], '');
    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[14], '');
    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 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;
  if ( markup.charAt(0) == '<' ) {
    insertAtCursor(document.getElementById(where), 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_WEIGHT_qnr = 'Q_WEIGHT_' + qnr;
  if ( qsttype == '' ) {
    if ( markup == '{}' ) {
      markup = '{a}'
      score = prompt(texts[1],'0');
      if ( ! score ) return;
      insertAtCursor(document.getElementById(where), markup);
      document.getElementById(Q_WEIGHT_qnr).value = score + ' ';
      for ( var i = 0; i <=1; ++ i )
        document.getElementById(which).options[2] = null;
    }
    else if ( markup == '[]' ) {
      markup = '[a]';
      insertAtCursor(document.getElementById(where), markup);
      score = prompt(texts[2],'0');
      if ( ! score ) return;
      document.getElementById(Q_WEIGHT_qnr).value = score + ' ';
      document.getElementById(which).options[1] = null;
      document.getElementById(which).options[2] = null;
    }
    else if ( markup == '[$]' ) {
      loif = prompt(texts[3],'10');
      if ( ! loif ) return;
      insertAtCursor(document.getElementById(where),'['+loif+']');
      for ( var i = 0; i <= 1; i++ )
        document.getElementById(which).options[1] = null;
    }
    else if ( markup == '&' ) {
      char = prompt(texts[4],'');
      if ( ! char ) return;
      markup = char.replace(/(.)/g,'&$1');
      insertAtCursor(document.getElementById(where), markup);
    }
    else if ( markup == 'I' ) {
      img = prompt(texts[11],'');
      if ( ! img ) return;
      insertAtCursor(document.getElementById(where), img + ' ');
      return;
    }
    else if ( markup == 'I2' ) {
      img = prompt(texts[11],'');
      if ( ! img ) return;
      width = prompt(texts[12],'');
      if ( ! width ) return;
      height = prompt(texts[13],'');
      if ( ! height ) return;
      img = img + '_' + width + '_' + height;
      insertAtCursor(document.getElementById(where), img + ' ');
      return;
    }
    else if ( markup == '!!' ) {
      if ( confirm(texts[5]) ) {
        txt = document.getElementById(where).value.replace(/\{[a-z]\} */g,'');
        txt = txt.replace(/\[[a-z]\] */g,'');
        txt = txt.replace(/\[\d+\] */g,'');
        document.getElementById(where).value = txt;
        document.getElementById(Q_WEIGHT_qnr).value = '';
        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 ) + '}';
    score = prompt(texts[1],'0');
    if ( ! score ) return;
    insertAtCursor(document.getElementById(where), markup);
    document.getElementById(Q_WEIGHT_qnr).value += score + ' ';
    if ( document.getElementById(which).options.length == 6 )
      for ( var i = 0; i <=1; ++ i )
        document.getElementById(which).options[2] = null;
  }
  else if ( markup == '[]' ) {
    if ( mismatch( qsttype.charAt(1) == '}' || qsttype.charAt(0) == '$') ) return;
    markup = '[' + String.fromCharCode( qsttype.charCodeAt(0) + 1 ) + ']';
    score = prompt(texts[2],'0');
    if ( ! score ) return;
    insertAtCursor(document.getElementById(where), markup);
    document.getElementById(Q_WEIGHT_qnr).value += score + ' ';
    if ( document.getElementById(which).options.length == 6 ) {
      document.getElementById(which).options[1] = null;
      document.getElementById(which).options[2] = null;
    }
  }
  else if ( markup == '[$]' ) {
    if ( mismatch( qsttype != '$]' ) ) return;
    loif = prompt(texts[3],'10');
    if ( ! loif ) return;
    insertAtCursor(document.getElementById(where),'['+loif+']');
    if ( document.getElementById(which).options.length == 6 )
      for ( var i = 0; i <= 1; i++ )
        document.getElementById(which).options[1] = null;
  }
  else if ( markup == '&' ) {
    char = prompt(texts[4],'');
    if ( ! char ) return;
    markup = char.replace(/(.)/g,'&'+'$1');
    insertAtCursor(document.getElementById(where), markup);
    return;
  }
  else if ( markup == 'I' ) {
    img = prompt(texts[11],'');
    if ( ! img ) return;
    insertAtCursor(document.getElementById(where), img + ' ');
    return;
  }
  else if ( markup == 'I2' ) {
    img = prompt(texts[11],'');
    if ( ! img ) return;
    width = prompt(texts[12],'');
    if ( ! width ) return;
    height = prompt(texts[13],'');
    if ( ! height ) return;
    img = img + '_' + width + '_' + height;
    insertAtCursor(document.getElementById(where), img + ' ');
    return;
  }
  else if ( markup == '!!' ) {
    if ( confirm(texts[5]) ) {
      txt = document.getElementById(where).value.replace(/\{[a-z]\}/g,'');
      txt = txt.replace(/\[[a-z]\]/g,'');
      txt = txt.replace(/\[\d+\]/g,'');
      document.getElementById(where).value = txt;
      document.getElementById(Q_WEIGHT_qnr).value = '';
      document.getElementById(qsttypename).value = '';
      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);
  }
  document.getElementById(qsttypename).value = qsttype;
}

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;
}

