function listLinks () {
    i = 1;
    document.write("<ul>");
    
    while (getLink(i)) {
  
	  linkInfo = getLink(i);

	  url = getLinkURL(linkInfo);
	  name = getName(linkInfo);
	 
	  putLink(name,url);
	
	  i++;
    }
    document.write("</ul>");
}
function putLink(name,url) {
	
	document.write("<li><a href=\"" + url + "\">"  + name + "</a></li>");
}
function getLinkURL(linkInfo) {
	return getParameter(linkInfo,2);	
}
function getName(linkInfo) {
	return getParameter(linkInfo,1);	
}
function getParameter(parameterList,pN) {

	index = parameterList.indexOf('#');

	if ((index == -1) && (pN == 1)) {
		parameter = parameterList;
	}
	else if ((index != -1) && (pN > 0)) {
	
		parameter = parameterList.substring(0,index);
		rest = parameterList.substring(index+1,parameterList.length);
		
		if (pN != 1)
			parameter = getParameter(rest,pN-1);
	}
	else	
		parameter = null;
		
	return parameter;
}
function listQuiz () {
    i = 1;
    while (getQuizInfo(i)) {
  
	
	  quizInfo = getQuizInfo(i);

	  title = getTitle(quizInfo);
	  url = getURL(quizInfo);
	  
	  putTest(title,url);
	  
	
	  i++;
    }
}
function listQuizKrusher () {
    i = 1;
    while (getQuizInfoKrusher(i)) {

	  quizInfo = getQuizInfoKrusher(i);

	  title = getTitle(quizInfo);
	  url = getURL(quizInfo);
	  
	  putTestKrusher(title,url,i);
	  i++;
    }
}

function listOtherQuizs () {
    
    document.write("<select name=\"archivo\" onchange='document.location.href=this.options[this.selectedIndex].value;'");
    document.write("<option value=\""+ location.href +"\" selected>Lista de PixQuiz</option>");
  
    i = 1;
    while (getQuizInfo(i)) {
	
	  quizInfo = getQuizInfo(i);

	  title = getTitle(quizInfo);
	  url = getURL(quizInfo);
	 
	  document.write("<option value='" + url + "'>" + title + "</option>");	 
	
	  i++;
    }
    document.write("</select>");
	
}


function putTest(title,url) {
	portada = getDir(url) + "imagen_1.jpg";
	document.write("<li><a href=\"" + url + "\"><img src=\"" + portada + "\" alt=\"" + title +"\" title=\""+ title +"\">"+ title +"</a></li>");	
}
function putTestKrusher(title,url,i) {
	portada = "kquiz/" + i + ".jpg";
	document.write("<li><a href=\"" + url + "\"><img src=\"" + portada + "\" alt=\"" + title +"\" title=\""+ title +"\">"+ title +"</a></li>");
	
}
function getTitle(info) {
	return getParameter(info,1);
}

function getURL(info) {
	return getParameter(info,2);
}

function getDir(url) {
	return url.substring(0,url.lastIndexOf("/")+1);
}


function getParameter(parameterList,pN) {

	index = parameterList.indexOf('#');

	if ((index == -1) && (pN == 1)) {
		parameter = parameterList;
	}
	else if ((index != -1) && (pN > 0)) {
	
		parameter = parameterList.substring(0,index);
		rest = parameterList.substring(index+1,parameterList.length);
		
		if (pN != 1)
			parameter = getParameter(rest,pN-1);
	}
	else	
		parameter = null;
		
	return parameter;
}
//QUIZ

function showQuiz () {
	showTitle();
	showQuestions();
	showSocialNetworks();
}
function showSocialNetworks() {
	
	urlFacebook = "http://www.facebook.com/sharer.php?u=" + location.href; 
	urlTwitter = "http://twitter.com/home?status=" + location.href;
	urlDelicious = "http://delicious.com/save?title=PixQuiz&url=" + location.href;

	document.write("<h2>Compartir PixQuiz</h2>");
	
	document.write("<p>Puedes compartir este PixQuiz con tus amigos en las redes sociales y competir a ver quién obtiene la mejor puntuación.</p>");
	
	
	document.write("<table width=\"100%\" border=\"0\" cellspacing=\"2\" cellpadding=\"0\">");
	
	document.write("<tr>");
	
		document.write("<td><p align=\"center\"><a href=\"" + urlFacebook + "\" ><img src=\"../img/social/facebook.png\" /></a></p></td>");
		document.write("<td><p align=\"center\"><a href=\"" + urlTwitter + "\" ><img src=\"../img/social/twitter.png\" /></p></td>");
		document.write("<td><p align=\"center\"><a href=\"" + urlDelicious + "\" ><img src=\"../img/social/delicious.png\" /></p></td>");
		
	document.write("</tr>");
	document.write("<tr>");
	
		document.write("<td><p align=\"center\"><a href=\"" + urlFacebook + "\" >Compartir en Facebook</a></p></td>");
		document.write("<td><p align=\"center\"><a href=\"" + urlTwitter + "\" >Publicar en Twitter</a></p></td>");
		document.write("<td><p align=\"center\"><a href=\"" + urlDelicious + "\" >Añadir a Del.icio.us</a></p></td>");
		
	document.write("</tr>");
	
	document.write("</table>");
	
	
}

