// Global variables and functions that need to run as the inital page loads.

var questions = new Array();
var numquestions = 5; // for display puposes only
var count = 0;
var counter = 1;

function loader(lang) // Routine that prepares the quiz when it is first loaded.
{
  clearform(); // Clear any previous selections from form when the page is loaded.
  toggleoptions("on"); // Enable any option boxes that may have been disabled.
  openhide(); // Hide all unwanted content when the page first loads.
  questions = makerandomquestions(questions, questions.length); // Randomize questions.
  show (questions[0]); // Show the initial question.

  if(lang == "e")
  {
    var numbering = "Question " + counter + " of " + numquestions; 
  }
  if(lang == "f")
  {
   var numbering = "Question " + counter + " de " + numquestions; 
  }
  document.getElementById("counter").firstChild.data = numbering;
}

/**************************************************************

Functions that Control the navigation

**************************************************************/

function next(lang)
{
  var ischecked = validator(lang);
  var results = new Array();
  var numqstleft = questions.length;
  var pagetitle = "";
  hide("appnav");
  if (counter >= 5)
    {
      counter = 1;
      if(lang == "e")
      {
        var numbering = "Question " + counter + " of " + numquestions; 
      }
      if(lang == "f")
      {
        var numbering = "Question " + counter + " de " + numquestions; 
      }
      document.getElementById("counter").firstChild.data = numbering;
	}
  
  if(!ischecked) // If a selection wasn't made, prompt the user and do not go to the next question.
  {
    return false;
  }
  
  if((ischecked) && (count < (numquestions - 1))) // If a selection was made and the user has answered less than five questions, go to the next question.
  {
    hide (questions[count]);
    count++;
    show (questions[count]);
    counter++;
    if (lang == "e")
    {
      numbering = "Question " + counter + " of " + numquestions;
      pagetitle = "Please make a selection";
    }
    if (lang == "f")
    {
      numbering = "Question " + counter + " de " + numquestions;
      pagetitle = "Veuillez faire un choix";
    }
    document.getElementById("counter").firstChild.data = numbering;
    document.getElementById("head").firstChild.data = pagetitle;
    return false;
  }
  
  if(count == (numquestions - 1)) // Show the answers once the user has answered all five questions.
  {
    count++;
    var score = 0;
	var bigarray = new Array();
 	bigarray = makebigarray();
	
	
	//var testArray = new Array();
	//var qstArray = new Array();
	//var userPicksArray = new Array();
	//var testArray = new Array(qstArray, userPicksArray);
	
	//var x = 0;
	
	//for (var i = 0; i < bigarray[3].length; i++){
		//if(bigarray[3][i] != 0){
	 		//alert(bigarray[3][i]);
			//testArray[x] = i; //load the qst's NUMBER
			//testArray[x] = bigarray[0][i]; //load the qst's NUMBER
			//x++;
			//}
		//}

		//for (var i = 0; i < testArray.length; i++){
		
	 		//alert(testArray[i]);
	
		//}
		
		
		
	//for (var x = 0; x < testArray.length; x++)
	for (var i = 0; i < bigarray[0].length; i++)
    {
	//i = testArray[x];
	//alert("question " + testArray[x]);
      if(bigarray[3][i] != 0)
      {
        show(bigarray[0][i]); 
        show(bigarray[1][i]);
      }
      if(bigarray[2][i] == bigarray[3][i]) // A correct answer
      {
        score++;
        bigarray[4][i].className = "correct"
        if(lang == "e")
        {
          bigarray[4][i].firstChild.data = "You answered correctly";
        }
        if(lang == "f")
        {
          bigarray[4][i].firstChild.data = "Vous avez choisi la bonne réponse";
        }
        bigarray[5][i].className = "invisible"
      }
      if(bigarray[2][i] != bigarray[3][i]) // An incorrect answer
      {
        bigarray[4][i].className = "wrong"
        if(lang == "e")
        {
          bigarray[4][i].firstChild.data = "You answered incorrectly";
        }
        if(lang == "f")
        {
          bigarray[4][i].firstChild.data = "Vous n'avez pas choisi la bonne réponse";
        }
      }
    }
    var hdtwos = getalltags("h2", "invisible"); // Retrieve all invisible h2 tags
    changeclass(hdtwos, "question");
    if (lang == "e")
    {
      pagetitle = "Your Results";
      numbering = "Your score: " + score + " of 5"
	  /********/
      document.getElementById("nextbtn").value = "Take a New Test";
	  document.getElementById("nextbtn").style.width = '150px'; //Added P Smith Feb 15, 06
    }
    if (lang == "f")
    {
      pagetitle = "Résultats";
      numbering = "Votre résultat: " + score + " sur 5"
      document.getElementById("nextbtn").value = "Nouveau test";
	  document.getElementById("nextbtn").style.width = '150px'; //Added P Smith Feb 15, 06
    }
    document.getElementById("counter").firstChild.data = numbering;
    document.getElementById("head").firstChild.data = pagetitle;
    
	//Added P Smith Feb 17, 06 - Show little results arrow when test is over
	//document.getElementById("result_image").style.display = 'block';
	
    styleanswers();
    toggleoptions("off");
    show("appnav");
    return false;
  }
  
  if(count == numquestions)
  {
    if (questions.length >= 10)
    {
      questions = questions.slice(5); // Remove the first five values from array so that questions are not repeated.
      var bigarray = new Array();
      var hdtwos = getalltags("h2", "correct"); // Retrieve all invisible h2 tags
      changeclass(hdtwos, "answer");
	  var hdtwoss = getalltags("h2", "wrong"); // Retrieve all invisible h2 tags
      changeclass(hdtwoss, "answer");
   	  var ps = getalltags("p", "invisible"); // Retrieve all invisible h2 tags
      changeclass(ps, "answertitle");
	  bigarray = makebigarray();
      for (var i = 0; i < bigarray[0].length; i++)
      {
        if(bigarray[3][i] != 0)
        {
          hide(bigarray[0][i]); 
          hide(bigarray[1][i]); 
        }
      }
      clearform();
      toggleoptions("on");
      count = 0;
      show(questions[0]);
	  
	  if (lang == "f"){
	  	document.getElementById("nextbtn").value = "Suivante"; // Added P Smith Feb 16, 06
		pagetitle = "Veuillez faire un choix";
		numbering = "Question " + counter + " de " + numquestions;
	  }
	  else {
      	document.getElementById("nextbtn").value = "Next";
		pagetitle = "Please make a selection";
		numbering = "Question " + counter + " of " + numquestions;
	  }
      
      document.getElementById("head").firstChild.data = pagetitle;      
      document.getElementById("counter").firstChild.data = numbering;
	  

         
      var hdtwos = getalltags("h2", "question"); // HIde all Question h2 tags
      changeclass(hdtwos, "invisible");
      return false;
     }
     else
     {
     if(lang == "e")
     {
       alert("You have answered all of the questions. Select the 'OK' button below to return to the Spam IQ Test home page.");
       document.getElementById("nextbtn").value = "Home";
       document.getElementById("nextbtn").onClick = window.location = "../../../en/ca02126e.html";
     }
     if(lang == "f")
     {
       alert("Vous avez repondu à tous les questions. Choisissez le bouton 'OK' pour retourner à la page d'acceuil.");
       document.getElementById("nextbtn").value = "Acceuil";
       document.getElementById("nextbtn").onClick = window.location = "../../../fr/ca02126f.html";
     }
       return false;
     }
  }
}

