/************************************************************/
/** Die Funktion Answer erzeugt das Objekt Antwort        **/
/** Answer.len enth�lt die Anzahl der m�glichen Antworten **/
/** Answer.bez enth�lt die Bezeichnungen der Antworten    **/
/** Answer.kor enth�lt die L�sungen                       **/
/************************************************************/
function Answer(MyText)
{
 awert = MyText.split("|");
 this.len = awert.length;
 this.bez = new Array();
 this.kor = new Array();

 for (i=0; i < this.len; i++)
 {
  twert = awert[i].split("~");
  this.bez[i] = twert[0];
  this.kor[i] = twert[1];
 }
}

/**************************************************************************************/
/** Die Funktion include_bild  was eine Vereinfachung, die in jeder Eingabezeile vorkommt **/
/**************************************************************************************/
function include_bild(Bild,Wd,Ht) {
 MyText =  "<IMG SRC= '" + Bild + "' WIDTH= '" + Wd + "' HEIGHT= '" + Ht + "' BORDER= '0' >";
 return MyText;
}

/**************************************************************************************/
/** Die Funktion include_ancor  was eine Vereinfachung, die in jeder Eingabezeile vorkommt **/
/**************************************************************************************/
function include_ancor(Mytext,href,name) {
 MyText = "<A HREF= '" + href + "' NAME= '" + name + "' TARGET= '_blank' >" + Mytext + "</A>";
 return MyText;
}

/**************************************************************************************/
/** Die Funktion einfach1  was eine Vereinfachung, die in jeder Eingabezeile vorkommt **/
/**************************************************************************************/
function einfach1(MyText) {
 Count++;
 ndwin.document.writeln ("     <tr>");
 ndwin.document.writeln ("      <td><table border='0' width='100%'><tr><td width='85'>Question " + Count + " : </td><td>" + MyText + "</td></tr></table></td>");
 ndwin.document.writeln ("      <td>");
 MyText = "Question_" + Count;
 return MyText;
}

/**************************************************************************************/
/** Die Funktion einfach2  was eine Vereinfachung, die in jeder Eingabezeile vorkommt **/
/**************************************************************************************/
function einfach2(MyText,x) {
 Alen[Count-1] = x;
 ndwin.document.writeln ("       <input type='hidden'  name='h_" + x + "' value='" + MyText + "'>");
 ndwin.document.writeln ("      </td>\n     </tr>");
}

/***********************************************************************/
/** Die Funktion cb_col_func erzeugt eine Eingabezeile mit Checkboxen **/
/***********************************************************************/
function cb_col_func(Quest,MyText) {
 with(ndwin) {
  with(document) {
   Test = new Answer(MyText);
   AName = einfach1(Quest);
   writeln ("       <table border='0' width='100%'>");
/*   writeln ("        <colgroup align='left' span='"+Test.len+"'></colgroup>"); */
   writeln ("        <tr align='left'>");
   for (i=0; i < Test.len; i++) {
    writeln ("         <td><input type='checkbox' name='" + AName + "'><br>" + Test.bez[i] + "</td>");
   }
   writeln ("        </tr>\n       </table>");
   einfach2(Test.kor.join(";"),Test.len);
  }
 }
}

/***********************************************************************/
/** Die Funktion cb_row_func erzeugt eine Eingabezeile mit Checkboxen **/
/***********************************************************************/
function cb_row_func(Quest,MyText) {
 with(ndwin) {
  with(document) {
   Test = new Answer(MyText);
   AName = einfach1(Quest);
   writeln ("       <table border='0' width='100%'>");
/*   writeln ("        <colgroup align='left' span='2'></colgroup>"); */
   for (i=0; i < Test.len; i++) {
    writeln ("        <tr align='left'><td width='25'><input type='checkbox' name='" + AName + "'></td><td align='left'>" + Test.bez[i] + "</td></tr>");
   }
   writeln ("       </table>");
   einfach2(Test.kor.join(";"),Test.len);
  }
 }
}

/*************************************************************************/
/** Die Funktion rb_col_func erzeugt eine Eingabezeile mit Radiobuttons **/
/*************************************************************************/
function rb_col_func(Quest,MyText) {
 with(ndwin) {
  with(document) {
   Test = new Answer(MyText);
   AName = einfach1(Quest);
   writeln ("       <table border='0' width='100%'>");
/*   writeln ("        <colgroup align='left' span='"+Test.len+"'></colgroup>"); */
   writeln ("        <tr align='left'>");
   for (i=0; i < Test.len; i++) {
    writeln ("         <td><input type='radio' name='" + AName + "'><br>" + Test.bez[i] + "</td>");
   }
   writeln ("        </tr>\n       </table>");
   einfach2(Test.kor.join(";"),Test.len);
  }
 }
}

