//initializes the quiz into the div given BB/10/08
// additional comments added December 23, 2008, slr.


function Quiz(divName){
this.divObj=document.getElementById(divName);
this.questions = new Array();
}

Question.prototype.isCorrect= function (){
  if (this.inputType=='text'){
    return this.question.childNodes[0].value == this.answer;
  }
  else if(this.inputType=='select'){
    return this.question.childNodes[0].options[this.question.childNodes[0].selectedIndex].value == this.answer;
  }
  else if(this.inputType=='radio'){
    for (var i=0;i<this.question.childNodes.length;i++){
    
      if (this.question.childNodes[i].nodeName=='INPUT'){
        if (this.question.childNodes[i].checked){
          return this.question.childNodes[i].value ==this.answer;
        }
      }
    }
  }

}

Quiz.prototype.writePrintPreview= function(){
  var questionList = document.createElement('ul')
  
  for (var i=0;i<this.questions.length;i++){
    var question = document.createElement('li');
    question.childNodes[0].nodeValue = questions[i].questionText;
    questionList.appendChild(question) 
  }
  this.divObj.appendChild(questionList);
}

function Question(inputType,text,options,correctAnswer){
if (document.all) {    var sFloat="styleFloat"; } else {  var sFloat="cssFloat";}
  
  this.inputType=inputType;
  this.answer=correctAnswer;
  this.questionText=text;
  this.options=options;
  
  this.question = document.createElement("div");
  this.question.style.width='290px';
  this.question.style.paddingTop='.5em';
  this.question.align="right"
  this.question.style.color="#2461AA"
  this.question.style[sFloat]='left'

  this.text=document.createElement("p")
  this.text.appendChild(document.createTextNode(text));
  this.text.style.width='230px'
  this.text.style[sFloat]='left'

  
  
  if (inputType=='text'){
    question = document.createElement("input");
    question.type = 'text';
    text=document.createElement("span");
    question.style.height='16px';
    
    this.question.className='question_text';
    
    if (options=="$"){
    text.appendChild(document.createTextNode(options+"  "));
    this.question.appendChild(text);
        question.maxLength = '7';

    }

    this.question.appendChild(question);

    if (options=="yrs"){
    text.appendChild(document.createTextNode(" "+ options))
    this.question.appendChild(text);
    question.maxLength = '2';

    }
    if (options=="%"){
    text.appendChild(document.createTextNode(" "+ options))
    this.question.appendChild(text);
    question.maxLength = '5';

    }

    
  }
  else if(inputType=='select'){
    this.question.className='question_select';
    question = document.createElement("select");
    for (var i=0;i<options.length;i++){
      var questionOption=document.createElement('option')
      questionOption.text =options[i];
      questionOption.value =options[i];
      try {question.add(questionOption,null);} // standards compliant
      catch(ex) {question.add(questionOption);} // IE only
    }

    this.question.appendChild(question);

  }
  else if(inputType=='radio'){
    for (var i=0;i<options.length;i++){
      try{
        question= document.createElement('<input type="radio" name="'+text+'" value="'+options[i]+'>');
      }
      catch (e){
        question = document.createElement("input");
        question.type = 'radio';
        question.name=text;
        question.value=options[i];
        
      }
      
      this.question.appendChild(question);
      this.question.appendChild(document.createTextNode(options[i]));
    }
  }
}

var genworthQuiz;


