var useranswers = new Array();

var answered = 0;

var QuestionsPerPage = 3;

var usedQuestion = new Array(questions.length-1);

var actualQuestion;

var anzbeantwfragen = 0;

var anzkorrantworten = 0;

var fragenanzahl = 99;

var abstand = 100;

var vonoben = 150;



//----------------------------------------------------------------------------------------------------------------------------

function Main()

{

  HideAllQuestions();

  EnableAllQuestions();

  ShowQuestions();

  ShowButtons();



}





//----------------------------------------------------------------------------------------------------------------------------

function HideAllQuestions()

{

	for(i=0;i<questions.length;i++)

	{

		document.writeln('<span id="test'+i+'"style="position:absolute">');

		document.writeln( '<div class="question">' + questions[i] + ' <span id="result_' + i + '"></span></div>' );

		for(j=0;j<choices[i].length;j++)

		{

			document.writeln('<input type="radio" name="answer_' + i + '" value="' + choices[i][j] + '" id="answer_' + i + '_' + j + '" class="question_' + i + '"   onclick="submitAnswer(' + i + ', this, \'question_' + i + '\', \'label_' + i + '_' + j + '\')" /><label id="label_' + i + '_' + j + '" for="answer_' + i + '_' + j + '"> ' + choices[i][j] + '</label><br />');

			}

		document.writeln('</span>');
		
		document.getElementById('test'+i).style.visibility = "hidden";

	}

}



//----------------------------------------------------------------------------------------------------------------------------

function ShowButtons()

{

	document.writeln('&nbsp;<br> &nbsp;<br> &nbsp;<br> &nbsp;<br> &nbsp;<br> &nbsp;<br> &nbsp;<br> &nbsp;<br>&nbsp;<br> &nbsp;<br> &nbsp;<br> &nbsp;<br> &nbsp;<br> &nbsp;<br> &nbsp;<br> &nbsp;<br> &nbsp;<br><p><input type="submit" value="weitere Fragen" onclick="weiter()" /> <input type="submit" value="Spielstand" onclick="showScore()" /> <input type="submit" value="Neustart" onclick="refreshQuiz()"/> <input type="submit" value="beenden" onclick="beenden()" /> </p><div style="display:none"><img src="../../bilder/quiz/correct.gif" style="border:0" alt="Correct!" /><img src="../../bilder/quiz/incorrect.gif" style="border:0" alt="Incorrect!" /></div>');

}

//----------------------------------------------------------------------------------------------------------------------------

function ShowQuestions()

{

  answered=0;

  for(i=0;i<QuestionsPerPage;i++)

  {

	VisibleQuestion=QuizQuestion();

   	if (VisibleQuestion>-1)

   	{

   		document.getElementById('test'+VisibleQuestion).style.visibility = "visible";

   		document.getElementById('test'+VisibleQuestion).style.top = (i * abstand) + vonoben;
		
	}

	else

	{

		QuestionsHide();

		alert('Keine neuen Fragen mehr vorhanden!');

	}

  }

}



function QuestionsHide()

{

	for(i=0;i<questions.length;i++)

	{

	    document.getElementById('test'+i).style.visibility = "hidden";

	}

}



//----------------------------------------------------------------------------------------------------------------------------



function resetQuiz(showConfirm)

{

	if(showConfirm)

		if(!confirm("Bist du sicher, dass du nochmal von vorn anfangen willst?"))

			return false;

	QuestionsHide();

	ShowQuestions();

}



//----------------------------------------------------------------------------------------------------------------------------



function refreshQuiz()

{

	self.location.reload();

}



//----------------------------------------------------------------------------------------------------------------------------



function ExistsUnusedQuestion()

{

	for(k=0;k<questions.length;k++)

	{

		if(!(usedQuestion[k]==true))

		{

			return true;

		}

	}

	return false;

}



function randomINT()

{

	return Math.round(Math.random()*(questions.length-1));



}



function QuizQuestion()

{



		if (!(ExistsUnusedQuestion()==true)) return -1;



		do

		{

			actualQuestion=(randomINT());

		}

		while (usedQuestion[actualQuestion]==true);

		usedQuestion[actualQuestion]=true;

//		alert(actualQuestion);

		return actualQuestion;





}



//----------------------------------------------------------------------------------------------------------------------------

function submitAnswer(questionId, obj, classId, labelId)

{

	useranswers[questionId] = obj.value;

	document.getElementById(labelId).style.fontWeight = "bold";

	disableQuestion(classId);

	showResult(questionId);

	answered++;

         anzbeantwfragen = anzbeantwfragen + answered / 2;

}

//----------------------------------------------------------------------------------------------------------------------------

function showResult(questionId)

{

	if(answers[questionId] == useranswers[questionId])

	{

		document.getElementById('result_' + questionId).innerHTML = '<img src="../../bilder/quiz/correct.gif" style="border:0" alt="Correct!" />';

	}

	else

	{

		document.getElementById('result_' + questionId).innerHTML = '<img src="../../bilder/quiz/incorrect.gif" style="border:0" alt="Incorrect!" />';

	}

}

