// JavaScript Document

function validate(field, text){
	with (field){
		if(field.value==null||field.value==""){
			(alert(text));
			return false;
		}
		else{
			return true;
		}
	}
}

//validacion de formas
function validateForm(form){
	for(var i=0;i<form.elements.length;i++){
		//var elementName=form.elements[i].name;
		//var elementValue=form.elements[i].value;
		if(form.elements[i].checked){
			return true;
		}
	}
	alert("Ay chihuahua! You need to pick an answer before going to the next question");
	return false;
}

function getValores(form){
	str="";
	for(var i=0;i<form.elements.length;i++){//cuenta el numero de elementos en la forma
		if((form.elements[i].type=='radio' && form.elements[i].checked)){
			str+= form.elements[i].value
		}
	}
	return parseInt(str);
}

var ranking = 0;
var question_num = 1;
var question;

function nextQuestion(form){
	if (validateForm(form)==false){
		return false;
	}
	ranking = ranking+getValores(form);
	var q1 = document.getElementById('question_'+question_num);
	if(question_num<7){
		createQuestion(q1,form);
	}
	if(question_num==6){
		var button = document.getElementById('next');
		button.value = 'Final question >';
	}
	if(question_num==7){
		var button = document.getElementById('next');
		button.value = 'Show me my results!';
		button.setAttribute('onclick', 'showResults(this.form)');
	}
}

function createQuestion(prev_question,form){
	var nextQuestion_num = ++question_num;
	var questions_array = questionsArray(nextQuestion_num);
	var prev_ol = form.getElementsByTagName('ol');
	olParent = prev_ol[0].parentNode;
	olParent.removeChild(prev_ol[0]);
	var ol = document.createElement('ol');
	ol.type = 'a';
	for(var i = 0;i<questions_array.length;i++){
		var li = document.createElement('li');
		var input = document.createElement('input');
		input.type = 'radio';
		input.name = 'question_'+nextQuestion_num;
		input.value = questions_array[i][1];
		var answer_text = questions_array[i][0];
		var input_text = document.createTextNode(answer_text);
		//li.appendChild(input);
		//li.appendChild(input_text);
		ol.innerHTML += '<li><input type="radio" name="question_'+nextQuestion_num+'" value="'+questions_array[i][1]+'" />'+questions_array[i][0]+'</li>';
		//ol.appendChild(li);
	}	
	var p = document.createElement('p');
	p.id = 'question_'+nextQuestion_num;
	var question_text = question;
	p_text = document.createTextNode(question_text);
	//p.appendChild(p_text);
	p.innerHTML = question_text;
	p.appendChild(ol);
	prev_question = form.replaceChild(p,prev_question);
}

function questionsArray(question_num){
	var question_answers = new Array(4);
	for (var i = 0;i<question_answers.length;i++){
		question_answers[i] = new Array(2);
	}
	switch(question_num){
		case 2:
			question = "You hear a strange knock at the door. What do you do?";
			question_answers = [["I peek out the window to see if it's a skeleton","1"],
				["I just open the door -it could be my abuelita with the laundry and my freshly pressed cholo pants.","0"],
				["I send someone else, preferably small and na&iuml;ve, like my Chihuahua dog, to go out and investigate","3"],
				["I rush to start cleaning my house","2"]];
			break;
		case 3:
			question = "Sr. Calavera, a dainty skeleton wearing a fedora has found you, and he is signaling for you to come along with him. What do you do?";
			question_answers = [["I hide inside the refrigerator","1"],
				["I hit him with a broom","2"],
				["I wave good-bye to my friends and go with him right away","0"],
				["I invite him to my house; then I tell him I need to finish baking my chocolate birthday cake","3"]];
			break;
		case 4:
			question = "You bumped into Se&ntilde;or Calavera, and you realize he is on the way to visit your dearest loved one, what would you do?";
			question_answers = [["I'd try to remember the Spanish alphabet","1"],
				["I would stick my foot out to make him trip","2"],
				["I'd ask Se&ntilde;or Calavera if he is bringing a present","3"],
				["I'd offer to show him the way","0"]];
			break;
		case 5:
			question = "You are walking down the street and in the distance you see Se&ntilde;or Calavera coming for you. Where would you run?";
			question_answers = [["I would run to the corner store, jump behind the counter, and pretend I am Pepe selling chorizos","2"],
				["I would rush to the nearest Halloween store and put on a cute Chihuahua costume","3"],
				["I will run to my abuelita's house and hide inside her giant tamale pot","1"],
				["I wouldn't run, because my cholo pants might fall down","0"]];
			break;
		case 6:
			question = "If you knew Se&ntilde;or Calavera was coming today to pick up your old abuelita, would you turn off your computer and ride your bicycle across town to try to convince Se&ntilde;or Calavera to give her another year of life?";
			question_answers = [["Yes, I could probably make it there before it is too late","3"],
				["No, my abuelita is too old already, ya pa'que?","0"],
				["My abuelita knows Karate and she doesn't need my help","1"],
				["My abuelita makes incredibly delicious tamales; I would go to her house any time","2"]];
			break;
		case 7:
			question = "If by accident you have agreed to go with Se&ntilde;or Calavera to the afterlife, what would you do next?";
			question_answers = [["I would try to escape while he is looking at his watch","1"],
				["I would talk him into going to my abuelita's house instead. She makes incredible tamales","2"],
				["I would trade my life back for teaching him how to count in Spanglish","3"],
				["I would still go with Se&ntilde;or Calavera. It might be a nice ride","0"]];
			break;
		case 8:
			return 0;
	}
	return question_answers;
}

