/*-------------------------------------------------------------------------
This code is copyrighted to BIS : Begeleid Individueel Studeren,
a department of Ministerie Vlaamse Gemeenschap, 2002-2003.
Developping was done in co-operation with Education Services:
tel. +32(0)3/227 02 06 / fax. +32(0)3/227 32 65 info@educationservices.be
-------------------------------------------------------------------------*/


// hw_tekstmc.js, versie 1.3, 12 november 2002
// aanpassing 1.3 : feedback initially hidden en met layout
// ma: multiple answers: based on tmc
// 2003-03-27: implementing layered feedback
// 2003-04-18: Debug: on line 361 the following statement 
//             "!String(array1[i])" gave an "Undefined" string when the 
//             element of the array didn't exist. Changed it back to 
//             "!array1[i]".
// 2003-05-14: Adapted: more than 10 options/MA available now. 
// 	       Attention!!: antw[i] = new Array("00","01") 
//	       have to be zerofill(2).


//------------------- Declaring variables ---------------------------------
var isSelected = new Array();
//var juist = 0;
var verbeterd = 1;	// Indicates whether the Ma has been corrected or
			// not. If not everything is ok. If it is, trash in
			// arrays etc is removed
var currFB = 0;		// Indicates the level in the Layered Feedback
var FBLocked = true;	// Prevents the user to click 5 times on the 
			// SelfCorr-button and so skipping through the LFB
//-------------------------------------------------------------------------

//------------------- INDEX -----------------------------------------------
//	function vulveld()
//	function fb_close()
//	function isUndefined()
//	function verbeterMA()
//	function selfCorrect()
//	function fillGaps()
//	function resetGaps() 
//	function getGaps()
//	function colourGaps()
//	function getIndex()
//	function myreset()
//	function fb_mover()
//	function toonFeedback()
//	function areArraysEqual() 


//-------------------------------------------------------------------------
function vulveld(nr,deel)
{
        if (verbeterd == 1)
        {
         for (var i = 0; i < document.forma.length; i++)
		{
	        var gapID = document.forma.elements[i];
	        if (gapID.type == "text" && 
	            gapID.style.background != "#ffff00" && 
	            gapID.style.background != "#99ff99")
			{
			gapID.value = "";
			gapID.style.background = '#FDFEE2';
			gapID.style.display = 'none';
			document.all.feedback.innerHTML = "";
			}
		}
	}
        verbeterd = 0;
	var gapName = "gap"+nr+ZeroFill(deel,2);
	//alert(gapName);
        var gapID = document.getElementById(gapName);
        var laagID = document.getElementById(gapName).parentNode;
        gapID.value = opgave[nr][deel];
        gapID.style.display = 'block';
        isSelected = new Array();
	isSelected.length = 0;
}
//-------------------------------------------------------------------------


//------------------- Zerofill function -----------------------------------
function ZeroFill(iNum, iDigits) 
{
var sNum = CvtNtoS(iNum); 
while (sNum.length < iDigits) sNum = "0" + sNum; 
return sNum;
} 
//-------------------------------------------------------------------------


//-------------------------------------------------------------------------
function CvtNtoS(iNum) 
{
return ("" + iNum);
} 
//-------------------------------------------------------------------------


//-------------------------------------------------------------------------
function fb_close()
{
document.all.feedbacklayer.style.display = 'none';
}
//-------------------------------------------------------------------------


//------------------- undefined function catcher --------------------------
function isUndefined(property) {
  return (typeof property == 'undefined');
}
//-------------------------------------------------------------------------


//------------------- Array.push() ----------------------------------------
//		Add an element to the end of an array
//		Used by old browsers.
if (isUndefined(Array.prototype.push) == true) {
  Array.prototype.push = function() {
  	var currentLength = this.length;
  	for (var i = 0; i < arguments.length; i++) {
  		this[currentLength + i] = arguments[i];
  	}
  	return this.length;
  };
}
//-------------------------------------------------------------------------