//----------------------------------------------------------------------------------------------------------------------------

function disableQuestion(classId)

{

	var alltags = document.all? document.all : document.getElementsByTagName("*")

	for (i=0; i<alltags.length; i++)

	{

		if (alltags[i].className == classId)

		{

			alltags[i].disabled = true;

		}

	}

}

//----------------------------------------------------------------------------------------------------------------------------

function showScore()

{

	if(answered != QuestionsPerPage)

	{

		alert("Beantworte erst alle drei Fragen dieser Seite.");

		return false;

	}

	correct = 0;

	incorrect = 0;

	for(i=0;i<questions.length;i++)

	{

		if (document.getElementById('test'+i).style.visibility == "visible")

		{

		if(useranswers[i] == answers[i])

                 {

			correct++;

                         anzkorrantworten++;

                 }

		else

                         incorrect++;

		}

	}

	pc = Math.round((anzkorrantworten / anzbeantwfragen) * 100);

	alertMsg = "Du hast " + anzkorrantworten + " von " + anzbeantwfragen + " Fragen richtig beantwortet ,das sind unglaubliche " + pc + "%\n\n";

	if(pc == 100)

		alertMsg += response[0];

	else if(pc >= 90)

		alertMsg += response[1];

	else if(pc >= 70)

		alertMsg += response[2];

	else if(pc > 50)

		alertMsg += response[3];

	else if(pc >= 40)

		alertMsg += response[4];

	else if(pc >= 20)

		alertMsg += response[5];

	else if(pc >= 10)

		alertMsg += response[6];

	else

		alertMsg += response[7];



	if(pc <= 100)

	{

		if(confirm(alertMsg))

			resetQuiz(false);

		else

			return false;

	}

	else

	{

		alert(alertMsg);

	}

}

//----------------------------------------------------------------------------------------------------------------------------

function weiter()

{

	if(answered != QuestionsPerPage)

	{

		alert("Beantworte erst alle drei Fragen dieser Seite.");

		return false;

	}

	correct = 0;

	incorrect = 0;

	for(i=0;i<questions.length;i++)

	{

		if (document.getElementById('test'+i).style.visibility == "visible")

		{

		if(useranswers[i] == answers[i])

                 {

			correct++;

                         anzkorrantworten++;

                 }

		else

                         incorrect++;

		}

	}

         	pc = Math.round((anzkorrantworten / anzbeantwfragen) * 100);

	alertMsg = "Du hast " + anzkorrantworten + " von " + anzbeantwfragen + " Fragen richtig beantwortet ,das sind unglaubliche " + pc + "%\n\n";



	if(pc == 100)

		alertMsg += response[0];

	else if(pc >= 90)

		alertMsg += response[1];

	else if(pc >= 70)

		alertMsg += response[2];

	else if(pc > 50)

		alertMsg += response[3];

	else if(pc >= 40)

		alertMsg += response[4];

	else if(pc >= 20)

		alertMsg += response[5];

	else if(pc >= 10)

		alertMsg += response[6];

	else

		alertMsg += response[7];

         alertMsg += "\n\n" + "Es gibt keine weiteren Fragen mehr."



         if (anzbeantwfragen < fragenanzahl)

         {

         	resetQuiz(false);

         }

         else

         {

         	alert(alertMsg);

                 window.location.href="seite3.html"

         }

}



//----------------------------------------------------------------------------------------------------------------------------

function beenden()

{

	if(answered != QuestionsPerPage)

	{

		alert("Beantworte erst alle drei Fragen dieser Seite.");

		return false;

	}

	correct = 0;

	incorrect = 0;

	for(i=0;i<questions.length;i++)

	{

		if (document.getElementById('test'+i).style.visibility == "visible")

		{

		if(useranswers[i] == answers[i])

                 {

			correct++;

                         anzkorrantworten++;

                 }

		else

                         incorrect++;

		}

	}

         	pc = Math.round((anzkorrantworten / anzbeantwfragen) * 100);

	alertMsg = "Du hast " + anzkorrantworten + " von " + anzbeantwfragen + " Fragen richtig beantwortet ,das sind unglaubliche " + pc + "%\n\n";



	if(pc == 100)

		alertMsg += response[0];

	else if(pc >= 90)

		alertMsg += response[1];

	else if(pc >= 70)

		alertMsg += response[2];

	else if(pc > 50)

		alertMsg += response[3];

	else if(pc >= 40)

		alertMsg += response[4];

	else if(pc >= 20)

		alertMsg += response[5];

	else if(pc >= 10)

		alertMsg += response[6];

	else

		alertMsg += response[7];

         alertMsg += "\n\n" + "Hasta pronto. Bis bald."



         alert(alertMsg);

         window.location.href="seite3.html"

}



//----------------------------------------------------------------------------------------------------------------------------

function EnableAllQuestions()

{

	var alltags = document.all? document.all : document.getElementsByTagName("*")

	for (i=0;i<alltags.length;i++)

	{

			alltags[i].disabled = false;

	}

}

//=============================================================================================================================
