// pour l'instant, la fonction alea() a été supprimée et remplacée par i++;  à la fonction question();


/* ************************************************** VARIABLES  *********************************** */

var questnb = 0 //pour afficher les n° des questions
var point = 0;  //pour l'évaluation de l'exercice
var dmpoint=0;  //demi-point éventuel si variable = 1 et bonne réponse à la 2ème tentative
var i = 0;
var djf = new Array;
var reponse = new Array;
var nbquestfaite = 0; // pour incrémenter le nombre de questions faites
var note = "";

var now = new Date();
var tpsdebut = now.getTime();

var mot = "";
var face = "";
var appreciation = new Array;
appreciation[0] = "Was that a joke?<br><br>";
appreciation[1] = "Maybe that was just an accident? &nbsp; Try to find some help.<br>";
appreciation[2] = "You can do better. Don't give up!<br><br>";
appreciation[3] = "Take courage! You should improve.<br><br>";
appreciation[4] = "You're on the right way. <br>Try again!<br>";
appreciation[5] = "Not really bad, not good yet... <br>Keep on trying!<br>";
appreciation[6] = "Above average, but it can still be improved.<br>";
appreciation[7] = "Not bad at all...<br><br>";
appreciation[8] = "Good work!<br><br>";
appreciation[9] = "You're so good at it!<br><br>";
appreciation[10] ="Excellent!<br><br>";

/* ****************************************** AFFICHAGE QUESTION *********************************** */

	function remettreazero()         // effacement du commentaire de la réponse précédente
        {var cdrecommentaire = document.getElementById("cdrecommentaire");
		 var commentaire = document.getElementById("commentaire");
         var commentaireplus = document.getElementById("commentaireplus");
         document.getElementById("cdrecommentaire").style.backgroundColor="#F3F0F7"; //rétablit la couleur de fond
         commentaire.innerHTML = "";  commentaireplus.innerHTML = "";         
		 }

    function question()              // réinitialisation
        {dmpoint=0; 
		var cdrequest = document.getElementById("cdrequest");  
		var cdreboutons = document.getElementById("cdreboutons");
		var cdreinfo = document.getElementById("cdreinfo");		
        var cdrecommentaire = document.getElementById("cdrecommentaire"); 
		remettreazero(); 
		questnb = i+1; info();  if(im[i]==undefined){im[i]=""};         
				 
		cdrequest.innerHTML = '<img src="trainpic/bottmleft2.gif" class="bl"><img src="trainpic/bottmright2.gif" class="br">'
		 
									  // ÉCRITURE QUESTION
         cdrequest.innerHTML += tab[i];   
		 cdrecommentaire.innerHTML += '';
		 if (djf[i]==1)					// = si question déjà traitée
			  {ecritdjfait();}
		 else {ecritafaire()};												
		 
		 if (nbquestfaite == nbmax) {btnresultat()} // si toutes questions faites
		 else if (djf[i]==1) {btnquestionfaite();}	// = si question déjà traitée			  
		 else {btncheckquestion();};		 
        }

	function ecritafaire()										// qcm si question non encore faite
		{var cdrequest = document.getElementById("cdrequest");  var cdreboutons = document.getElementById("cdreboutons");
		 cdrequest.innerHTML +='<br><br><input type="radio" name="reponse" value=A id="pret">' + '<span style="position:relative; top:-3px;"> ' + choix[i]  + '</span>'; 
         cdrequest.innerHTML +='<br>	<input type="radio" name="reponse" value=B>' + '<span style="position:relative; top:-3px;"> ' + choix2[i]  + '</span>';
         cdrequest.innerHTML +='<br>	<input type="radio" name="reponse" value=C>' + '<span style="position:relative; top:-3px;"> ' + choix3[i]  + '</span>';
         if (choix4[i]!=="")                                                                    // en cas de 4ème choix
			{cdrequest.innerHTML +='<br><input type="radio" name="reponse" value=D>' + '<span style="position:relative; top:-3px;"> ' + choix4[i]   + '</span>' };
		 document.getElementById("pret").focus();
		}

	function ecritdjfait()										// qcm désactivé si question déjà traitée
		{var cdrequest = document.getElementById("cdrequest");  var cdreboutons = document.getElementById("cdreboutons");
		 cdrequest.innerHTML +='<br><br>&nbsp;<img src="trainpic/checkcircle.gif" style="position:relative; top:0px;">&nbsp;<span style="position:relative; top:-3px; color:#9f9f9f;"> ' + choix[i]  + '</span>'; 
         cdrequest.innerHTML +='<br>&nbsp;<img src="trainpic/checkcircle.gif" style="position:relative; top:2px;">&nbsp;<span style="position:relative; top:1px;  color:#9f9f9f;"> ' + choix2[i]  + '</span>';
         cdrequest.innerHTML +='<br>&nbsp;<img src="trainpic/checkcircle.gif" style="position:relative; top:2px;">&nbsp;<span style="position:relative; top:1px;  color:#9f9f9f;"> ' + choix3[i]  + '</span>';
         if (choix4[i]!=="")                                                                    // en cas de 4ème choix
			{cdrequest.innerHTML +='<br>&nbsp;<img src="trainpic/checkcircle.gif" style="position:relative; top:2px;">&nbsp;<span style="position:relative; top:1px;  color:#9f9f9f;"> ' + choix4[i]   + '</span>' };
		
												// commentaire si question déjà traitée
		var commentaire = document.getElementById("commentaire");
        commentaire.innerHTML  = '<span class="intro">The correct answer was:<br></span>';
        commentaire.innerHTML += '<span class="comment">' + brep[i] + ' </span>';
		if (reponse[i]==brep[i]){commentaire.innerHTML += '<span class="introplus"><br>Your answer was correct!</span>';}
		else if (!reponse[i]) {commentaire.innerHTML += '<span class="introplus" style="top:10px;">You gave no answer!';}
		else{
			commentaire.innerHTML += '<span class="introplus">Your answer:&nbsp; <br></span>';
			commentaire.innerHTML += '<span class="commentplus">' + reponse[i] + ' </span>'}
		}
		
