

var debug_flag = false;
var count_correct = 0;

var red_eye = new Image();
red_eye.src = "redeye.gif";

var blue_eye = new Image();
blue_eye.src = "blueeye.gif";

function get_group(id) {
	var group_id;
	var i;

	i = id.indexOf("_");
	
	if (i==-1) return "";
	else {
		group_id = id.substring(0,i);
		return group_id;
	}
}

function is_correct(id) {
	return id.substring(id.length-1,id.length) == "X";
}	

function bind_elements() {
// bind the onclick_handler() function to all the image onclick events
var i;
var el;

	for (i=0;i<document.all.length;i++) {
		el = document.all[i];
		if (el.id != "" && el.id != null) {
			// there is an id, is it a picture
			if (el.src != null && el.src != "") {
				// there is a source value
				el.attachEvent("onclick",onclick_handler);
			} // if element has src
		} // if there is an id
	} // for loop

	
} // function
		
function onclick_handler() {
var el;
var id;
var group_id;
var i;
var el_id;
var correct_flag;

	el = event.srcElement;
	id = el.id;
	
	if (id == "debug") {
		debug_flag = !debug_flag;
		alert ("debug flag = " + debug_flag);
		return;
	}
	
	if (id.substring(0,1) == "q") {
		if (debug_flag) alert ("clicked " + el.id);		
		group_id = get_group(id);
		// alert ("group =  " + group_id);

		if (is_correct(id)) {
			if (debug_flag) alert ("correct!");
			correct_flag = 1;
		}
		else correct_flag = 0;

		if (el.src == red_eye.src) {
			// real click
			if (correct_flag == 1) {
				count_correct++;
				if (debug_flag) alert ("count correct");
			}
		}
		
		
		for (i=0;i<document.all.length;i++) {
			el_id = document.all[i].id;
			if (el_id != "" && el_id != null) {
				if (el_id.substring(0,1) == "q") {
					if (get_group(el_id) == group_id && el_id != id) {
						if (document.all[i].src == blue_eye.src) {
							if (is_correct(el_id)) {
								if (debug_flag) alert ("count less one");
								count_correct--;
							} // was the correct answer
							document.all[i].src = red_eye.src;
						} // this element is set to selected (blue)
					} // element in this group
				} // element is a question
			} // we have an id
		} // for
		el.src = blue_eye.src;

		document.all.spy.innerText = "Correct = " + count_correct;
		
	} // if this id is a q
	
}

function check_next_page(next_page) {

	if (next_page == '') {
		alert ('You got ' + count_correct + ' questions correct');
		return;
	}

	if (count_correct>=7) {
		alert ('You got ' + count_correct + ' questions correct and can go to the next Quiz');
		location.href=next_page;
	}
	else {
		alert ("You have not got enough questions correct to move on to the next Quiz");
	}
}

bind_elements();
	