function init(){
genworthQuiz = new Quiz('quiz');

/*  Questions and answers copy here.

Values in this order: question type, question text, answer choices, correct answer

	As in: 
	genworthQuiz.add(new Question('select','What is the answer to my question.',new Array("Is it this?","Is it that?"),'Is it this?'));

Note: The correct answer value MUST MATCH the answer choice or it will return incorrect. */

genworthQuiz.add(new Question('radio','By reducing your mortgage amortization period you will save interest costs over the life of the mortgage.',new Array("TRUE","FALSE"),'TRUE'));
genworthQuiz.add(new Question('radio','You can use the equity in your home to consolidate debt, renovate your home or to make investments.',new Array("TRUE","FALSE"),'TRUE'));
genworthQuiz.add(new Question('select','As a general rule, your total housing costs and other monthly payments should not exceed ___ of your gross (pre-tax) income.',new Array("30%","40%","50%","60%"),'40%'));
genworthQuiz.add(new Question('select','Which payment frequency pays down your mortgage the fastest?',new Array("Monthly","Semi-Monthly","Bi-Weekly Accelerated"),'Bi-Weekly Accelerated')); // Removed "Weekly Accelerated" option as per client request, January 7, 2009, slr.
genworthQuiz.add(new Question('select','Making one extra mortgage payment each year shortens the life of a 30-year mortgage by approximately.',new Array("8 years","5 years","15 years","It doesn't shorten the life of the loan"),'5 years'));
genworthQuiz.add(new Question('select','First time home buyers can withdraw up to ____ from their RRSP\'s to put towards a down payment on their first home.',new Array("$5,000","$15,000","$25,000","$30,000"),'$25,000')); // Changed $20,000 to $25,000 as per client request, February 4, 2010, slr.
genworthQuiz.add(new Question('radio','With an open term mortgage you can pay the balance owing at any time without incurring a penalty.',new Array("TRUE","FALSE"),'TRUE'));
genworthQuiz.add(new Question('radio','The term of the mortgage is the length of time it takes to pay off the entire amount lent to you.',new Array("TRUE","FALSE"),'FALSE'));
genworthQuiz.add(new Question('select','Which of the following could impact your credit rating and ultimately impact your ability to qualify for a mortgage.',new Array("Late bill payments","Carrying a high balance on your credit card","A previous bankruptcy","All of the above"),'All of the above'));
genworthQuiz.add(new Question('radio','Historically, you will pay less interest with a variable rate mortgage over the life of your loan versus a fixed rate mortgage.',new Array("TRUE","FALSE"),'TRUE'));
}

window.onload=init;

function score(button){
button.childNodes[0].childNodes[0].nodeValue='TAKE QUIZ AGAIN?';
genworthQuiz.colorCorrect();
//Messages
if (personType()=='Expert')
document.getElementById('resultMessage').innerHTML='<p style="vertical-align: middle;">Congratulations! You answered '+genworthQuiz.getScore()+' questions correctly and qualify as an expert. To learn more, <a style="color:#fff;text-decoration:underline;font-weight:bold;" href="../c_on-your-terms/index.asp">click here</a></p>';
else if (personType()=='Learner')
document.getElementById('resultMessage').innerHTML='<p style="vertical-align: middle;">Getting there! You answered '+genworthQuiz.getScore()+' questions correctly and qualify as a learner. To fill in the gaps, <a style="color:#fff;text-decoration:underline;font-weight:bold;" href="../c_have-it-all/index.asp">click here</a></p>';
else if (personType()=='Novice')
document.getElementById('resultMessage').innerHTML='<p style="vertical-align: middle;">You answered '+genworthQuiz.getScore()+' questions correctly and are ranked a novice. To learn more, <a style="color:#fff;text-decoration:underline;font-weight:bold;" href="../c_achieve-your-dreams/index.asp">click here</a></p>';

document.getElementById('resultMessage').style.display="block";
button.onclick=function(){takeAgain(button)};

}

function takeAgain(button){
document.getElementById('resultMessage').style.display="none";
button.childNodes[0].childNodes[0].nodeValue='SHOW ME THE RESULTS';
button.onclick=function(){score(button)};
}

function personType(){
if (genworthQuiz.getScore()>7)
return 'Expert';
else if (genworthQuiz.getScore()>5)
return 'Learner'
return 'Novice'
}

Quiz.prototype.colorCorrect =function(){
  for (var i=0;i<this.questions.length;i++){
    if (!this.questions[i].isCorrect()){
      this.questions[i].text.style.color='red';
	  
    }
    else{
      this.questions[i].text.style.color='';
    }
  }
}

Quiz.prototype.add = function(quest){
  this.questions.push(quest);
  var questionDiv=document.createElement("div");
  //questionDiv.style.border="1px solid black";
  questionDiv.appendChild(quest.text);
  questionDiv.appendChild(quest.question);
  
  var push=document.createElement("br");
  push.clear = 'all'
  this.divObj.appendChild(push)
  
  var hr =document.createElement("hr");
  hr.style.border='0';
  hr.style.backgroundColor ="#2461AA" 
  hr.style.color ="#2461AA" 
  this.divObj.appendChild(hr)

    this.divObj.appendChild(questionDiv);
}

Quiz.prototype.getScore = function(){
  var score=0;
  for (var i=0;i<this.questions.length;i++){
    if (this.questions[i].isCorrect())
      score++;
  }
  return score;
}
