var mult_points = 0;
var comp_points = 0;
var earn_points = true;

function init() {
	setScoreFromQueryString();
	if (question == 'results') {
		if (document.getElementById('total_mult_points')) document.getElementById('total_mult_points').innerHTML = mult_points;
		if (document.getElementById('total_comp_points')) document.getElementById('total_comp_points').innerHTML = comp_points;
		document.getElementById('total_questions').innerHTML = document.location.href.substring(document.location.href.indexOf('question-')+9,document.location.href.indexOf('.html'))-1;
	} else {
	  switch(questionType) {
	    case 'multipleChoice':
	      var form = document.getElementById('question_form');
    		document.getElementById('question_text').innerHTML= questionText;
    		for(i in possibleAnswers) {
    			var l = document.createElement('label');
    			var r = document.createElement('input');
    			r.setAttribute('type','radio');
    			r.setAttribute('name','answers');
    			r.setAttribute('id','choice_'+i);
    			l.appendChild(r);
    			var t = document.createTextNode(possibleAnswers[i][0]);
    			l.appendChild(t);
    			var p = document.createElement('p');
    			p.appendChild(l);
    			form.appendChild(p);    		
    		  addEvent(document.getElementById('choice_'+i),'click',multiple_choice)
    		  if (possibleAnswers[i][1]) document.getElementById('choice_'+i).className = 'correct';
        }
	      break;
	    case 'comparison':
        document.getElementById('question_text').innerHTML= questionText;
        var form = document.getElementById('question_form');
        var a = document.createElement('textarea');
        var p1 = document.createElement('p');
        a.setAttribute('rows',12);
        a.setAttribute('cols',42);
        a.setAttribute('id','answer_area');
        p1.appendChild(a);
        var i = document.createElement('input');
        var p2 = document.createElement('p');
        i.setAttribute('type','button');
        i.setAttribute('value','Compare');
        i.setAttribute('id','comparison_btn');
        p2.appendChild(i);
        form.appendChild(p1);
        form.appendChild(p2);
        addEvent(document.getElementById('comparison_btn'),'click',comparison);
	      break;
	    default: break;
	  }
	}
}

function setScoreFromQueryString() {
  var vars = window.location.search.substring(1).split("&");
  for (var i in vars) {
    var pair = vars[i].split('=');
    switch(pair[0]) {
      case 'mp': mult_points = pair[1]; break
      case 'cp': comp_points = pair[1]; break
      default: break
    }
  } 
}

function multiple_choice() {
	if (this.className == 'correct') {
		if (earn_points) mult_points++;
		var inputs = document.getElementById('question').getElementsByTagName('input');
		for(var i=0;i<inputs.length;i++) inputs[i].disabled = true;
		document.getElementById('response_body').innerHTML = imageTag('correct') + '<p>' + correctMessage + '</p>' + '<p><a href=\"question-' + (question+1) + '.html?mp=' + mult_points + '&cp=' + comp_points + '\">Next Question</a>' + imageTag('next') + '</p>';
	} else {
		this.disabled = true;
		earn_points = false;
		document.getElementById('response_body').innerHTML = "<p><strong>INCORRECT</strong><br /> Please try again.</p>";
	}
}

function imageTag(img) {
	var relativeUrl;
	if (document.location.href.indexOf('response') > 0) {
		relativeUrl = '../../../../images/';
	} else {
		relativeUrl = '../../../images/';
	}
	switch(img) {
		case 'next':
			var tag = '<img src=\"' + relativeUrl + 'practice-tests/next.jpg" width="80" height="67" style="float:right" />';
		break
		
		case 'correct':
			var tag = '<img src=\"' + relativeUrl + 'practice-tests/correct.jpg" width="100" height="75" style="float:left" />';
		break
		
		default:
			var tag = '!';
		break
	}
	return tag;
}

function comparison() {
  var answerText = document.getElementById('answer_area').value;
  var form = document.getElementById('question_form');
  while (form.hasChildNodes()) form.removeChild(form.firstChild);
  var t1 = document.createTextNode(correctMessage);
  var t2 = document.createTextNode(answerText);
  var t3 = document.createTextNode('A Top Scoring Example:');
  var t4 = document.createTextNode('Your Response:');
  var bq1 = document.createElement('blockquote');
  var bq2 = document.createElement('blockquote');
  var h1 = document.createElement('h3');
  var h2 = document.createElement('h3');
  bq1.setAttribute('class','handwriting');
  bq2.setAttribute('class','handwriting');
  bq1.appendChild(t1);
  bq2.appendChild(t2);
  h1.appendChild(t3);
  h2.appendChild(t4);
  form.appendChild(h1);
  form.appendChild(bq1);
  form.appendChild(h2);
  form.appendChild(bq2);
	for(var i in possibleAnswers) {
		var l = document.createElement('label');
		var c = document.createElement('input');
		c.setAttribute('type','checkbox');
		c.setAttribute('name','answers');
		c.setAttribute('id','self_check_'+i);
		l.appendChild(c);
		var t = document.createTextNode(possibleAnswers[i][0]);
		l.appendChild(t);
		var p = document.createElement('p');
		p.appendChild(l);
		form.appendChild(p);
  }
  var i = document.createElement('input');
  var p = document.createElement('p');
  i.setAttribute('type','button');
  i.setAttribute('value','Check');
  i.setAttribute('id','comparison_check_btn');
  p.appendChild(i);
  form.appendChild(p);
  addEvent(document.getElementById('comparison_check_btn'),'click',comparisonCheck);
}

function comparisonCheck() {
  var count = 0;
  for(var i in possibleAnswers) {
    if (document.getElementById('self_check_'+i).checked) count++;
		document.getElementById('self_check_'+i).disabled = true;
  }
  if (count >= possibleAnswers.length) comp_points++;
  document.getElementById('response_body').innerHTML = '<p>You checked ' + count + ' out of ' + possibleAnswers.length + ' boxes. </p><p>Move on to the <a href=\"question-' + (question+1) + '.html?mp=' + mult_points + '&cp=' + comp_points + '\">Next Question</a>' + imageTag('next') + '</p>';
  window.scroll(0,0);
}

addEvent(window,'load',init);