function picturesPreLoad() {
    var y;
    if (document.images)
    {
      preload_image_object = new Image();
      image_url = new Array();
      image_url[0] = theRootURL+"x.gif"; y=0;
      image_url[1] = theRootURL+"ok.gif"; y=y+1;
      image_url[2] = theRootURL+"teeth-banner.gif"; y=y+1;
      image_url[3] = theRootURL+"wmds-a.jpg"; y=y+1;
      image_url[4] = theRootURL+"wmds-b.jpg"; y=y+1;
      image_url[5] = theRootURL+"wmds-c.jpg"; y=y+1;
      image_url[6] = theRootURL+"wmds-d.jpg"; y=y+1;
      image_url[7] = theRootURL+"wmds-e.jpg"; y=y+1;
      image_url[7] = theRootURL+"wmds-f.jpg"; y=y+1;
       var i = 0;
       for(i=0; i<=y; i++) { 
         preload_image_object.src = image_url[i];
       }
    }
}

function determineRootURL(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;
}
theRootURL=determineRootURL(""+self.document.location);
theRootURL=theRootURL.substring(0,theRootURL.lastIndexOf("/")+1);

picturesPreLoad();

defaultMsg = 'One of the quizes from <a href="http://www.animated-teeth.com/" target="main">Animated-Teeth.com</a>.&#160;&#160;&#160;&#160;/&#160;&#160;Copyright (c) 2009 WMDS, Inc.';
curr=-1;
buf='';
msg=defaultMsg;
elapsedTime=0;
timerID=null;
numQuest=-1;

function question (str1,str2,str3) {
 numQuest++;
 theQuestions[numQuest]=str1
 theHints[numQuest]=str2
 thePictures[numQuest]=str3
 theAnswers[numQuest]=''
 numQuestions=numQuest+1;
}

function choice (code,str) {
 if (!theChoices[numQuest]) {
  theChoices[numQuest]=new Array();
  answerCodes[numQuest]=new Array();
  nc=0;
 }
 answerCodes[numQuest][nc]=code;
 theChoices[numQuest][nc]=str;
 nChoices[numQuest]=nc+1;
 theAnswers[numQuest]+='0';
 nc++;
}

function initializeAll() {
 var s=0;
 for (var i=0;i<numQuestions;i++) {
  nCorrectChoices[i]=0;
  for (var k=0;k<nChoices[i];k++) {
   if (isAnswerCorrect(i,k)) {
    nCorrectChoices[i]++;
    s++;
   }
  }
 }
}

function showTheDirections() {
 self.frames[0].location.replace(theRootURL+'f-intro.html');
}

function showQuestionHeading() {
 buf+='<div style="border-width:4px; border-style:solid; text-align:center; margin: 5px 3px 0px 15px;"><H1 class="topheading"><font color="#000090"><a href="http://www.animated-teeth.com/" target="main">Animated-Teeth.com:</a></font>&#160;&#160;<font color="#ffffff">'+quizName+'</h1></div><div style="padding-top:4px; padding-right:3px; text-align:right;"><font color="'+self.headingColor+'">(&#160;&#160;'+self.questionLeader+' '+(curr+1)+' of&#160;&#160;'+numQuestions+'&#160;&#160;)</font></div>';
}

function showQuestionData() {
 buf+='<div style="border-width:6px; border-style:solid; border-color: white; padding: 20px; margin-right: 17px;">';
 buf+='<font color="#ffffff"><b>'+theQuestions[curr]+'</b></font>';
 buf+='<div style="padding:4px 0px 4px 0px;"><font color="#ffffff">'+theHints[curr]+'</font></div>';
 buf+='</div>';
}

function showPicture() {
 buf+=thePictures[curr];
}

function showQuestionChoices() {
 buf+='\n<form name=f1><table border=0 cellspacing=2 cellpadding=0>\n';
 for (var k=0;k<nChoices[curr];k++) {
  buf+='<tr><td>&#160;&#160;</td><td valign=baseline><input name=c'+k+' type=radio onClick=parent.clkbox('+k+')></td>';
  buf+='<td>'+theChoices[curr][k]+'</td></tr>\n';
  answerBoxes[k]=0;
 }
 buf+='</table>';
}

