/*
 * Config
 */
CORRECT_ID = "correto";
INCORRECT_ID = "incorreto";
CORRECT_COLOR = "#0032c2";
INCORRECT_COLOR = "#ff0000";
TAGNAME_USED_FOR_ALTERNATIVES = "span";
TEXTO_TOTAL_ACERTOS = "<b>Your score:</b><br>%NUM_ACERTOS% out of %NUM_QUESTOES%<BR><BR>";
ID_TOTAL_ACERTOS    = "total";
/*
 * Objetos
 */
Simulado = new Object();
Simulado.Questoes = new Array();

function Questao(txt)
{
	this.Pergunta = txt;
	this.Alternativas = new Array();
}

function Alternativa(txt,correto,motivo)
{
	this.texto = txt;
	this.isCorreto = correto;
	this.motivo = motivo;
}


/*
 * Funcoes uteis
 */
function mostraQuestoes()
{
    document.write("<font size=2 face=\"verdana,arial\">");

	for (i=0; i<Simulado.Questoes.length; i++)
	{
		document.write("<form name=frm" + i + " onsubmit='return false'>");
		document.write("<b><div style='float:left;text-align:center;font-family:arial;width:30px;font-size:20px;background-color:#f3b800;color:#ffffff;padding:3px 0;margin-right:5px'>" + (i+1) + "</div>" + Simulado.Questoes[i].Pergunta + "</b><br class=a10nb clear=left><br class=a10nb>");

		for (z=0;z<Simulado.Questoes[i].Alternativas.length;z++)
			document.write("<" + TAGNAME_USED_FOR_ALTERNATIVES + " id='" + (Simulado.Questoes[i].Alternativas[z].isCorreto?CORRECT_ID:INCORRECT_ID) + "' style='font-family:arial;margin-left:13px'><input type='radio' name='alternativa'> " + Simulado.Questoes[i].Alternativas[z].texto + "</" + TAGNAME_USED_FOR_ALTERNATIVES + "><br>")

		document.write("</form><div style='border:solid #ece8d6;border-width:1px 0 0 0;margin:15px 0'></div>");
	}

	document.write("</font>");
}


function confereSimulado()
{
	objs = document.getElementById('alts').getElementsByTagName(TAGNAME_USED_FOR_ALTERNATIVES);

	for (i=0;i<objs.length;i++)
	{
		if (objs[i].id == CORRECT_ID){
			objs[i].style.color = CORRECT_COLOR;
			objs[i].style.fontWeight = "bold";
		}
	}

	acertos = contaAcertos();

	divAcertos = document.getElementById(ID_TOTAL_ACERTOS);
	txt = TEXTO_TOTAL_ACERTOS;
	txt = txt.replace("%NUM_ACERTOS%",acertos);
	txt = txt.replace("%NUM_QUESTOES%",Simulado.Questoes.length);

	divAcertos.innerHTML = txt;
	divAcertos.style.display = "block";
	document.getElementById('attention').style.display = "block";
	window.scrollTo(0,0);
}

function contaAcertos()
{
	acertos  = 0;
	objs = document.getElementById('alts').getElementsByTagName(TAGNAME_USED_FOR_ALTERNATIVES);
	num_alternativa = 0;

	for (i=0;i<Simulado.Questoes.length;i++)
	{
		//Busca alternativa correta
		correta = -1;
		for (p=0;p<Simulado.Questoes[i].Alternativas.length;p++)
		{
			if (Simulado.Questoes[i].Alternativas[p].isCorreto)
				correta = p;
		}


		//Busca selecionado pelo usuário
		AsAlternativas = eval("document.frm"+i+".alternativa");
    TotAlternativas  = AsAlternativas.length;
		selecionado = -1;
		for (r=0; r<TotAlternativas; r++)
		{
			if (AsAlternativas[r].checked)
			{
				selecionado = r;
				break;
			}
		}

		//verifica se o usuario acertou
		if (correta == selecionado)
		{
			acertos += 1;
		}
		else
		{
			if (selecionado > -1) {
				objs[num_alternativa + selecionado].style.color = INCORRECT_COLOR;
				objs[num_alternativa + selecionado].style.fontWeight = "bold";
			}
		}

		num_alternativa += Simulado.Questoes[i].Alternativas.length;

	}
		w = 0;
		for (y=0; y<Simulado.Questoes.length; y++)
		{
			for (v=0;v<Simulado.Questoes[y].Alternativas.length;v++)
			{
				if (objs[w].style.fontWeight == "bold") {
					checado = 0;
					if (objs[w].getElementsByTagName('input')[0].checked == true) checado = 1;
					if (Simulado.Questoes[y].Alternativas[v].motivo != "")
						objs[w].innerHTML += "<font style=font-weight:normal> (" + Simulado.Questoes[y].Alternativas[v].motivo + ")</font>";
					if (checado == 1)
						objs[w].getElementsByTagName('input')[0].checked = true;
				}
				w++;
			}
		}
	document.getElementById('enviar').style.display = "none";
	return acertos;
}