//-------------------------------------------------------------------------
function verbeterMA()
{
document.all.feedbacklayer.style.visibility = 'hidden';
if (verbeterd == 0) 
	{
	verbeterd = 1;
// evaluate the answer of the student
	for (var i = 0; i < document.forma.length; i++)
		{
		var gapID = document.forma.elements[i];
		if (gapID.type == "text" && gapID.name != "gapeval")
			{
			var antwoord = gapID.value;
			var gapName = gapID.name;
			var gapFIndex = gapName.charAt(gapName.length-3);			
			var gapSIndex = gapName.substr(gapName.length-2)//charAt(gapName.length-1);
			if (!isSelected[gapFIndex]) 
				{
				isSelected[gapFIndex] = new Array();
				}			
			if (antwoord != "")
				{
				isSelected[gapFIndex].push(gapSIndex);
				}
			}
		}
	for (var j = 0; j < isSelected.length; j++)
		{
		if (isSelected[j] == "") 
			{
			colourGaps(j,'#cccccc');
			}
		else if (areArraysEqual(antw[j],isSelected[j])) 
			{
			// juist
			colourGaps(j,'#99FF99');
			}
		else if (!areArraysEqual(antw[j],isSelected[j]))
			{
			// fout
			//alert("fout");
			colourGaps(j,'#FF9999');
			}
		}	
	}
	FBLocked = false;
}
//-------------------------------------------------------------------------


//-------------------------------------------------------------------------
function selfCorrect() {
document.all.feedbacklayer.style.display = 'none';	// hide the feedback
for (var i = 0; i < document.forma.length; i++)
	{
	var gapID = document.forma.elements[i];
	if (gapID.type == "text")
		{
		if (gapID.style.background == "#ff9999" 
		    || gapID.style.background == "#cccccc" )
			{
			var gapName = gapID.name;
			var gapFIndex = gapName.charAt(gapName.length-3);
			if (currFB >= 
			    fbfout[gapFIndex].length-1)
				{
				resetGaps(gapID,gapFIndex);
				colourGaps(gapFIndex,'#FFFF00')
				fillGaps(gapFIndex);
				} else {
				resetGaps(gapID,gapFIndex);
				}
		 	}
		}
	}
if (!FBLocked)
	{  // Disable clicking 2 times on the SelfCorr button 
	currFB = currFB +1;
	FBLocked = true;
	}
}
//-------------------------------------------------------------------------


//------------------- Fills the gap with the correct answer(s) ------------
function fillGaps(index){
for (var i=0; i < antw[index].length; i++)
	{
	if (antw[index][i] != "") 
		{
		var gapName = "gap"+index+antw[index][i];
        	var gapID = document.getElementById(gapName);
        	gapID.value = opgave[index][eval(antw[index][i])];
        	}
	}
}
//------------------------------------------------------------------------- 


//------------------- Reset a gap to it's initial state -------------------
function resetGaps(gapID,i) {
var theGaps = getGaps(i);
for (var i =0; i<theGaps.length; i++)
	{
	var thisGapID = document.getElementById(theGaps[i]);
	thisGapID.style.background = '#FDFEE2';
	thisGapID.style.display = 'none';
	thisGapID.value = "";
	}
}
//-------------------------------------------------------------------------


//------------------- Get the gaps belonging to this MA -------------------
//		Is called with an index (integer) which stands
//		for the current MA exercise. 
//		Returns the names of the gaps belonging to this MA, 
//		in an array of strings.
function getGaps(index) {
var theGaps = new Array ();
for (var i = 0; i < document.forma.length; i++)
	{
	var gapID = document.forma.elements[i];
	var gapName = gapID.name;
	var gapFIndex = gapName.charAt(gapName.length-3);	
	if (gapID.type == "text")
		{
		if (gapFIndex == index) 
			{
			theGaps.push(gapName);
			}
		}
	}
	return theGaps;
}
//-------------------------------------------------------------------------


