var correct;
var partial = false;
var feedbackArr = new Array();
var finalIncorrectFeedback;
var tries = 0;
var thisNumber = 0;
var currentType = "";
var tempWin = null;
var mcUserAns = null;
var msUserAns = new Array();
var tfUserAns = null;
var toggled = false;
var finished = false;
var toggleAdded = false;

var movieName = new Object();

function getFlashMovieObject(movieName){
	if (window.document[movieName]){
		return window.document[movieName];
	}
	if (navigator.appName.indexOf("Microsoft Internet")==-1){
	if (document.embeds && document.embeds[movieName])
		return document.embeds[movieName];
	}
	else // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
	{
		return document.getElementById(movieName);
	}
}


function SendDataToFlashMovie(isCorrect,quesNum){
	var flashMovie=getFlashMovieObject("knowrev_2_2");
	flashMovie.doTry(isCorrect,quesNum);
}

function setupKR_mc(newKR,correctAns,feedback,finalIncorrect,quesNum){
	document.getElementById("kr").style.width = 0+"px";
	document.getElementById("kr").style.height = 0+"px";
	document.getElementById("kr_js").innerHTML = newKR;
	document.getElementById("kr_js").focus()
	document.multch.group1.onClick = 'showSub()';
	correct = correctAns;
	thisNumber = quesNum;
	feedbackArr = feedback.split("^");
	currentType = "mc";
	finalIncorrectFeedback = finalIncorrect;
	toggleAdded = false;
	document.getElementById("krsubmit").style.display="none";
}

function setupKR_ms(newKR,correctAns,feedback,quesNum){
	document.getElementById("kr").style.width = 0+"px";
	document.getElementById("kr").style.height = 0+"px";
	document.getElementById("kr_js").innerHTML = newKR;
	document.getElementById("kr_js").focus();
	feedbackArr = feedback.split("^");
	correct = correctAns.split("^");
	thisNumber = quesNum;
	currentType = "ms";
	finalIncorrectFeedback = feedbackArr[1];
	toggleAdded = false;
	document.getElementById("krsubmit").style.display="none";
}

function setupKR_tf(newKR,correctAns,feedback,quesNum){
	document.getElementById("kr").style.width = 0+"px";
	document.getElementById("kr").style.height = 0+"px";
	document.getElementById("kr_js").innerHTML = newKR;
	document.getElementById("kr_js").focus();
	if(correctAns == "true")
		correct = true;
	else correct = false;
	feedbackArr = feedback.split("^");

	thisNumber = quesNum;
	currentType = "tf";
	finalIncorrectFeedback = feedbackArr[1];
}

function checkAnswer(quesType,group){
	if(currentType == "mc"){
		iscorrect = false;
		var answeredNum = 0;
		for(var i=0; i < document.multch.group1.length; i++){
			if(document.multch.group1[i].checked) {
				answeredNum = i;
				mcUserAns = i;
				if(i == correct)
					iscorrect = true;
			}
		}
		var flashMovie=getFlashMovieObject("knowrev_2_2");
		document.getElementById('krfeedback').innerHTML = feedbackArr[answeredNum];
		flashMovie.doTry(iscorrect,thisNumber);
	}
	else if(currentType == "ms"){
		var flashMovie=getFlashMovieObject("knowrev_2_2");
		var retry = flashMovie.canRetry();
		partial = false;
		iscorrect = true;
		for(var j=0; j < document.multsel.elements.length; j++){
			if(document.multsel.elements[j].checked)
				msUserAns.push(j);
		}//this is the code that checks to see if they checked any correct boxes
		for(var i=0; i < document.multsel.elements.length; i++){
			
			if(document.multsel.elements[i].checked && correct[i] == "T"){
				partial = true;
			}
			
			if((document.multsel.elements[i].checked && correct[i] == "F") || (!document.multsel.elements[i].checked && correct[i] == "T"))
				iscorrect = false;

		}
		/* this is the old check code
		for(var i=0; i < document.multsel.elements.length; i++){
			if((document.multsel.elements[i].checked && correct[i] == "F") || (!document.multsel.elements[i].checked && correct[i] == "T"))
				iscorrect = false;
			else partial = true;

		}*/
		if(iscorrect){
			document.getElementById('krfeedback').innerHTML = feedbackArr[0];
		}
		else if(partial){
			document.getElementById('krfeedback').innerHTML = feedbackArr[2];
		}
		else if(retry){
			document.getElementById('krfeedback').innerHTML = feedbackArr[3];
		}
		else{
			tempWin = false;
			document.getElementById('krfeedback').innerHTML = feedbackArr[1];
		}
		var flashMovie=getFlashMovieObject("knowrev_2_2");
		flashMovie.doTry(iscorrect,thisNumber);
		
	}
	else if(currentType == "tf"){
		if((group1[0].checked && correct) || (group1[1].checked && !correct)){
			iscorrect = true;
			document.getElementById('krfeedback').innerHTML = feedbackArr[0];
		}
		else{ 
			iscorrect = false;
			document.getElementById('krfeedback').innerHTML = feedbackArr[1];
		}
		var flashMovie=getFlashMovieObject("knowrev_2_2");
		flashMovie.doTry(iscorrect,thisNumber);
	}
}

