var currentQuestion = -1;
var testTitle = "";		
var inProgress = false;
var userName = "";
var questions = new Array();
var answersReceived = new Array();
var main = window;
window.onload=init;

function Question() {
	this.answers = new Array();
	this.text = "";
	this.type = "";
	this.page = "";
	this.rat = "";
	this.correctIndex = -1;
	
	this.setText = Question_setText;
	this.getText = Question_getText;
	this.setType = Question_setType;
	this.getType = Question_getType;
	this.setPage = Question_setPage;
	this.getPage = Question_getPage;
	this.setRationale = Question_setRationale;
	this.getRationale = Question_getRationale;
	this.setCorrectAnswer = Question_setCorrectAnswer;
	this.getCorrectAnswer = Question_getCorrectAnswer;
	this.addAnswer = Question_addAnswer;
	
}
function Question_setText(atext) {
	this.text = atext;
}
function Question_getText() {
	return this.text;
}
function Question_setType(atype) {
	this.type = atype;
}
function Question_getType() {
	return this.type;
}

function Question_setPage(apage) {
	this.page = apage;
}
function Question_getPage() {
	return this.page;
}
function Question_setRationale(arat) {
	this.rat = arat;
}
function Question_getRationale() {
	return this.rat;
}
function Question_setCorrectAnswer(aindex) {
	this.correctIndex = aindex;
}
function Question_getCorrectAnswer() {
	return this.correctIndex;
}
function Question_addAnswer(aanswer) {
	this.answers[this.answers.length] = aanswer;
}

function addQuestion(question) {
	questions[questions.length] = question;
}

function init() {
	if (questions.length > 0) {
		answersReceived = new Array(questions.length);
		currentQuestion = 0;
		inProgress = true;	
	}
}

function displayQuestion() {
	if (main.questions.length > 0) {
		var question = questions[currentQuestion];
		
		TestPanel.questionNumber.innerHTML="Question " + (currentQuestion+1) + " out of " + questions.length;
		TestPanel.section.innerHTML = question.getText();
		if (question.getType() == "tf") {
			TestPanel.panel.innerHTML = '<form name=answer><TABLE cellpadding=5 cellspacing=5 border=0><TR><TD><INPUT TYPE="radio" NAME="radioAnswer" ' +
			((answersReceived[currentQuestion] == 0)?"CHECKED":"") +
			'></TD><TD>True</TD></TR><TR><TD><INPUT TYPE="radio" NAME="radioAnswer" ' +
			((answersReceived[currentQuestion] == 1)?"CHECKED":"") +
			'></TD><TD>False</TD></TR></TABLE></form>';
		} else if (question.getType() == "mc" || question.getType() == "g") {
			var answers = question.answers;
			var html = "<form name=answer><TABLE cellpadding=5 cellspacing=5 border=0>";
			for (var i=0; i < answers.length; i++) {
				html += '<TR><TD><INPUT TYPE="radio" NAME="radioAnswer" ' + 
				((answersReceived[currentQuestion] == i)?"CHECKED":"") +'></TD><TD>' + answers[i] +"</TD></TR>";
			}
			html += "</TABLE></form>";
			TestPanel.panel.innerHTML = html;
		} else if (question.getType() == "sa") {
			var answers = question.answers;
			var html =  "<form name=answer>" + question.text + "</form>";
			for (var i=0; i < answers.length; i++) {
				textBox = '<INPUT TYPE="text" NAME="textAnswer' + i + '" VALUE="' + 
				((!answersReceived[currentQuestion])?"":answersReceived[currentQuestion][i]) +'">';
				html = html.replace(/\_+/, textBox);
			}
			TestPanel.panel.innerHTML = html;
		}
		TestPanel.page.innerHTML = "";
		TestPanel.rationale.innerHTML = "";
	}
}

function nextQuestion() {
	if (inProgress) {
		if (recordAnswer()) {
			if (currentQuestion < questions.length - 1) {
				currentQuestion++;
				displayQuestion();
			} else {
				calculateScore();
			}
		} else {
			alert("Please provide answer to proceed.\nYou can use the \"Back\" button to review or change your answers");
		}
	}
}

