// JavaScript Document
var currentQ = 1;

var r = new Array();
r[1] = "¿Cómo <strong> te </strong> llamas?";
r[2] = "Mis primas <strong> trabajan </strong> en una tienda.";
r[3] = "Perdone. ¿Hay <strong> una </strong> farmacia por aquí?";
r[4] = "Susana <strong> tiene </strong> cuarenta y dos años.";
r[5] = "Tengo los ojos <strong> verdes </strong> y el  pelo <strong> rubio </strong>.";
r[6] = "<strong> Son </strong> las dos y cuarto.";
r[7] = "Todos los sabados <strong> me acuesto </strong> tarde.";
r[8] = "Hoy <strong> estoy </strong> muy cansado.";
r[9] = "Los fines de semana mi amigo visita a <strong> sus </strong> hermanos.";
r[10] = "María compra carne <strong> una vez al mes </strong>.";
r[11] = "Esta película es muy buena pero la otra es <strong> mejor </strong>.";
r[12] = "Mira, está <strong> lloviendo </strong>. ¿Tienes un paraguas?";
r[13] = "Luís quiere ser abogado. Por eso <strong> tiene que </strong> estudiar mucho.";
r[14] = "¡Al fin! Juan y Ana <strong> han llegado </strong> a casa.";
r[15] = "Si <strong> estudias </strong> mucho seguro pasarás el examen.";
r[16] = "Cuando vivíamos en Barcelona, <strong> tuvimos </strong> que aprender Catalán.";
r[17] = "Estaba leyendo el periódico cuando de repente, <strong> llamaron </strong> a la puerta.";
r[18] = "El continente americano <strong> fue descubierto </strong> por Cristóbal Colón.";
r[19] = "No creo que Laura <strong> venga </strong>.";
r[20] = "Estaba nerviosa porque nunca <strong> había hecho </strong> un ejercicio tan complicado.";
r[21] = "¿Puedo abrir la ventana?  Sí, claro, <strong> ábrela </strong>.";
r[22] = "Si <strong> tuviera </strong> más tiempo viajaría a muchos países.";
r[23] = "Las calles por <strong> las cuales </strong> pasábamos eran anchas y oscuras.";
r[24] = "Si yo <strong> sabía </strong> que eso iba a suceder, no habría venido.";
r[25] = "Pensamos ir de vacaciones cuando <strong> hayamos </strong> aprobado el examen.";


var niveles = new Array();
niveles[1] = "Beginner";
niveles[2] = "Elementary";
niveles[3] = "Intermediate I";
niveles[4] = "Intermediate II";
niveles[5] = "Advanced";

var correctchoices=new Array()
correctchoices[1]='c' 
correctchoices[2]='a' 
correctchoices[3]='b'
correctchoices[4]='c'
correctchoices[5]='a'

correctchoices[6]='c'
correctchoices[7]='b'
correctchoices[8]='c'
correctchoices[9]='d'
correctchoices[10]='c'

correctchoices[11]='b'
correctchoices[12]='b'
correctchoices[13]='b'
correctchoices[14]='b'
correctchoices[15]='b'

correctchoices[16]='a'
correctchoices[17]='d'
correctchoices[18]='b'
correctchoices[19]='a'
correctchoices[20]='c'

correctchoices[21]='c'
correctchoices[22]='d'
correctchoices[23]='d'
correctchoices[24]='a'
correctchoices[25]='c'



function limpiarTodo(){
	esconderPreguntas(5);

	document.getElementById("resultado").style.display = "none";
	
	document.getElementById("btnAvanzar").style.display="none";
	document.getElementById("btnRetry").style.display="none";
	document.getElementById("btnGrade").style.display="none";
	
	document.getElementById("level").style.display="none";
	document.getElementById("correctas").style.display = "none";
}

/* Esconde todos los divs 'dificultad'+nro hasta cantNiveles.*/
function esconderPreguntas(cantNiveles){
	for(var i = 1; i<=cantNiveles; i++){
		document.getElementById('dificultad'+i).style.display = 'none';
	}
}
/* Muestra las preguntas de un nivel */
function mostrarPreguntas(nivel){
	document.getElementById('dificultad'+nivel).style.display = 'inline';
}

/* Muestra el texto de en q nivel se esta */
function mostrarNivel(nivel){
	document.getElementById('level').innerHTML = niveles[nivel] +" Level";
	document.getElementById("level").style.display="inline";
}

function genLose(nivel, correctas, totales){
	var ret = "¡Lo sentimos! You have not passed the "+niveles[nivel] +" Level. <br/>";
	ret += "You have "+ correctas + " out of " + totales + " correct answers.<br/>";

	ret += "Have a look at the correct answers. <br/>";

	return ret;
}

/*Genera el texto q se muestra si se gana*/
function genWin(nivel, correctas, totales){
	var ret = "¡Muy bien! You have passed the " + niveles[nivel] +" Level. <br/>";
	ret += "You have "+ correctas + " out of "+ totales +" correct answers. <br/>" 
	ret += "Have a look at the correct answers. <br/>"

	return ret;
}

function grade(){
	
	var actualchoices = new Array();
	var incorrectas = 0;
	//Para cada pregunta de esta tanda
	for(q = ((currentQ-1)*5)+1; q<=5*currentQ;q++){
		var thequestion=eval("document.myquiz.question"+q); 
		
		//revisa las opciones y llena actualchoices con la elegida
		for (c=0;c<thequestion.length;c++){
			if (thequestion[c].checked==true)
				actualchoices[q]=thequestion[c].value;
		}

		
		if (actualchoices[q]!=correctchoices[q]){ //process an incorrect choice
			incorrectas += 1;
		}
	}


	if (incorrectas>2){			//Si hay mas de 2 incorrectas
		texto  = genLose(currentQ, 5-incorrectas,5);	//Mostramos texto perdedor
		document.getElementById("btnAvanzar").style.display="none";
		document.getElementById("btnRetry").style.display="inline";
	}
	else{						//sino
		texto = genWin(currentQ, 5-incorrectas,5);		//Mostramos texto ganador
		if(currentQ!=5)
			document.getElementById("btnAvanzar").style.display="inline";
		document.getElementById("btnRetry").style.display="none";
	}
	esconderPreguntas(5);
	document.getElementById("btnGrade").style.display="none";
	document.getElementById("resultado").innerHTML = texto;
	document.getElementById("resultado").style.display = "inline";
	document.getElementById("correctas").innerHTML = genCorrectas(currentQ);
	document.getElementById("correctas").style.display = "inline";
}

function genCorrectas(nivel){
	var ret = "";
	for(q = ((nivel-1)*5)+1; q<=5*nivel;q++){
		ret += r[q] + "<br/>";

	}
	return ret;
}

function avanzarNivel(){
	currentQ +=1;
	limpiarTodo();
	mostrarNivel(currentQ);
	mostrarPreguntas(currentQ);
	document.getElementById("btnGrade").style.display="inline";
	
}

function retry(){
	currentQ -= 1;
	avanzarNivel();
}

/*
function grade(){
	for(q=((currentQ-1)*5)+1;q<5*currentQ;q++){
		var thequestion=eval("document.myquiz.question"+q)
		for (c=0;c<thequestion.length;c++){
			if (thequestion[c].checked==true)
			actualchoices[q]=thequestion[c].value
		}
		
		if (actualchoices[q]!=correctchoices[q]){ //process an incorrect choice
			if (incorrect==null)
				incorrect=q
			else
				incorrect+="/"+q
			}
		}
	}
	
	currentQ = currentQ + 1 ;
}
	*/