function getgoodanswers() // Returns an array containing the value of a correct answer. The correct answer value is located in each question's fieldset class.
{
  fieldsets = new Array();
  answers = new Array();
  fieldsets = document.getElementsByTagName("fieldset");
  for(var i=0; i < fieldsets.length; i++)
  {
    answers[i] = fieldsets[i].className;
  }
  return answers;
}

function getuseranswers()// Returns an array containing the value each question that the user has answered. If a question was not answered it's value is set to 0.
{
  var useranswers = new Array();
  var divs = new Array();
  divs = getalldivs(/question/);
  for(var i = 0; i < divs.length; i++)
  {
    var inputs = new Array();
    inputs = document.getElementById(divs[i]).getElementsByTagName("input");
    for(var j = 0; j < inputs.length; j++)
    {
      if(inputs[j].checked)
      {
        useranswers[i] = inputs[j].value;
        break; 
      }
      else
      {
        useranswers[i] = 0;
      }
    }
  }
  return useranswers;
}

/**************************************************************

Functions that gather answers and display results

**************************************************************/

function displayscore(results)
{
  var results = scorequiz(results);
  var score = 0;
  for (var i = 0; i < results.length; i++)
  {
    if (results[i] == "true")
    {
      score++;
    }
  }
  return score;
}

function styleanswers()
{
  var labels = document.getElementsByTagName("label");
  var radios = document.getElementsByTagName("input");
  for(var i=0; i < labels.length; i++)
  {
    if ((labels[i].title == radios[i].id) && (radios[i].checked))
    {
      var classname = "bold";
      labels[i].className = classname;
    }
  }
  var allheaders = document.getElementsByTagName("h2")
  var j = 0;
  for(var i=0; i < allheaders.length; i++)
  {
    if(allheaders[i].className == "question")
    {
      j++;
      var headingtext = "Question"; 
      allheaders[i].firstChild.data = headingtext;
    }
  }
}

/**************************************************************

Genral purpose Functions

**************************************************************/

function show(i) 
{
  var e = document.getElementById(i);
  var classname = "visible";
  e.className = classname;
}
function hide(i) 
{
  var e = document.getElementById(i);
  var classname = "invisible";
  e.className = classname;
}
function getalldivs(stringtofind)  // Returns an array of all div elements that contain the passed argument "stringtofind". //
{
  var regex = stringtofind;
  var divs = document.getElementsByTagName("div");
  var questions = new Array();
  var j = 0;
  for (var i = 0; i < divs.length; i++)
  {
    var divid = divs[i].id;
	if (regex.test(divid))
	{
	  questions[j] = divid;
	  j++;
	}
  }
  return questions;
}

