function rnd() {
        rnd.seed = (rnd.seed*9301+49297) % 233280;
        return rnd.seed/(233280.0);
};


function rand(number) {
        return Math.ceil(rnd()*number);
};


function rationalise(dividor,a,b,frac)
{
	c=Math.floor(a/dividor)
	d=a/dividor
	if (c==d)
	{
		e=Math.floor(b/dividor)
		f=b/dividor
		if (e==f)
		{
			if (frac=="first")
			{
				thisFirstTop=c
				thisFirstBottom=e;
			}
			else if (frac=="second")
			{
				thisSecondTop=c
				thisSecondBottom=e;
			}
			else
			{
				thisAnswerTop=c
				thisAnswerBottom=e;
			}
		}
	}
}

function addScore(increment,messageFlag,targetBreuk)
{
	if (increment=='right')
	{
		killklad()
		if (doneFlag==0)
		{
			gotRight++
			whichelement='signal' +gotRight ;
			document.getElementById(whichelement).style.backgroundColor="#33FF33"
			document.getElementById('gotright').value=gotRight;
		}
		if (messageFlag=='a')
		{
			alert("Goed!  " +thisAnswerWhole +" is het juiste antwoord");
		}
		else if (messageFlag=='b')
		{
			alert("Goed!  " +thisAnswerWhole +" " +thisAnswerTop +"/" +thisAnswerBottom +" is het juiste antwoord");
		}

		else if (messageFlag=='c')
		{
			alert("Goed!  " +document.getElementById('topAns').value +"/" +document.getElementById('bottomAns').value +" is het juiste antwoord")
		}
		if (bquestion<=9)
		{
				doneFlag=0
    		if (targetBreuk=='optellen')
    		{
    		doBreuk('optellen')
    		}
    		else if (targetBreuk=='aftrekken')
    		{
    		doBreuk('aftrekken')
    		}
    		else if (targetBreuk=='vermenigvuldigen')
    		{
    		doBreuk('vermenigvuldigen')
    		}
    		else if (targetBreuk=='delen')
    		{
    		doBreuk('delen')
    		}
		}
		else
		{
			 doScore()
		}
	}
	else if (increment=='wrong')
	{
		if (doneFlag==0)
		{
			gotWrong++
			whichelement='signal' +(21-gotWrong) ;
			document.getElementById(whichelement).style.backgroundColor="#FF0000"
			document.getElementById('gotwrong').value=gotWrong;
			doneFlag=1
		}
		eFlag=0
		k=2
		while (k<=bTop)
		{
			if (document.getElementById('topAns').value==(thisAnswerTop*k) && document.getElementById('bottomAns').value==(thisAnswerBottom*k)) 
			{
				eFlag=1;
				k=bTop;
			}
			k++
		}	
		if (eFlag==1)
		{
			beginner="Bijna goed!"
			ender=" - denk aan vereenvoudigen"
		}
		else
		{
			beginner="Helaas heb je het fout!"
			ender=""
		}
		if (messageFlag=='a')
		{
			alert(beginner +"  " +document.getElementById('wholeAns').value +" "+" is niet juist" + ender)
		}
		else if (messageFlag=='b')
		{
			alert(beginner +"  " +document.getElementById('wholeAns').value +" " +document.getElementById('topAns').value +"/" +document.getElementById('bottomAns').value +" is niet juist" + ender)
		}
		else if (messageFlag=='c')
		{
			alert(beginner +"  " +document.getElementById('wholeAns').value +" " +document.getElementById('topAns').value +"/" +document.getElementById('bottomAns').value +" is niet juist" + ender)
		}
		else if (messageFlag=='d')
		{
			alert(beginner +"  " +document.getElementById('topAns').value +"/" +document.getElementById('bottomAns').value +" is niet juist" + ender)
		}
	}



}

function checkAnswer(targetBreuk)
{
		var aCorrect=0
		if (thisAnswerWhole>=1)
		{
				if (document.getElementById('wholeAns').value==thisAnswerWhole && document.getElementById('topAns').value==thisAnswerTop && document.getElementById('bottomAns').value==thisAnswerBottom)
				{
						aCorrect=1;
				}
				else if(document.getElementById('wholeAns').value==thisAnswerWhole && thisAnswerTop==0 && document.getElementById('topAns').value=="" && document.getElementById('bottomAns').value=="")
				{
						aCorrect=1;
				}
						else
				{
						aCorrect=0;
				}
				if (aCorrect==1)
						{
						if (thisAnswerTop==0)
								{
									addScore('right','a',targetBreuk)
								}
						else
								{
									addScore('right','b',targetBreuk)
								}
						}
				else
				{
						if (document.getElementById('topAns').value=="")
						{
							addScore('wrong','a',targetBreuk)
						}
						else 
						{
							addScore('wrong','b',targetBreuk)
						}
				}
		}
		else
		{
				if ((document.getElementById('wholeAns').value=="" || document.getElementById('wholeAns').value=="0") && document.getElementById('topAns').value==thisAnswerTop && document.getElementById('bottomAns').value==thisAnswerBottom)
				{
						aCorrect=1;
				}
				else
				{
						aCorrect=0;
				}
				if (aCorrect==1)
				{
					addScore('right','c',targetBreuk)
				}
				else
				{
						if (document.getElementById('wholeAns').value!="")
						{
						addScore('wrong','c',targetBreuk)
						}
				else
						{
							addScore('wrong','d',targetBreuk)
						}
				}
		}
}

