addLoadEvent(function() {
	CONTEST_DETAILS.inicio();
});

function participateResults(jsonData)
{
	$("#mainQuestionsArea").hide('slow', function()
	{
		if(jsonData['result'] == 'ok'){
			$("#resultaMsgArea").show('slow');
		}
		else{
			document.location.href = "/portada/entrar";
		}
	});
}

var allQuestionCompleted = true;

var allClosedQuestions = 0;
var answeredClosedQuestions = 0;

var multipleQuestions = false;
var allMultipleQuestions = true;

CONTEST_DETAILS = {

	inicio : function()
	{
			
		$("#answerQuestionBtn").click(function()
		{
			if($(this).attr('class') != ''){
				document.location.href = $(this).attr('class');
				return false;
			}

			allQuestionCompleted = true;
			
			allClosedQuestions = 0;
			answeredClosedQuestions = 0;

			$(".freeQuestion").each(function()
			{
				if($(this).attr('value') == '')
				{
					$(this).focus();
					allQuestionCompleted = false;
				}
			});
			
			$(".closedQuestions").each(function()
			{
				allClosedQuestions++;
				for(i=0;i<document.forms['contestFrm'].elements[$(this).attr('id')].length;i++){
					if(document.forms['contestFrm'].elements[$(this).attr('id')][i].checked == true){
						answeredClosedQuestions++;
					}
				}
			});
			
			$(".multipleQuestions").each(function()
			{
				multipleQuestions = false;
				$(":checkbox[name^=" + $(this).attr('id') + "]").each(function()
				{
					if($(this).attr('checked')){
						multipleQuestions = true;
					}
				});
				
				if(multipleQuestions == false){
					allMultipleQuestions = false;
				}
				else{
					allMultipleQuestions = true;
				}
			});
			
			
			if((allQuestionCompleted == true) && (answeredClosedQuestions == allClosedQuestions) && (allMultipleQuestions)){
				sendPost('contestFrm', '/ajax/contests/participate/', participateResults, ajaxError); 
			}
			else{
				ShowMessage('Debes contestar a todas las preguntas para participar en el concurso. Muchas gracias!');
			}
			return false;
		});
		return false;
	}
}