/* *************************************** ANALYSE DES REPONSES ************************************ */

   function verif()                 //vérification de la réponse
       {nbquestfaite++;				// nombre de questions faites
	    djf[i] = 1;					// 1 = question déjà faite
	    casecoche();                // verification des cases cochées
        reponse[i]=rep;
		if (rep == brep[i]){displaygood()}        // réponse juste
        else {displaywrong()}                     // réponse fausse
        }

   function casecoche()
       {rep=document.form1.reponse;
            for(coche=0; rep.length; coche++)			// attribution de la valeur de la case cochée à la réponse
               {if (rep[coche].checked)  {rep=rep[coche].value; break;}
                else if (!rep[coche].checked && coche==rep.length-1) // si aucune case cochée
					{nbquestfaite--; djf[i] = 0;
					 alert("Choose an answer!")} 
               }
             switch (rep)								// réponse choisie par l'utilisateur
             {case "A":  rep=choix[i];  break;      case "B":  rep=choix2[i]; break;
              case "C":  rep=choix3[i]; break;      case "D":  rep=choix4[i]; break;}
            }
/* *************************************** AFFICHAGE COMMENTAIRES *********************************** */

   function displaywrong()
         {var commentaire = document.getElementById("commentaire");
		  attribnote();
          commentaire.innerHTML = '<span class="rightwrong"><br> Sorry... the correct answer was:<br></span>';
          commentaire.innerHTML +='<span class="comment">' + brep[i] + ' </span><br>';
		  commentaire.innerHTML +='<span class="commentplus">' + note + '&nbsp;  &nbsp; </span>';
		  dmpoint=1;          // signale une réponse fausse pour les points
		  point = point-0.5;
		  info();
		  if (nbquestfaite<nbmax) {btnquestionfaite()}
		  else {btnresultat()}
		  }

   function displaygood()
         {var commentaire = document.getElementById("commentaire");
		  attribnote();
          remettreazero();
          point++; info();
          commentaire.innerHTML = '<span class="rightwrong"><br>Your answer is right!</span><br>';
		  commentaire.innerHTML +='<span class="commentplus">' + note + ' </span>';
		  if (nbquestfaite<nbmax) {btnquestionfaite()}
		  else {btnresultat()}
          }

	function attribnote()	 
		{switch (reponse[i])
			{case choix[i]: note = rem[i]; break;    case choix2[i]: note = rem2[i]; break;
			 case choix3[i]: note = rem3[i]; break;  case choix4[i]: note = rem4[i]; break;}
		} 
		 
   function affbreponse()           // affiche réponse juste si "don't know"
       {var commentaire = document.getElementById("commentaire");
        commentaire.innerHTML =  '<span class="intro"><br>The correct answer was...<br></span>';
        commentaire.innerHTML += '<span class="comment">&nbsp; ' + brep[i] + '&nbsp;  &nbsp; </span><br>';
        djf[i] = 1;
		nbquestfaite++;	
		if (nbquestfaite<nbmax) {btnquestionfaite()}
        else {btnresultat()}
        }

	function info()
		{var cdreinfo = document.getElementById("cdreinfo");
		 cdreinfo.innerHTML = '<img src="trainpic/topleft3.gif" class="tl" style="top:-2px; left:-2px;" alt="">';
		 cdreinfo.innerHTML += '<img src="trainpic/bottmleft3.gif" class="bl" style="bottom:-2px; left:-2px;" alt="">';
		 cdreinfo.innerHTML += '<span style="position:relative; top:6px">n° ' + questnb + "/" + nbmax +'</span>';
		 cdreinfo.innerHTML += '<div class="milieu"></div>';
		 cdreinfo.innerHTML += '<br><br><br><span style="font-size:9pt; position:relative;top:3px;">score: ' + point + '</span>';	 
		}		
	
