function validate_review_radio(id1, id2, form, nextpage)
{
	if (document.getElementById(id1).checked)
	{
		updateword.submit();
	}
	else
	{
		if (document.getElementById(id2).checked)
		{
			window.location = nextpage;
		}
		else
		{
			alert('You must first select if you need to update your spellings or if you have no changes.');
			return false;
		}

	}

	return true;
}

function checkValidate(boxes)
{
	for (var i=0; i<boxes.length; i++)
	{
		if (document.getElementById(boxes[i]).checked == 0)
		{
			return false;
		}
	}

	return true;
}

function checkForUpdates(id)
{
	if ($(id).value != '')
	{
		changedSpelling = true;
	}
}

function noChanges(type)
{
	if (type == 'public')
	{
		window.location = '?page=roundtwo';
	}
	else
	{
		window.location = '?page=confirm';
	}
}

function handleEnter (field, event)
{
	var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	if (keyCode == 13) {
		var i;
		for (i = 0; i < field.form.elements.length; i++)
			if (field == field.form.elements[i])
				break;
		i = (i + 1) % field.form.elements.length;
		field.form.elements[i].focus();
		return false;
	}
	else
	return true;
}

function closeWordInfo(ids, x, type)
{
	resetImageIcons();
	var params = '';
	params += 'action=close-word-info';
	params += '&ids=' + ids;
	params += '&x=' + x;

	if (type)
	{
		params += '&type=' + type;
	}

	var ajaxUrl;
	ajaxUrl = '/test/test.php';

	var opt = {
		method: 'post',
		asynchronous: true,
		postBody: params,
		evalScripts: true
	}

	var update_div;
	update_div = 'info';

	new Ajax.Updater(update_div, ajaxUrl, opt);
}

function resetImageIcons()
{
	for (i=1; i<=50; i++)
	{
		var infoimage = 'info-image-' + i;
		if ($(infoimage))
		{
			$(infoimage).src = '/images/info.gif';
		}
	}
}

function getWordInfo(word_id, word_order, type, ids, x)
{
	resetImageIcons();
	var infoimage = 'info-image-' + word_order;
	$(infoimage).src = '/images/infored.gif';

	var params = '';
	params += 'action=word-info';
	params += '&word_id=' + word_id;
	params += '&word_order=' + word_order;
	params += '&type=' + type;
	params += '&ids=' + ids;
	params += '&x=' + x;

	var ajaxUrl;
	ajaxUrl = "/test/test.php";

	//var update_div;
	//update_div = 'messages';

	var opt = {
		method: 'post',
		asynchronous: true,
		postBody: params,
		evalScripts: true
	}

	var update_div;
	update_div = 'info';

	new Ajax.Updater(update_div, ajaxUrl, opt);
}

function updateSpelling(id, speller_id, spelling,type)
{
	var params = '';
	params += 'action=update-word';
	params += '&id=' + id;
	params += '&speller_id=' + speller_id;
	params += '&spelling=' + spelling;
	params += '&type=' + type;

	var ajaxUrl;
	ajaxUrl = "/test/test.php";

	var opt = {
		method: 'post',
		asynchronous: true,
		postBody: params,
		evalScripts: true
	}

	var update_div;
	update_div = 'wordInfo';

	new Ajax.Updater(update_div, ajaxUrl, opt);
}

function checkSpelling(word_id,spelling,type)
{
	var params = '';
	if (type == 'practice')
	{
		params += 'action=check-spelling-practice';
	}
	else
	{
		params += 'action=check-spelling';
	}
	//params += 'action=check-spelling';
	params += '&word_id=' + word_id;
	params += '&spelling=' + spelling;

	var ajaxUrl;
	ajaxUrl = "/test/test.php";

	var opt = {
		method: 'post',
		asynchronous: true,
		postBody: params,
		evalScripts: true
	}

	var update_div;
	update_div = 'wordInfo';

	new Ajax.Updater(update_div, ajaxUrl, opt);
}

function beginTest(checkboxes)
{
	if (!checkValidate(checkboxes))
	{
		Ext.onReady(function() {
			Ext.MessageBox.show({
				title: '',
				msg: 'You must read and check each box before you continue.',
				width:350,
				buttons: Ext.MessageBox.OK,
				multiline: false,
				modal:false
			});
		});
	}
	else
	{
		Ext.onReady(function(){
			Ext.MessageBox.show({
				title: '',
				msg: 'Are you sure you want to begin the test now?',
				width:300,
				buttons: Ext.MessageBox.YESNO,
				multiline: false,
				modal:false,
				fn: function(btn){
					if (btn == 'yes')
					{
						window.location = '?page=word';
					}
					else
					{
						window.location = 'index';
						//window.location = '?action=testlogout';
					}
				}
			});
		});
	}
}

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 ReceiveDataFromFlashMovie(type)
{
	// alert('blp');
	var flashMovie = getFlashMovieObject('testinput');
	try {
		var message = flashMovie.GetVariable('spelling_given');
	} catch(err) {
		var message = '';
	}

	$('wordbox').value = message;

	if (message.match(/^[ ]+$/) && type == 'public')
	{
		return false;
	}
	else if (message.match(/^[ ]+$/))
	{
		$('wordbox').value = '';
		return true;
	}
	else if (message.match(/^(\s+)?[a-zA-Z]+(\s+)?$/))
	{
		return true;
	}
	else if (message == '' && type == 'public')
	{
		return false;
	}
	else if (message == '')
	{
		return true;
	}
	else
	{
		alert("Your spelling includes a symbol or character that is not one of the 26 letters of the alphabet. Please change your spelling to include only the 26 letters of the alphabet.");
	}
}

