// These variables keep track of the total number of questions 
// in each chapter and is used for displaying the totals
var numQs = new Array(10);
numQs[0] = 0; //this one isn't used so the numbers match the chapters
numQs[1] = 8; //Chapter 1
numQs[2] = 9; //Chapter 2
numQs[3] = 6; //Chapter 3
numQs[4] = 8; //Chapter 4
numQs[5] = 3; //Chapter 5
numQs[6] = 5; //Chapter 6
numQs[7] = 2; //Chapter 7
numQs[8] = 1; //Chapter 8
numQs[9] = 3; //Chapter 9
numQs[10] = 5; //Pretest


var revealed = 0;
var correct = 0;
var incorrect = 0;

// this function removes '?&' from the query string - not sure where it gets in there
function removeMistake( url )
{		
	var re = new RegExp('\\?&',"gi");
	url = url.replace(re, '?');			
	
	return url;
}

function answerQuestion( question, answer )
{
	var url = window.top.location.toString();
	var query = url.indexOf('?');
	var amp = url.indexOf('&');
	
	//decide whether this answer is the first in the query string
	if ( query == -1 )
	{
		url += "?";
	}
	else
	{		
		//remove question if already in query string and remove n3 which contains the current response
		url = url.substring(0, query) + removeItem(question, '');
		
		//might need to do a check here to see if there are any other variables first
		url += '&';
	}

	//add the answer to the query string
	url += question + "=" + answer;
	
	//display answer only the first time
	//this should only be in the url immediately after an answer has been selected
	//any other time the selected answer should not be shown
	url += '&display=true';	
	
	//stay on same page but include the users answer in the query string
	location.href = removeMistake( url );
}

//Loops through all query string variable and determines the results
function getScore()
{
	var queryString = '';
	var answer;

	// Create variable is_input to see if there is a ? in the url
	var is_input = document.URL.indexOf('?');

	// Check the position of the ? in the url
	if (is_input != -1)
	{ 
		// Create variable from ? in the url to the end of the string
		addr_str = document.URL.substring(is_input+1, document.URL.length);

		// Loop through the url and write out values found
		// or a line break to seperate values by the &
		for (count = 0; count < addr_str.length; count++) 
		{
			if (addr_str.charAt(count) != "&")
			{
				queryString += addr_str.charAt(count);
			}

			if (addr_str.charAt(count) == "&" || count == addr_str.length - 1) 
			{
				//split the key/value pair
				answer = queryString.split("=");

				//check for a Q as the first character and a number as the second and count up the score
				//ignore other variables
				if (answer[0].indexOf('Q') == 0 && isNaN(answer[0].substring(1)) == false)
				{
					if (answer[1] == '0')
					{
						//answer was incorrect
						incorrect += 1;
					}
					else if (answer[1] == '1')
					{
						//answer was correct
						correct += 1;
					}
					else
					{
						//answer was revealed
						//this variable is not used
						revealed += 1;
					}
				}
				
				//reset query string
				queryString = '';
			}
		}
	}	
	else
	{
		// If there is no ? in the url state no values found
		queryString = "No values detected";
	}
}

//count up score to be displayed later in the code - run this every time page is loaded
getScore();

//rewrite this to just use replace()
function removeItem( question, url )
{	
	if (url == '')
	{
		url = document.URL;
	}
	
	var queryString = '';				// query string that will be returned	
	var is_input = url.indexOf('?');	// Create variable is_input to see if there is a ? in the url

	// Check the position of the ? in the url
	if (is_input != -1)
	{ 
		queryString = '';	
		
		var addr_str = new String(url.substring(is_input, url.length));	// Create variable from ? in the url to the end of the string		
		var location = addr_str.indexOf(question);						// location of item to be removed
		var locationEnd = addr_str.substring(location).indexOf("&");	// location of end of question
		var re = '';													// regular expression for item to be removed
		
		//remove &display=true
		//this should only be in the url immediately after an answer has been selected
		//any other time the selected answer should not be shown
		re = new RegExp('&display=true',"gi");
		addr_str = addr_str.replace(re, '');
		
		if (location != -1) 
		{					
			if (addr_str.charAt(location - 1) == "&")
			{
				//replace question with answer including the leading "&"
				re = new RegExp('&' + question + '=\\d*',"gi");
				queryString += addr_str.replace(re, '');				
			}			
			else if (addr_str.charAt(location - 1) == "?" && addr_str.charAt(locationEnd + 1) == "&")
			{	
				//replace question with answer including the trailing "&"			
				re = new RegExp(question + '=\\d*&',"gi");
				queryString += addr_str.replace(re, '');								
			}
			else if (addr_str.charAt(location - 1) == "?")
			{	
				//replace question with answer - it is the only variable in the query string
				re = new RegExp('\\' + question + '=\\d*',"gi");
				queryString += addr_str.replace(re, '');					
			}
			else
			{
				// must not be in there...
			}
		}
		else
		{
			// Item was not found in quer string - return entire query string
			queryString = addr_str;
		}
	}
	else
	{
		// There was no query string
		queryString = '';
	}	
	
	return queryString;
}

function movenext(nextUrl)
{
	location.href = removeMistake( nextUrl + removeItem('return whole string', '') );
}

function moveprev(prevUrl)
{	
	location.href = removeMistake( prevUrl + removeItem('return whole string', '') );
}

// correctscore(), incorrectscore(), and showanswer() were kept to minimize code change in quiz
// these all just call answerQuestion() and pass in the appropriate code for correct, incorrect, and shown answers

function correctscore( question )
{
	answerQuestion( question, 1 );
}

function incorrectscore( question )
{
	answerQuestion( question, 0 );
}

function showanswer( question )
{
	answerQuestion( question, 2 );
}


// OLD CODE --------------------------------------------------------

function GetVars( def ) 
{
	this._def_ = def;
	var query, queries = top.location.search.substring(1).split( /\&/ );
	
	for ( var i=0; (query = queries[ i ]); i++ )
	{
		query = query.split( /\=/ );
		this[query[0]] = ( typeof query[1] == 'undefined' ) ? def : unescape(query[1]).replace( /\+/g, " " );
	}
}

GetVars.prototype.exists = function( key )
{
	return ( typeof this[key] != 'undefined' );
}

GetVars.prototype.assign = function( key )
{
	return ( this.exists( key ) ) ? this[key] : this._def_;
}

var _GET = new GetVars( '' );
var choose = '';

//only set choose if answering the question for the first time
//otherwise don't set it so the answer selected won't be displayed
//this is used when using the previous and next question links
if (_GET.assign( 'display' ) == 'true')
{
	choose = _GET.assign( QNumber ); //changed from n3 to QNumber to elimate extranious variables
}