var iTimer=1500;
var tmpTimer=0;
var pointTimer=10;
var arrQuiz = new Array();
var arrSvar = new Array();
var lRnd=0;
var iQuizNo=0;
var iQuizTry=0;
var flgQuizReady=false;
var flgCorrect=false;
var flgPas=false;
var arrResult = new Array(10);
var replay=false;

function fnNewQset2(){
	document.getElementById("einstein").innerHTML='';
	if(udfordring=='0'){
		document.getElementById("teaser").innerHTML="&quot;Spørgsmål er på vej.&quot;";
		winHdn.location.replace("../spil/leksiquiz2.asp?qCount=" + document.getElementById("quizvalg").qCount.value + "&qCat=" + document.getElementById("quizvalg").qCat.value + "&qLevel=" + document.getElementById("quizvalg").qLevel.value);
	}
	else{
		fnStartQuiz();
	}
}
function fnStartQuiz(){
	fnInitQuiz();
	document.getElementById("teaser").innerHTML=arrQstart[Math.round(Math.random()*(arrQstart.length-1))];
	document.getElementById("hint").innerHTML=arrHints[0];
	document.getElementById("imgs0").src="../images/spil/neutral.gif";		
	document.getElementById("imgs1").src="../images/spil/neutral.gif";		
	document.getElementById("imgs2").src="../images/spil/neutral.gif";		
	document.getElementById("imgs3").src="../images/spil/neutral.gif";		
	document.getElementById("spm").innerHTML='';
	document.getElementById("s1").innerHTML='';
	document.getElementById("s2").innerHTML='';
	document.getElementById("s3").innerHTML='';
	document.getElementById("s4").innerHTML='';
	document.getElementById("stress").width=440;
	winWait=window.setTimeout("fnNewSpm()",2000);
}
function fnNewQuiz(nyvalg){
	if(nyvalg){
		with (document.getElementById("quizvalg")){
			document.getElementById("qCount").value=document.getElementById("antal").value;
			document.getElementById("qLevel").value=document.getElementById("niveau").value;
			document.getElementById("qCat").value=document.getElementById("kategori").value;
		}
	}
	winHdn.location.replace('../spil/lqgame.htm');
	document.getElementById("results").innerHTML="";
}
function fnInitQuiz(){
	iTimer=1500;
	tmpTimer=0;
	pointTimer=10;
	lRnd=0;
	iQuizNo=0;
	iQuizTry=0;
	flgQuizReady=false;
	flgCorrect=false;
	flgPas=false;
	arrResult = new Array();
	arrResult.tid=new Array(arrQuiz.length);
	arrResult.forsog=new Array(arrQuiz.length);
	arrResult.rigtig=new Array(arrQuiz.length);
	arrResult.pas=new Array(arrQuiz.length);
	arrResult.point=new Array(arrQuiz.length);
}
function fnStartCountDown(){
	tmpTimer=iTimer;
	//fnCountDown();
	winWait=window.setTimeout("fnCountDown()",tmpTimer);
}
function fnCountDown(){
	//oBGSound.loop = 1;
	obj=document.getElementById("stress")
	obj.width=(obj.offsetWidth-(obj.offsetWidth/10));
	if(tmpTimer>100){
		if(iQuizTry==0||udfordring!='0'){
			if(tmpTimer<=1000 && tmpTimer>500){
				document.getElementById("teaser").innerHTML = arrTeaser[1];
			}
			if(tmpTimer<=500){
				document.getElementById("teaser").innerHTML = arrTeaser[2];
			}
		}
		tmpTimer=tmpTimer-(tmpTimer/10);
		//oBGSound.src = oBGSound.src; // reload sound
		winWait=window.setTimeout("fnCountDown()",tmpTimer);
	}
	else{
		obj.width=2;
		fnTimeOut();
	}
}
function fnTimeOut(){
	switch(strCurrentGame){
	case "lq":
		fnQuizAnswer(5);
		break
	default:
		fnGameOver();
	}
}
function fnStartPointTimer(){
	pointTimer=0;
	pointWait=window.setTimeout("fnPointTimer()",1000);
}
function fnPointTimer(){
	pointTimer++;
	pointWait=window.setTimeout("fnPointTimer()",1000);
}	
function fnRndQuiz(){
	iQuizTry=(udfordring!='0')?1:0;
	//lRnd=(udfordring!='0')?0:Math.round(Math.random()*3);
	lRnd=Math.round(Math.random()*3);
	arrInc= new Array();
	arrInc[lRnd]=arrQuiz[iQuizNo].a1;
	a=2;
	for(i=0;i<4;i++){
		if(lRnd!=i){
			arrInc[i]=eval("arrQuiz[iQuizNo].a"+a);
			a++;
		}
	}
}
function fnQuizAnswer(a){
	if(flgQuizReady){
		flgCorrect=false;
		flgPas=false;
		iQuizTry++;
		if(a!=4&&udfordring=='0'){
			if(a!=5){
				oImg=document.getElementById("imgs"+a);
			}
			if(lRnd==a){
				oImg.src="../images/spil/Rigtigt.gif";
				document.getElementById("einstein").innerHTML='<img class=lqEinstein src="../images/spil/stjerner.gif">';
				document.getElementById("teaser").innerHTML=arrRos[Math.round(Math.random()*(arrRos.length-1))];
			}
			else{
				if(a!=5){
					document.getElementById("teaser").innerHTML=arrHaan[Math.round(Math.random()*(arrHaan.length-1))];
					oImg.src="../images/spil/Forkert.gif";
				}
				else{
					document.getElementById("teaser").innerHTML=arrNosvar[Math.round(Math.random()*(arrNosvar.length-1))];
				}
				if(iQuizTry==2 || a==5){
					oImg=document.getElementById("imgs"+lRnd);
					oImg.src="../images/spil/Rigtigt.gif";
					document.getElementById("einstein").innerHTML='<img class=lqEinstein align=top src="../images/spil/tunge2.gif">'
				}
			}
		}
		if(iQuizTry==2 || lRnd==a || a>3){
			clearTimeout(pointWait);
			clearTimeout(winWait);
			flgQuizReady=false;
			
			if(udfordring!='0'){
				arrResult.point[iQuizNo-1]=a;
				arrResult.tid[iQuizNo-1]=pointTimer;
				winWait=window.setTimeout("fnNewSpm()",1000);
			}
			else{	
				flgCorrect=(lRnd==a)?true:false;
				if(a==4){
					flgPas=true;
					fnOpsaml();
					fnNewSpm();
				}
				else{
					fnOpsaml();
					winWait=window.setTimeout("fnNewSpm()",1500);
				}
			}
		}
	}
}				
function fnNewSpm(){
	if(iQuizNo<arrQuiz.length){
		document.getElementById("einstein").innerHTML='';
		document.getElementById("imgs0").src="../images/spil/neutral.gif";		
		document.getElementById("imgs1").src="../images/spil/neutral.gif";		
		document.getElementById("imgs2").src="../images/spil/neutral.gif";		
		document.getElementById("imgs3").src="../images/spil/neutral.gif";		
		fnRndQuiz();
		document.getElementById("spm").innerHTML=arrQuiz[iQuizNo].q;
		document.getElementById("s1").innerHTML=arrInc[0];
		document.getElementById("s2").innerHTML=arrInc[1];
		document.getElementById("s3").innerHTML=arrInc[2];
		document.getElementById("s4").innerHTML=arrInc[3];
		document.getElementById("teaser").innerHTML=arrTeaser[0];
		//hint.innerHTML=arrHints[Math.round(Math.random()*(arrHints.length-1))];
		document.getElementById("stress").width=440;
		iQuizNo++;
		flgQuizReady=true;
		fnStartCountDown();
		fnStartPointTimer();
	}
	else {
		clearTimeout(pointWait);
		clearTimeout(winWait);
		if(udfordring=='0'){
			winHdn.location.replace("../spil/lqgameover.asp")
		}
		else{
			sResult=""
			
			udfordring=''+udfordring;
			for(x=0;x<arrQuiz.length;x++){
				sResult+=", s"+x+":"+arrResult.point[x]+", t"+x+":"+arrResult.tid[x];
			}					
			winHdn.location.replace("../spil/udfordr3.asp?udf="+udfordring+"&arrQ="+escape(sResult));
		}
	}
}
function fnResult(){
	var arrPas=new Array();
	var arrWrong=new Array();
	var arrRight=new Array();
	var iResult=0;
	for(x=0;x<arrQuiz.length;x++){
		if(arrResult.pas[x]){
			arrPas[arrPas.length]=x;
		}
		else{
			if(arrResult.rigtig[x]){
				arrRight[arrRight.length]=x;
			}
			else{
				arrWrong[arrWrong.length]=x;
			}
		}
		iResult+=arrResult.point[x];		
	}
	if(iHiScore==0||iHiScore<iResult){
		try{
			document.getElementById("score").style.display='block';
			document.getElementById("teaser").innerHTML="&quot;Tillykke! Du er kommet på TOP 20 listen. Skriv dit navn.&quot;";
			document.getElementById("einstein").innerHTML='<img class=lqEinstein src="../images/spil/stjerner.gif">';
		} catch(err){
			document.getElementById("teaser").innerHTML="&quot;Tillykke! Du er kommet på TOP 20 listen. Skriv dit navn.... Næh..Det kan du jo ikke!&quot;";
			document.getElementById("einstein").innerHTML='<img class=lqEinstein src="../images/spil/stjerner.gif">';
		}
	}
	else {
		try{
		document.getElementById("score").style.display='none';
		document.getElementById("teaser").innerHTML="&quot;Du fik ikke points nok til at komme på Top 20! Vil du prøve igen?&quot;";
		document.getElementById("einstein").innerHTML='';
		} catch(err){
			document.getElementById("teaser").innerHTML="&quot;Skulle du ikke få dig et abonnement?&quot;";
			document.getElementById("einstein").innerHTML='';
		}
	}
	document.getElementById("point").innerHTML="Du fik <b>" + iResult + "</b> point";
	var sResults='';
	if(arrPas.length>0){
		sResults+='<H1>Du meldte pas på følgende spørgsmål:</H1>'
		for(x=0;x<arrPas.length;x++){
			iQuizNo=arrPas[x];
			sResults += '<p>' + arrQuiz[iQuizNo].q + ' <b><a href="../ns/link.asp?e=' + arrQuiz[iQuizNo].refId + '&c=s" target=vhdnSearch>' + arrQuiz[iQuizNo].a1 + "</a></b></p>"
		}
	}
	if(arrWrong.length>0){
		sResults+="<H1>Du svarede forkert på følgende spørgsmål:</H1>"
		for(x=0;x<arrWrong.length;x++){
			iQuizNo=arrWrong[x];
			sResults += '<p>' + arrQuiz[iQuizNo].q + ' <b><a href="../ns/link.asp?e=' + arrQuiz[iQuizNo].refId + '&c=s" target=vhdnSearch>' + arrQuiz[iQuizNo].a1 + "</a></b></p>"
		}
	}
	if(arrRight.length>0){
		sResults+="<H1>Du svarede rigtigt på følgende spørgsmål:</H1>"
		for(x=0;x<arrRight.length;x++){
			iQuizNo=arrRight[x];
			sResults += '<p>' + arrQuiz[iQuizNo].q + ' <b><a href="../ns/link.asp?e=' + arrQuiz[iQuizNo].refId + '&c=s" target=vhdnSearch>' + arrQuiz[iQuizNo].a1 + "</a></b></p>"
		}
	}
	return sResults;
}
function fnLQHiscore(){
	if(document.getElementById("sName").value!=""){
		document.getElementById("score").style.display='none';
		winHdn.location.replace("../spil/lqhi.asp?sName=" + document.getElementById("sName").value + "&iPoint=" + iResult + "&iNiveau=" + document.getElementById("quizvalg").qLevel.value + "&iCount=" + document.getElementById("quizvalg").qCount.value + "&iCat=" + document.getElementById("quizvalg").qCat.value);
		iResult=0;
		document.getElementById("teaser").innerHTML="&quot;Vil du prøve igen?&quot;";
	}
	else{
		alert("Du skal indtaste dit navn, hvis du vil på listen!")
	}
}
function showLqHiscore(){
	with (document.getElementById("quizvalg")){
		document.getElementById("qCount").value=document.getElementById("antal").value;
		document.getElementById("qLevel").value=document.getElementById("niveau").value;
		document.getElementById("qCat").value=document.getElementById("kategori").value;
	}
	winHdn.location.replace("../spil/lqhi.asp?sName=0&iNiveau=" + document.getElementById("quizvalg").qLevel.value + "&iCount=" + document.getElementById("quizvalg").qCount.value + "&iCat=" + document.getElementById("quizvalg").qCat.value);
}
function fnOpsaml(){
	iResult=pointTimer;
	if(flgPas||(iQuizTry==0)){
		iResult=0-pointTimer;
	}
	else{	
		if(iQuizTry==1 && flgCorrect){
			iResult=10+(15-pointTimer);
		}
		if(iQuizTry==2 && flgCorrect){
			iResult=2+(15-pointTimer);
		}
		if(!flgCorrect){
			iResult=0-(15-pointTimer)-10;
		}
	}
	arrResult.point[iQuizNo-1]=iResult;
	arrResult.tid[iQuizNo-1]=pointTimer;
	arrResult.forsog[iQuizNo-1]=iQuizTry;
	arrResult.rigtig[iQuizNo-1]=flgCorrect;
	arrResult.pas[iQuizNo-1]=flgPas;
	if(iQuizNo!=1){
		iResult+=parseInt(document.getElementById("point").innerHTML.replace(/Point:&nbsp;/,""),10);
	}
	document.getElementById("point").innerHTML=(udfordring!='0')?"":"Point:&nbsp;" + iResult;
}
function fnAddMailto(){
	document.getElementById("mailto").options[document.getElementById("mailto").options.length]=new Option(document.getElementById("cName").value)
	document.getElementById("cName").value='';
}
function fnRemMailto(){
	document.getElementById("cName").value=document.getElementById("mailto").options[document.getElementById("mailto").selectedIndex].text
	document.getElementById("mailto").options[document.getElementById("mailto").selectedIndex]=null
}
function fnNewUdfordring(){
	var loc="";
	obj=document.getElementById("mailto")
	for(x=0;x<obj.options.length;x++){
		loc+=escape(obj.options[x].text)+",";
	}
	loc="../spil/udfordr2.asp?qCount=" + document.getElementById("quizvalg").qCount.value + "&qCat=" + document.getElementById("quizvalg").qCat.value + "&qLevel=" + document.getElementById("quizvalg").qLevel.value + "&sName="+escape(document.getElementById("sName").value)+"&email="+escape(document.getElementById("email").value)+"&haan="+escape(document.getElementById("haaner").value)+"&arr="+loc;
	winHdn.location.replace(loc);
}
var fldSort="";
function fnReplay(iPlayers){
	//arrSvar[1]={name:Jørgen, haan:BEDST!!, s0:1, t0:7, s1:1, t1:4, s2:3, t2:5, s3:0, t3:3, s4:0, t4:8, s5:3, t5:7, s6:3, t6:4, s7:1, t7:8, s8:2, t8:4, s9:1, t9:5};
	winHdn.location.replace("../spil/lqgame.htm");
	loc="../spil/players.asp?players="+iPlayers;
	winListe.location.replace("loc");	
	document.getElementById("teaser").innerHTML="&quot;Hvordan synes du selv det er gået?&quot;";
	iQuizNo=0;
}
var arrTmpSvar=new Array();
var iPlayer=0;
function fnNewSpmReplay(){
	arrTmpSvar=new Array();
	for(x=1;x<arrSvar.length;x++){
		eval("arrTmpSvar["+x+"]={player:"+x+", t:"+eval("arrSvar[x].t"+iQuizNo)+"}");
		with(vListe){
			eval("p"+(x-1)+"Img.src='../images/spil/players/grubler_"+x+".jpg'");
			eval("p"+(x-1)+"Svar.innerHTML=''");
			eval("p"+(x-1)+"Tid.innerHTML=''");
		}
	}
	imgs0.src="../images/spil/neutral.gif";		
	imgs1.src="../images/spil/neutral.gif";		
	imgs2.src="../images/spil/neutral.gif";		
	imgs3.src="../images/spil/neutral.gif";		
	fnRndQuiz();
	teaser.innerHTML=arrQuiz[iQuizNo].q;
	spm.innerHTML=arrQuiz[iQuizNo].q;
	s1.innerHTML=arrInc[0];
	s2.innerHTML=arrInc[1];
	s3.innerHTML=arrInc[2];
	s4.innerHTML=arrInc[3];
	arrTmpSvar.sort(fnSortSvar);
	iPlayer=0;
	winWait=window.setTimeout("fnShowSorted()",3000);
}
function fnShowSorted(){
	y=arrTmpSvar[iPlayer].player;
	pAnswer=String.fromCharCode(65+eval("arrSvar["+y+"].s"+iQuizNo));
	pAnswer=(pAnswer=='E')?'PAS':pAnswer;
	teaser.innerHTML=arrSvar[y].name+" svarede "+pAnswer+" på tiden "+arrTmpSvar[iPlayer].t+" sekunder";
	y--;
	with(vListe){
		eval("p"+y+"Img.src='../images/spil/players/svar_"+y+".jpg'");
		eval("p"+y+"Svar.innerHTML='Svar: "+pAnswer+"'");
		eval("p"+y+"Tid.innerHTML='Tid: "+arrTmpSvar[iPlayer].t+"'");
	}
	
	iPlayer++
	if(iPlayer<arrSvar.length-1){
		winWait=window.setTimeout("fnShowSorted()",1000);
	}
	else{	
		winWait=window.setTimeout("fnSvarReplay()",3000);
	}
}
function fnSvarReplay(){
	iSandt=eval("arrSvar[0].s"+iQuizNo)
	oImg=eval("imgs"+iSandt);
	oImg.src="../images/spil/Rigtigt.gif";
	teaser.innerHTML="Svaret er "+String.fromCharCode(65+iSandt)+": "+arrInc[iSandt];
	for(x=1;x<arrSvar.length;x++){
		if(eval("arrSvar["+x+"].s"+iQuizNo)==iSandt){
			eval("vListe.p"+(x-1)+"Img.src='../images/spil/players/rigtig_"+(x-1)+".jpg'");
		}
		else{
			eval("vListe.p"+(x-1)+"Img.src='../images/spil/players/forkert_"+(x-1)+".jpg'");
		}
	}
	iQuizNo++;
	winWait=window.setTimeout("fnNewSpmReplay()",3000);
}
function fnSortSvar(a, b){
	a = a.t;
	b = b.t;
	return ((a < b) ? -1 : ((a > b) ? 1 : 0));
}
		
function fnDummy2(){}
var iHiScore=0;