function getalltags(tag, theclass)
{
 var alltags = document.getElementsByTagName(tag); // Show all Question h2 tags
 var tagarray = new Array();
 var j = 0;
 for(var i=0; i < alltags.length; i++)
 {
   if(alltags[i].className == theclass)
   {
    tagarray[j] = alltags[i];
    j++;
   }
  }
  return tagarray;
}

function changeclass(tagarray, newclass)
{
  for(var i=0; i < tagarray.length; i++)
  {
    var classname = newclass;
    tagarray[i].className = classname;
  }
}

function toggleoptions(action) /* Disable/Enable radio buttons. */
{
  var inputs = document.getElementsByTagName("input");
  for (var j = 0; j < inputs.length; j++)
  {
    if ((inputs[j].type == "radio") && (inputs[j].className == "radio"))
    {
      if (action == "off")
      {
        inputs[j].disabled = true;
      }
      if (action == "on")
      {
        inputs[j].disabled = false;
      }
    }
  } 
}

function makerandomquestions(q, num) // Function that randomizes the questions. q = an array. num = number of items.
{
  var i = num;
  if (i == 0)
  {
    return false;
  }
  while (--i)
  {
      var j = Math.floor( Math.random() * ( i + 1 ) );
      var tempi = q[i];
      var tempj = q[j];
      q[i] = tempj;
  	  q[j] = tempi;
  }
  return q;
}
function validator(lang) /* Check to see if a radio button was selected for the visible question */
{
  var divs = document.getElementsByTagName("div");
  for (var i = 0; i < divs.length; i++)
  {
    if (divs[i].className == "visible")
    {
      var idtofind = divs[i].id;
      var inputs = document.getElementById(idtofind).getElementsByTagName("input");     
      for (var j = 0; j < inputs.length; j++)
      {
        if ((inputs[j].type == "radio") && (inputs[j].className == "radio") && (inputs[j].checked))
        {
          return true;
        } 
      }
    }
   }
  if (lang == "e")
  {
    alert("Please make a selection");
  }
  if (lang == "f")
  {
    alert("Veuillez faire un choix");
  }
  return false;
}
function makebigarray()
{
    var qst = new Array();
    var ans = new Array();
    var answers = new Array();
    var userpicks = new Array();
    var headinsga = new Array();
    var answerp = new Array();
	
    qst = getalldivs(/question/);
    ans = getalldivs(/answer/);
    answers = getgoodanswers();
    userpicks = getuseranswers();
    headingsa = getalltags("h2", "answer");
    answerp = getalltags("p", "answertitle");
	
	var bigarray = new Array(qst, ans, answers, userpicks, headingsa, answerp);
	return bigarray;
}
function openhide()
{
  questions = getalldivs(/question/);
  var answers = getalldivs(/answer/); // Create an array of answer ids
  for(var i=0; i < questions.length; i++) // Hide all questions
  {
    var divtohide = questions[i];
    hide(divtohide);
  }
  for(var i=0; i < answers.length; i++) // Hide all answers.
  {
    var divtohide = answers[i];
    hide(divtohide);
  }
  var hdtwos = getalltags("h2", "question"); // Hide all Question h2 tags
  for(var i=0; i < hdtwos.length; i++)
  {
    var classname = "invisible";
    hdtwos[i].className = classname;
  }
  hide("appnav");
}

function clearform() // Uncheck all radio buttons on a form //
{
  var radios = document.getElementsByTagName("input");
  for (var j = 0; j < radios.length; j++)
  {
    if ((radios[j].type == "radio") && (radios[j].className == "radio"))
    {
       radios[j].checked = false;
    }
	if ((radios[j].type == "radio") && (radios[j].className != "radio"))
    {
       radios[j].checked = true;
    } 
  }
  document.getElementById("nextbtn").style.width = '75px'; //P Smith added Feb 15, 06
  //Reset little arrow for results at end of test by hiding it
  //document.getElementById("result_image").style.display = 'none'; //P Smith added Feb 17, 06
}

function insertspambanner() // Replace the existing h1 text with the spam IQ Banner //
{
  if(document.getElementById)
  {
    var headerone = new Array();
    var headertext = "";
    var img = document.createElement("img");
  
    img.setAttribute("src","../vwapj/SpamIQBannerHome.gif/$FILE/SpamIQBannerHome.gif");
    headerone = document.getElementsByTagName("h1");
    headerone[0].appendChild(img);
    headerone[0].firstChild.data = headertext;
  }
}
function insertspambannerhome(url, path) // Replace the existing h1 text with the spam IQ Banner //
{
  if(document.getElementById)
  {
    var headerone = new Array();
    var headertext = "";
    var img = document.createElement("img");
    var a = document.createElement("a");
    img.setAttribute("src", path);
    a.setAttribute("href",url);
    headerone = document.getElementsByTagName("h1");
    headerone[0].appendChild(a);
    a.appendChild(img);
    headerone[0].firstChild.data = headertext;
  }
}