function showResults(form){
	if (validateForm(form)==false){
		return false;
	}
	ranking = ranking+getValores(form);
	var div_result = document.createElement('div');
	div_result.id = 'resultados';
	var p = document.createElement('p');
	var image = document.createElement('img');
	var sm_image = document.createElement('img');
	if(ranking<=6){
		div_result.innerHTML = '<h1>'+ranking+'</h1> <p>You mean, you are still alive?</p>';
		image.src = '../images/badge5.gif';
		sm_image.src = '../images/badge_5sml.gif';
		//text ='<h1>'+ranking+'</h1> You mean, you are still alive?';
	}
	else if(ranking>6 && ranking<=10){
		div_result.innerHTML = '<h1>'+ranking+'</h1> <p>Nah, you couldn\'t trick anybody. Buy your coffin now.</p>';
		image.src = '../images/badge4.gif';
		sm_image.src = '../images/badge_4sml.gif';
		//text = '<h1>'+ranking+'</h1> Nah, you couldn\'t trick anybody. Buy your coffin now';
	}
	else if(ranking>10 && ranking<=14){
		div_result.innerHTML = '<h1>'+ranking+'</h1> <p>You wont last long. Have your birthday party now - of course it is not your birthday yet, but have it while you can.</p>';
		image.src = '../images/badge3.gif';
		sm_image.src = '../images/badge_3sml.gif';
		//text = '<h1>'+ranking+'</h1>You won\'t last long. Have your birthday party now—of course it is not your birthday yet, but have it while you can';
	}
	else if(ranking>14 && ranking<=18){
		div_result.innerHTML = '<h1>'+ranking+'</h1> <p>Go ahead and finish your school homework for tomorrow. Se&ntilde;or Calavera thinks you might trick him into living long enough to regret it if you don\'t.</p>';
		image.src = '../images/badge2.gif';
		sm_image.src = '../images/badge_2sml.gif';
		/*text = '<h1>'+ranking+
		'</h1> Go ahead and finish your school homework for tomorrow. Se&ntilde;or Calavera thinks you might trick him into living long enough to regret it if you don\'t.';*/
	}
	else if(ranking>18 && ranking<=21){
		div_result.innerHTML = '<h1>'+ranking+'</h1> <p>Congratulations, you could trick your own grandma! Expect to live forever!</p>';
		image.src = '../images/badge1.gif';
		sm_image.src = '../images/badge_1sml.gif';
		/*image.src = '../images/badge_1';
		text = '<h1>'+ranking+'</h1> Congratulations, you could trick your own grandma! Expect to live forever!';*/
	}
	//image.style.position = 'relative';
	//image.style.cssFloat = 'left';
	var text = 'Copy and paste the HTML code below into your blog or profile to show everyone your trickster abilities!';
	p_text = document.createTextNode(text);
	p.appendChild(p_text);
	div_result.appendChild(p);//adds text to result div
	
	var div_bigBadge = document.createElement('div');
	div_bigBadge.id = 'bigBadge';
	div_bigBadge.appendChild(image);
	var textarea = document.createElement('textarea');
	textarea.style.marginTop = '10px';
	var html_text = '<a href="http://www.srcalavera.com/quiz" target="_blank"><img src="'+image.src+'" width="320" height="360" border="0" alt="trickster badge"/></a>';
	var textarea_textNode = document.createTextNode(html_text);
	textarea.appendChild(textarea_textNode);
	//textarea.innerHTML = '<a href="http://www.srcalavera.com/quiz"><img src="'+image.src+'" width="320" height="360" alt="trickster badge"/></a>';
	textarea.rows = '4';
	textarea.cols = '40';
	div_bigBadge.appendChild(textarea);
	div_result.appendChild(div_bigBadge);
	
	var div_smallBadge = document.createElement('div');
	div_smallBadge.id = 'smallBadge';
	div_smallBadge.appendChild(sm_image);
	var sm_textarea = textarea.cloneNode(true);
	sm_textarea.innerHTML = '<a href="http://www.srcalavera.com/quiz" target="_blank"><img src="'+sm_image.src+'" width="180" height="203" border="0" alt="trickster badge small"/></a>';;
	sm_textarea.cols = '30';
	sm_textarea.rows = '8';
	div_smallBadge.appendChild(sm_textarea);
	div_result.appendChild(div_smallBadge);
	
	
	//div_result.appendChild(image);
	var quiz = document.getElementById('quiz');
	var quizParent = quiz.parentNode;
	quiz = quizParent.replaceChild(div_result, quiz);
	
}