function endKR(){
	if(!finished){
		document.getElementById("krsubmit").innerHTML = "";/*
		act1.disabled = true;*/
		if(!iscorrect){
			finalFeedback();
		}
		finished = true;
		krEnd();
	}
}
function finalFeedback(){
	addToggle();
	if((currentType == "ms" && !iscorrect) || (currentType == "mc" && !iscorrect))
		document.getElementById('krfeedback').innerHTML = finalIncorrectFeedback;
}
function nextQues(){
	var flashMovie=getFlashMovieObject("knowrev_2_2");
	flashMovie.doNext();
	
//	document.getElementById("krsubmit").innerHTML = "";
//	act1.value = "Continue";
//	document.getElementById("krsubmit").innerHTML = "<input type='submit' value='Continue' name='act1' onClick='doNext()' tabindex='70'>";
//	act1.onClick=flashMovie.doNext();
//	thisNumber++;
}
function showSub(){
	document.getElementById("krsubmit").style.display="inline";
}
function resetQues(){
	document.getElementById("krsubmit").innerHTML = "";
	document.getElementById("krsubmit").innerHTML = "<input type='submit' value='Reset' name='act1' onClick='realReset()' tabindex='70'>";
/*	if(currentType == "ms" && !tempWin)
		document.getElementById("krfeedback").innerHTML = feedbackArr[2];
	act1.value = "Reset";
	act1.onClick="realReset()";*/
}
function realReset(){
	//check if mc
	if(currentType == "mc"){
		document.getElementById("krfeedback").innerHTML = "";
		for(var i = 0; i < document.multch.group1.length; i++){
			document.multch.group1[i].checked = false;
		}
		document.getElementById("krsubmit").innerHTML = "";
		document.getElementById("krsubmit").innerHTML = "<input type='submit' value='Submit' name='act1' onClick='checkAnswer(\"mc\",\"group1\")' tabindex='70'>";
		document.getElementById("krsubmit").style.display="none";
	}
	//check if ms
	if(currentType == "ms"){
		msUserAns = new Array();
		for(var i=0; i < document.multsel.elements.length; i++){
//			if(document.multsel.elements[i].checked && correct[i] == "F")
			document.multsel.elements[i].checked = false;
		}
		document.getElementById("krfeedback").innerHTML = "";
		document.getElementById("krsubmit").innerHTML = "";
		document.getElementById("krsubmit").innerHTML = "<input type='submit' value='Submit' name='act1' onClick='checkAnswer(\"ms\",null)' tabindex='70'>";
		//document.getElementById("krsubmit").style.display="none";
	}
	//check if tf
	if(currentType == "tf"){
		group1[0].checked = false;
		group1[1].checked = false;
		document.getElementById("krfeedback").innerHTML = "";
		document.getElementById("krsubmit").innerHTML = "";
		document.getElementById("krsubmit").innerHTML = "<input type='submit' value='Submit' name='act1' onClick='checkAnswer(\"tf\",null)' tabindex='70'>";
	}
}

function setContinue(){
	document.getElementById("krsubmit").innerHTML = "";
	document.getElementById("krsubmit").innerHTML = "<input type='submit' value='Continue' name='act1' onClick='nextQues()' tabindex='70'>";
	if(currentType == "ms");

//		document.getElementById("krfeedback").innerHTML = feedbackArr[2];
}

function hideJS(){
	document.getElementById("kr_js").innerHTML = "";
}

function addToggle(){
	if(!toggleAdded && !iscorrect){
		toggled = true;
		var tempHTML = document.getElementById("krsubmit").innerHTML;
		document.getElementById("krsubmit").innerHTML = "<input type='button' value='Show correct answer' name='toggle' onClick='toggleAnswer()'>" + tempHTML + "<br />";
		toggleAdded = true;
	}
}

function toggleAnswer(){
	if(currentType == "mc"){
		if(!toggled){
			document.multch.toggle.value = "Show correct answer";
			for(var i = 0; i < document.multch.group1.length; i++){
				if(i == mcUserAns){
					document.multch.group1[i].checked = true;
				}
				else document.multch.group1[i].checked = false;
			}
		}
		else{
			document.multch.toggle.value = "Show my answer";
			for(var i = 0; i < document.multch.group1.length; i++){
				if(i == correct){
					document.multch.group1[i].checked = true;
				}
				else document.multch.group1[i].checked = false;
			}
		}
		toggled = !toggled;
	}// end multiple choice toggle
	
	else if(currentType == "ms"){
		if(!toggled){
			document.multsel.toggle.value = "Show correct answer";
			//need to show user's answer here
			for(var j = 0; j < document.multsel.elements.length; j++)
				document.multsel.elements[j].checked = false;
			for(var i = 0; i < msUserAns.length; i++){
				document.multsel.elements[msUserAns[i]].checked = true
			}
		}
		else{
			document.multsel.toggle.value = "Show my answer";
			//need to show correct answer here
			for(var j = 0; j < document.multsel.elements.length; j++)
				document.multsel.elements[j].checked = false;
			for(var i = 0; i < correct.length; i++){
				if(correct[i] == "T")
					document.multsel.elements[i].checked = true
			}
			
		}
		toggled = !toggled;
	}//end multiple select toggle
	
	else{//true false toggle
		
	}
}

// JavaScript Document
function resizeKRdiv(newW,newH){
	var temp = document.getElementById("kr");
	temp.style.width=newW+"px";
	temp.style.height=newH+"px";
}

function krStart(){
	document.getElementById("nb1").style.display="none";
	document.getElementById("nb2").style.display="none";
}
function krEnd(){
	document.getElementById("nb1").style.display="inline";
	document.getElementById("nb2").style.display="inline";
}