function showQuestionButton() {
 buf+='&#160;&#160;&#160;&#160;<input type=button value="Submit your answer." onClick=parent.submitAnswer()></form>'
}

function askQuestion() {
 if (curr==-1) curr=0;
 clearMessage();
 buf='';
 showQuestionHeading();
 buf+='<center><table width="98%"><tr><td>';
 showQuestionData();
 buf+='</td><td>';
 showPicture();
 buf+='</td><td>';
 showQuestionChoices();
 showQuestionButton();
 buf+='</td></tr></table></center>';
 self.frames[0].location=self.theRootURL+'f-question.html';
 secondsLeft=secondsPerQuestion;
 setTimeout("self.status='"+secondsLeft+" seconds left to answer this question.';timer()",300);
}

function clearMessage() {
 msg=defaultMsg;
 self.frameBorder.location=''+self.frameBorder.location;
}

function clkbox(k) {
 if (nCorrectChoices[curr]==1) {
  for (var n=0;n<nChoices[curr];n++) {
   eval('self.frameQuestion.document.f1.c'+n+'.checked=false');
   answerBoxes[n]=0;
  }
  eval('self.frameQuestion.document.f1.c'+k+'.checked=true');
  answerBoxes[k]=1;
 }
 else {
  if (answerBoxes[k]==0) {
   eval('self.frameQuestion.document.f1.c'+k+'.checked=true');
   answerBoxes[k]=1;
  }
  else {
   eval('self.frameQuestion.document.f1.c'+k+'.checked=false');
   answerBoxes[k]=0;
  }
 }
}

function timer() {
 if (secondsLeft>0) {
  if (secondsLeft==15) {
   msg='<center><font color=#ffffff size="2"><b>You must answer this question within the next '+secondsLeft+' seconds!</b></font></center>';
   self.frameBorder.location=''+self.frameBorder.location;
   setTimeout("parent.status='"+secondsLeft+" seconds left to answer this question.'",300);
  }
  self.status=secondsLeft+' seconds left to answer this question.';
  elapsedTime++;
  secondsLeft--;
  timerID=setTimeout('timer()',1000);
 }
 else getAnswer();
}

function submitAnswer() {
 clearTimeout(timerID);
 getAnswer();
}

function getAnswer() {
 theAnswers[curr]='';
 for (k=0;k<nChoices[curr];k++) {
  theAnswers[curr]+=answerBoxes[k];
 }
 curr++;
 if (curr<numQuestions) {
  setTimeout("askQuestion()",100);
  if (numAd == 0) numAd=3;
  else numAd=numAd+1;
  if (numAd==7) numAd=1;
  if (numAd==5) self.frameAds.location=''+self.frameAds.location;
  if (numAd==3) self.frameAds.location=''+self.frameAds.location;
  if (numAd==1) self.frameAds.location=''+self.frameAds.location;
 }
 else {
   curr=-1; 
   setTimeout("resultsBox()",100);
 }
}

