/* 
******************************************************************************
				C O N T R O L    V A R I A B L E S
******************************************************************************
*/

	var passedParameters = window.location.href; // get the control parameters from the URL string
	
	var parameterIndex = passedParameters.lastIndexOf("?");
	if (parameterIndex == -1) {
		// no parameters passed
		passedParameters = new Array ("0","","","","0"); // defaults
	}
	else {
		passedParameters = passedParameters.split("?");
		passedParameters = passedParameters[1].split("&");
		// passedParameters[0] stores the current question ID
		// passedParameters[1] stores the correct answer
		// passedParameters[2] stores the user selected answer
		// passedParameters[3] stores a brief description
		// passedParameters[4] stores the number of correct answers
	}
	
	var quizPageName = getQuizPageName(); // get the name of the page we are working on
	
	var questionID = parseInt(passedParameters[0]); // current question we are working on
	var numberCorrectAns = parseInt(passedParameters[4]); // number of correct answers so far

	var numberOfQuestions = getNumberOfQuestions(); // get total number of questions
	var newQuestion = getQuestion(questionID); // get the text for the new question
	var questionOptions = getQuestionOptions(questionID); // get the options for the new question
	var questionHint = getQuestionHint(questionID); // get the text for the new question's hints
	//var briefDescription = getBriefDescription(questionID); // get any description for the new question

	var questionOptionsIDs = "a~b~c~d~e~f~g~h~i"; // standard options... you can have here any number of valid option ids
		questionOptionsIDs = questionOptionsIDs.split("~");

/* 
******************************************************************************
				F U N C T I O N S
******************************************************************************
*/			
			
		
	/*  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -
		countCorrectAnswers()
		Created:  	August 2001
		Author:  	Yassen Bakalov (ybakalov@icfconsulting.com)
		Modified:  	______________
		
		Comments: this is a simple counter that increments the number of correct answers with each question
		##########################################################################
	    -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  */
	function countCorrectAnswers(){
		numberCorrectAns ++;
	}

	/*  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -
	userAnswer()
	Created:  	August 2001
	Author:  	Yassen Bakalov (ybakalov@icfconsulting.com)
	Modified:  	______________
	
	Comments: returns the value of the selected radio button id=q1
	##########################################################################
    -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  */		
	function userAnswer(){
	var userSelection = "";
		for (i=0; i<questionOptions.length; i++) {
			if (document.forms[0].q1[i].checked) {
				userSelection = document.forms[0].q1[i].value;
				break;
			}
		}
	return userSelection;
	}
	
	/*  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -
	validateQuestion()
	Created:  	August 2001
	Author:  	Yassen Bakalov (ybakalov@icfconsulting.com)
	Modified:  	______________
	
	Comments: validates user's selection -- if no selection is made, nothing happens
				Based on the answer, a new string URL is generated that holds user's answer
	##########################################################################
    -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  */	
	function validateQuestion() {
		if (userAnswer() != "") {
			var correctAns = correctAnswer(questionID);//window.opener.
			var myAnswer = userAnswer();
			if (correctAns == myAnswer) countCorrectAnswers();//window.opener.
			
			window.location.href = quizPageName + "?" + passedParameters[0] + "&" + correctAns + "&" +  myAnswer + "&1" + "&" + numberCorrectAns;
		}
	}
	
	/*  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -
	nextQuestion(nextQuestionID)
	Created:  	August 2001
	Author:  	Yassen Bakalov (ybakalov@icfconsulting.com)
	Modified:  	______________
	
	Comments: this is our navigational function -- if there are more questions to be 
				answered we move on, otherwise we bring the results page
	##########################################################################
    -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  */			
	function nextQuestion(nextQuestionID){
			if (nextQuestionID < numberOfQuestions) window.location.href = quizPageName + "?" + nextQuestionID + "&&&" + "&" + numberCorrectAns; // let's move to the next question
			//else window.location.href = "results.html?" + "&&&&" + numberCorrectAns; // we don't care about any other parameters for now
			else window.location.href = quizPageName + "?" + "-1&&&&" + numberCorrectAns; // we don't care about any other parameters for now
	}

	