/*************************************************************************/
/** Die Funktion rb_row_func erzeugt eine Eingabezeile mit Radiobuttons **/
/*************************************************************************/
function rb_row_func(Quest,MyText) {
 with(ndwin) {
  with(document) {
   Test = new Answer(MyText);
   AName = einfach1(Quest);
   writeln ("       <table border='0' width='100%'>");
/*   writeln ("        <colgroup align='left' span='2'></colgroup>"); */
   for (i=0; i < Test.len; i++) {
    writeln ("        <tr align='left'><td><input type='radio' name='" + AName + "'><br>" + Test.bez[i] + "</td></tr>");
   }
   writeln ("       </table>");
   einfach2(Test.kor.join(";"),Test.len);
  }
 }
}

/***********************************************************************/
/** Die Funktion pd_func erzeugt eine Eingabezeile mit Pull-Down-Men� **/
/***********************************************************************/
function pd_func(Quest,MyText,S) {
 with(ndwin) {
  with(document) {
   Test = new Answer(MyText);
   AName = einfach1(Quest);
   writeln ("       <select name='" + AName + "' size='" + S + "' multiple>");
   for (i=0; i < Test.len; i++) writeln ("        <option>" + Test.bez[i]);
   writeln ("       </select>");
   einfach2(Test.kor.join(";"),Test.len);
  }
 }
}

/*****************************************************************/
/** Die Funktion tf_func erzeugt eine Eingabezeile mit Textfeld **/
/*****************************************************************/
function tf_func(Quest,MyText) {
 with(ndwin) {
  with(document) {
   AName = einfach1(Quest);
   writeln ("       <input type='text' name='" + AName + "' size='" + MyText.length + "'>");
   einfach2(MyText,1);
  }
 }
}

/*******************************************************************/
/** Die Funktion spacer erzeugt eine optische Trennung von Fragen **/
/** Dicke der Trennung in Pixels �bergebbar                       **/
/*******************************************************************/
function spacer(thick) {
 ndwin.document.writeln ("       <tr><td colspan='2' height='"+thick+"' bgcolor='#d0d0c0'></td></tr>");
}

/***********************************************************************/
/** Die Funktion startwin schreibt den Head-Tag mit �bergebenem Titel **/
/** und den Beginn des Body-, Formular-, center- und Table-Tags       **/
/***********************************************************************/
function startwin(txt,ql,hd) {
 browser = true;
 with (ndwin) {
  with (document) {
   writeln ("<html>\n <head><title>" + txt + "</title></head>");
   if (browser) writeln (" <body bgcolor='#d0d0c0' onMousedown='StopIt(event)' onKeydown='StopIt(event)'>");
   else writeln (" <body bgcolor='#d0d0c0'>");
   writeln ("  <center>");
   writeln ("  <H2>"+hd+"</H2>");
   writeln ("   <form name='Testform'>");
   writeln ("    <table width='100%' border='1' bgcolor='#f0f0e0'>");
   writeln ("     <tr><th width='"+ql+"%'>Questions :</th><th>Answers :</th></tr>");
  }
 }
}

