// JQwiz 1.1 

// Client-side quiz generator
// 
// Copyright (C) 1999 Alexei Kourbatov
//
// You may reuse this code provided that 
// you do NOT change JQwiz files in any way
// and, in particular, do NOT delete 
// any copyright notices. Enjoy!


// PARAMETERS

if (!self.quizTitle) self.quizTitle=self.document.title;
if (!self.questionTitle) self.questionTitle="Question";


defaultMsg = '<small>'
 +'Made with <a target=_blank href="http://www.javascripter.net/jqwiz-guide/index.htm">JQwiz 1.1</a>. '
 +'Copyright &#169; 1999 <a href="mailto:alexei@kourbatov.com?subject=JQwiz">Alexei Kourbatov</a>'
 +'</small>'

JQwizIntroPage = 'JQ_intro.htm'
JQwizMainPage  = 'JQwiz_f1.htm'


// ROOT URL

function regularize(theURL) {
 for (j=0;j<theURL.length;j++) {
  if (theURL.charAt(j)=="\\") theURL=theURL.substring(0,j)+"/"+theURL.substring(j+1,theURL.length);
 }
 return theURL;
}
trueRootURL=regularize(""+self.document.location);
trueRootURL=trueRootURL.substring(0,trueRootURL.lastIndexOf("/")+1);


// VARIABLES

currPhase=0
curr=-1;
buf='';
msg=defaultMsg;
elapsedTime=0;
timerID=null;


// Functions for intializing individual questions/choices

nq=-1;
function question (str) {
 nq++;
 questions[nq]=str
 answers[nq]=''
 nQuestions=nq+1;
}
function choice (code,str) {
 if (!choices[nq]) {
  choices[nq]=new Array();
  codes[nq]=new Array();
  nc=0;
 }
 codes[nq][nc]=code;
 choices[nq][nc]=str;
 nChoices[nq]=nc+1;
 answers[nq]+='0';
 nc++;
}


function initAll() {
 var s=0;
 for (var i=0;i<nQuestions;i++) {
  nCorrectChoices[i]=0;
  for (var k=0;k<nChoices[i];k++) {
   if (isAnswerCorrect(i,k)) {
    nCorrectChoices[i]++;
    s++;
   }
  }
 }
 OneCorrectAnswer=(s==nQuestions)?true:false;
 if (choiceStyle.toLowerCase()!='radio') choiceStyle='checkbox';
}

function toMinSec(sec) {
 if (sec<=60) return " "+sec+"&nbsp;seconds ";
 var rsec=sec%60;
 var min=(sec-rsec)/60;
 if (rsec==0) return " "+min+"&nbsp;minutes ";
 return " "+min+"&nbsp;min&nbsp;"+rsec+"&nbsp;sec ";
}


function showDirections() {
 if (self.introPageURL && (''+self.introPageURL).toLowerCase().indexOf('.htm')!=-1) {
  self.frames[0].location.replace(trueRootURL+self.introPageURL);
 }
 else self.frames[0].location.replace(trueRootURL+self.JQwizIntroPage);
}

function showStartButton() {
 buf='\n<p><form name=f1><input type=button value="'+self.StartButtonText+'" onClick=parent.askQuestion()></form>\n';
 self.frames[0].document.write(buf);
 buf='';
}

function askQuestion() {
 if (curr==-1) curr=0;
 if (!self.isUsingWizard) clearMessage();
 showQuestion();
 buf+='<p><input type=button value="'+self.SubmitButtonText+'" onClick=parent.submitAnswer()></form>'
 self.frames[0].location=self.trueRootURL+JQwizMainPage;
 secondsLeft=secPerQuestion;
 if (!self.isUsingWizard) setTimeout("self.status='"+secondsLeft+" sec left';timer()",300);
}

function clearMessage() {
 msg=defaultMsg;
 self.f2JQwiz.location=''+self.f2JQwiz.location;
}

function showQuestion() {
 buf='<H3><font color="'+self.hdrColor+'">'+self.questionTitle+' '+(curr+1)+'</font></H3>';
 buf+=questions[curr];
 buf+='\n<form name=f1><table border=0 cellspacing=2 cellpadding=0>\n';

 for (var k=0;k<nChoices[curr];k++) {
  buf+='<tr><td valign=baseline><input name=c'+k+' type='+choiceStyle+' onClick=parent.clkbox('+k+')></td>'
  buf+='<td>'+choices[curr][k]+'</td></tr>\n'

  boxes[k]=0;
 }
 buf+='</table>'
}