function getBreukOptellen(maxVal){
  thisFirstTop=rand(maxVal-1);
  while (thisFirstTop>=thisFirstBottom){
  thisFirstBottom=rand(maxVal);
  }
  thisSecondTop=rand(maxVal-1);
  while (thisSecondTop>=thisSecondBottom){
  thisSecondBottom=rand(maxVal);
  }
  i = 6
  while (i >= 2)
  {
  rationalise(i,thisFirstTop,thisFirstBottom,'first')
  i=i-1
  }
  
  i = 6
  while (i >= 2)
  {
  rationalise(i,thisSecondTop,thisSecondBottom,'second')
  i=i-1
  }
  document.getElementById('firstTop').innerHTML=thisFirstTop;
  document.getElementById('firstBottom').innerHTML=thisFirstBottom;
  document.getElementById('secondTop').innerHTML=thisSecondTop;
  document.getElementById('secondBottom').innerHTML=thisSecondBottom;
  document.getElementById('wholeAns').value="";
  document.getElementById('topAns').value="";
  document.getElementById('bottomAns').value="";
}

function getBreukAftrekken(maxVal){
  thisFirstTop=0;
  while (thisFirstTop<=3){
  thisFirstTop=rand(maxVal-1);
  }
  thisFirstBottom=0;
  while (thisFirstTop>=thisFirstBottom || thisFirstBottom<=1 ){
  thisFirstBottom=rand(maxVal);
  }
  pc1=thisFirstTop/thisFirstBottom
  thisSecondTop=1
  thisSecondBottom=1
  while((thisSecondTop/thisSecondBottom)>=pc1 || thisSecondTop==0 ){
  thisSecondTop=rand(thisFirstTop-1);
  while (thisSecondTop>=thisSecondBottom){
  thisSecondBottom=rand(maxVal);
  }
  }
  i = 6
  while (i >= 2)
  {
  rationalise(i,thisFirstTop,thisFirstBottom,'first')
  i=i-1
  }
  
  i = 6
  while (i >= 2)
  {
  rationalise(i,thisSecondTop,thisSecondBottom,'second')
  i=i-1
  }
  document.getElementById('firstTop').innerHTML=thisFirstTop;
  document.getElementById('firstBottom').innerHTML=thisFirstBottom;
  document.getElementById('secondTop').innerHTML=thisSecondTop;
  document.getElementById('secondBottom').innerHTML=thisSecondBottom;
  document.getElementById('wholeAns').value="";
  document.getElementById('topAns').value="";
  document.getElementById('bottomAns').value="";
}

function getBreukVermenigvuldigen(maxVal){
    thisFirstTop=rand(maxVal-1);
    while (thisFirstTop>=thisFirstBottom){
    thisFirstBottom=rand(maxVal);
    }
    thisSecondTop=rand(maxVal-1);
    while (thisSecondTop>=thisSecondBottom){
    thisSecondBottom=rand(maxVal);
    }
    i = 6
    while (i >= 2)
    {
    rationalise(i,thisFirstTop,thisFirstBottom,'first')
    i=i-1
    }
    
    i = 6
    while (i >= 2)
    {
    rationalise(i,thisSecondTop,thisSecondBottom,'second')
    i=i-1
    }
    document.getElementById('firstTop').innerHTML=thisFirstTop;
    document.getElementById('firstBottom').innerHTML=thisFirstBottom;
    document.getElementById('secondTop').innerHTML=thisSecondTop;
    document.getElementById('secondBottom').innerHTML=thisSecondBottom;
    document.getElementById('wholeAns').value="";
    document.getElementById('topAns').value="";
    document.getElementById('bottomAns').value="";
  }
  
  
  function getBreukDelen(maxVal){
  thisFirstTop=0;
  while (thisFirstTop<=3){
  thisFirstTop=rand(maxVal-1);
  }
  while (thisFirstTop>=thisFirstBottom){
  thisFirstBottom=rand(maxVal);
  }
  pc1=thisFirstTop/thisFirstBottom
  thisSecondTop=1
  thisSecondBottom=1
  while((thisSecondTop/thisSecondBottom)>=pc1){
  thisSecondTop=rand(thisFirstTop-1);
  while (thisSecondTop>=thisSecondBottom){
  thisSecondBottom=rand(maxVal);
  }
  }
  i = 6
  while (i >= 2)
  {
  rationalise(i,thisFirstTop,thisFirstBottom,'first')
  i=i-1
  }
  
  i = 6
  while (i >= 2)
  {
  rationalise(i,thisSecondTop,thisSecondBottom,'second')
  i=i-1
  }
  document.getElementById('firstTop').innerHTML=thisFirstTop;
  document.getElementById('firstBottom').innerHTML=thisFirstBottom;
  document.getElementById('secondTop').innerHTML=thisSecondTop;
  document.getElementById('secondBottom').innerHTML=thisSecondBottom;
  document.getElementById('wholeAns').value="";
  document.getElementById('topAns').value="";
  document.getElementById('bottomAns').value="";
}


