// quiz_yourself.js

var problem_preamble = "Since you answered these questions mostly True, you may have a blindspot when it comes to ";
var quizes = {
  1 : {
    completed: false,
    has_problem: false,
    problem_title: "not involving others",
    problem_text: "Failing to ask for help or listening to what other people might have to say is a common blind spot most of us are familiar with. Asking for help is never easy but by pretending to have all the answers we actually take the chance of not having nearly enough. Being a team player never hurts and besides it's usually a heckuva lot more fun.",
    ok_text: "Nice! Sounds like you're good at involving others in your decision making.",
    questions: [
      "I'm more likely to tell others what to do than to ask their opinions.",
      "I avoid asking others for their opinions if I think we'll disagree.",
      "Sometimes I take advice from people I like (versus people who could best help me).",
      "Sometimes I just don't have time to ask for other peoples' advice.",
      "I'd rather lead than be part of a team."
    ]},
  2 : {
    completed: false,
    has_problem: false,
    problem_title: "not having the right information",
    problem_text: "We need information in order to effectively solve problems. If we do not have the necessary information -or if the information is not complete or accurate, we are not reaching informed conclusions. Information can often be overwhelming and much of it is not often directly relevant. More is not necessarily better. However, we need to be able to identify, access, and utilize the right information in order to reach the right conclusions and effectively solve problems.",
    ok_text: "Good job! When making decisions, you try hard to get all the right information.",
    questions: [
      "Sometimes I ignore information that doesn't support what I want to do.",
      "Sometimes I'll say: 'I wish I'd known that earlier!' after forging ahead with a decision.",
      "I think it's better to have too much information than not enough."
    ]},
  3 : {
    completed: false,
    has_problem: false,
    problem_title: "ineffective prioritizing",
    problem_text: "We all have a favorite way of deciding what to work on. Maybe we like to get the hardest things over with first, or maybe the easiest, maybe we tend to deal with the issues pertaining to who yells the loudest. The problem is, how we set priorities may not be related to what is important in the overall scheme of things. If we get too focused on the wrong things, the right things don't get done.",
    ok_text: "Awesome! You know how to prioritize issues when you make a decision.",
    questions: [
      "When I have a lot to do, I'll tackle things I can do quickly (not necessarily the most important things) first.",
      "I often feel busy but I'm not really sure what I'm accomplishing.",
      "I'm often in a rush to get important things done."
    ]},
  4 : {
    completed: false,
    has_problem: false,
    problem_title: "ineffective planning for next steps",
    problem_text: "Too often, a lot of discussion, brainstorming, and \"planning\" occurs but then nothing happens. Next steps and change require careful planning. it's important to make sure it happens and to plan, assign responsibilities, and deadlines which will maximize the likelihood that it will happen.",
    ok_text: "Sounds like you're on the right track - you know how to plan next steps and see things through!",
    questions: [
      "When I work with others, I find there's a lot of discussion but not a lot of action.",
      "Sometimes I feel we talk about the same things over and over, and nothing really changes.",
      "When I'm responsible for getting things done, I find it easier to do it all myself."
    ]},
  5 : {
    completed: false,
    has_problem: false,
    problem_title: "not being specific or clear enough",
    problem_text: "One common pitfall when dealing with difficult and persistent issues is to not be specific enough. When we specify what we mean and when we listen to what others mean, we begin to break complicated problems down into obvious and manageable pieces or steps. This is the only way we can begin to see how to address all the important elements of a problem.",
    ok_text: "Nice work! You understand that getting specific information is an important part of making good decisions.",
    questions: [
      "Sometimes, I assume I knew what someone meant and later find out they meant something totally different.",
      "Sometimes, it's hard to know where to start when I'm facing a difficult problem.",
      "I sometimes find I've spent too much attention on certain issues and totally overlooked others."
    ]},
  6 : {
    completed: false,
    has_problem: false,
    problem_title: "not asking good questions",
    problem_text: "Using effective, well-sequenced questioning to address difficult situations increases the likelihood that: relevant data will be utilized, important factors will be identified and addressed, conclusions will be well-considered and appropriate, and the right people will be effectively involved. We also need to be able to listen to and understand what others tell us, to know when and how to ask clarifying questions in order to comprehend, validate and represent their concerns within the problem-solving process.",
    ok_text: "You've got it! When it comes to decision making, you're good at asking questions, and listening to other people's input.",
    questions: [
      "I see the value in asking questions (I just have trouble figuring out what to ask).",
      "I trust my own judgment more than anyone else's, so I don't bother seeking out other people's opinions.",
      "I sometimes feel I ask questions randomly.",
      "I can ask plenty of questions, but I don't know what to do with the answers I get.",
      "In the past, I've missed out on crucial information from others, because I didn't ask for it."
    ]}
};


