var questionCount = 0;
function loadQuestions() {
  //first loop through each question section
  questions.each(function(questionSection,index) {

    // first create the outer question div
    var questionSectionDiv = new Element("div",{"class": "questionSection"});

    //first insert the label
    var sectionLabel = new Element("label",{"class": "sectionLabel"}).update("Part " + (index + 1) + ": " + questionSection.questionSection);
    Element.insert(questionSectionDiv,sectionLabel);

    questionList = new Element("ol",{"class": "questionList"});
    // now build the table.
    questionSection.questions.each(function(question,index) {
      if (index % 2 == 0) {
        questionLi = new Element("li",{"class": "even"})
      } else {
        questionLi = new Element("li",{"class": "odd"})
      }

      questionTable = new Element("table", {"class":"questionTable"});
      questionTbody = new Element("tbody");

      questionTr= new Element("tr",{"class": "questionUl"});

      questionNumTd = new Element("td",{"class": "questionNum"}).update((questionCount +1) + ".");
      Element.insert(questionTr,questionNumTd);

      questionTextTd = new Element("td", {"class": "questionText", "id": "question"+questionCount+"question"});

      Element.insert(questionTr,questionTextTd);
      Element.insert(questionTbody,questionTr);
      Element.insert(questionTable,questionTbody);

      //questionContent = new Element("p");
	//questionContent.innerHTML = ;
      
Element.insert(questionTextTd,question.question);
      
      responseTd = new Element("td", {"class": "responseLi"});

      yesLabel = new Element("label",{"class":"yesLabel"}).update("Yes");
      noLabel = new Element("label").update("No");
      answerId = "question" + questionCount + "answer";
      yesRadioButton = new Element("input",{"type": "radio", "name": answerId, "class": "answer", "value": "1"});
      noRadioButton = new Element("input",{"type": "radio", "name": answerId, "class": "answer", "value": "0"});

      // increment the global question count
      questionCount += 1;

      Element.insert(responseTd,yesRadioButton);
      Element.insert(responseTd,yesLabel);

      Element.insert(responseTd,noRadioButton);
      Element.insert(responseTd,noLabel);

      Element.insert(questionTr,responseTd);

      Element.insert(questionLi,questionTable);

      Element.insert(questionList,questionLi);
    });

    Element.insert(questionSectionDiv,questionList);


    //now that everything is built, insert it into the question 
    Element.insert($("questionContentInner"),questionSectionDiv);
  });

}

function submitForm() {

  unanswered = false;
  answeredQuestions = $('questionContent').serialize().toQueryParams();
  rawQuestions = new Array();

  index = 0;

  //gather the questions
  questions.each(function(questionSection) {
    questionSection.questions.each(function(question) {
      rawQuestions[index++] = question;
    });
  });


  rawQuestions.each(function(question,index) {
      $('question'+index+'question').style.color = "#000";
      if (answeredQuestions["question"+index+"answer"] == null) {
        unanswered = true;
        $('question'+index+'question').style.color = "red";
      }
  });

  if (unanswered) {
    alert('Please answer all of the questions before submitting the form.');
    return;
  }      



  $('questionContent').hide();

  // clear out old div
  $('answerTips').innerHTML = "";

  text = new Element("p",{"class":"instruction"}).update("Based on your responses, click on each question below to get a customized tip.");
  Element.insert($('answerTips'),text);

  var count = 0;


  var aiir_rating = 0;

  ul = new Element("ul",{"id": "answerList"});
  // now score the test
  rawQuestions.each(function(q,index) {
      answer = answeredQuestions["question" + index + "answer"];

      // insert the answer into the answerTips
      li = new Element("li");

			// wrapperDiv = new Element("div", {"style":"cursor:pointer;", "id": "eventHandlerDiv_" + index, "onmousedown": "javascript:toggleTip("+index+");event.cancelBubble=true;return false;"});
			// Element.insert(li, wrapperDiv); 

      //table.
      t = new Element("table");
			tbody = new Element("tbody");
      Element.insert(t,tbody);

      tr = new Element("tr");
      Element.insert(tbody,tr);

      td = new Element("td", {"class": "questionNumTd"});
      Element.insert(tr,td);

       Element.insert(li,t);
			//Element.insert(wrapperDiv,t);

      // place the question
      holder = new Element("div", {"id":"numHolderDivID_"+index,"class": "numHolderDiv"});
      Element.insert(td,holder);
      iconA = new Element("a",{"href": "javascript: toggleTip("+index+");"});
      icon = new Element("img",{"border": "0","src": "icon_arrow_down.gif", "class": "aiirDownIcon"});
      Element.insert(iconA,icon);
      Element.insert(holder,iconA);
      questionNum = new Element("strong",{"class":"questionNum"}).update((index+1) + ".");
      Element.insert(holder,questionNum);

      td2 = new Element("td");
      question = new Element("strong");
      questionLink = new Element("a",{"href": "javascript: toggleTip("+index+");", "class": "questionStrong", "onmouseover":"javascript:changeNumberColor(" + index + ")", "onmouseout":"javascript:changeNumberColor(" + index + ")"}).update(q.question);

      Element.insert(question,questionLink);

      Element.insert(td2,question);
      Element.insert(tr,td2);

      tip = new Element("div",{"class": "customizedTip", "id": "customizedTip" + index, "style": "display:none"});
      tipp = new Element("p");
      Element.insert(tip,tipp);

      if (answer == "0") {
        if (q.customizedTipNo) {
          tipp.update(q.customizedTipNo);
          Element.insert(li,tip);
          Element.insert(ul,li);
        }

      } else if (answer == "1") {
        if (q.customizedTipYes) {
          tipp.update(q.customizedTipYes);
          Element.insert(li,tip);
          Element.insert(ul,li);
        }

        // calculate rating here as well
        if (q.countsTowardsRating == true) 
          aiir_rating++;
      }
  });
  Element.insert($('answerTips'),ul);

  switch(true) {
  case (aiir_rating > 11):
    $('aiir_rating').update("Extremely High");
    break;
  case ((aiir_rating >= 8) && (aiir_rating <= 10)):
    $('aiir_rating').update("Very High");
    break;
  case ((aiir_rating >= 3) && (aiir_rating <= 7)):
    $('aiir_rating').update("Somewhat High");
    break;
  case ((aiir_rating >= 1) && (aiir_rating <= 2)):
    $('aiir_rating').update("Moderate");
    break;
  case (aiir_rating == 0):
    $('aiir_rating').update("Low");
    break;
  }

  scroll(0,0);
  $('aiirDescription').hide();
  $('answerContent').show();
};

function toggleTip(questionIndex) {
  $$('.customizedTip').each(function(t) {
      if (t.readAttribute("id") != "customizedTip"+questionIndex)
			{
				t.style.display = 'none';
			}
			else
			{
			  if (t.style.display != 'none')
				{
					t.style.display = 'none';
				} 
				else
				{
					Effect.BlindDown(t, {duration: 0.3});
					t.onclick = function(){t.style.display = 'none'};				}  
			}
  });    
};


function toggleSubmitHoverImage() {
	var elem = $('submitDiv');
		if(elem.hasClassName('over'))
		{
			elem.removeClassName('over');
		}
		else
		{
			elem.addClassName('over');
		}
}

function changeNumberColor(index) {
	var elem = $('numHolderDivID_' + index);
		if(elem.hasClassName('over'))
		{
			elem.removeClassName('over');
		}
		else
		{
			elem.addClassName('over');
		}
}