var input;

function quiz(array, ele)
{
	input = array;
	ele = (document.getElementById) ? document.getElementById('quiz') : document.all('quiz');
	
	a = '<form name="quizform"><ol>';
	for (i=0; i<array.length; i++)
	{
		temp = array[i].split('~');
		a += '<li>' + temp[0];
		for (j=2; j<temp.length; j++)
		{
			a +='<p><input type="radio" name="' + String.fromCharCode(i+65) + '" value="' + (j-1) + '" />' + temp[j] + '</p>';
		}
		a+= '</li>';
	}
	a += '</ol><input type="button" id="quiz-submit" name="quiz-submit" value="Check answers!" onclick="check()" /></form><div id="quizresults"></div>';
	ele.innerHTML += a;
}

function check()
{
	count = 0;
	a = '';
	ele = (document.getElementById) ? document.getElementById('quizresults') : document.all('quizresults');
	for (i=0; i<input.length; i++)
	{
		tmp = input[i].split('~');
		k=i+65;
		count2 = 0;
		for (j=0; j < eval('document.quizform.' + String.fromCharCode(k) + '.length;'); j++)
		{
			if (eval('document.quizform.' + String.fromCharCode(k) + '[' + j + '].checked'))
			{
				if (j+1 == parseInt(tmp[1]))
				{
					//a += '<p>Answer for question ' + (i+1) + ' is correct.</p>';
					count++;
				}
				else
					a += '<p>Question ' + (i+1) + ': the correct answer is "' + tmp[parseInt(tmp[1])+1] + '.</p>';
				break;
			}
			else
				count2++;
		}
		if (count2 == 4)
		{
			a += '<p>Question ' + (i+1) + ': the correct answer is "' + tmp[parseInt(tmp[1])+1] + '".</p>';
		}
	}
	if (count == input.length)
		ele.innerHTML = 'Well done! You\'ve answered all questions correctly!';
	else
		ele.innerHTML = '<p>Your score is: ' + count + '/' + input.length + '. Here are the questions that you\'ve failed to answer correctly:</p>' + a;
}
