function submitForm(formId, message, errorStrings)
{
	var val = new FormValidator(formId);
	if(typeof(errorStrings) != 'undefined')
	{
		var errors = errorStrings.split(",");
		for(var i = 0; i < errors.length; i++)
		{
			val.options.errorStrings[i] = errors[i];
		}
	}

	if(val.validate())
	{	
		$(formId).submit();
	} else {
		if(message != null) {
			alert(message);	
		}
	}
}

function submitQuiz(id, questionCount)
{
	if(!checkAnwers(questionCount))
	{
		new Ajax('/en-ca/take_the_quiz/xsrv_save?'+$('formQuestionaires').toQueryString(), {
			method: 'post'
		}).request();
		$('questions_incomplete').style.visibility = 'visible';
		return;
	}
	submitForm(id);
}

function skipQuiz()
{
	$("1_1").checked = true ;
	$("2_3").checked = true ;
	$("3_1").checked = true ;
	$("4_2").checked = true ;
	$("5_3").checked = true ;
	$("6_1").checked = true ;
	$("7_2").checked = true ;
	$("8_1").checked = true ;
	$("9_1").checked = true ;
	$("10_3").checked = true ;
	submitQuiz('formQuestionaires', 10);
}

function saveForLater(id)
{
	$("save_later").value = "1";
	submitForm(id);
}

function submitRegistration(formId, errorStrings)
{
	var val = new FormValidator(formId);
	var errors = errorStrings.split(",");
	for(var i = 0; i < errors.length; i++)
	{
		val.options.errorStrings[i] = errors[i];
	}
		
	if(val.validate())
	{
		checkExistingUserName(formId);
	} else {
		location.href='#top_of_form'; // there are errors - go to top of form
	}
}

function updateProfile(formId)
{
	 submitForm(formId);
}


function checkAnwers(questionCount)
{
	var radios = $$(".radios");
	var correctAnswers = $$(".answer_input");
	var validCount = 0;
	for(var i = 0; i < questionCount; i++)
	{
		var questionNum = (i+1);
		var groupName = "question_"+ questionNum;
		var radioGroup = $$('input[name='+groupName+']');
		
		
		var theCorrectAnswer = $("correctAnswer_"+ questionNum).value;
		var gotQuestionCorrect = false;
		var selectedCount = 0;
		for (j=0;j<radioGroup.length;j++)
		{
			if(radioGroup[j].checked == true)
			{
				selectedCount = 1;
				gotQuestionCorrect = (radioGroup[j].id  == theCorrectAnswer);
			}
		}
		
/* oh, hell no		if(questionNum == 6)
		{
			gotQuestionCorrect = true;
		}
*/		
		
		if(gotQuestionCorrect && selectedCount == 1)
		{

			validCount++;
			$('questionImg_' + questionNum).style.display = "block";
			$('questionBox_' + questionNum).style.display = "none";
		}
		else
		{
			$('questionImg_' + questionNum).style.display = "none";
			$('questionBox_' + questionNum).style.display = "block";
		}
	}
		
	if(validCount == questionCount) // got all questions correct
	{
		return true;	
	}
	return false;
}

function checkExistingUserName(formId)
{
		var url = base_locale_url + "register/ajaxCheckUserNameEmail/"+ $('username').value + "/" +  encodeURIComponent($('email').value) + "/";
		var ajax_call = new Ajax(url, 
		{
			method: 'get',
			onComplete: function(request)
			{
				if(request) 
				{
					$('userExist').innerHTML = request ;
						location.href='#top_of_form'; // user exists - go to top of form
				}
				else
				{
					$('userExist').innerHTML = "";	
					$(formId).submit();
				}
			}
		}).request();
}



function submitenter(myfield,e)
{
	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	else return true;
	
	if (keycode == 13)
	   {
	   myfield.form.submit();
	   return false;
	   }
	else
	   return true;
}
