﻿function getquestions() {

     var n = questions.length - 1;
     var randomquestion = Math.round(Math.random() * n) + 0;
     return randomquestion;

}




function startquiz() {
       
    var last = parseInt(params['last']);
    var level = parseInt(params['level']);
    var all = parseInt(params['all']);
    
    var forumsubmit = '<input type="submit" value="Continue >>"></form>';

    if (last > 0) {
          var start = last;
          if (last == (level-1)) {
               document.writeln(formresults);
//               document.writeln('<form type="get" action="bl_car_seat_quiz_results.htm" >');
               forumsubmit = '<input type="submit" value="Submit Answers"></form>';
          } else {
              document.writeln(formquiz);
//              document.writeln('<form type="get" action="bl_car_seat_quiz.htm" >');
          } 
          document.writeln('<input type="hidden" name="level" value=' + level + '>');
          document.writeln('Here is question ' + (last+1) + ' out of ' + level + ' questions:<p>');
    } else {
         var start = 0;
         document.writeln(formquiz);
//         document.writeln('<form type="get" action="bl_car_seat_quiz.htm" >');
         document.writeln(introduction);
         document.writeln('Choose your difficulty level<br>');
         document.writeln('<select name="level">');         
         document.writeln('<option value="4">Easy - Four Questions');
         document.writeln('<option value="6">Medium - Six Questions');
         document.writeln('<option value="8">Hard - Eight Questions');
         document.writeln('<option value="10">Very Hard - Ten Questions');
         document.writeln('<option selected value=' + questions.length + '>Expert Level - All ' + questions.length + ' Questions</select><p>');
    }
    
     

     var youranswers = new Array();
     for (var r=0; r<questions.length; r++) {
          var questionnumber = "question" + (r + 1);
          youranswers[r] = params[questionnumber];
          if (youranswers[r]) {
               document.writeln('<input type="hidden" name=' + questionnumber + ' value=' + youranswers[r] + '>');
          }
     }

     var alreadyasked = new Array();
     for (var r=0; r<questions.length; r++) {
          var q = "q" + r;
          if (params[q]) {
               alreadyasked[params[q]] = 1;
               document.writeln('<input type="hidden" name=' + q + ' value=' + params[q] + '>');
          }
     }
     

    if (all) {
       var end = questions.length;
       document.writeln('</form>' + formresults);
//       document.writeln('</form><form type="get" action="bl_car_seat_quiz_results.htm" >');
       document.writeln('<input type="hidden" name="level" value=' + questions.length + '>');
       forumsubmit = '<input type="submit" value="Submit Answers"></form>';
    } else {
        var end = start + 1;
    }
    
    document.writeln('<input type="hidden" name="last" value=' + end + '>');
    
    

        
	for (var questionIndex = start; questionIndex < end; questionIndex++) {
	

        var questionfound = 1;
        
        if (all) {
             randomquestion = questionIndex;
             var q = "q" + (questionIndex);
             document.writeln('<input type="hidden" name=' + q + ' value=' + randomquestion + '>');
        } else {
           while (questionfound == 1) {
	         randomquestion = getquestions();
	    
             if (alreadyasked[randomquestion] != 1) {
                  alreadyasked[randomquestion] = 1;
                  var q = "q" + (questionIndex);
                  document.writeln('<input type="hidden" name=' + q + ' value=' + randomquestion + '>');
                  questionfound=0;
             } 
             
          }
        }
             


					
						// Separate each array element to form the questions and answr options
		var questionnumber 	= questions[randomquestion].substring(0, questions[randomquestion].indexOf('##'));
		var question 	= questions[randomquestion].substring(questions[randomquestion].indexOf('##') + 2, questions[randomquestion].indexOf('++'));
		var imageurl 	= questions[randomquestion].substring(questions[randomquestion].indexOf('++') + 2, questions[randomquestion].indexOf('$$'));
		var optionA 	= questions[randomquestion].substring(questions[randomquestion].indexOf('$$') + 2, questions[randomquestion].indexOf('%%'));
		var optionB 	= questions[randomquestion].substring(questions[randomquestion].indexOf('%%') + 2, questions[randomquestion].indexOf('^^'));
		var optionC 	= questions[randomquestion].substring(questions[randomquestion].indexOf('^^') + 2, questions[randomquestion].indexOf('&&'));
		var optionD 	= questions[randomquestion].substring(questions[randomquestion].indexOf('&&') + 2, questions[randomquestion].indexOf('@@'));
		var explanation 	= questions[randomquestion].substring(questions[randomquestion].indexOf('@@') + 2, questions[randomquestion].indexOf('!!'));
	    var url 	= questions[randomquestion].substring(questions[randomquestion].indexOf('!!') + 2, questions[randomquestion].length);

        document.writeln('<tr><td bgcolor="red" width="10%"><div align="center"><font size="4" color="white"><b>');
		document.writeln((questionIndex + 1));
		document.writeln('</b></font></div></td><td colspan="3" bgcolor="#d3d3d3">' + question + '</td></tr>');
		
		if (imageurl != 'none') {
			document.writeln('<tr><td width="10%"></td><td colspan="3" bgcolor="#d3d3d3">');
			document.writeln('<img src="' + imageurl + '" alt="quiz photo" border="0" align="left">' + "</td></tr>");
		}
		
		var test = 'question' + (questionIndex + 1);
		
		document.writeln('<tr><td width="10%"></td><td colspan="3" bgcolor="#d3d3d3">');
		document.writeln("<INPUT TYPE=\"RADIO\" NAME=" + test + " VALUE=\"a\">a) " + optionA + "</td></tr>");
		      
		document.writeln('<tr><td width="10%"></td><td colspan="3" bgcolor="#d3d3d3">');
		document.writeln("<INPUT TYPE=\"RADIO\" NAME=" + test + " VALUE=\"b\">b) " + optionB + "</td></tr>");
		       
		document.writeln('<tr><td width="10%"></td><td colspan="3" bgcolor="#d3d3d3">');
		document.writeln("<INPUT TYPE=\"RADIO\" NAME=" + test + " VALUE=\"c\">c) " + optionC + "</td></tr>");
		     
		document.writeln('<tr><td width="10%"></td><td colspan="3" bgcolor="#d3d3d3">');
		document.writeln("<INPUT TYPE=\"RADIO\" NAME=" + test + " VALUE=\"d\">d) " + optionD + "</td></tr>");   

        document.writeln('<tr><td width="10%"><br></td><td width="10%"></td><td width="10%"></td><td width="69%"></td></tr>');	
	 }	
	  document.writeln('<tr><td></td><td>');
	  document.writeln(forumsubmit);
	  document.writeln('</td><td></td><td></td></tr>');
	 
}		