function clkbox(k) {
 if (nCorrectChoices[curr]==1) {
  for (var n=0;n<nChoices[curr];n++) {
   eval('self.f1JQwiz.document.f1.c'+n+'.checked=false');
   boxes[n]=0;
  }
  eval('self.f1JQwiz.document.f1.c'+k+'.checked=true');
  boxes[k]=1;
 }
 else {
  if (boxes[k]==0) {
   eval('self.f1JQwiz.document.f1.c'+k+'.checked=true');
   boxes[k]=1;
  }
  else {
   eval('self.f1JQwiz.document.f1.c'+k+'.checked=false');
   boxes[k]=0;
  }
 }
}

function timer() {
 if (secondsLeft>0) {
  if (secondsLeft==10) {
   msg='<center><H3><font color=red>'+secondsLeft+' seconds left!</font></H3></center>';
   self.f2JQwiz.location=''+self.f2JQwiz.location;
   setTimeout("parent.status='"+secondsLeft+" sec left'",300);
  }
  self.status=secondsLeft+' sec left'
  elapsedTime++;
  secondsLeft--;
  timerID=setTimeout('timer()',1000);
 }
 else getAnswer();
}

function submitAnswer() {
 clearTimeout(timerID);
 getAnswer();
}

function getAnswer() {
 answers[curr]='';
 for (k=0;k<nChoices[curr];k++) {
  answers[curr]+=boxes[k];
 }
 curr++;
 if (curr<nQuestions) setTimeout("askQuestion()",100);
 else {curr=-1; currPhase=1; setTimeout("displayResults()",100);}
}

function displayResults() {
 nCorr=correctAnswersCount();
 buf='<H3><font color="'+self.hdrColor+'">Congratulations!</font></H3>';
 buf+='<H4>You completed the <i>'+self.quizTitle+'</i> with the following results:</H4>';
 buf+='<table border=0 cellpadding=2 cellspacing=2>';
 buf+='<tr><td>Correct answers:</td><td>'+nCorr+' of '+nQuestions+'</td></tr>';
 buf+='<tr><td>Percentage correct:</td><td>'+(Math.round(100*nCorr/nQuestions))+'\%</td></tr>';
 buf+='<tr><td>Elapsed time:</td><td>'+elapsedTime+' sec</td></tr>';
 buf+='<tr><td>Average time per question:</td><td>'+(Math.round(elapsedTime/nQuestions))+' sec</td></tr>';
 buf+='</table>';

 buf+='<form name=f1>' 
 buf+='<input type=button value="'+self.ShowAnswersButtonText+'" onClick="parent.showCorrectAnswer()">';
 if (self.onExitDoThis) buf+='<input type=button value="'+self.ExitButtonText+'" onClick="'+self.onExitDoThis+'">'
 buf+='</form>'

 self.frames[0].location=trueRootURL+JQwizMainPage;
}

function getCorrectAnswer(n) {
 var str='';
 for (var k=0;k<nChoices[n];k++) str+=codes[n][k];
 return str;
}

function isAnswerCorrect(n,k) {
 return (codes[n][k]==1);
}

function correctAnswersCount(n) {
 var n=0;
 for (var k=0;k<nQuestions;k++) {
  if ((getCorrectAnswer(k)).indexOf(answers[k])==0) n++;
 }
 return n;
}

// preload images
crossImg=new Image(); crossImg.src="cross.gif"
checkImg=new Image(); checkImg.src="check.gif"
bkImg   =new Image(); if (self.bgrImage) bkImg.src=bgrImage;


function showCorrectAnswer() {
 curr++; if (curr>=nQuestions) curr=0;

 buf='<H3><font color="'+self.hdrColor+'">'+self.questionTitle+' '+(curr+1)+'</font></H3>';
 buf+=questions[curr];
 buf+='\n<table border=0 cellspacing=2 cellpadding=0>\n';

 for (var k=0;k<nChoices[curr];k++) {
  buf+='<tr><td valign=baseline>'
  buf+=(codes[curr][k]==1)?'<img src="check.gif"></td>':((answers[curr].charAt(k)==1)?'<img src="cross.gif"></td>':'</td>')
  buf+='<td>'+choices[curr][k]+'</td></tr>\n'
 }
 buf+='</table>'

 if (curr==nQuestions-1) {
  buf+='<form name=f1>' 
  buf+='<input type=button value="'+self.ShowScoreButtonText+'" onClick="parent.displayResults()">'
  if (self.onExitDoThis) msg+='<input type=button value="'+self.ExitButtonText+'" onClick="'+self.onExitDoThis+'">'
  buf+='</form>'
 }
 else {
  buf+='<form name=f1>' 
  buf+='<input type=button value="'+self.NextAnswerButtonText+'" onClick="parent.showCorrectAnswer()">'
  buf+='</form>'
 }
 self.f1JQwiz.location=''+self.f1JQwiz.location;
}