function updateHiddenSpelling(x,id)
{
	var flashMovie = getFlashMovieObject('reviewinput' + x);
	var message = flashMovie.GetVariable('spelling_given');
	$(id).value = message;
}

function ReceiveDataFromFlashMovieReview(x, ids, check)
{
	var count = 0;
	var nonalpha = false;
	for (i = 1; i <= x; i++)
	{
		var movieName = 'reviewinput' + i
		var flashMovie = getFlashMovieObject(movieName);
		if (flashMovie)
		{
			try {
				var message = flashMovie.GetVariable('spelling_given');
			} catch(err) {
				var message = '';
			}

			if (message)
			{
				myDiv = $(ids[i-1]);
				myDiv.value = message;

				if (check)
				{
					count++;
				}
				if (!message.match(/^(\s+)?[a-zA-Z]+(\s+)?$/))
				{
					nonalpha = true;
				}
			}
		}
	}

	if (count > 0)
	{
		var answer = confirm("You have made changes to your spellings without updating them.  Do you wish to proceed and lose any changes you have made?");

		if (answer)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	else if (nonalpha)
	{
		alert("Your spelling(s) includes a symbol or character that is not one of the 26 letters of the alphabet. Please change your spelling(s) to include only the 26 letters of the alphabet.");
	}
	else
	{
		f = document.forms[0];
		f.submit();
		return true;
	}
}

function focusFlash(id)
{
	var flashMovie = getFlashMovieObject(id); // alert(flashMovie);
}

function convertToFlash(i, id)
{
	// blp
	var params = '';
	params += 'action=convert-to-flash';
	params += '&i=' + i;
	params += '&id=' + id;

	var ajaxUrl;
	ajaxUrl = "/test/test.php";

	var opt = {
		method: 'post',
		asynchronous: true,
		postBody: params,
		evalScripts: true
	}

	var update_div;
	update_div = 'my' + i;

	new Ajax.Updater(update_div, ajaxUrl, opt);
}

function backButtonOverride()
{
	setTimeout("backButtonOverrideBody()", 1);
}

function backButtonOverrideBody()
{
	// Works if we backed up to get here
	try {
		history.forward();
	} catch (e) {
		// OK to ignore
	}
	// Every quarter-second, try again. The only
	// guaranteed method for Opera, Firefox,
	// and Safari, which don't always call
	// onLoad but *do* resume any timers when
	// returning to a page
	setTimeout("backButtonOverrideBody()", 500);
}

function loadAdministerPage(speller_id)
{
	var params = '';
	params += 'action=load-administer-page';
	params += '&speller_id='+speller_id;

	var ajaxUrl;
	ajaxUrl = "/test/test.php";

	var update_div;
	update_div = 'administer';

	var opt = {
		method: 'post',
		asynchronous: true,
		postBody: params,
		evalScripts: true
	}

	new Ajax.Updater(update_div, ajaxUrl, opt);
}

function getSpellingConfirmation(speller_number)
{
	var params = '';
	params += 'action=spelling-confirmation';
	params += '&speller_number=' + speller_number;

	var ajaxUrl;
	ajaxUrl = "/test/test.php";

	var update_div;
	update_div = 'administer';

	var opt = {
		method: 'post',
		asynchronous: true,
		postBody: params,
		evalScripts: true
	}

	new Ajax.Updater(update_div, ajaxUrl, opt);

	/*var params = '';
	params += 'action=reload-spellers';

	var ajaxUrl = '/test/test.php';
	var update_div = 'spellers';

	var opt = {
		method: 'post',
		asynchronous: true,
		postBody: params,
		evalScripts: true
	}

	new Ajax.Updater(update_div, ajaxUrl, opt);*/
}

function submitAddlRound(id,spelling,round)
{
	var params = '';
	params += 'action=submit-test';
	params += '&id=' + id;
	params += '&spelling=' + spelling;
	params += '&round=' + round;

	var ajaxUrl = "/test/test.php";

	var update_div = 'wordInfo';

	var opt = {
		method: 'post',
		asynchronous: true,
		postBody: params,
		evalScripts: true
	}

	new Ajax.Updater(update_div, ajaxUrl, opt);
}

function checkSampleTaken(speller)
{
	var params = '';
	params += 'action=check-sample-taken';
	params += '&speller_id=' + speller;

	var ajaxUrl = "/test/test.php";

	var update_div;
	update_div = 'test';

	var opt = {
		method: 'post',
		asynchronous: true,
		postBody: params,
		evalScripts: true
	}

	new Ajax.Updater(update_div, ajaxUrl, opt);
}
