this_included=0;
closes=0;
score=0;
highlighted_box=0;
zoomed=0;

function change_class(id, newClass)
{
	identity=document.getElementById(id);
	if (identity)	identity.className=newClass;
}

function change_link(ques_num, link_text)
{
	identity=document.getElementById('q' + ques_num + 'res');
	identity.onclick=function() { window.open(link_text); };
}

function get_score(id)
{
	score_text=document.getElementById(id).value;

	if (score_text.indexOf("Partial")>=0)	return(-2);
	if (score_text.indexOf("Close")>=0)		return(-1);
	if (score_text.indexOf("Correct")>=0)	return(1);
	return(0);
}

function update_scores()
{
	document.getElementById('scorebox').value="Score: " + score + "/" + questions;
	document.getElementById('scorebox2').value="Score: " + score + "/" + questions;
	document.getElementById('closebox').value="[" + closes + " Close]";
	document.getElementById('closebox2').value="[" + closes + " Close]";
}

function highlight_box(ques_num)
{
	if (ques_num==highlighted_box)	return;
	if ((highlighted_box>0) && (highlighted_box<=questions))
	{
		change_class('q' + highlighted_box + 'box', 'question-outer-unsel');
	}
	change_class('q' + ques_num + 'box', 'question-outer-sel');
	highlighted_box=ques_num;
}

function changecorrects(checkBox)
{
	if (checkBox.checked)
	{
		for (ques_num=1; ques_num<=questions; ques_num++)
		{
			this_score=get_score('q' +  ques_num + 'res');
			visual_qid=((ques_num - 1) % 4) + 4 * Math.floor((ques_num - 1) / (4 * quiz_type)) + 1;
			if (this_score==1)
			{
				change_class('q' +  visual_qid + 'pic', 'pic-correct');
				change_class('q' +  visual_qid + 'text', 'text-correct');
			}
		}
	} else
	{
		for (ques_num=1; ques_num<=questions; ques_num++)
		{
			this_score=get_score('q' +  ques_num + 'res');
			visual_qid=((ques_num - 1) % 4) + 4 * Math.floor((ques_num - 1) / (4 * quiz_type)) + 1;
			if (this_score==1)
			{
				change_class('q' +  visual_qid + 'pic', 'pic-close');
				change_class('q' +  visual_qid + 'text', 'text-close');
			}
		}
	}
}