/*****************************************************************/
/** Die Funktion endwin schlie�t die obigen Tags und enth�lt    **/
/** die Auswertungsfunktionen im Fensterinternen Script-Bereich **/
/** dabei wird mit der Variablen browser unterschieden ob       **/
/** browser == true  : Internet Explorer                        **/
/** browser == false : Netscape Navigator                       **/
/*****************************************************************/
function endwin(Max) {
 with (ndwin) {
  with (document) {
   writeln ("    </table><br>");
   if (browser) {
    writeln ("    <div id='Auswertung'><input type='button' value='Evaluate Input!' onClick='checkChoices(1,"+Max+")'></div>");
   }
   else {
    writeln ("    <input type='button' value='Evaluate answers!'  onClick='checkChoices("+Max+")'><br>");
    writeln ("    <input type='hidden' name='VirtCount' value='1'>");
   }

   writeln ("    <br><input type='button' value='Close Window!' onClick='self.close()'>");
   writeln ("   </form>\n  </center>");
   writeln ("  <script language=\"JavaScript\">");
   writeln ("   k = 0;");
   write   ("   LLength = new Array(");

   for (i=0; i < Count; i++) {
    if ( i < Count-1) write (Alen[i]+",");
    else write (Alen[i]+");\n");
   }

   if (browser) {
    writeln ("   function StopIt(e) {");
    writeln ("    atxt = \"Nicht gestattet!\";");
    writeln ("    if (e.button == 2) {");
    writeln ("     alert(atxt);");
    writeln ("     return;");
    writeln ("    }");
    writeln ("    if (e.shiftKey && e.keyCode == 121) {");
    writeln ("     alert(atxt);");
    writeln ("     return;");
    writeln ("    }");
    writeln ("    if (e.keyCode == 93) {");
    writeln ("     alert(atxt);");
    writeln ("     return;");
    writeln ("    }");
    writeln ("   }\n");
   }

   writeln ("   function research() {");
   writeln ("    for (i=0; i < " + Count + "; i++) {");
   writeln ("     tp = document.forms[0].elements[k].type;");
   writeln ("     switch (tp) {");
   writeln ("      case \"text\":");
   writeln ("       Eingabe = document.forms[0].elements[k].value;");
   writeln ("       k++;");
   writeln ("       Loesung[i] = document.forms[0].elements[k].value;");
   writeln ("       k++;");
   writeln ("       Suche = /^ /;");
   writeln ("       while (Suche.test(eingabe)) eingabe = eingabe.replace(/^ /,\"\");");
   writeln ("       Suche = / $/;");
   writeln ("       while (Suche.test(eingabe)) eingabe = eingabe.replace(/ $/,\"\");");
   writeln ("       break;\n");
   writeln ("      case \"select-multiple\":");
   writeln ("       inp = new Array();");
   writeln ("       max = document.forms[0].elements[k].length;");
   writeln ("       for (x=0; x < max; x++) inp[x] = (document.forms[0].elements[k].options[x].selected == true) ? \"L\" : \"0\";");
   writeln ("       Eingabe = inp.join(\";\");");
   writeln ("       k++;");
   writeln ("       Loesung[i] = document.forms[0].elements[k].value;");
   writeln ("       k++;");
   writeln ("       break;\n");
   writeln ("      default:");
   writeln ("       inp = new Array();");
   writeln ("       for (x=0; x < LLength[i]; x++)  inp[x] = (document.forms[0].elements[k+x].checked == true) ? \"L\" : \"0\";");
   writeln ("       k += x - 1;");
   writeln ("       Eingabe = inp.join(\";\");");
   writeln ("       k++;");
   writeln ("       Loesung[i] = document.forms[0].elements[k].value;");
   writeln ("       k++;");
   writeln ("       break;");
   writeln ("     }");
   writeln ("     comp[i] = (Eingabe == Loesung[i]) ? 1 : 0;");
   writeln ("    }");
   writeln ("   }\n");

   if (browser) {
    writeln ("   function restarter(Vcnt,Mx) {");
/*    writeln ("    document.forms[0].reset();");*/
    writeln ("    inText = \"<input type='button' value='Evaluate Input!' onClick='checkChoices(\"+Vcnt+\",\"+Mx+\")'>\";");
    writeln ("    document.all.Auswertung.innerHTML = inText;");
    writeln ("   }\n");
   }

   writeln ("   function letzteW(MyText,i) {");
   writeln ("    inp = new Array();");
   writeln ("    chk = new Array();\n");
   writeln ("    inp = MyText.split(\";\");");
   writeln ("    itx = \"<td>The correct answer to question</td><td>\" + (i+1) + \"</td><td>would have been</td><td>\";");
   writeln ("    for (x=0; x < inp.length; x++) {");
   writeln ("     if (inp[x] != 'L' && inp[x] != '0') {");
   writeln ("      ap = chk.length;");
   writeln ("      chk[ap] = \"'\" + inp[x] + \"'\";");
   writeln ("     }");
   writeln ("     if (inp[x] == 'L') {");
   writeln ("      ap = chk.length;");
   writeln ("      chk[ap] = x+1;");
   writeln ("     }");
   writeln ("    }");
   writeln ("    itx += \"Nr. \"+chk.join(\" & \") + \"</td><td> .</td>\";");
   writeln ("    return itx;");
   writeln ("   }\n");

   if (browser) writeln ("   function checkChoices(Vcnt,Mx) {");
   else writeln ("   function checkChoices(Mx) {");
   writeln ("    k=0;");
   if (!browser) writeln ("    Vcnt = document.forms[0].VirtCount.value;");
   writeln ("    inText = \"\";");
   write ("    comp    = new Array(");
   txt = "";
   for (i=0; i<Count;i++) txt += (i < Count-1) ? "0," : "0);\n";
   write (txt);
   write ("    Loesung = new Array(");
   txt = "";
   for (i=0; i<Count;i++) txt += (i < Count-1) ? "0," : "0);\n";
   writeln (txt);
   writeln ("    research();");
   writeln ("    x = 0;");
   writeln ("    for (i=0; i < " + Count + "; i++) x += comp[i];");
   writeln ("    ready = ( x == " + Count + ") ? true : false;");
   writeln ("    if (ready) {");
   writeln ("     inText +=  \"You answered  all " + Count + " questions correctly.<br><br>\";");
   writeln ("     inText += \"</table><br>Bye now. See you next time.\";");
   if (!browser) writeln ("     inText += \"<br>\\n<form><input type='button' value='Ausgang' onClick='exits()'></form>\";");
   writeln ("    }");
   writeln ("    else {");
   writeln ("     x = 0;");
   writeln ("     Vcnt++;");
   writeln ("     if (Vcnt > Mx) {");
   writeln ("      inText = \"<table border=0><colgroup><col align='left'><col align='right'><col align='center'><col align='center'><col align='right'></colgroup>\";");
   writeln ("      for (i=0; i < " + Count + "; i++) inText += \"<tr>\" + letzteW(Loesung[i],i) + \"</tr>\";");
   writeln ("      inText += \"</table><br>Bye now. See you next time.\";");
   if (!browser)
   writeln ("      inText += \"<br>\\n<form><input type='button' value='Ausgang' onClick='exits()'></form>\";");
   writeln ("     }");
   writeln ("     else {");
   writeln ("      if (Vcnt < Mx) {");
   writeln ("       for (i=0; i < " + Count + "; i++) x += comp[i];");
   writeln ("       inText +=  \"You answered \" + x + \" of " + Count + " questions correctly.<br><br>\";");
   writeln ("      }");
   writeln ("      else {");
   writeln ("       for (i=0; i < " + Count + "; i++) {");
   writeln ("        inText += \"Question \" + (i+1) + \"  was \";");
   writeln ("        if (comp[i] == 1) inText += \"correctly\";");
   writeln ("        else inText += \"wrongly\";");
   writeln ("        inText += \" answered.<br>\";");
   writeln ("       }");
   writeln ("      }");
   if (browser)
   writeln ("      inText += \"<br>\\n<input type='button' value='Go to \" + Vcnt + \". attempt!' onClick='restarter(\"+Vcnt+\",\"+Mx+\")'>\";");
   else
   writeln ("      inText += \"<br>\\n<form><input type='button' value='Go to \" + Vcnt + \". !' onClick='restarter(\"+Vcnt+\")'></form>\";");
   writeln ("     }");
   writeln ("    }");

   if (browser) writeln ("    document.all.Auswertung.innerHTML = inText;");
   else {
/**********************************************************************/
/** F�r den Netscape Navigator mu� ein neues Fenster ge�ffnet werden **/
/**********************************************************************/
    writeln ("    ewin = open(\"about:blank\",\"WertungsFenster\",\"width=500,height=300,status=no,menubar=no,resizable=yes,scrollbars=no\");");
    writeln ("    ewin.moveTo(0,0);\n");
    writeln ("    with (ewin) {");
    writeln ("     with (document) {");
    writeln ("      writeln (\"<html><head><title>\"+(Vcnt-1)+\". Ergebnis</title></head>\");");
    writeln ("      writeln (\"<body bgcolor='#e0f0e0'>\");");
    writeln ("      writeln (\"<center>\"+inText+\"</center>\");");
    writeln ("      writeln (\"<script language='JavaScript'>\");");
    writeln ("      writeln ('function restarter(x) {');");
/*    writeln ("      writeln ('opener.document.forms[0].reset();');");*/
    writeln ("      writeln ('opener.document.forms[0].VirtCount.value = x;');");
    writeln ("      writeln ('self.close();');");
    writeln ("      writeln ('}');");
    writeln ("      writeln ('function exits() {');");
    writeln ("      writeln ('opener.close();');");
    writeln ("      writeln ('self.close();');");
    writeln ("      writeln ('}');");
    writeln ("      write ('</script');");
    writeln ("      writeln (\">\\n</body>\");");
    writeln ("      writeln (\"</html>\");");
    writeln ("     }");
    writeln ("    }");
   }
   writeln ("   }");
   writeln ("  </script>\n </body>\n</html>");
  }
 }
}