var current_quiz = 0;

function show_quiz(quiz_num) {
  // hide the current quiz
  if (current_quiz != 0) {
    var el = document.getElementById("quiz_" + current_quiz);
    el.style.display = "none";
    el.innerHTML = "";
  }
  current_quiz = quiz_num;

  // fill the new quiz
  var quiz = quizes[quiz_num];
  var quiz_div = document.getElementById("quiz_" + quiz_num);
  var html = "";
  if (quiz.completed) {
    html = quiz.has_problem ? "<div class='quiz_problem_text'>" + quiz.problem_text + "</div>\n": "<div class='quiz_ok_text'>" + quiz.ok_text + "</div>\n";
  }
  else {
    html = "<form name='answers'>\n";
    var i = 0;
    for (var i=0; i<quiz.questions.length; i++) {
      var question = quiz.questions[i];
      html += "<div class='quiz_question'>" + question + "</div>\n<input type='radio' class='quiz_answer' name='a_" + i + "' value='1'> <span class='quizAnswer'>True</span></input> <input type='radio' class='quiz_answer' name='a_" + i + "' value='0'> <span class='quizAnswer'>False</span></input>\n<hr class='quiz_hr'>\n";
    }
    html += "<input type='button' onclick='submit_quiz(" + quiz_num + "); return false;' value='Submit'>\n</form>\n";
  }
  quiz_div.innerHTML = html;

  // show the new quiz
  quiz_div.style.display = "block";
  
  // set the quiz image colors
  for (var i in quizes) {
    var img = document.getElementById("quiz_img_" + i);
    if (img) {
      if (i == quiz_num)
        img.src = "../images/quizzes/0" + i + ".gif"
      else
        img.src = "../images/quizzes/0" + i + "-off.gif"
    }
  }
}

function submit_quiz(quiz_num) {
  if (quiz_num == current_quiz) {
    var quiz = quizes[quiz_num];
    if (!quiz.completed) {

      // make sure all the buttons are checked
      var all_checked = true;
      for (var i=0; i<quiz.questions.length; i++)
        all_checked = all_checked && (document.answers["a_" + i][0].checked || document.answers["a_" + i][1].checked);
    
      if (all_checked) {
      
        // score the quiz
        var answer_total = 0;
        for (var i=0; i<quiz.questions.length; i++)
          answer_total += document.answers["a_" + i][0].checked ? 1 : 0;
          
        // does the user have a problem (more than 60% of the answers are true)
        quiz.has_problem = (answer_total >= (quiz.questions.length * 0.6));
        if (quiz.has_problem) {
          // merge in the boilerplate text
          quiz.problem_text = problem_preamble + "<em>" + quiz.problem_title + "</em>.<p><span class='solution'>Solution:</span><br>" + quiz.problem_text;
        }

        // show the completed text
        quiz.completed = true;
        show_quiz(quiz_num);
      }
      else
        alert("Please answer all the quiz questions");
    }
  }
}

function mouseout_quiz(quiz_num) {
  var el = document.getElementById("quiz_img_" + quiz_num);
  if (current_quiz == 0)
    el.src = "../images/quizzes/0" + quiz_num + ".gif";
  else
    el.src = "../images/quizzes/0" + quiz_num + (quiz_num == current_quiz ? "" : "-off") + ".gif";
}
