/* ---------------- Intelectual property and copyright ----------------------
 
Lectora extranal exercise methods
All Rights Reserved. 
Dirk Pauwels, dirk.pauwels@xaurum.be, 2002 - 2007
this code may not be used in publications other than granted by the author.
Code usage granted to Ministerie Vlaamse Gemeenschap / BIS online

modifications by Education Services 2004-2006
-------------------------------------------------------------------------- */

//------------------- Declaring Variables----------------------------------
var lockedGaps = new Array("");
var contentcounter = 0;
var lijst = "";
var currFB = 0;
var FBLocked = true;
//-------------------------------------------------------------------------



//------------------- General Functions -----------------------------------
function getCookie(NameOfCookie)
{
  if (document.cookie.length > 0) 
	{ 
    begin = document.cookie.indexOf(NameOfCookie+"="); 
    if (begin != -1)
   		{ 
		begin += NameOfCookie.length+1; 
      	end = document.cookie.indexOf(";", begin);
      	if (end == -1) end = document.cookie.length;
      	return unescape(document.cookie.substring(begin, end));       } 
  	}
return null;  
}

function lockGap(gapID) {
var tempArray = new Array(gapID.name);
lockedGaps = lockedGaps.concat(tempArray);
}

function locked(gapID) {
if (ereg(lockedGaps, gapID.name)) {
return true;
} else {
return false;
}
}

function ereg(myArray,myVal) {
myArrayStr = ";"+myArray.join(";")+";";
myValStr= ";"+myVal+";";
if (myArrayStr.search(myValStr) != -1){
return true;
}else {
return false;
}
}
//-------------------------------------------------------------------------


//------------------- Zelf-correctie - 25-03-2003 -------------------------
//		Functie wordt aangeroepen vanuit Lectora door 
//		de correctieknop. Dit aan de hand van volgende
//		code: javascript:self_correct(); in een weblink.
//		Wat doet deze knop/functie? 
//		De cursist klikt op de verbeter knop om de juiste 
//		antwoorden te laten staan en enkel door te gaan 
//		met de foute oefeningen voor een 2e maal in te vullen.
//-------------------------------------------------------------------------
function self_correct() {	
	lockedGaps = new Array ("");
	document.forma.fblock.value = 1;
	document.forma.selectvar.value = "";
	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" )
			       	{
			       	//alert(currFB+ " || "+fbfout[getIndex(gapID.name)].length);
				if (currFB >= 
				    fbfout[getIndex(gapID.name)].length-1)
					{
					autoCorrect(gapID,i);
					lockGap(gapID);
					} else {
					resetGap(gapID,i);
					}
			 	} 
			 	else 
			 	{
			 	lockGap(gapID);
			 	} 
			}
		}
	if (!FBLocked){  // Disable clicking 2 times on the SelfCorr button 
	currFB = currFB +1;
	FBLocked = true;
	}		
}
//-------------------------------------------------------------------------


//------------------- Fills the gap with the correct answer(s) ------------
function autoCorrect(gapID,i){
	gapID.style.background = '#FFFF00';
	gapID.value = antw[i][0];
	var setcookie = "gap" + i +"=1";
	document.cookie = setcookie; 	
}
//------------------------------------------------------------------------- 


//------------------- Invoked by the Submit button ------------------------
function verbeterSD()
{
	document.forma.selectvar.value = "";
	for (var i = 0; i < document.forma.length; i++)
	{
	var gapID = document.forma.elements[i];
	if (gapID.type == "text")
		{
		lockGap(gapID);
		var antwoord = gapID.value;
		antwoord = escape(antwoord);
		//antwoord = antwoord.replace(/%A0/g,"");
		var juist = antw[i];
		juist = escape(juist);
		//juist = juist.replace(/%20/g,"");
		document.all.feedback.innerHTML = 
		"Klik op de verbeterde antwoorden voor toelichting.";
		if (antwoord != "" && gapID.style.background != "#ffff00")
			{
			document.forma.fblock.value = 0;
			//alert(antwoord + " || " + juist);
			if (antwoord == juist)
				{
				gapID.style.background = '#99FF99';gapID.style.borderBottomWidth = "2"; //'none';
				gapID.readOnly = 'false';
				var setcookie = "gap" + i +"=1";
				document.cookie = setcookie; 
				} else {
				gapID.style.background = '#FF9999';
				gapID.readOnly = 'false';
				var setcookie = "gap" + i +"=0";
				document.cookie = setcookie; 
				}
			} 
		if (antwoord == "")
			{
			gapID.style.background = '#CCCCCC';
			var setcookie = "gap" + i +"=0";
			document.cookie = setcookie;
			}
		} else {
		gapID.style.visibility = "hidden"; 
		}
	}
	FBLocked = false;	
} 
//-------------------------------------------------------------------------