function previousQuestion() {
	if (inProgress) {
		recordAnswer();
		if (currentQuestion > 0)
			currentQuestion--;
		displayQuestion();
	}
}

function recordAnswer() {
	var bGotAnswer = false;
	var question = questions[currentQuestion];
	if (question.type == "mc" || question.type == "g") {
		for (var i=0; i < question.answers.length; i++) {
			if (TestPanel.answer.radioAnswer[i].checked) {
				answersReceived[currentQuestion] = i;
				bGotAnswer = true;
			}
				
		}
	} else
	if (question.type == "tf") {
		for (var i=0; i < 2; i++) {
			if (TestPanel.answer.radioAnswer[i].checked) {
				answersReceived[currentQuestion] = i;
				bGotAnswer = true;
			}

		}
	} else {
		answersReceived[currentQuestion] = new Array();
		for (var i=0; i < question.answers.length; i++) {
			answersReceived[currentQuestion][i] = 
				TestPanel.answer.elements["textAnswer" + i].value;
			bGotAnswer = true;
		}
	}
	return bGotAnswer;
}

function checkAnswer() {
	var bAnswer = false;
	var answers = "";
	var question = questions[currentQuestion];

	TestPanel.page.innerHTML = "<B><U>Page</U>:</B>" + question.getPage();
	TestPanel.rationale.innerHTML = "<B><U>Rationale</U>:</B>" + question.getRationale();

	if (question.type == "mc" || question.type == "g") {
		for (var i=0; i < question.answers.length; i++) {
			if (TestPanel.answer.radioAnswer[i].checked) {
				bAnswer = (question.getCorrectAnswer() == i);
				break;
			}
				
		}
	} else
	if (question.type == "tf") {
		for (var i=0; i < 2; i++) {
			if (TestPanel.answer.radioAnswer[i].checked) {
				bAnswer = (question.getCorrectAnswer() == i);
				break;
			}

		}
	} else {
		answersReceived[currentQuestion] = new Array();
		for (var i=0; i < question.answers.length; i++) {
			answers += "\"" + TestPanel.answer.elements["textAnswer" + i].value + "\" ";
		}
		var a = "";
		for (var i=0; i < question.answers.length; i++) {
			a += "\"" + question.answers[i] + "\" ";
		}
		answers += (a == answers)?" is Correct":" is Incorrect";
	}
	if (answers.length > 0) {
		alert("Your answer: " + answers);
	} else {
		if (bAnswer) {
			alert("Your answer is Correct");
		} else {
			alert("Your answer is Incorrect");
		}
	}
}


