// survey.js

// javascript for the survey component.

function showOnlyLayer(which){
  hideAllLayers();
  showLayer(which);
  showLayer('Results');
}

function showLayer(which){
  if(document.getElementById(which)){
    document.getElementById(which).style.display='block';
  }
}
function hideLayer(which){
  if(document.getElementById(which)){
    document.getElementById(which).style.display='none';
  }
}
function hideAllLayers(){
  hideLayer('CheckedNone');
  hideLayer('NoneApply');
  
  // hides QU1 through QU9
  for(var i=1;i<10;i++){
      hideLayer('QU'+i);
  }
  hideLayer('TagLine');
  hideLayer('WhichClicked');
}

function handleAssessment(f){
  // "score" the user and see how many checks there were
  var q=Array(); // e.g., q[0..N-1]=2 indicates question 2 was checked. this is used to determine the questions that were answered
  var a=Array(); // answered. e.g., a[5]=1 indicates that answer 5 was checked
  var num=0; // how many checks total
  for(var i=1;i<10;i++){
    if(f['q'+i].checked) { q[q.length]=i; a[i]=1; num++;}
  }
  
  // if there were clicks, prepare text to show the user which ones were checked
  var txt='You selected question' + (num==1?'':'s') + ' ';
  if(num==1){
    txt+= '<strong class="orange">' + q[q.length-1] + '</strong>';
  }
  if(num>1){
    var tmp=q.slice(0,num-1);
    txt+= '<strong class="orange">';
    txt+= tmp.join('</strong>, <strong class="orange">');
    txt+= '</strong>';
    txt+= " &amp; <strong class='orange'>" + q[q.length-1] + '</strong>';
  }
  txt+= '. ';
  
  // you'll always reveal something to the user, even if it's a message to click something...
  hideAllLayers();
  showLayer('Results');

  if(num==0){ // no question was answered
    showOnlyLayer('CheckedNone');
    return;
  }
  
  // show them which was/were clicked, to confirm
  if(document.getElementById('WhichClicked')){
    document.getElementById('WhichClickedText').innerHTML=txt;
  }  
  showLayer('WhichClicked');
  
  // show the answers to each checked question 1-8, in correct order.
  for(i=1;i<9;i++){
    if(a[i]){ 
      showLayer('QU'+i);
    } else {
      hideLayer('QU'+i);
    }
  }
  
  // show the answer to 9 only if it was the only one checked
  if(num==1&&a[9]){
    showLayer('QU9');
  }
  
  // show the common tagline unless no clicks (already handled above), or just 9
  if(!(num==1&&a[9])){
    showLayer('TagLine');
  }
  
}

function unclickRest(f){
// uncheck all but the last one. this clears them out in case
//  any were accidentally checked.
   var i;
   for(i=1;i<9;i++){
     if(f['q'+i]&&f['q'+i].checked){
       f['q'+i].checked=false;
     }
   }
}

function resetAll(f){
// uncheck all boxes, including number 9
   unclickRest(f);
   if(f['q9']&&f['q9'].checked){
     f['q9'].checked=false;
   }
   hideLayer('Results');
   
}