//------------------- Invoked by Resetbutton ------------------------------
function fb_close()
{
document.all.feedbacklayer.style.visibility = 'hidden';
}
//-------------------------------------------------------------------------


//------------------- Invoked by Feedback Close ---------------------------

/*function fb_close()
{
document.all.feedbacklayer.style.display = 'none';
}*/
//-------------------------------------------------------------------------


//------------------- Invoked by SubmitButton -----------------------------
function fb_hide()
{
document.all.feedbacklayer.style.display = 'none';
}
//-------------------------------------------------------------------------


//------------------- Invoked by Reset button -----------------------------
function clearAttribAndVars() 
{
	lockedGaps = new Array ("");
	document.forma.fblock.value = 1;
	document.forma.selectvar.value = "";
	for (var i = 0; i < document.forma.length; i++) 
		{
		var gapID = document.forma.elements[i];
		if (gapID.type == "text")
			{
			resetGap(gapID,i);
			/*gapID.style.background = 'transparent';
			gapID.readOnly = 'false';
			gapID.value = "";
			document.all.feedback.innerHTML = "";
			var setcookie = "gap" + i +"=2";
			document.cookie = setcookie; */
			}
		}
	currFB = 0;			
}
//-------------------------------------------------------------------------


//------------------- Reset a gap to it's initial state -------------------
function resetGap(gapID,i) {
	gapID.readOnly = 'false';
	//gapID.style.background = '#FFFFFF';
	//gapID.style.display = 'none';
	gapID.style.borderBottomWidth = "2";
	gapID.style.background = 'transparent';
	//gapID.style.visibility = "visible"; 
	gapID.value = "";
	document.all.feedback.innerHTML = "";
	var setcookie = "gap" + i +"=2";
	document.cookie = setcookie;
	//tel = 0;
}
//-------------------------------------------------------------------------


//------------------- Invoked by click on a gap ---------------------------
//		Deze functie wordt aangeroepen bij een 
//		klik op de gap, tegelijkertijd met de 
//		"toonfeedback" functie
//-------------------------------------------------------------------------
function sdvulin(wat)
{
var keuze = document.forma.selectvar.value;
	//if (document.forma.fblock.value == 1) // wat is de huidige modus
	if (!locked(wat))
	{
	wat.value = unescape(keuze);
	wat.readOnly = 'true';
	}
}
//-------------------------------------------------------------------------


//------------------- Invoked by click on a gap ---------------------------
function toonFeedback(deze)
	{
	//if (document.forma.fblock.value == 0) // wat is de huidige modus
	if (locked(deze))
		{
		var gapID = deze.name;
		gapIndex = getIndex(gapID);
		fback(gapID, gapIndex);
		fb_mover();
		}
	}
//-------------------------------------------------------------------------


//-------------------------------------------------------------------------
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 fback(gapID, gapIndex)
	{
	var juist = getCookie(gapID)
	if (juist == 1)
		{
		document.all.feedback.innerHTML = fbjuist[gapIndex];
		}
	else if (juist == 0)
		{
		var tempFB = fbfout[gapIndex][currFB];
		var i = 1;
		while (tempFB == "") {
		tempFB = fbfout[gapIndex][currFB-i];
		i++;
		}			
		document.all.feedback.innerHTML = tempFB;
		}
	else
		{
		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';
}
//-------------------------------------------------------------------------


//------------------- Invoked by click on woordlijst-item -----------------
function setSelectVar(lijstitem)
{
	document.forma.selectvar.value = lijstitem;
	XAsetfocusToGaps();
}
//-------------------------------------------------------------------------


//------------------- At runtime scripts ----------------------------------
for (var i = 0; i < woordlijst.length; i++)
	{
	if (woordlijst[i] != "")
	contentcounter ++;
	}


for (var i = 0; i < woordlijst.length; i++)
{
if (woordlijst[i] != "")
	{
	lijst = lijst + "<a href="
	lijst = lijst + "javascript:setSelectVar(&quot;" 
	              + woordlijst[i] + "&quot;)>"
	lijst = lijst + unescape(woordlijst[i]);
	lijst = lijst + "</a>"
	if (i < (contentcounter - 1))
		{
		lijst = lijst + " / ";
		}
	lijst = "<b>" + lijst + "</b>"
	}
}
//-------------------------------------------------------------------------

function XAsetfocusToGaps()
{
	for (var i = 0; i < document.forma.length; i++)
	{
	var gapID = document.forma.elements[i];
	if (gapID.type == "text")
		{
		gapID.style.background = '#FFFFCC';
		// safari, mozilla
		gapID.style.opacity = '.7';
		//MSIE
		gapID.style.filter = 'alpha(opacity=70)';
		}
	}
}

//---------------------------------- eof ----------------------------------
