window.onload = initAllQuizzes;

var initHTML = ["","","",""]
var containerCount = [8,8,3,3];
var containerScore = [8,8,1,1];
var correctCount = [0,0,0,0];
var attemptCount = [0,0,0,0];
var question_scored = [false,false,false,false];
                 
function initAllQuizzes()
{
	for (var i = 0; i < containerCount.length; i++)
	{
	    initQuiz(i + 1);
	}
}

function initQuiz(containerId)
{
	// INIT SPECIFIC CONTAINER
	initHTML[(containerId - 1)] = xGetElementById("container" + containerId).innerHTML

    if( containerId == 1  || containerId == 2) {
	    for (var i = 0; i < containerCount[(containerId - 1)]; i++)
	    {
	    	
		    var elem;
    		
	        // Set up drag capability
	        elem = xGetElementById("c" + containerId + "-drag_" + ( i + 1));
	        xEnableDrag("c" + containerId + "-drag_" + (i + 1), fnDragStart, fnDrag, fnDragEnd);
	        elem.initX = xLeft(elem);
	        elem.initY = xTop(elem);	      
		   
	    }
	}		
	
} // end of function initQuiz

function fnDragStart(ele, mdx, mdy)
{
  var container =  ele.offsetParent;
	while (container.parentNode && (container.id.indexOf('container')==-1)) container = container.offsetParent;
	ele.offL = 0;
	ele.offR = 0;
	ele.offT = 0;
	ele.offB = 0;
	ele.bL = xPageX(container)-xPageX(ele);
	ele.bT = xPageY(container)-xPageY(ele);
	ele.bR = ele.bL+xWidth(container)-xWidth(ele);
	ele.bB = ele.bT+xHeight(container)-xHeight(ele);
	ele.style.clear = "left";
	ele.style.zIndex = 1000;
}

function fnDrag(ele, mdx, mdy)
{
  ele.offL+=mdx;
  ele.offT+=mdy;
	var x = Math.min(ele.bR,Math.max(ele.bL,ele.offL));
	var y = Math.min(ele.bB,Math.max(ele.bT,ele.offT));

	
	xMoveTo(ele, x, y);
}


function fnDragEnd(ele, mx, my)
{
	var ret;
	var containerId = ele.id.substring(2, 1);
	
	ret = fnValidTarget(ele.id, mx, my);

	attemptCount[(containerId - 1)]++;

	if (ret == null)
	{
		if( ele.id.substring(2, 1) == 1 ){
			ele.className = "quiz_drag4";
		}
		else if( ele.id.substring(2, 1) == 2 ){
			ele.className = "quiz_drag5";
		}
		
		xSlideTo(ele, ele.initX, ele.initY, 250);
	}
	else
	{		
		if( ele.id.substring(2, 1) == 1 ){
			ret.innerHTML = ret.innerHTML + "<span class='quiz_drop_correct_8'>"+ele.innerHTML+"</span";
		}
		else if( ele.id.substring(2, 1) == 2 ){
			ret.innerHTML = ret.innerHTML + "<span class='quiz_drop_correct_9'>"+ele.innerHTML+"</span";
		}  		   		   
	    		
		xDisableDrag(ele);
		ele.style.visibility = "hidden";			
		    					
		correctCount[(containerId - 1)]++;
	}

	if (correctCount[(containerId - 1)] == containerScore[(containerId - 1)])
	{
		fnShowScore(containerId);
	}
}

function fnValidTarget(id, mx, my)
{
	var dragged = xGetElementById(id);
	var elem;
	var ret = null;
	var sep = id.indexOf("_");
	var num = id.substring(sep + 1);
	var dropPrefix = "c" + id.substring(2, 1) + "-";
	var dragID = 0;
	var dropID = 0;
	var target;
	var dragValue;	
	
	if (id.substring(1,2) == 1 )
	{				
		for (var i = 0; i < 8; i++)
		{
			elem = xGetElementById("c1-drop_" + ( i + 1));
			if (mx >= xPageX(elem) && mx <= (xPageX(elem) + xWidth(elem)))
			{
				if (my >= xPageY(elem) && my <= (xPageY(elem) + xHeight(elem)))
				{
					sep = elem.id.indexOf("_");			
					
					if (dragged.attributes.col.nodeValue == elem.attributes.col.nodeValue){								
						ret = elem;
					}
				}
			}
		}	
	}
	else if( id.substring(2, 1) == 2 )
	{			
		for (var i = 0; i < 8; i++)
		{
			elem = xGetElementById("c2-drop_" + ( i + 1));
			if (mx >= xPageX(elem) && mx <= (xPageX(elem) + xWidth(elem)))
			{
				if (my >= xPageY(elem) && my <= (xPageY(elem) + xHeight(elem)))
				{
					sep = elem.id.indexOf("_");					
					if (elem.id.substring(sep + 1) == num || elem.attributes.dropAlt.nodeValue == num)
					{
						ret = elem;
					}
				}
			}
		}		
	}
	
	
	return ret;
}