function checkanswer(thisform, thisanswerbox, ques_num)
{
	new_text = thisform.value;
	new_text = new_text.toLowerCase().replace('&','and');
	new_text = replace_special_chars(new_text);
	new_text = new_text.replace(/[^a-z0-9]+/g,'');

	comp_text=hex_md5(new_text);

	this_score=get_score('q' +  ques_num + 'res');

	visual_qid=((ques_num - 1) % 4) + 4 * Math.floor((ques_num - 1) / (4 * quiz_type)) + 1;

	if (new_text.length>0)
	{
		for (mycount=0; mycount<ans_text[ques_num].length; mycount++)
		{
			if (comp_text == ans_text[ques_num][mycount][1])
			{
				if (this_score != 1)
				{
					thisanswerbox.value='Correct (Click to Search)';
					if (quiz_type<2)
					{
						change_class('q' +  visual_qid + 'ques', 'ques-correct');
						if (document.AnswerForm.hidecorrects.checked)
						{
							change_class('q' +  visual_qid + 'pic', 'pic-correct');
							change_class('q' +  visual_qid + 'text', 'text-correct');
						}
					} else 
					{
						// Check to see if the other answer is also correct before changing these
						if ((Math.floor((ques_num - 1) / 4) % 2) == 0)
						{
							other_ques=ques_num + 4;
						} else
						{
							other_ques=ques_num - 4;
						}
						other_score=get_score('q' +  other_ques + 'res');
						if (other_score==1)
						{
							change_class('q' +  visual_qid + 'ques', 'ques-correct');
							if (document.AnswerForm.hidecorrects.checked)
							{
								change_class('q' +  visual_qid + 'pic', 'pic-correct');
								change_class('q' +  visual_qid + 'text', 'text-correct');
							}
						}
					}
					change_class('q' +  ques_num + 'ans', 'ans-correct');
					change_class('q' +  ques_num + 'res', 'res-correct');
					change_link(ques_num, clue_target + ques_num + "&text=" + thisform.value);
					score++;
					if (this_score==-1)	closes--;
					update_scores();
				}
				return(false);
			}
		}
	}

	close_new_text = new_text.replace(/[aeiou]+/g, '_');
	close_new_text = close_new_text.replace(/__/g, '_');

	close_comp_text=hex_md5(close_new_text);

	if (window.close_text[ques_num])
	{
		for (mycount=0; mycount<close_text[ques_num].length; mycount++)
		{
			if (my_strlen(new_text)>=close_text[ques_num][mycount][0])
			{
				for (charcount=0; charcount<=(my_strlen(new_text)-close_text[ques_num][mycount][0]); charcount++)
				{
					comp_text=hex_md5(new_text.substring(charcount, charcount+close_text[ques_num][mycount][0]));
					if ((comp_text == close_text[ques_num][mycount][1]) || (close_comp_text == close_text[ques_num][mycount][1]))
					{
						if (this_score != -1)
						{
							thisanswerbox.value='Close (Click for Clue)';
							change_class('q' +  visual_qid + 'ques', 'ques-close');
							change_class('q' +  visual_qid + 'pic', 'pic-close');
							change_class('q' +  visual_qid + 'text', 'text-close');
							change_class('q' +  ques_num + 'ans', 'ans-close');
							change_class('q' +  ques_num + 'res', 'res-close');
							change_link(ques_num, clue_target + ques_num);
							closes++;
							if (this_score>0)	score--;
							update_scores();
						}
						return(false);
					}
				}
			}
		}
	
		does_match=true;
	
		if ((my_strlen(new_text)>=4)  && (window.close_text[ques_num]))
		{
			for (charcount=0; (charcount<(my_strlen(new_text)-3)) && does_match; charcount++)
			{
				comp_text=hex_md5(new_text.substring(charcount, charcount+4));
				not_found_match=true;
				for (mycount=0; (mycount<partial_text[ques_num].length) && not_found_match; mycount++)
				{
					if (comp_text == partial_text[ques_num][mycount])
					{
						not_found_match=false;
					}
				}
				if (not_found_match)
				{
					does_match=false;
				}
			}
			if (does_match)
			{
				if (this_score != -2)
				{
					thisanswerbox.value='Partial Ans. (Click for Clue)';
					change_class('q' +  visual_qid + 'ques', 'ques-close');
					change_class('q' +  visual_qid + 'pic', 'pic-close');
					change_class('q' +  visual_qid + 'text', 'text-close');
					change_class('q' +  ques_num + 'ans', 'ans-close');
					change_class('q' +  ques_num + 'res', 'res-close');
					change_link(ques_num, clue_target + ques_num);
					if (this_score==-1)	closes--;
					if (this_score>0)	score--;
					update_scores();
				}
				return(false);
			}
		}
	}

	if (this_score != 0)
	{
		thisanswerbox.value='Wrong (Click for Clue)';
		change_class('q' +  visual_qid + 'ques', 'ques-wrong');
		change_class('q' +  visual_qid + 'pic', 'pic-wrong');
		change_class('q' +  visual_qid + 'text', 'text-wrong');
		change_class('q' +  ques_num + 'ans', 'ans-wrong');
		change_class('q' +  ques_num + 'res', 'res-wrong');
		change_link(ques_num, clue_target + ques_num);
		
		if (this_score==-1)	closes--;
		if (this_score>0)	score--;
		update_scores();
	}
	return(false);
}

function OnSubmitForm(doRatingCheck)
{
	doSubmit=true;
	document.AnswerForm.action ="";
	if (doRatingCheck)
	{
		if (document.AnswerForm.rating.value<1)
		{
			doSubmit=confirm("You haven't rated this quiz. Proceed anyway?");
		}
	}
	if (doSubmit)
	{
		if(document.pressed == 'Save')
		{
			document.AnswerForm.action ="online_save.php";
		} else if(document.pressed == 'Cancel Load')
		{
			document.AnswerForm.answerfile.value ="";
		} else
		{
			document.AnswerForm.action ="";
		}
  	}
  	return(doSubmit);
}

function isSet( variable )
{
	return( typeof( variable ) != 'undefined' );
}

function my_strlen(my_string)
{
	if(isSet(my_string))
	{
		return(my_string.length);
	}
	return(0);
}

var find_string=String.fromCharCode(181,224,225,226,227,228,229,231,232,233,234,235,236,237,238,239,240,241,242,243,244,245,246,248,249,250,251,252,253,254,255);
//									u	a	a	a	a	a	a	c	e	e	e	e	i	i	i	i	d	n	o	o	o	o	o	o	u	u	u	u	y	p	y
var replace_string="uaaaaaaceeeeiiiidnoooooouuuuypy";

var re1 = new RegExp('([' + String.fromCharCode(223) + '])', 'g');
var re2 = new RegExp('([' + String.fromCharCode(230) + '])', 'g');
var re3 = new RegExp('([' + find_string + '])', 'g');

function replace_special_chars(input_text)
{	
	var output_string=input_text.replace(re3,
		function(a,b) {
			return(replace_string.charAt(find_string.indexOf(a)));
		});
	
	return(output_string.replace(re1, "ss").replace(re2, "ae"));
}