function showQuestions() {
	
	   i = 1;
	   j = 1;
	   k = 1;
	   col = 4;
	   row = 8;
	   imagen = "";
	  
	   document.write("<form name=\"formulario\" id=\"formulario\">");
	   
	   document.write("<div id=\"questions\">");
	   document.write("<table width=\"100%\" border=\"0\" cellspacing=\"2\" cellpadding=\"0\">");


	   while (j <= row) {
	   	
		document.write("<tr>");
		
		while (i <= col) {
			
		    document.write("<td>");
		   	imagen = "imagen_" + k + ".jpg";
		    	document.write("<p align=\"center\"><img src=\""+ imagen +"\" width=\"125\" height=\"125\" /></p>");
		    
		    	document.write("<p align=\"center\"><label><input type=\"text\" name=\"textBox" + k + "\" id=\"textBox" + k + "\" onClick=\"checkUserReply(this," + k + ")\" onChange=\"checkUserReply(this," + k + ")\" onKeyUp=\"checkUserReply(this," + k + ")\" /></label></p>"); 
		    
		    document.write("</td>");	

		    k++;
		    i++;
		}
		
		document.write("</tr>");
		
		j++;
		i=1;   
	   }
	   document.write("</table>");
	   document.write("</div>");
	   showResults();
	   document.write("</form>");

}
function showResults() {


	   document.write("<h2>Resultados</h2>");
	   
	   document.write("<div align=\"center\">");
		 
		 document.write("<table width=\"50%\" border=\"0\" cellspacing=\"2\" cellpadding=\"0\">");
		     document.write("<tr><td><p align=\"center\"><img src=\"../img/results/bien.png\" alt=\"aciertos\" /></p></td><td><p align=\"center\"><label><input type=\"text\" readonly=\"readonly\" name=\"textBox_Aciertos\" id=\"textBoxAciertos\" /></label></p></td></tr>");
		     document.write("<tr><td><p align=\"center\"><img src=\"../img/results/mal.png\" alt=\"fallos\" /></p></td><td><p align=\"center\"><label><input type=\"text\" readonly=\"readonly\" name=\"textBox_Fallos\" id=\"textBoxFallos\" /></label></p></td></tr>");	
		     document.write("<tr><td><p align=\"center\"><img src=\"../img/results/stats.png\" alt=\"porcentaje\" /></p></td><td><p align=\"center\"><label><input type=\"text\" readonly=\"readonly\" name=\"textBox_Porcentaje\" id=\"textBoxPorcentaje\" /></label></p></td></tr>");	 
		 document.write("</table>");		   
					 
	   document.write("</div>");
	
}

function checkUserReply (textArea,id) {
	
    userReply = textArea.value;
    
   
    if (isSol(userReply,id)) {
	
	textArea.style.backgroundColor = "#00CC00";
	
    } else {

	textArea.style.backgroundColor = "#FF0000";

    }
    
    checkScores();    
   
}

function checkScores() {

	acertadas=0;
	
	for (i=0;(ele=document.formulario.elements[i]);i++) {
		if (i < 32) {	
			if ((ele.style.backgroundColor) && (ele.style.backgroundColor == "rgb(0, 204, 0)"))		
					acertadas++;
		}
		else if (i == 32) {
			
			ele.value = acertadas;
		}
		else if (i == 33) {
			ele.value = 32-acertadas;	
		}
		else if (i == 34) {
			
			ele.value = (acertadas * 100 / 32) + "%";
		}	
	}	
}
function isSol(userReply,id) {
	
	solList = getSolList(id);	
	i=1;
	
	while (getParameter(solList,i) != null) {
		solution = getParameter(solList,i);	
		
		if (solution.toUpperCase() == userReply.toUpperCase())
			return true;
		i++;	
	}
	return false;
}
function getParameter(parameterList,pN) {

	index = parameterList.indexOf('#');

	if ((index == -1) && (pN == 1)) {
		parameter = parameterList;
	}
	else if ((index != -1) && (pN > 0)) {
	
		parameter = parameterList.substring(0,index);
		rest = parameterList.substring(index+1,parameterList.length);
		
		if (pN != 1)
			parameter = getParameter(rest,pN-1);
	}
	else	
		parameter = null;	
	return parameter;
}	



