/******************************************
 * @author Maria A. Zamora
 * @company Digitec Interactive Inc.
 * @version 0.1
 * @date 12/23/2010
 * 
 * This is the Quiz class for the CEE application
 * CS5 version of com.digitec.cee.Quiz
 *  
*******************************************/
package com.digitec.cee
{
	import flash.display.MovieClip;	
	import flash.display.Sprite;		
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.events.Event;	
	
	public class Quiz 
	{						
		private var quiz_instructions:String;
		private var num_of_questions:int;		
		private var is_quiz_complete:Boolean;		
		private var num_correct_answers:int;
		private var question:Question;
		private var are_questions_random:Boolean;		
		//ARRAY OF QUESTION OBJECTS
		private var questions_arr:Array;		
		public var dwkeepthispos:Number;
		 
		 public function Quiz(xml:XML, _maxAnswers:int, dwthisispos)		
		 {								
		 dwkeepthispos=dwthisispos;
			trace("dwkeepthispos"+dwkeepthispos);
		 	is_quiz_complete = false;						
			xml.ignoreWhitespace = true;													
			quiz_instructions = xml.start_screen.instructions.toString();				
			
			//IF QUESTIONS HAVE TO BE RANDOM
			if( xml.quizconcept.question_screen.random_questions[dwkeepthispos].toString() == "true" || xml.quizconcept.question_screen.random_questions[dwkeepthispos].toString() == "yes" )
			{
				are_questions_random = true;
			}
			else{ are_questions_random = false; }
			//Max number of answer display objects in application
			questions_arr = setQuestions(xml,_maxAnswers );		
			trace("questions_arr  "+questions_arr);
		}
		public function dwkeeppos(dwthisisposba){
			dwkeepthispos=dwthisisposba;
			trace("dwkeepthispos"+dwkeepthispos);
		}
		
		/*******************************************************************
	 	* SET FUNCTIONS
		*******************************************************************/				
		public function set quizInstructions( str:String ):void{  quiz_instructions = str; }
			
		public function set numOfQuestions( num:int ):void{  num_of_questions = num; }
		
		public function set isQuizComplete( itis:Boolean ):void	{  is_quiz_complete = itis; }
		
		public function set numCorrectAnswers( num:int ):void	{  num_correct_answers = num; }
		
		public function set areQuestionsRandom( itis:Boolean ):void	{  are_questions_random = itis; }		
		
		public function set questionsArr( arr:Array ):void	{  questions_arr = arr; }		
		
		
		/*******************************************************************
	 	* GET FUNCTIONS
		*******************************************************************/				
		public function get quizInstructions():String{ return quiz_instructions; }
			
		public function get numOfQuestions():int{ return num_of_questions; }
		
		public function get isQuizComplete():Boolean	{ return is_quiz_complete; }
		
		public function get numCorrectAnswers():int	{ return num_correct_answers; }
		
		public function get areQuestionsRandom():Boolean	{ return are_questions_random; }		
		
		public function get questionsArr():Array	{ return questions_arr; }		
		
		
		/*******************************************************************
	 	* FUNCTION:  TO LOAD TEXT FROM A XML FILE
		*******************************************************************/		
		private function setQuestions(_xml:XML, maxAnswers:int ):Array
		{				
			trace("dwkeepthispo2  "+dwkeepthispos);
			var question_array:Array = new Array();
			
			//GET THE QUESTIONS FOR THE question_exercise NODES
			//trace("this no trace"+_xml.quizconcept.question_screen[dwkeepthispos].elements("question_exercise"));
			num_of_questions = _xml.quizconcept.question_screen[dwkeepthispos].elements("question_exercise").length();
			//trace("Number of questions = " + num_of_questions); 
			for( var i:int=0; i < num_of_questions; i++ )
			{
				//SEND XML PIECE TO QUESTION CONSTRUCTOR
				//xml.quizconcept.question_screen.random_questions[dwkeepthispos]
				question = new Question( _xml.quizconcept.question_screen[dwkeepthispos].question_exercise[i], maxAnswers );				
				question_array[i] =question;				
			}			
			
			//MAKE RANDOM ORDER QUESTION ARRAY
			if( are_questions_random == true && question_array != null && question_array.length > 1 )
			{
				var randomInt:int = Math.floor(Math.random()*(question_array.length));							
				var temp:Question = question_array[randomInt];				
				var other:int;
				if(randomInt == 0){other = 1; } else {other = 0;}
				question_array[randomInt] = question_array[other];				
				question_array[other] = temp;							
			}
			
			return question_array;						
		}	
				
				 
	} // Main class
 
} //end package


/*****************************QUIZ XML FILE***************************************
<question_screen>
		<random_questions>yes</random_questions>		
		<question_exercise>
		<random_answers>yes</random_answers> 
		<question>Question number 1. Multiline text field and are loaded from a XML</question> 
			<correct_answer>Correct Answer 1 This answer will display randonly</correct_answer> 
			<answer>Incorrect Answer 1 </answer>
			<answer>Incorrect Answer 2 </answer>
			<answer>Incorrect Answer 3 </answer>
			<answer>Incorrect Answer 4 </answer>
			<answer>Incorrect Answer more in case... </answer>
		</question_exercise>
		<question_exercise>
		<random_answers>yes</random_answers> 
		<question>Question number 2. Multiline text field and are loaded from a XML</question> 
			<correct_answer>Correct Answer 1 This answer will display randonly</correct_answer> 
			<answer>Answer 2 </answer>
			<answer>Answer 3 </answer>
			<answer>Answer 4 </answer>
			<answer>Answer 5 </answer>
			<answer>Answer more </answer>
		</question_exercise>
		<question_exercise>
		<random_answers>yes</random_answers> 
		<question>Question number 3. Multiline text field and are loaded from a XML</question> 
			<correct_answer>Correct Answer 1 This answer will display randonly</correct_answer> 
			<answer>Answer 2 </answer>
			<answer>Answer 3 </answer>
			<answer>Answer 4 </answer>
			<answer>Answer 5 </answer>
			<answer>Answer more </answer>
		</question_exercise>
		<question_exercise>
		<random_answers>yes</random_answers> 
		<question>Question number 4. Multiline text field and are loaded from a XML</question> 
			<correct_answer>Correct Answer 1 This answer will display randonly</correct_answer> 
			<answer>Answer 2 </answer>
			<answer>Answer 3 </answer>
			<answer>Answer 4 </answer>
			<answer>Answer 5 </answer>
			<answer>Answer more </answer>
		</question_exercise>
		<question_exercise>
		<random_answers>yes</random_answers> 
		<question>Question number 5. Multiline text field and are loaded from a XML</question> 
			<correct_answer>Correct Answer 1 This answer will display randonly</correct_answer> 
			<answer>Answer 2 </answer>
			<answer>Answer 3 </answer>
			<answer>Answer 4 </answer>
			<answer>Answer 5 </answer>
			<answer>Answer more </answer>
		</question_exercise>
	</question_screen>
	***************************************************************/