function resultsBox() {
 if (numAd>4) {
  numAd=1;}
 else if (numAd>2) {
  numAd=5;}
 else {
  numAd=3;}
 self.frameAds.location=''+self.frameAds.location;
 clearMessage();
 self.status='This quiz has been completed.';
 nCorr=correctAnswersCount();
 buf='<div style="border-style: solid; padding-right: 10px; padding-left: 10px; padding-bottom: 10px; padding-top: 6px; text-align: center; margin: 20px 12px 5px 20px;">';
 buf+='<div style="text-align: center;"><a href="http://www.animated-teeth.com/" target="main"><img src="teeth-banner.gif" height="60" width="468" border="0"></a></div>';
 buf+='<H1><font color="'+self.headingColor+'">Congratulations from </font><a href="http://www.animated-teeth.com/" target="main">Animated-Teeth.com</a><font color="'+self.headingColor+'"> !</font></H1>';
 buf+='<H2>You have completed the '+self.quizName+'.<br>Here are your results!</H2>';
 buf+='<center><table cellpadding=3 cellspacing=12>';
 buf+='<tr><td><b>Number of questions that you answered correctly&#160;:</b></td><td><b>'+nCorr+' out of &#160;'+numQuestions+'</b></td></tr>';
 buf+='<tr><td><b>Percentage of questions that you answered correctly&#160;:</b></td><td><b>'+(Math.round(100*nCorr/numQuestions))+'\&#160;%</b></td></tr>';
 buf+='<tr><td><b>Length of time that it took you to complete the quiz&#160;:</b></td><td><b>'+elapsedTime+' seconds</b></td></tr>';
 buf+='</table></center></div>';
 buf+='<div style="text-align: center;"><form name=f1>'; 
 buf+='<input type=button value="'+"Show the correct answers."+'" onClick="parent.showCorrectAnswer()">';
 if (self.onExitRestart) buf+='<input type=button value="'+self.exitRestartText+'" onClick="'+self.onExitRestart+'">';
 buf+='<br> <br>';
 if (self.onExitHome) buf+='<input type=button value="'+self.exitHomeText+'" onClick="'+self.onExitHome+'">';
 buf+='<br>';
 if (self.onExitDoThis) buf+='<input type=button value="'+self.exitButtonText+'" onClick="'+self.onExitDoThis+'">';
 buf+='</form>';
 buf+='</div>';
 self.frames[0].location=theRootURL+'f-question.html';
}

function getCorrectAnswer(n) {
 var str='';
 for (var k=0;k<nChoices[n];k++) str+=answerCodes[n][k];
 return str;
}

function isAnswerCorrect(n,k) {
 return (answerCodes[n][k]==1);
}

function correctAnswersCount(n) {
 var n=0;
 for (var k=0;k<numQuestions;k++) {
  if ((getCorrectAnswer(k)).indexOf(theAnswers[k])==0) n++;
 }
 return n;
}

function showCorrectAnswer() {
 curr++; if (curr>=numQuestions) curr=0;
 buf='<div style="border-width:4px; border-style:solid; text-align:center; margin: 5px 3px 0px 15px;"><H1 class="topheading"><font color="#000090"><a href="http://www.animated-teeth.com/" target="main">Animated-Teeth.com:</a></font>&#160;&#160;<font color="#ffffff">'+quizName+'</h1></div>';
 buf+='<div style="margin: 30px 0px 0px 40px; text-align: center;"><font size="+2" color="#ffffff">'+self.questionLeader+' '+(curr+1)+' : '+theQuestions[curr]+'</font><br> <br>';
 buf+='<center><table><tr><td>';
 buf+=thePictures[curr];
 buf+='</td><td>';
 buf+='\n<table border=0 cellspacing=2 cellpadding=0>\n';
 for (var k=0;k<nChoices[curr];k++) {
  buf+='<tr><td valign=baseline>';
  buf+=(answerCodes[curr][k]==1)?'<img src="ok.gif"></td>':((theAnswers[curr].charAt(k)==1)?'<img src="x.gif"></td>':'</td>');
  buf+='<td>'+theChoices[curr][k]+'</td></tr>\n';
 }
 buf+='</table>';
 if (curr==numQuestions-1) {
  buf+='<form name=f1>'; 
  buf+='&#160;&#160;&#160;&#160;<input type=button value="'+"&#160;Back&#160; to the\nresults page."+'" onClick="parent.resultsBox()">';
  if (self.onExitDoThis) msg+='<input type=button value="'+self.exitButtonText+'" onClick="'+self.onExitDoThis+'">';
  buf+='</form>';
 }
 else {
  buf+='<form name=f1>' ;
  buf+='&#160;&#160;&#160;&#160;<input type=button value="'+"Next question."+'" onClick="parent.showCorrectAnswer()">';
  buf+='</form>';
 }
 buf+='</div></td></tr></table></center>';
 self.frameQuestion.location=''+self.frameQuestion.location;
}