function calculateScore() {
	inProgress= false;
	
	var count = 0;
	var countTotal = 0;
	var result = "";
	var emailBody = "";
	
	TestPanel.page.innerHTML = "";
	TestPanel.rationale.innerHTML = "";

	for (var i = 0; i < questions.length; i++) {
		var question = questions[i];
		result += (i + 1) + ". " + question.text + "<P>";
		emailBody += (i + 1) + ". " + question.text + "\n";
		if (question.type == "mc" || question.type == "g") {
			var correctAnswer = question.getCorrectAnswer();
			var userAnswer = answersReceived[i];
			result += "Your answer: <B>" + ((!question.answers[userAnswer])?"":question.answers[userAnswer]) + "</B><BR>";
			result += "Correct answer: <B>" + question.answers[correctAnswer] + "</B><BR>";
			if (correctAnswer == userAnswer) {
				result += '<IMG SRC="check.gif"><SPAN class=correct>Correct</SPAN>';
				emailBody += "+ ";
				count++;
			} else {
				emailBody += "- ";
				result += '<IMG SRC="wrong.gif"><SPAN class=incorrect>Incorrect</SPAN>';
			}
			result += "<P>";
			emailBody += ((question.answers[userAnswer])?"":question.answers[userAnswer]);
			countTotal++;
		} else
		if (question.type == "tf") {
			var correctAnswer = question.getCorrectAnswer();
			var userAnswer = answersReceived[i];
			result += "Your answer: <B>" + ((userAnswer==0)?"True":((userAnswer==1)?"False":"")) + "</B><BR>";
			result += "Correct answer: <B>" + ((correctAnswer==0)?"True":"False") + "</B><BR>";
			if (correctAnswer == userAnswer) {
				result += '<IMG SRC="check.gif">';
				emailBody += "+ ";
				count++;
			} else {
				result += '<IMG SRC="wrong.gif"><BR><U>Rationale</U><I>: ' + question.rationale + '</I>';
				emailBody += "- ";
			}
			result += "<P>";
			emailBody += ((userAnswer==0)?"True":((userAnswer==1)?"False":""));
			countTotal++;
		} else
		if (question.type == "sa") {
			var userAnswers = answersReceived[i];
			var userAnswer = "";
			for (var j=0; j < userAnswers.length; j++) {
				if (userAnswer == "") {
					userAnswer = userAnswers[j]
				} else {
					userAnswer += ", " + userAnswers[j]
				}
			}
			result += "Your answer: <B>" + userAnswer + "</B><BR>";

			var correctAnswer = "";
			for (var j=0; j < question.answers.length; j++) {
				if (correctAnswer == "") {
					correctAnswer = question.answers[j]
				} else {
					correctAnswer += ", " + question.answers[j]
				}
			}
			result += "Correct answer: <B>" + correctAnswer + "</B><BR>";

			if (correctAnswer.toUpperCase() == userAnswer.toUpperCase()) {
				result += '<IMG SRC="check.gif">';
				emailBody += "+ ";
				count++;
			} else {
				result += '<IMG SRC="wrong.gif"><BR><U>Rationale</U><I>: ' + question.rationale + '</I>';
				emailBody += "- ";
			}
			result += "<P>";
			emailBody += userAnswer;
			countTotal++;
		}
		emailBody += "\n\n";
	}
	result += "<TABLE BORDER=1 CELLPADDING=10><TR><TD>Correct: <B>" + count + "</B> out of " + countTotal + "&nbsp;&nbsp;&nbsp;&nbsp;" + (Math.round(100 * count/countTotal)) + "%</TD></TR></TABLE>";
	TestPanel.buttons.innerHTML = "You may now exit your browser or go to a different page."; 
	if (main.showScore) {
		TestPanel.questionNumber.innerHTML="Correct: " + count + " out of " + countTotal + "&nbsp;&nbsp;&nbsp;&nbsp;" + (Math.round(100 * count/countTotal)) + "%" + "<BR><BR>You have completed this study guide! Your score and results summary are displayed below. Please print this page for your records. If your instructor chose to receive your study guide results via e-mail, they have been automatically submitted.";
		TestPanel.section.innerHTML = result;
		TestPanel.panel.innerHTML = "";
	} else {
		
		TestPanel.questionNumber.innerHTML = "You have completed this test";
		TestPanel.section.innerHTML = "You have completed this study guide! Your study guide results have been automatically scored and submitted to your instructor via e-mail.You may now exit your browser or go to a different page.";
		TestPanel.panel.innerHTML = "";
	}
	emailBody = testTitle + "\n" +
		"Student: " + userName + "\n" +
		"Correct: " + count + " out of " + countTotal + " " + (Math.round(100 * count/countTotal)) + "%\n\n" +
		emailBody;
	sendEmail(emailBody);

}
function sendEmail(emailBody) {
	PROCESSOR.document.forms["MAIL"].ToName.value=testAuthor;
	PROCESSOR.document.forms["MAIL"].ToEmail.value=testAuthorEmail;
	PROCESSOR.document.forms["MAIL"].Subject.value="Test Result: " + userName;
	PROCESSOR.document.forms["MAIL"].BodyText.value=emailBody;
	/*
	alert(
		"To: " + PROCESSOR.document.forms["MAIL"].ToName.value + 
		" <" + PROCESSOR.document.forms["MAIL"].ToEmail.value + ">" +
		"\nSubject: " + PROCESSOR.document.forms["MAIL"].Subject.value + 
		"\nBody: " + PROCESSOR.document.forms["MAIL"].BodyText.value
	);
	*/
	PROCESSOR.document.forms["MAIL"].submit();
}
function authenticate(fname,lname)
{
	main.userName = fname + " " + lname;
	TestPanel.location.href = "test.html";
}

