$(document).ready(function() { 
		
	// Default type is 'radio';
	var type = 'radio';
		
	// We now have two types of quiz so we will determine the type		
	if($('#quiz').attr('class') == 'advanced') type = 'select';	
		
	// Hide the no javascript message
	$('.nojs').hide();
	
	// Array to hold the quiz
	var quiz = '';	
		
	// Add the title and intro
	quiz = quiz + '<h2>' + title + '</h2>';
		
	// Give message depending on type
	if(type=='radio') quiz = quiz + '<div class="box"><p>Complete the following sentences or answer the questions by choosing one option:</p></div>';
	else if(type=='select') quiz = quiz + '<div class="box"><p>Complete the following sentences by filling in the blanks:</p></div>';
	
	// Add helpful text to results
	$('#results').html('<p>Click the button above and we will show your results here...</p>');
	
	// Format the actual quiz
	if(type=='radio') {
		// Radio select box type
		quiz = quiz + '<ol id="questions">';
		for(i=0; i<questions.length; i++) {
			var question = '<li><strong>' + questions[i].replace('_', '_____________') + '</strong><br />';
			var responses = answers[i].split(";");
			for(m=0; m<responses.length; m++) {
				question = question + '<input type="radio" name="question' + i + '" id="question' + i + 'answer' + m + '" /> <label for="question' + i + 'answer' + m + '">' + responses[m] + '</label><br />';
			}
			question = question + '</li>';
			quiz = quiz + question;
		}
		quiz = quiz + '</ol>';	
	} else if(type=='select') {
		// Fill in the selects type
		quiz = quiz + '<p>';
		for(i=0; i<questions.length; i++) {
			var text = questions[i] + ' ';
			var option = '<select name="question' + i + '" id="question' + i + '"><option value="">&nbsp;</option>';
			var responses = answers[i].split(";");	
			for(m=0; m<responses.length; m++) {
				option = option + '<option>' + responses[m] + '</option>';
			}	
			option = option + '</select>';
			text = text.replace('_', option)
			quiz = quiz + text;
		}	
		quiz = quiz + '</p>';	
	}
		
	// Add a submit button
	quiz = quiz + '<br /><p><input type="submit" name="submit" value="Get your results" /> &nbsp; <small><a href="#" class="reset">Clear answers and start over?</a></small></p>';				
	
	// Put it all in a form
	quiz = '<form method="post" id="quizform" action="">' + quiz + '</form>';
		
	// Add it to the page
	$('#quiz').html(quiz);
	
	// Get the results
	$('#quizform').submit(function() { 
	
		// Vars
		var error = '';
		var correct = 0;
		var incorrect = 0;
		var unanswered = 0;				
	
		// Loop over each question to determine the results
		for(i=0; i<questions.length; i++) {
		
			// Need to calculate differently for each type of quiz
			if(type=='radio') {
		
				// Split the possible answers
				var responses = answers[i].split(";");
	
				// Variable to hold answer given
				var given = false;

				// Loop over and check each one
				for(m=0; m< responses.length; m++) {
			
					if($('#question' + i + 'answer' + m).attr('checked') == true) {
					
						// Set as response given
						given = responses[m];
						
						// If correct or not give a colour
						if(solutions[i] == responses[m]) $('label[for=question' + i + 'answer' + m + ']').css('color','green');	
						else $('label[for=question' + i + 'answer' + m + ']').css('color','red');						
					
					
					} else {
					
						$('label[for=question' + i + 'answer' + m + ']').css('color','');
					
					}	
					
				}
				
				// If no answer give then increment unanswered
				if(!given) unanswered++;
				// Determine if they answered correctly
				else if(solutions[i] == given) correct++;
				// Otherwise they answered incorrectly
				else incorrect++;
				
			} else if(type=='select') {
			
				// Hold the user response
				var selected = $('#question' + i).val();
	
				// Did they answer it at all
				if(selected == '') {
					$('#question' + i).css('color','');
					unanswered++;
				} else {
	
					// Did they get it correct?
					if(selected == solutions[i]) {
						$('#question' + i).css('color','green');
						correct++;
					}
			
					// They got it wrong
					else {
						$('#question' + i).css('color','red');
						incorrect++;
					}
					
				}

			}

		}
		
		// Append a results div
		$('#results').html('<h3>Results</h3><p><big><strong>Your score is ' + Math.round(((correct/questions.length)*100)) + '%</strong></big><br />You answered ' + (correct + incorrect) + ' out of ' + questions.length + ' total questions<br /><strong>' + correct + '</strong> correct<br /><strong>' + incorrect + '</strong> incorrect</p><h4>Suggestions</h4><p>' + suggestions + '</p>');
		
		// Stop the form from submitting
		return false;

	});		
	
	$('.reset').click(function() { 
		$('input[type=radio]').removeAttr('checked');
		$('select').val('').css('color','');
		$('label').css('color','');
		$('#results').html('<p>Click the button above and we will show your results here...</p>')
		return false;
	});
	
});