function doBreuk(targetBreuk)
{	
		if (bquestion<3)
		{
				bTop=6
		}
		else if (bquestion<5)
		{
				bTop=7
		}	
		else if (bquestion<6)
		{
				bTop=8
		}	
		else if (bquestion<9)
		{
				bTop=9
		}			
		else
		{
				bTop=10
		}
		if (targetBreuk=='optellen')
		{
		getBreukOptellen(bTop)
		}
		else if (targetBreuk=='aftrekken')
		{
		getBreukAftrekken(bTop)
		}
		else if (targetBreuk=='vermenigvuldigen')
		{
		getBreukVermenigvuldigen(bTop)
		}
		else if (targetBreuk=='delen')
		{
		getBreukDelen(bTop)
		}
		bquestion++
}

function checkSumOptellen()
{
	a1=Math.floor(document.getElementById('wholeAns').value);
	a2=Math.floor(document.getElementById('topAns').value);
	a3=Math.floor(document.getElementById('bottomAns').value);
	if ((isNaN(a1) || isNaN(a2) || isNaN(a3)) || (document.getElementById('wholeAns').value.length>=2 || document.getElementById('topAns').value.length>=3 || document.getElementById('bottomAns').value.length>=3))
	{
		alert("Doe 'ns normaal!")
	}
	else
	{

	thisAnswerTop=(thisFirstTop*thisSecondBottom)+(thisSecondTop*thisFirstBottom);
	thisAnswerBottom=thisFirstBottom*thisSecondBottom;
	thisAnswerRawTop=thisAnswerTop;
	thisAnswerRawBottom=thisAnswerBottom;
	if(Math.floor(thisAnswerTop/thisAnswerBottom)>=1)
		{
		thisAnswerWhole=Math.floor(thisAnswerTop/thisAnswerBottom);
		thisAnswerTop=thisAnswerTop-(thisAnswerWhole*thisAnswerBottom);
		thisAnswerRawTop=thisAnswerTop;
		}
	else
	{
	thisAnswerWhole=0
	}	
	i = 45
	while (i >= 2)
		{
		rationalise(i,thisAnswerTop,thisAnswerBottom,'')
		i=i-1
		}
		checkAnswer('optellen');
	}
}

function checkSumAftrekken()
{
	a1=Math.floor(document.getElementById('wholeAns').value);
	a2=Math.floor(document.getElementById('topAns').value);
	a3=Math.floor(document.getElementById('bottomAns').value);
	if ((isNaN(a1) || isNaN(a2) || isNaN(a3)) || (document.getElementById('wholeAns').value.length>=2 || document.getElementById('topAns').value.length>=3 || document.getElementById('bottomAns').value.length>=3))
	{
		alert("Doe 'ns normaal!")
	}
	else
	{
	thisAnswerTop=(thisFirstTop*thisSecondBottom)-(thisSecondTop*thisFirstBottom)
	thisAnswerBottom=thisFirstBottom*thisSecondBottom
	if(Math.floor(thisAnswerTop/thisAnswerBottom)>=1)
		{
		thisAnswerWhole=Math.floor(thisAnswerTop/thisAnswerBottom);
		thisAnswerTop=thisAnswerTop-(thisAnswerWhole*thisAnswerBottom);
		}
	else
	{
	thisAnswerWhole=0
	}	
	i = 45
	while (i >= 2)
		{
		rationalise(i,thisAnswerTop,thisAnswerBottom,'')
		i=i-1
		}
		checkAnswer('aftrekken');
	}
}

