/***********************************************
* JavaScriptKit.com Multiple Choice Quiz Script (http://www.javascriptkit.com)
* Copyright 2003 JavaScript Kit- http://www.javascriptkit.com
* This notice and footnote must stay intact for use
* Visit JavaScript Kit (http://www.javascriptkit.com/) for full source code
***********************************************/

//Enter total number of questions:
var actualchoices = [];
var totalquestions=10;

//Enter the solutions corresponding to each question:
var correctchoices=new Array()
correctchoices[1]='b'; //question 1 solution
correctchoices[2]='b'; //question 2 solution, and so on.
correctchoices[3]='a';
correctchoices[4]='a';
correctchoices[5]='b';
correctchoices[6]='a';
correctchoices[7]='b';
correctchoices[8]='b';
correctchoices[9]='b';
correctchoices[10]='b';



function gradeit(){
	var incorrect=null;
	for (q=1;q<=totalquestions;q++){
	    var thequestion=eval("document.myquiz.question"+q);
		
		for (c=0;c<thequestion.length;c++){
			if (thequestion[c].checked==true)
				actualchoices[q]=thequestion[c].value
		}
		
		if (actualchoices[q]!=correctchoices[q]){ //process an incorrect choice
			if (incorrect==null)
				incorrect=q
			else
				incorrect+="/"+q
		}
	}

	if (incorrect==null)
		incorrect="a/b"
	document.cookie='q='+incorrect;
	
}


function showSolution(){
	
	var results=document.cookie.split(";");

	for (n=0;n<=results.length-1;n++){
		if (results[n].charAt(0)=='q'){
			parse=n
		}
	}

	var incorrect=results[parse].split("=");
	incorrect=incorrect[1].split("/");
	if (incorrect[incorrect.length-1]=='b') incorrect="";
	
	var win2=window.open("","win2","width=220,height=390, scrollbars")
	win2.focus()
	win2.document.open()
	win2.document.write('<title>Solution</title>')
	win2.document.write('<body bgcolor="#FFFFFF">')
	win2.document.write('<center><h3>Solution to Quiz</h3></center>')
	win2.document.write('<center><font face="Arial">')
	for (i=1;i<=totalquestions;i++){
		for (temp=0;temp<incorrect.length;temp++){
			if (i==incorrect[temp])
				wrong=1
		}
		if (wrong==1){
			win2.document.write("Question "+i+"="+correctchoices[i].fontcolor("red")+"<br>")
			wrong=0
		}
		else
			win2.document.write("Question "+i+"="+correctchoices[i]+"<br>")
	}
	win2.document.write('</center></font>')
	win2.document.write("<h5>Note: The solutions in red are the ones to the questions you had incorrectly answered.</h5><p align='center'><small><a href='http://www.javascriptkit.com' target='_new'>JavaScript Kit quiz script</a></small>")
	win2.document.close()
}

function trim(s){
	return s.replace(/^\s+|\s+$/g, '');
}

function showResult(){
	var wrong=0
		
	var results=document.cookie.split("; ")
	var parse = -1;
	for (n=0;n<=results.length-1;n++){
		results[n] = trim(results[n]);
		if (results[n].charAt(0)=='q'){
			parse=n
		}
	}

	if (parse==-1){
	  $('#results').addClass('nodisplay');
	  return;
	}
	else {
	  $('#note').addClass('nodisplay');
	}

	var incorrect=results[parse].split("=");
	incorrect=incorrect[1].split("/");
	if (incorrect[incorrect.length-1]=='b') incorrect="";
	var right = totalquestions-incorrect.length; //+" out of "+totalquestions;
	var grade =(totalquestions-incorrect.length)/totalquestions*100+"%";
	var wrong = '';
	for (temp=0;temp<incorrect.length;temp++)
		wrong += incorrect[temp]+", ";
		
	$('#result_right').attr('innerHTML',right);
	$('#result_grade').attr('innerHTML',grade);
	$('#result_wrong').attr('innerHTML',wrong);
}