function getresults() {

     var level = parseInt(params['level']);

//     var youranswers = new Array();
//     for (var r=0; r<10; r++) {
//          var questionnumber = "question" + (r + 1);
//          youranswers[r] = params[questionnumber];
//     }
     
     var youranswers = new Array();
     var alreadyasked = new Array();
     for (var r=0; r<level; r++) {
          var q = "q" + r;
          alreadyasked[r] = params[q];
          var questionnumber = "question" + (r + 1);
          youranswers[r] = params[questionnumber];
     }

     
     document.writeln('Here are the results of your quiz:<p>Correct answers are marked with a <img src="http://z.about.com/d/pediatrics/1/0/2/3/correct.jpg" alt="" height="15" width="15" border="0">, while wrong answers have a <img src="http://z.about.com/d/pediatrics/1/0/1/3/wrong.gif" alt="" height="19" width="10" border="0"> next to them.</p>');

     var questionIndex = 0;
     var score = 0;
//      document.writeln('<form type="get" action="bl_car_seat_quiz.htm" >');
         document.writeln(formquiz);
     for (var questionIndex = 0; questionIndex < alreadyasked.length; questionIndex++) {
     
        var thequestion = alreadyasked[questionIndex];
					
						// Separate each array element to form the questions and answr options
		var questionnumber 	= questions[thequestion].substring(0, questions[thequestion].indexOf('##'));
		var question 	= questions[thequestion].substring(questions[thequestion].indexOf('##') + 2, questions[thequestion].indexOf('++'));
		var imageurl 	= questions[thequestion].substring(questions[thequestion].indexOf('++') + 2, questions[thequestion].indexOf('$$'));
		var optionA 	= questions[thequestion].substring(questions[thequestion].indexOf('$$') + 2, questions[thequestion].indexOf('%%'));
		var optionB 	= questions[thequestion].substring(questions[thequestion].indexOf('%%') + 2, questions[thequestion].indexOf('^^'));
		var optionC 	= questions[thequestion].substring(questions[thequestion].indexOf('^^') + 2, questions[thequestion].indexOf('&&'));
		var optionD 	= questions[thequestion].substring(questions[thequestion].indexOf('&&') + 2, questions[thequestion].indexOf('@@'));
		var explanation 	= questions[thequestion].substring(questions[thequestion].indexOf('@@') + 2, questions[thequestion].indexOf('!!'));
	    var url 	= questions[thequestion].substring(questions[thequestion].indexOf('!!') + 2, questions[thequestion].length);

		  document.writeln('<tr><td bgcolor="red" width="5%"><div align="center"><font size="4" color="white"><b>');
		  document.writeln((questionIndex + 1));

		  document.writeln('</b></font></div></td><td bgcolor="red" width="10%"><div align="center"><font size="4" color="white"><b>')
               if (youranswers[questionIndex] == correctanswers[alreadyasked[questionIndex]]) {
                     document.write("Correct");
                     score++;
               } else if (youranswers[questionIndex] != "a" && youranswers[questionIndex] != "b" && youranswers[questionIndex] != "c" && youranswers[questionIndex] != "d") {
                     document.write("Not Answered");
               } else {
                     document.write("Wrong");
               }
		  document.writeln('</b></font></div></td>');

		  document.writeln('<td colspan="2" bgcolor="#d3d3d3" width="85%">' + question + '</td></tr>');
		  var test = 'question' + (questionIndex + 1);
		
		if (imageurl != 'none') {
			document.writeln('<tr><td width="5%"></td><td width="10%"></td><td width="2%"></td><td bgcolor="#d3d3d3" width="83%">');
			document.writeln('<img src="' + imageurl + '" alt="quiz photo" border="0" align="left">' + "</td></tr>");
		}

          document.writeln('<tr><td width="5%"><div align="center"></div></td><td width="10%"></td><td width="2%"><div align="left">');
		  if (youranswers[questionIndex] == 'a') {
               if (youranswers[questionIndex] == correctanswers[alreadyasked[questionIndex]]) {
                    document.write('<img src="http://z.about.com/d/pediatrics/1/0/2/3/correct.jpg" alt="" height="15" width="15" border="0">');
               } else {
                    document.write('<img src="http://z.about.com/d/pediatrics/1/0/1/3/wrong.gif" alt="" height="15" width="15" border="0">');
               }
          }	else if (correctanswers[alreadyasked[questionIndex]] == 'a') {
                    document.write('<img src="http://z.about.com/d/pediatrics/1/0/2/3/correct.jpg" alt="" height="15" width="15" border="0">');
          }
          
          document.writeln('</td><td bgcolor="#d3d3d3" width="83%">');
          document.writeln("<INPUT TYPE=\"RADIO\" NAME=");
          document.writeln(test);
		  document.writeln(" VALUE=\"a\"");
		       if (youranswers[questionIndex] == 'a') {
                    document.write("CHECKED");
               }
		  document.writeln(">a) " + optionA + '</td></tr>');        

          document.writeln('<tr><td width="5%"><div align="center"></div></td><td width="10%"></td><td width="2%">');
		  if (youranswers[questionIndex] == 'b') {
               if (youranswers[questionIndex] == correctanswers[alreadyasked[questionIndex]]) {
                    document.write('<img src="http://z.about.com/d/pediatrics/1/0/2/3/correct.jpg" alt="" height="15" width="15" border="0">');
               } else {
                    document.write('<img src="http://z.about.com/d/pediatrics/1/0/1/3/wrong.gif" alt="" height="15" width="15" border="0">');
               }
          }	else if (correctanswers[alreadyasked[questionIndex]] == 'b') {
                    document.write('<img src="http://z.about.com/d/pediatrics/1/0/2/3/correct.jpg" alt="" height="15" width="15" border="0">');
          }	
          document.writeln('</td><td bgcolor="#d3d3d3">');
		  document.writeln("<INPUT TYPE=\"RADIO\" NAME=");
          document.writeln(test);
		  document.writeln(" VALUE=\"b\"");
		       if (youranswers[questionIndex] == 'b') {
                    document.write("CHECKED");
               }
		  document.writeln(">b) " + optionB + '</td></tr>');        

          document.writeln('<tr><td width="5%"><div align="center"></div></td><td width="10%"></td><td width="2%">');
		  if (youranswers[questionIndex] == 'c') {
               if (youranswers[questionIndex] == correctanswers[alreadyasked[questionIndex]]) {
                    document.write('<img src="http://z.about.com/d/pediatrics/1/0/2/3/correct.jpg" alt="" height="15" width="15" border="0">');
               } else {
                    document.write('<img src="http://z.about.com/d/pediatrics/1/0/1/3/wrong.gif" alt="" height="15" width="15" border="0">');
               }
          }	else if (correctanswers[alreadyasked[questionIndex]] == 'c') {
                    document.write('<img src="http://z.about.com/d/pediatrics/1/0/2/3/correct.jpg" alt="" height="15" width="15" border="0">');
          }	
          document.writeln('</td><td bgcolor="#d3d3d3">');
		  document.writeln("<INPUT TYPE=\"RADIO\" NAME=");
          document.writeln(test);
		  document.writeln(" VALUE=\"c\"");
		       if (youranswers[questionIndex] == 'c') {
                    document.write("CHECKED");
               }
		  document.writeln(">c) " + optionC + '</td></tr>');        

          document.writeln('<tr><td width="5%"><div align="center"></div></td><td width="10%"></td><td width="2%">');
		  if (youranswers[questionIndex] == 'd') {
               if (youranswers[questionIndex] == correctanswers[alreadyasked[questionIndex]]) {
                    document.write('<img src="http://z.about.com/d/pediatrics/1/0/2/3/correct.jpg" alt="" height="15" width="15" border="0">');
               } else {
                    document.write('<img src="http://z.about.com/d/pediatrics/1/0/1/3/wrong.gif" alt="" height="15" width="15" border="0">');
               }
          } else if (correctanswers[alreadyasked[questionIndex]] == 'd') {
                    document.write('<img src="http://z.about.com/d/pediatrics/1/0/2/3/correct.jpg" alt="" height="15" width="15" border="0">');
          }
          document.writeln('</td><td bgcolor="#d3d3d3">');
		  document.writeln("<INPUT TYPE=\"RADIO\" NAME=");
          document.writeln(test);
		  document.writeln(" VALUE=\"d\"");
		       if (youranswers[questionIndex] == 'd') {
                    document.write("CHECKED");
               }
		  document.writeln(">d) " + optionD + '</td></tr>');  
		  
		  document.writeln('<tr><td  colspan="3"></td><td>');      
		  if (youranswers[questionIndex] == correctanswers[alreadyasked[questionIndex]]) {
               document.write("Good Job! ");
               document.write('The correct answer is "' + correctanswers[alreadyasked[questionIndex]] + '". ( <a href="' + url  + '" target="_blank">more info</a> )</td></tr>');
          } else {
               document.write("Your answer is wrong! ");
               document.write('The correct answer is "' + correctanswers[alreadyasked[questionIndex]] + '". ' + explanation + ' ( <a href="' + url + '" target="_blank"> more information</a> )</td></tr>');
          }
          
//          document.writeln('<tr><td  colspan="3"></td><td>');      
//		  document.write("For more information, please visit - ");
//          document.write('<a href="' + url + '">' + more information + '</a>.</td></tr>');
          
     document.writeln('<tr><td colspan="4"><br></td></tr>');	
	}			
	
     
     document.writeln('<tr><td colspan="4"><b>Your Score:</b> You got ' + score + ' right out of ' + level + ' questions, ');
     
     var percent = parseInt(score/level * 100);
     
     document.writeln(' for a score of ' + percent + ' percent.</td></tr>'); 
     document.writeln('<tr><td colspan="4"><br></td></tr>');

     document.writeln('<tr><td colspan="4">');
     if (percent >= 90) {
          document.writeln(grades[2]);
     } else if (percent >= 70) {
          document.writeln(grades[1]);
     } else  {
          document.writeln(grades[0]);
     }
     
     if ((questions.length - level) > 0) {
          document.writeln('<tr><td colspan="4"><br></td></tr>');     
          document.writeln('<tr><td colspan="4">There are ' + (questions.length - level) + '  different questions left in the quiz, so click the button below to try again and get a different quiz.</td></tr>');
     }
        
     document.writeln('<tr><td colspan="4"><br></td></tr>');
     document.writeln('<tr><td colspan="4" bgcolor="red"><br></td></tr>');
     document.writeln('<tr><td colspan="4"><br></td></tr>');

    
}	