/* *********************************************  BOUTONS *********************************************** */

	function btncheckquestion()                   // bouton check + navigation
         {var cdreboutons = document.getElementById("cdreboutons"); 
         cdreboutons.innerHTML = '<img src="trainpic/topleft3.gif" class="tl"><img src="trainpic/topright3.gif" class="tr"><img src="trainpic/bottmleft3.gif" class="bl"><img src="trainpic/bottmright3.gif" class="br">'
		 cdreboutons.innerHTML += '<input type="button" class="bouton"  value=" Check answer" onclick=verif()>'; 
         cdreboutons.innerHTML += '<br><input type="button" class="bouton" value="I don\'t know!" onclick=affbreponse()><br>';
		 navigation(); bouton = 2;
         }
		 
	function btnquestionfaite()                  // bouton check désactivé + navigation
         {var cdreboutons = document.getElementById("cdreboutons"); 
         cdreboutons.innerHTML = '<img src="trainpic/topleft3.gif" class="tl"><img src="trainpic/topright3.gif" class="tr"><img src="trainpic/bottmleft3.gif" class="bl"><img src="trainpic/bottmright3.gif" class="br">'
		 cdreboutons.innerHTML += '<input type="button" class="bouton"  value=" Check answer" disabled=true>'; 
         cdreboutons.innerHTML += '<br><input type="button" class="bouton" value="I don\'t know!" disabled=true><br>';
		 navigation(); if (i<nbmax-1){bouton=1}
         }		 

	function navigation()
		{var cdreboutons = document.getElementById("cdreboutons");
			if (i==0) {cdreboutons.innerHTML += '<input type="button"  value=" <= " class="btnav" disabled=true ><input type="button"  value=" => " class="btnav" onclick=avancer()></form>';}
			else if (i==nbmax-1){cdreboutons.innerHTML += '<input type="button"  value=" <= " class="btnav" onclick=reculer()><input type="button"  value=" => " class="btnav" disabled=true></form>';}
			else {cdreboutons.innerHTML += '<input type="button"  value=" <= " class="btnav" onclick=reculer()><input type="button"  value=" => " class="btnav" onclick=avancer() ></form>';}
		}
		function avancer()
		{i++; onclick=question();}		 
		function reculer()
		{i--; onclick=question();}		 
		
		
   function btnresultat()                       // bouton pour voir résultats
           {var cdreboutons = document.getElementById("cdreboutons");
            cdreboutons.innerHTML = '<img src="trainpic/topleft3.gif" class="tl"><img src="trainpic/topright3.gif" class="tr"><img src="trainpic/bottmleft3.gif" class="bl"><img src="trainpic/bottmright3.gif" class="br">'
			cdreboutons.innerHTML += '<input type="button" class="bouton"  value=" See results" onclick=results()>'; 
			cdreboutons.innerHTML += '<div class="ligne"></div>';
			navigation();
            bouton = 4;
            }

												// test touche entrée 

   function test_entree() // si touche entrée redirection comme si appui sur bouton correspondant
       {switch (bouton)
        {case 1: avancer(); break;
         case 2: verif(); break;
         case 3: affbreponse(); break;
         case 4: results();break;
         case 5: recommencer(); break;}
         }
			
			
