// Script checks form for answers, builds a GET-style query, passes it to showUser function, which uses AJAX to handle form submission and display of results. It also hides the original quiz.

var textstring = '';
var elementsChecked = 0;

function hideIt(id)
{
	document.getElementById(id).style.display="none";
}

function showIt(id)
{
	document.getElementById(id).style.display="inline";
}

function checkmine()
{
	// Gather the values of the radio buttons in the quiz, then build a query string out of them.
	
	// First, how many elements are there total? Minus the submit button, of course.
	
	var x = (document.forms['happinessQuiz'].elements.length) - 1;

	// Get value of the radio buttons, counting up to the total elements (x, defined above).
	
	user_input = '';
	for (i=0;i<x;i++) {
		if (document.forms['happinessQuiz'].elements[i].checked) {
			elementsChecked++; // Now build that query string.
			user_input += '&' + document.forms['happinessQuiz'].elements[i].name + '=' + document.forms['happinessQuiz'].elements[i].value;
		}
	}
	textstring += 'Last: ' + user_input + '\n';
}


function showUser(str)
{
if (str=="")
  {
  document.getElementById("txtHint").innerHTML="";
  return;
  }
if (elementsChecked!=15)
  {
  document.getElementById("txtHint").innerHTML="<p><strong><span style=\"color: #FF0000;\">Please answer all questions ("+elementsChecked+" out of 15 have been answered so far).</span></strong></p>";
  elementsChecked = 0;
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
	hideIt('happinessQuiz'); // Hide the quiz.
	showIt('sharebuttons'); // Show the share buttons
	window.location.hash='top'; // Move us up to the top of the page.
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","http://www.raisinghappiness.com/wp-content/themes/raisinghappiness/checkquiz.php?q=1"+str,true);
xmlhttp.send();
}
