var lookup_email = ''
var lookup_case_number = ''

function lookup() {
	var has_changed = false
	
		new Ajax.Request('/student_lookup?' + Form.Element.serialize('lookup_email') + '&' + Form.Element.serialize('lookup_case_number') + '&' + Form.Element.serialize('lookup_name'),
				{asynchronous:true, evalScripts:true})
	
}

function updateStatus(status, user_id) {
	new Ajax.Request('/update_status?status=' + status + '&user_id=' + user_id, {asynchronous:true, evalScripts:true})
}

function sendCert(user_id) {
	new Ajax.Request('/send_cert?user_id=' + user_id, {asynchronous:true, evalScripts:true})
}

var has_been_notified = false

function inform_of_completion() {
	if (!has_been_notified) {
		has_been_notified = true
		alert("Congratulations!\n\nYou have completed this course. Please check your dashboard for more information.")
	}
	
	return false
}


var question_data = [["D", "T", "D", "F", "T", "D", "T", "T", "F", "D", "B", "A", "D"], ["C", "T", "D", "T", "D", "T", "T", "T", "D", "B"], ["T", "T", "T", "D", "T", "C", "T", "T", "T", "A", "T", "T", "C", "D", "A", "B", "A", "C", "A", "C", "B"], ["T", "T", "D", "D", "D"]];

function lookupValue(mq) {
	var element_selected = false;
	var selected_value = '';
	
	$('test_form').getInputs('radio', mq).each(function(el){
		if (el.checked) {
			element_selected = true;
			selected_value = el.value;
		}
	})
	
	if (element_selected) {
		return selected_value;
	}
	return false;
}

function highlightCorrect(el,ans) {
	$(el + '_question').addClassName('correct');
	if ($(el + '_question').hasClassName('incorrect')) $(el + '_question').removeClassName('incorrect')
	return false;
}

function highlightIncorrect(el,ans) {
	$(el + '_question').addClassName('incorrect');
	if ($(el + '_question').hasClassName('correct')) $(el + '_question').removeClassName('correct')
	return false;
}

function highlightUnanswered(el,ans) {
	new Effect.Highlight(el + '_question', {duration:1,startcolor:'#fffe81', endcolor:'#CFDFA0'})
	new Effect.Highlight(el + '_answers', {duration:1,startcolor:'#fffe81', endcolor:'#CFDFA0'})
	return false;
}

function checkValues(module_num, last_page) {
	
	if (last_page == undefined)
		last_page = false
	
	var num_correct = 0;
	var num_incorrect = 0;
	var num_unanswered = 0;
	
	var module = question_data[module_num-1];
	
	for (var qi = 0, ql = module.length; qi < ql; ++qi) {
		var answer = module[qi];
		var question_num = qi+1;
		
		var mq = 'm' + module_num + '_q' + question_num;
		if ((lv = lookupValue(mq)) != false) {
			if (lv == answer) {
				num_correct++
			} else {
				num_incorrect++
			}
		} else {
			num_unanswered++
		}
	}
	
	if (num_unanswered == 0) {
		for (var qi = 0, ql = module.length; qi < ql; ++qi) {
			var answer = module[qi];
			var question_num = qi+1;

			var mq = 'm' + module_num + '_q' + question_num;
			if ((lv = lookupValue(mq)) != false) {
				if (lv == answer) {
					highlightCorrect(mq,lv)
				} else {
				  highlightIncorrect(mq,lv)
				}
			}
		}
		
		page = ("" + window.top.location).split('/')
		
		new Ajax.Request('/page_view?page_name=' + page[page.length-1], {asynchronous:true, evalScripts:true, method:'put'})
		
		var message = "You scored " + ( (num_correct / (num_correct+num_incorrect)) * 100).toFixed(0) + "% (You answered " + num_correct + " correct).\n\n"
		if (last_page)
			message += "Congratulations! You have completed the course. Please return to your Dashboard for more information."
		else
			message += "You can review the questions you answered incorrectly, or move on to the next section."
		
		alert(message)
	} else {
		alert("Please answer all of the questions.")
		for (var qi = 0, ql = module.length; qi < ql; ++qi) {
			var answer = module[qi];
			var question_num = qi+1;

			var mq = 'm' + module_num + '_q' + question_num;
			
			if ((lv = lookupValue(mq)) == false) {
				highlightUnanswered(mq,lv)
			}
		}
	}
	
  return false;
}

function toggleWaived() {
	if (Element.visible('waived_instructions')) {
		Element.hide('waived_instructions')
	} else {
		Element.show('waived_instructions')
	}
}

function toggleReadingStatus() {
	if (Element.visible('reading_status')) {
		Element.hide('reading_status')
	} else {
		Element.show('reading_status')
	}
}

function certify() {
	new Ajax.Request('/certify', {asynchronous:true, evalScripts:true, method:'put'})
}