// potential replacement for mcbuildertestb.js to display only incorrect distractor

counter=0; //keep track of number of questions displayed on page

  if(typeof gametitle!='string'){gametitle=''} // 150211 - use gametitle if available
  
//populate the question bank
banksize=questions.length-1;

for (i=1;i<=banksize;i++)
     { testsplit=questions[i].split('~')
        stemarray[i]=testsplit[0];   // load stem
        answer[i]=testsplit[6];      // load answer

      // Randomise location of CORRECT answer
      // locate answer at newplace 1-5
      newplace=Math.ceil(Math.random()*5);
      newletter=String.fromCharCode(newplace+64); //convert number 1-5 to letter A-E

      //correctletter=answer[i]; //keep original letter of answer
      answer[i]=newletter; // replace answer with new letter

      // swap actual answer A and randomly selected distractor
       tempvar=testsplit[newplace]; //keep distractor currently in answers new location
       testsplit[newplace]=testsplit[1]; // move correct answer to new spot
       testsplit[1]=tempvar; // move replaced distractor to A

       // assign new distractor locations to distractor arrays
        adistract[i]=testsplit[1];   //load distractors
        bdistract[i]=testsplit[2];
        cdistract[i]=testsplit[3];
        ddistract[i]=testsplit[4];
        edistract[i]=testsplit[5];

      // if difficulty level not in question bank set it to 1 or use its actual value
      // added 230605
        if (testsplit.length<8){difficulty[i]='1'}
        else {difficulty[i]=testsplit[7];}

      // if game level not in question bank set it to 1 or use its actual value
      // added 230508
        if (testsplit.length<9){level[i]='1'}
        else {level[i]=testsplit[8];}

        if (testsplit.length<10){}
        else {hint[i]=testsplit[9];
              
             }

      }// end of for

function buildquiz(){
     // 150211 - use gametitle if available see questions.txt
    document.getElementsByTagName('h1')[0].innerHTML=gametitle+' Multiple Choice Quiz';
    
  quest='';

  for(var i=1;i<=banksize;i++)
     {
      counter=counter+1;

      // build questions with radio buttons for options
      quest=quest+'<div>Q '+counter+':<br />'+stemarray[i]+'</div>';
      quest=quest+'<div><input type="radio" id="q'+i+'" name="q'+i+'" value="A" onclick="checksingleanswer(\''+i+'\');" />A) '+adistract[i]+'</div>';
      quest=quest+'<div><input type="radio" id="q'+i+'" name="q'+i+'" value="B" onclick="checksingleanswer(\''+i+'\');" />B) '+bdistract[i]+'</div>';
      quest=quest+'<div><input type="radio" id="q'+i+'" name="q'+i+'" value="C" onclick="checksingleanswer(\''+i+'\');" />C) '+cdistract[i]+'</div>';
      quest=quest+'<div><input type="radio" id="q'+i+'" name="q'+i+'" value="D" onclick="checksingleanswer(\''+i+'\');" />D) '+ddistract[i]+'</div>';
      quest=quest+'<div><input type="radio" id="q'+i+'" name="q'+i+'" value="E" onclick="checksingleanswer(\''+i+'\');" />E) '+edistract[i]+'</div>';
      quest=quest+'<div id="feedback'+i+'" class="hint">Question Feedback:<br />'+hint[i]+'</div><hr />';
     }
  document.getElementById('quiz').innerHTML=quest; // display questions

}

function checksingleanswer(qnum){

   // hide any currently displayed hints
   for(var i=1;i<=banksize;i++)
      {document.getElementById('feedback'+i).style.display='none';}

   var picked='';

   //get all radio buttons that have same question name
   var buttons = document.getElementsByName('q'+qnum);

   // examine each radio button to see which is checked store buttons.value in picked
   for (var j=0; j<buttons.length; j++)
       {
        if (buttons[j].checked)
           {picked=buttons[j].value;
                      
           }
       }

   x = document.getElementById('q'+qnum)

   // find radio button and if checked is answer right letter
   if (answer[qnum]==picked)
        {// alertboxer('<h1>Correct Answer!</h1>')
         document.getElementById('feedback'+qnum).innerHTML='<h1>Correct Answer!</h1>';
	     
         document.getElementById('feedback'+qnum).style.display='block';
        }
   else {
         // display only incorrect distractor chosen as suggested by Alastair Sterling
         // not currently working as order of distractors is randomly changed 
         // var picked is the letter turn it into the distractor
         var pickedtext=picked.toLowerCase()+'distract['+qnum+']';
         pickedtext=eval(pickedtext);
         
 // alert(pickedtext)    
 // if (pickedtext contains any meta char){fix any meta characters}
 // metapattern=/([\/'])/g;
 //pickedtext=pickedtext.replace(metapattern,"\\$1");
 //alert(pickedtext);
 
         // searchfor is the answer selected with <br /> on either side
     
         //take text and create a new regex pattern with distractor in it
         searchfor= new RegExp(".*<br \/>(.+)("+pickedtext+").*<br \/>");
 
         // return distractor in  context with its stem from the hint and store in, thehint
         thehint=hint[qnum].replace(searchfor,"$1$2");
     
         document.getElementById('feedback'+qnum).innerHTML='Question Feedback - Incorrect Answer<br />'+thehint
     
         document.getElementById('feedback'+qnum).style.display='block';

        }

}

window.onload=function(){
                         buildquiz();
                         alertsetup();
                         startupmessage="Test generated by<br />MCBuilder";
                         startupmessage=startupmessage+"<p><img src='mcburger.png' alt='logo' /><br />[The Fast Food of Quiz Creation]</p>";
                         startupmessage=startupmessage+"http://cybertrain.info/<br />"
                         startupmessage=startupmessage+"&copy;2009 mike capstick<br />capsticm@gmail.com</p>"
                         
                         alertboxer(startupmessage);
              }