//------------------- colourGaps ------------------------------------------
function colourGaps(index,colour) {
for (var i = 0; i < document.forma.length; i++) 
	{
	var gapID = document.forma.elements[i];
	if (gapID.type == "text" && gapID.style.background != "#ffff00")
		{
		var gapName = gapID.name;
		var gapFIndex = gapName.charAt(gapName.length-3);
		if (gapFIndex == index) 
			{
			// weerhoud de gaps met het huidige geevalueerde antwoord
			gapID.style.background = colour;
			gapID.style.display = 'block';
			}
		}
	}
}
//-------------------------------------------------------------------------


//-------------------------------------------------------------------------
function getIndex(gapID) {
    for (var i=0;i < document.forma.elements.length;i++)
        if (document.forma.elements[i].name == gapID)
            return i;
    return -1;
}
//-------------------------------------------------------------------------


//-------------------------------------------------------------------------
function myreset()
{
verbeterd = 1;
currFB = 0;
document.all.feedbacklayer.style.display = 'none';
isSelected = new Array();
isSelected.length = 0;
for (var i = 0; i < document.forma.length; i++)
         {
         var gapID = document.forma.elements[i];
         gapID.value = "";
         if (gapID.type == "text")
                 {
                 gapID.style.background = '#FDFEE2';
                 gapID.style.display = 'none';
                 document.all.feedback.innerHTML = "";
                 }
        }
}
//-------------------------------------------------------------------------


//-------------------------------------------------------------------------
function fb_mover()
{
var cur_ypos = window.event.y;
var new_ypos = cur_ypos + 10;
var cur_xpos = window.event.x;
var temp_xpos = cur_xpos - 150;
	if(temp_xpos > 470){new_xpos = 470;}
	else if(temp_xpos < 20){new_xpos = 20;}
	else new_xpos = temp_xpos;
document.all.feedbacklayer.style.top = '50';
document.all.feedbacklayer.style.left = '0';
document.all.feedback.style.width = '300';
document.all.feedback.style.height = '50';
document.all.feedbacklayer.style.display = 'block';
}
//-------------------------------------------------------------------------


//-------------------------------------------------------------------------
function toonFeedback(deze)
        {
        if(verbeterd == 1)
        {
        document.all.feedbacklayer.style.visibility = 'visible';
        var gapName = deze.name;
        var gapFIndex = gapName.charAt(gapName.length-3);
        if (deze.style.background == '#99ff99' || 
            deze.style.background == '#ffff00')
                {
                // juist
                document.all.feedback.innerHTML = fbjuist[gapFIndex];
                fb_mover();
                }
        else if (deze.style.background == '#ff9999' || 
        	 deze.style.background == '#cccccc')
                {
                var tempFB = fbfout[gapFIndex][currFB];
		var i = 1;
		while (tempFB == "") {
			tempFB = fbfout[gapFIndex][currFB-i];
			i++;
		}		
		document.all.feedback.innerHTML = tempFB;
                fb_mover();
                }
        else
                {
                document.all.feedback.innerHTML = "";
                fb_mover();
                }
        }
        }
//-------------------------------------------------------------------------


//-------------------------------------------------------------------------
function areArraysEqual(array1, array2) { 	//antw[j],isSelected[j]
//alert(array1+ " | "+ array2);
for (var i = 0; i < document.forma.length; i++)
	{
	var gapID = document.forma.elements[i];
	array1.sort();
	array1.reverse();
	array2.sort();
	array2.reverse();
	if (gapID.type == "text")
		{
		if (!array2[i]) // de geselecteerde array is ten einde
		{
		if (!array1[i]) // de antwoorden array is ten einde
			{
			//juist = 1;
			//alert(array1[i]+"true");
			return true;
			} else {// de antwoorden array is niet ten einde
			//juist = 0;
			//alert(array1[i]);
			return false;
			}
		}
		if (String(array1[i],2) != ZeroFill(array2[i],2)) 
			{
			//alert(String(ZeroFill(array1[i],2)+String(ZeroFill(array2[i],2))));
			//juist = 0;
			//alert("false");
			return false;
			}
		}
	}
	//juist = 1;
	//alert("true");
	return true;
}
//-------------------------------------------------------------------------
//---------------------------------- eof ----------------------------------