/* ****************************************** FIN D'EXERCICE *************************************** */

   function results()
        {
                  var fin = new Date();
                  var tpsfin = fin.getTime();
                  var duree = tpsfin - tpsdebut;     //durée en millisecondes
                  seconde = Math.floor(duree/1000);  //en secondes
                  minute = Math.floor(seconde/60);   //en minutes
                  reste = seconde-(minute*60);       // secondes restantes

          remettreazero(); affresults();
          bouton = 5; /* ATTENTION  bouton = 5 différent dans phrases à trous */
          btnfin();
        }

   function affresults()
         {var cdrecommentaire = document.getElementById("cdrecommentaire");
		  var commentaire = document.getElementById("commentaire");
          var cdreinfo = document.getElementById("cdreinfo");
		  var mn = "";
		  var scd = "";
          if (point <0) {point = 0};
		  info();
		  if (nbmax ==8) {convertir()};
		  if (nbmax ==5) {point = point*2; nbmax = nbmax*2};
		  if (memo =="oui" && point == nbmax) {point-=1};
		  infovide();		  
		  commentaire.innerHTML = '<br><span class="introfinale" style="position:relative; top:-5px;">Your final score: </span>';
          commentaire.innerHTML += '<span class="introresult" style="position:relative; top:-5px;">' + point + ' / ' + nbmax + '</span>';
          commentairefinal();
		  commentaire.innerHTML += '<br><span class="appreciation">' + mot + '</span>';
		  // commentaire.innerHTML += '<span class="introfinale" style="font-size:8pt;">Exercise completed in: </span>';
		  // if (minute ==1) {mn='minute'} else if (minute > 1) {mn = 'minutes'} else {minute = ''; mn=''};
		  //if (reste ==1) {scd='second'} else if (reste > 1) {scd = 'seconds'} else {scd = ''; reste=''};
		  //commentaire.innerHTML += '<span class="intro" style="font-size:8pt; font-style:italic;">' + minute + '&nbsp;' + mn + '&nbsp;' + reste + '&nbsp;' + scd +'</span>';
		  if (memo =="oui") {commentaire.innerHTML += '<br /><span class="intro" style="font-size:8pt;">N.B. 9/10 is the maximum score when <i>Memo</i> is used.</span><br />&nbsp;'};
		  imagefinale();
		  cdrecommentaire.innerHTML += face;		  
        }

	function infovide()
		{var cdreinfo = document.getElementById("cdreinfo");
		 cdreinfo.innerHTML = '<img src="trainpic/topleft3.gif" class="tl" style="top:-2px; left:-2px;" alt="">';
		 cdreinfo.innerHTML += '<img src="trainpic/bottmleft3.gif" class="bl" style="bottom:-2px; left:-2px;" alt="">';
		 cdreinfo.innerHTML += '<span style="position:relative; top:6px"></span>';
		 cdreinfo.innerHTML += '<div class="milieu"></div>';
		 cdreinfo.innerHTML += "<br><br><br><span position:relative;top:3px;;></span>";		
		}		
		
   function convertir() // met les notes sur 8 sur 10
	   {var recalcul = point *5;  recalcul = recalcul/4; nbmax = 10; // ligne pour 8 questions
		var arrondibas = Math.floor(recalcul);    var arrondihaut = Math.ceil(recalcul);   var milieu = arrondibas + 0.5;
			 if (recalcul > arrondibas && recalcul < (arrondibas + 0.26))  		{point = arrondibas} //entre x et x.25 inclus 			
		else if (recalcul < arrondihaut && recalcul > (milieu + 0.25))   		{point = arrondihaut} // entre x.76 inclus et x +1	
		else if (recalcul > (arrondibas + 0.25) && recalcul <= (milieu + 0.25)) {point = milieu} //entre x.26 inclus et x.75 inclus	
		else    {point = recalcul};
		}			
		
		
   function commentairefinal()
		{switch (point)
		{case 0: mot = appreciation[0]; break;	case 0.5: mot = appreciation[1]; break;
		 case 1: mot = appreciation[1]; break;	case 1.5: mot = appreciation[1]; break;
		 case 2: mot = appreciation[2]; break;	case 2.5: mot = appreciation[2]; break;
		 case 3: mot = appreciation[3]; break;	case 3.5: mot = appreciation[3]; break;
		 case 4: mot = appreciation[4]; break;	case 4.5: mot = appreciation[4]; break;
		 case 5: mot = appreciation[5]; break;	case 5.5: mot = appreciation[5]; break;
		 case 6: mot = appreciation[6]; break;	case 6.5: mot = appreciation[6]; break;
		 case 7: mot = appreciation[7]; break;	case 7.5: mot = appreciation[7]; break;
		 case 8: mot = appreciation[8]; break;	case 8.5: mot = appreciation[8]; break;
		 case 9: mot = appreciation[9]; break;	case 9.5: mot = appreciation[9]; break;
		 case 10:mot = appreciation[10];break;	case 10.5: mot = appreciation[10];break;	
		}}
	
   function imagefinale()
		{switch (point)
		{case 0: face = '<img src="trainpic/face0.gif" style="position:absolute; top:15px; right:20px;">'; break;	case 0.5: face = '<img src="trainpic/face0.gif" style="position:absolute; top:15px; right:20px;">'; break;
		 case 1: face = '<img src="trainpic/face1.gif" style="position:absolute; top:15px; right:20px;">'; break;	case 1.5: face = '<img src="trainpic/face1.gif" style="position:absolute; top:15px; right:20px;">'; break;
		 case 2: face = '<img src="trainpic/face2.gif" style="position:absolute; top:15px; right:20px;">'; break;	case 2.5: face = '<img src="trainpic/face2.gif" style="position:absolute; top:15px; right:20px;">'; break;
		 case 3: face = '<img src="trainpic/face3.gif" style="position:absolute; top:15px; right:20px;">'; break;	case 3.5: face = '<img src="trainpic/face3.gif" style="position:absolute; top:15px; right:20px;">'; break;
		 case 4: face = '<img src="trainpic/face4.gif" style="position:absolute; top:15px; right:20px;">'; break;	case 4.5: face = '<img src="trainpic/face4.gif" style="position:absolute; top:15px; right:20px;">'; break;
		 case 5: face = '<img src="trainpic/face5.gif" style="position:absolute; top:15px; right:20px;">'; break;	case 5.5: face = '<img src="trainpic/face5.gif" style="position:absolute; top:15px; right:20px;">'; break;
		 case 6: face = '<img src="trainpic/face6.gif" style="position:absolute; top:15px; right:20px;">'; break;	case 6.5: face = '<img src="trainpic/face6.gif" style="position:absolute; top:15px; right:20px;">'; break;
		 case 7: face = '<img src="trainpic/face7.gif" style="position:absolute; top:15px; right:20px;">'; break;	case 7.5: face = '<img src="trainpic/face7.gif" style="position:absolute; top:15px; right:20px;">'; break;
		 case 8: face = '<img src="trainpic/face8.gif" style="position:absolute; top:15px; right:20px;">'; break;	case 8.5: face = '<img src="trainpic/face8.gif" style="position:absolute; top:15px; right:20px;">'; break;
		 case 9: face = '<img src="trainpic/face9.gif" style="position:absolute; top:15px; right:20px;">'; break;	case 9.5: face = '<img src="trainpic/face9.gif" style="position:absolute; top:15px; right:20px;">'; break;
		 case 10:face = '<img src="trainpic/face10.gif" style="position:absolute; top:15px; right:20px;">';break;	case 10.5: face = '<img src="trainpic/face10.gif" style="position:absolute; top:15px; right:20px;">'; break;
		}}		
		
   function btnfin()
       {var cdreboutons = document.getElementById("cdreboutons");
        cdreboutons.innerHTML = '<img src="trainpic/topleft3.gif" class="tl"><img src="trainpic/topright3.gif" class="tr"><img src="trainpic/bottmleft3.gif" class="bl"><img src="trainpic/bottmright3.gif" class="br">'
		cdreboutons.innerHTML += '<input type="button"  class="bouton" value=" => See results" disabled=true>';
        cdreboutons.innerHTML += '<div class="ligne"></div>';
		cdreboutons.innerHTML += '<input type="button" class="bouton" value="=> Start again" onclick=recommencer()>';
        }

   function recommencer()
        {location.reload();
         }