function fnResetQuestion(containerId)
{
	xGetElementById("container" + containerId).innerHTML = initHTML[(containerId - 1)];
	correctCount[(containerId - 1)] = 0;
	attemptCount[(containerId - 1)] = 0;
	question_scored[(containerId - 1)] = false;
	initQuiz(containerId);
}
/*
function fnShowScore(containerId)
{
	var popup;
	var message;
	var score;
	var attCount = attemptCount[(containerId - 1)];
	var threshold = containerScore[(containerId - 1)];
	
	question_scored[(containerId - 1)] = true;
	message = "your quiz ranking is: <br/>";
	
	if (attCount == threshold)
	{
		score = "A* - An excellent score!";
	}
	else if (attCount <= (threshold + 2))
	{
		score = "A - Very well done!";
	}
	else if (attCount <= (threshold + 4))
	{
		score = "B - Well done!";
	}
	else if (attCount > (threshold + 4))
	{
		score = "C - Try the quiz again after re-reading the section pages.";
	}

	xGetElementById("quiz_result_text").innerHTML = message;
	xGetElementById("quiz_result_score").innerHTML = score;
	xGetElementById("quiz_result_title").innerHTML = "Congratulations!";
	popup = xGetElementById("quiz_result_popup");
	popup.style.display = "block";
	var st = (document.documentElement)?document.documentElement.scrollTop:document.body.scrollTop;
	xMoveTo(popup, (xClientWidth() - xWidth(popup)) / 3, st);
}*/

function fnShowScore(containerId)
{
	var popup;
	var message;
	var score;
	var attCount = attemptCount[(containerId - 1)];
	var threshold = containerScore[(containerId - 1)];
	var wrap = xGetElementById("wrapper");
	var warp2 = xGetElementById("MainCol_inner");
	var cont = xGetElementById("container"+containerId);

	message = "your quiz ranking is: <br/>";
	
	if (attCount == threshold)
	{
		score = "A* - An excellent score!";
	}
	else if (attCount <= (threshold + 2))
	{
		score = "A - Very well done!";
	}
	else if (attCount <= (threshold + 4))
	{
		score = "B - Well done!";
	}
	else if (attCount > (threshold + 4))
	{
		score = "C - Try the quiz again after re-reading the section pages.";
	}

	xGetElementById("quiz_result_text").innerHTML = message;
	xGetElementById("quiz_result_score").innerHTML = score;
	xGetElementById("quiz_result_title").innerHTML = "Congratulations!";		
	popup = xGetElementById("quiz_result_popup");
	popup.style.display = "block";
	xMoveTo(popup, (xWidth(wrap) - xWidth(popup)) / 2, xOffsetTop(wrap)+xOffsetTop(cont)+ 100);
}



function fnHideScore()
{
	var popup;
	popup = xGetElementById("quiz_result_popup");
	popup.style.display = "none";
	return false;
}

/**
 * checks the answer of the element
 **/
function fnCheckAnswer( p_element, p_correct )
{
    var containerID;
    var questionID;
    var answerID;
    var tickImage;
    var selectText;
    var nOptions;
    
    containerID = p_element.id.substring(1, 2);
    questionID = p_element.id.substring(3, 4);
    answerID = p_element.id.substring(5, 6);
       
    tickImage = "selectText_c" + containerID + "_" + questionID + "_" + answerID; 
    selectText = p_element.attributes.selectText.nodeValue;
    nOptions = p_element.attributes.nOptions.nodeValue;  
    attemptCount[(containerID - 1)] = attemptCount[(containerID - 1)] + 2;    
    
    for(i = 1; i <= nOptions; i++){    	     	
    	 if(i == answerID){
    	  	 xGetElementById("selectText_c" + containerID + "_" + questionID + "_" + i).innerHTML = selectText;
    	 }else{
	    	 xGetElementById("selectText_c" + containerID + "_" + questionID + "_" + i).innerHTML = '&nbsp;';
    	 }
    }
    
    if(containerID == 5) return;
   
    if( p_correct == 1 )
    {
        correctCount[(containerID - 1)]++;       
    }
       
    // if they have answered all the questions then show their score
    if (correctCount[(containerID - 1)] == containerScore[(containerID - 1)] && !question_scored[(containerID - 1)] )
	{
		fnShowScore(containerID);
	}
    
} // end of function fnCheckAnswer


function fnShowScore2(containerId)
{
	var popup;
	var message;
	var score;
	var attCount = correctCount[(containerId - 1)];
	var threshold = containerScore[(containerId - 1)];
    
    question_scored[(containerId - 1)] = true;
	message = "your quiz ranking is: <br/>";
	
	if (attCount == threshold)
	{
		score = "A* - An excellent score!";
	}
	else if (attCount == (threshold - 1))
	{
		score = "A - Very well done!";
	}
	else if (attCount == (threshold - 2))
	{
		score = "B - Well done!";
	}
	else if (attCount <= (threshold - 3))
	{
		score = "C - Try the quiz again after re-reading the section pages.";
	}
	xGetElementById("quiz_result_text").innerHTML = message;
	xGetElementById("quiz_result_score").innerHTML = score;
	xGetElementById("quiz_result_title").innerHTML = "Congratulations!";
	popup = xGetElementById("quiz_result_popup");
	popup.style.display = "block";
	var st = (document.documentElement)?document.documentElement.scrollTop:document.body.scrollTop;
	xMoveTo(popup, (xClientWidth() - xWidth(popup)) / 3, st);
}