function checkSumVermenigvuldigen()
{
	a1=Math.floor(document.getElementById('wholeAns').value);
	a2=Math.floor(document.getElementById('topAns').value);
	a3=Math.floor(document.getElementById('bottomAns').value);
	if ((isNaN(a1) || isNaN(a2) || isNaN(a3)) || (document.getElementById('wholeAns').value.length>=2 || document.getElementById('topAns').value.length>=3 || document.getElementById('bottomAns').value.length>=3))
	{
		alert("Doe 'ns normaal!")
	}
	else
	{
	thisAnswerTop=(thisFirstTop*thisSecondTop)
	thisAnswerBottom=thisFirstBottom*thisSecondBottom
	if(Math.floor(thisAnswerTop/thisAnswerBottom)>=1)
		{
		thisAnswerWhole=Math.floor(thisAnswerTop/thisAnswerBottom);
		thisAnswerTop=thisAnswerTop-(thisAnswerWhole*thisAnswerBottom);
		}
	else
	{
	thisAnswerWhole=0
	}	
	i = 45
	while (i >= 2)
		{
		rationalise(i,thisAnswerTop,thisAnswerBottom,'')
		i=i-1
		}
		checkAnswer('vermenigvuldigen');
	}
}

function checkSumDelen()
{	
	a1=Math.floor(document.getElementById('wholeAns').value);
	a2=Math.floor(document.getElementById('topAns').value);
	a3=Math.floor(document.getElementById('bottomAns').value);
	if ((isNaN(a1) || isNaN(a2) || isNaN(a3)) || (document.getElementById('wholeAns').value.length>=2 || document.getElementById('topAns').value.length>=3 || document.getElementById('bottomAns').value.length>=3))
	{
		alert("Doe 'ns normaal!")
	}
	else
	{
	thisAnswerTop=(thisFirstTop*thisSecondBottom)
	thisAnswerBottom=(thisSecondTop*thisFirstBottom)
	if(Math.floor(thisAnswerTop/thisAnswerBottom)>=1)
		{
		thisAnswerWhole=Math.floor(thisAnswerTop/thisAnswerBottom);
		thisAnswerTop=thisAnswerTop-(thisAnswerWhole*thisAnswerBottom);
		}
	else
	{
	thisAnswerWhole=0
	}	
	i = 45
	while (i >= 2)
		{
		rationalise(i,thisAnswerTop,thisAnswerBottom,'')
		i=i-1
		}
		checkAnswer('delen');
	}
}

function doScore()
{
	percRes=(gotRight/10)*100;
	alert("Oefening voltooid. Uw score = " +percRes +"%");
	s=location.href
	if (s.indexOf("optellen")>-1)
	{
		if (confirm("Wilt u doorgaan naar de volgende oefening"))
		{
			location.href="aftrekken.asp";
		}
	}
	else if (s.indexOf("aftrekken")>-1)
	{
		if (confirm("Wilt u doorgaan naar de volgende oefening"))
		{
			location.href="vermenigvuldigen.asp";
		}
	}
	else if (s.indexOf("vermenigvuldigen")>-1)
	{
		if (confirm("Wilt u doorgaan naar de volgende oefening"))
		{
			location.href="delen.asp";
		}
	}
	else
	{
		alert("Gefeliciteerd - U heeft alle oefeningen voltooid")
	}
} 

function ad2klad(aChar)
{
document.all['klad'].innerHTML=document.all['klad'].innerHTML +'&nbsp;<br><table border="0" cellpadding="2" cellspacing="0" class="opgave" style="width:154px;">' +
'<tr><td rowspan="3">=</td>' +
'<td><input type="text" style="width:25px"></td>' +
'<td rowspan="3">' +aChar +'</td>' +
'<td><input type="text" style="width:25px"></td>' +
'<td rowspan="3">=</td>' +
'<td rowspan="3"><input type="text" style="width:15px"></td>' +
'<td><input type="text" style="width:25px"></td>' +
'</tr>' +
'<tr><td><img src="generic/deelstreep.gif" width=25 height=1></td>' +
'<td><img src="generic/deelstreep.gif" width=25 height=1></td>' +
'<td><img src="generic/deelstreep.gif" width=25 height=1></td></tr>' +
'<tr><td><input type="text" style="width:25px"></td>' +
'<td><input type="text" style="width:25px"</td>' +
'<td><input type="text" style="width:25px"</td></tr>' +
'</table>'
}

function killklad(){
document.all['klad'].innerHTML=''
}

function showHideLayers(w)
{ 
	if(document.getElementById(w).style.visibility=='hidden'){
		document.getElementById(w).style.visibility='visible';
	} else {
		document.getElementById(w).style.visibility='hidden';
	}
}

rnd.today=new Date();
rnd.seed=rnd.today.getTime();
riser=0
bTop=0
thisFirstTop=0;
thisFirstBottom=0;
thisSecondTop=0;
thisSecondBottom=0;
thisAnswerRawTop=0;
thisAnswerRawBottom=0;
thisAnswerWhole=0;
thisAnswerTop=0;
thisAnswerBottom=0;
bquestion=0;
gotRight=0;
gotWrong=0;
doneFlag=0;

