// JSON example
var candidates = [ {
  "name": "Ellie Phanta",
  "party": "Republican",
  "stances" : [1,1]
  },
  {
  "name": "Jack Bass",
  "party": "Democrat",
  "stances" : [1,1]
  },
  {
  "name": "Paul Stanton",
  "party": "Libertarian",
  "stances" : [5,5]
  }
 ];

Questions = [ 
  {
  "Issue" : "Force",
  "Question" : "Should goods and services be provided by force?",
  "Responses" : [
    ["Absolutely",1],
    ["Yes", 2],
    ["Maybe", 3],
    ["No", 4],
    ["Never", 5]]
},
   {
  "Issue" : "Roads",
  "Question" : "Without government, Who will build the roads?",
  "Responses" : [
    ["No one at all",1],
    ["Shady people you can't trust", 2],
    ["Someone might maybe possibly", 3],
    ["Construction companies", 4],
    ["Where we're going, we won't need roads.", 5]]
}            
];

Imports = [["Could not care less",1],["Don't care",2],["Might care",3],["Care",4],["Care a lot",5]];

responses = [];
importance = [];

for (i = 0; i < Questions.length; i++) {
  responses.push(3);
  importance.push(3);
}




function loadQuestions() {
  var chunk = '';
  for (i = 0; i < Questions.length; i++) {
    chunk += '<div id="q' + i + '>'; 
    chunk += '<p class="question">' + Questions[i].Question + '</p>';
    chunk += '<p class="answers">';
    for (j = 0; j < Questions[i].Responses.length; j++) {
     var qr = Questions[i].Responses[j];
     chunk += '<button onclick="tally(' + i + ',' + qr[1] + ')">' + qr[0] + '</button>';
    }
    chunk += '</p>';
    chunk += '<p class="importance">';
    for (k = 0; k < Imports.length; k++) {
     chunk += '<button onclick="tallyImports(' + i + ',' + Imports[k][1] + ')">' + Imports[k][0] + '</button>';
    }    
    chunk += '</p></div>';
  }
  chunk += '<p><button onclick="compareToCandidates()">Calculate</button></p>';
  document.getElementById("questionArea").innerHTML = chunk;
  document.getElementById("test3").innerHTML = results;
};

function tally(questionNumber,response) {
  //qId = 'q' + questionNumber;
  responses[questionNumber] = response;
  document.getElementById("test").innerHTML = responses;
  //document.getElementById(qId).innerHTML = 'Response Recorded';
};

function tallyImports(questionNumber,response) {
  //qId = 'q' + questionNumber;
  importance[questionNumber] = response;
  document.getElementById("test2").innerHTML = importance;
  //document.getElementById(qId).innerHTML = 'Response Recorded';
};

function maxDiff(QuestionNumber) {
  qa = Questions[QuestionNumber].Responses;
  var max = 0;
  var test = 0;
  for (i = 1; i < qa.length; i++) {
    for (j = 0; j < i; j++) {
      test = Math.abs(qa[i][1] - qa[j][1]);
      if  (test > max) {
        max = test;
      }
    }
  }

  return max;
};
  
function calcDiff(c,q) {
  var maxD = maxDiff(q); // Maximum possible difference
  
  var dif = Math.abs(responses[q] - candidates[c].stances[q]); // difference between candidate score and your score 

  var wDif = (maxD-dif)*importance[q]; // Difference weighted based on importance
  perc = 100 - (Math.round(dif / maxD * 10) * 10);
  //document.getElementById("test").innerHTML = perc;
  results[c] += wDif;
}
  
function compareToCandidates() {
  results = [];
  for (j = 0; j < candidates.length; j++) {
    results.push(0);
  }
  
  
  var resultsStr = '<table><tr><th>Candidates</th>';
  for (i = 0; i < Questions.length; i++) {
    resultsStr += '<th>' + Questions[i].Issue + '</th>';    
  }
  resultsStr += '</tr>';
  
  for (i = 0; i < candidates.length; i++) {
    resultsStr += '<tr><td>' + candidates[i].name + '</td>';
    for (j = 0; j < Questions.length; j++) {
      calcDiff(i,j);
      resultsStr += '<td>' + perc.toString() + '%</td>';  
    }
    resultsStr += '</tr>';
  }
  resultsStr += '</table>';
  document.getElementById("results").innerHTML = resultsStr;
  document.getElementById("test3").innerHTML = results;
};

loadQuestions();

@@ -266,16 +266,20 @@
  %7D%0A %5D;%0A%0A
+var 
 Question
@@ -806,16 +806,20 @@
    %0A%5D;%0A%0A
+var 
 Imports 
@@ -912,16 +912,20 @@
 %22,5%5D%5D;%0A%0A
+var 
 response
@@ -932,16 +932,20 @@
 s = %5B%5D;%0A
+var 
 importan
@@ -952,16 +952,49 @@
 ce = %5B%5D;
+%0Avar results = %5B%5D;%0Avar perc = 50;
 %0A%0Afor (i
@@ -2097,32 +2097,33 @@
 lementById(%22test
+2
 %22).innerHTML = r
@@ -2130,26 +2130,24 @@
 esponses;%0A  
-//
 document.get
@@ -2150,35 +2150,38 @@
 .getElementById(
-qId
+%22test%22
 ).innerHTML = 'R
@@ -2256,40 +2256,8 @@
 ) %7B%0A
-  //qId = 'q' + questionNumber;%0A
   im
@@ -2350,26 +2350,24 @@
 portance;%0A  
-//
 document.get
@@ -2378,19 +2378,22 @@
 entById(
-qId
+%22test%22
 ).innerH
@@ -2808,19 +2808,16 @@
 ference%0A
-  %0A
   var di
@@ -2925,17 +2925,16 @@
  score %0A
-%0A
   var wD
@@ -3135,16 +3135,17 @@
  wDif;%0A%7D
+;
 %0A  %0Afunc
@@ -3261,19 +3261,16 @@
 %0A  %7D%0A  %0A
-  %0A
   var re

Update wme.js