/* 
******************************************************************************
				DOCUMENT.WRITE  F U N C T I O N
******************************************************************************
*/	


	// from here onwards, we build the page layout


	/*  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -
	buildQuizPage()
	Created:  	August 2001
	Author:  	Yassen Bakalov (ybakalov@icfconsulting.com)
	Modified:  	______________
	
	Comments: this is our most important function -- it will create the page on the fly
				Any layout will be defined here.
	##########################################################################
    -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  */					
	function buildQuizPage(){
	
		var htmlCode = "";
	
		if (questionID != -1) { // if it is not the results page
			
			htmlCode += newQuestion + '<br><br><table cellpadding="0" cellspacing="0" border="0">'; // the text of the question
			
			for (qOpt=0; qOpt<questionOptions.length; qOpt++) {
				htmlCode += '<tr><td valign=top>';
				if (passedParameters[2] != "") {
					// we want to show the correct and selected answers to the question

					if (passedParameters[1] == questionOptionsIDs[qOpt]) { // the correct answer matches this option's ID ("a" == "a")
						htmlCode += '<img src="../images/correct.gif" width=20 height=20 alt="Correct answer"></td><td>&nbsp;';
						if (passedParameters[2] == questionOptionsIDs[qOpt]) // user's answer matches this option's ID ("a" == "a")
							htmlCode += '<strong><font size="4">&middot;</font>' + questionOptions[qOpt] + '</strong> -&gt; Correct answer!!!';
						else 
							htmlCode += '<strong><font size="4">&middot;</font>' + questionOptions[qOpt] + '</strong> -&gt; Sorry, this is the correct one...';  // user's answer was wrong;
					
					}
					else if (passedParameters[2] == questionOptionsIDs[qOpt]) {
						htmlCode += '<img src="../images/wrong.gif" width=20 height=20 alt="Wrong answer!"></td><td>&nbsp;';
						htmlCode += '<strong><font size="4">&middot;</font></strong>'+questionOptions[qOpt];
						
					}
					
					else htmlCode += '<img src="../images/clearpixel.gif" width=20 height=20 alt=" "></td><td>&nbsp;<strong><font size="4">&middot;</font></strong>' + questionOptions[qOpt]; // the correct answer is not this one
								
				} // end if (passedParameters[2] != "")
				else {
					htmlCode += '<input type="radio" name="q1" value="' + questionOptionsIDs[qOpt] + '"></td><td>' + questionOptions[qOpt];
				}
				
				htmlCode += '</td></tr>'
				
			} // end for (qOpt=0; qOpt<questionOptions.length; qOpt++) loop
		
			htmlCode += '</table><br><br>'
			
			//if (passedParameters[3] != "") htmlCode += briefDescription + '<br><br>'; // show the description
			
			
			if (passedParameters[2] != "") { // if there are passed parameters it means that we just validated the question, so go to the next question now
				//htmlCode += '<a href="javascript:void(nextQuestion('+(questionID+1)+'))">Next Question</a>'
				if ((numberOfQuestions-1) > questionID) htmlCode += '<input type=button value="Next Question" onclick="nextQuestion('+(questionID+1)+')">'
				else htmlCode += '<input type=button value="Tally My Score" onclick="nextQuestion('+(questionID+1)+')">'
			}
			else { // no passed parameters? then we need to validate the question
				//htmlCode += '<a href="javascript:void(validateQuestion('+questionID+'))">Submit Your Answer</a>'
				htmlCode += questionHint + "<br><br>";
				htmlCode += '<input type=button value="Submit Your Answer" onclick="validateQuestion('+questionID+')">'
			}
			
		} // end if (questionID != -1)
		
		else { // if the page to be generated is the RESULTS page
			htmlCode += '<table width="350"><tr align="center"><td>You got ' + numberCorrectAns + ' correct answers out of ' + numberOfQuestions + ' possible.<br></td></tr>';
			if (numberCorrectAns == numberOfQuestions)
				htmlCode += '<tr align="center"><td><img src="../images/sun.gif" alt="smiling sun"><BR></td></tr>'
			//htmlCode += '<a href="javascript:void(opener.focus(),self.close())">CLOSE</a>'
			htmlCode += '<tr align="center"><td><input type=button value="Close Window" onclick="if (opener) opener.focus();self.close()"></td></tr></table>'
		}
		
	document.write(htmlCode);		
	}
		
		

