function ScrollToAnchor(window, anchorname)
{
  with (window.document) {
    for (var Index = 0; Index < anchors.length; Index++) {
      if (anchors[Index].name == anchorname)
        window.scrollTo(anchors[Index].x, anchors[Index].y);
    }
  }
}

Anchors     = ['0', '21', '56'];
Explanation = '<table align="center", cellpadding="8" cellspacing="0" border="0">' +
'<tr><td valign=top nowrap><b>0 - 20</b></td>' +
'<td valign=top><a name="0"></a>Compared to most people, you have a relatively low level of diabetes distress. In fact, your score places you in the green zone, the least distressed of our three categories. Congratulations! Still, there may be some specific issues about diabetes that are tough for you. Give special attention to any items that you rated as particularly bothersome. Addressing such issues might help you to become even more comfortable and confident with your diabetes.<br>&nbsp;</td></tr>' + 
'<tr><td valign=top nowrap><b>21 - 55</b></td>' +
'<td valign=top><a name="21"></a>Compared to most people, you have an average level of diabetes distress, which places you in the yellow zone. But that does not mean you should settle for this! The good news is that there is almost always a way to resolve stresses like the ones you are experiencing. Remember that you can not address all of these problems at once. Instead, focus on just the top 2 or 3 items you found most bothersome.<br>&nbsp;<br>To get started, remember that the best bet is to talk to your health care provider about what is bothering you. He or she is likely to be your best resource for figuring out what your next steps should be. If you do not have a good doctor or other health care provider, or perhaps you are unhappy with the one you have, talk to your health plan about finding a provider who is right for you. For more specific help, check out the programs, services and other links listed here on our website.<br>&nbsp;</td></tr>' + 
'<tr><td valign=top nowrap><b>56 - 102</b>&nbsp;&nbsp;&nbsp;</td>' +
'<td valign=top><a name="56"></a>Compared to most people, you have a relatively high level of diabetes distress. In fact, your score places you in the red zone, the most distressed of our three categories. But do not despair. The good news is that there is almost always a way to resolve such problems. The first step is to realize that you can not addresss all of these problems at once. Instead, focus on just the top 2 or 3 items that are most bothersome.<br>&nbsp;<br>To get started, remember that the best bet is to talk to your health care provider about what is bothering you. He or she is likely to be your best resource for figuring out what your next steps should be. If you do not have a good doctor or other health care provider, or perhaps you are unhappy with the one you have, talk to your health plan about finding a provider who is right for you. For more specific help, check out the programs, services and other links listed here on our website.<br>&nbsp;</td></tr>' +
'</table>';

function Score() {
  // Calculate the score
  var Result = 0;
  for (var Index = 0; Index < document.Quiz.elements.length; Index++)
    if (document.Quiz.elements[Index].type == 'radio' &&
        document.Quiz.elements[Index].checked)
      Result += Number( document.Quiz.elements[Index].value );

  // Calculate its anchor
  var Index;
  for (Index = 0; Index < Anchors.length; Index++) {
    if (Result == Number(Anchors[Index]) || Index+1 == Anchors.length || Result < Number(Anchors[Index+1])) break;
  }
  var Anchor = Anchors[Index];

  // Highlight the right section
  var re = new RegExp('(valign=top><a name="' + Anchor + '">)', '');
  var thisExplanation = Explanation.replace(re, 'bgcolor="#ECE5DB" $1' );

  // Build the score window
  var newWindow = window.open('', 'score_window', 'width=720,height=700,scrollbars=yes');
  with (newWindow.document) {
    open();
    write('<html><head><title>Your Score</title></head>',
          '<frameset rows="60,*" border=0>',
          '<frame name="Score"       src="about:blank" marginheight=4 marginwidth=4 scrolling="none" >',
          '<frame name="Explanation" src="about:blank" marginheight=4 marginwidth=4 scrolling="auto">',
          '</frameset></html>');
    close();
  }
  newWindow.focus();
  with (newWindow.Score.document) {
    open();
    write('<html><head><link href="css/stylesheet.css" rel="stylesheet" type="text/css"></head><body class="quiz-header">\n',
          '<p class="quiz-score">Your score is ', Result, '&nbsp;&nbsp;&ndash;&nbsp;&nbsp;on a scale of 0-102.</p>\n',
          '</body></html>');
    close();
  }
  with (newWindow.Explanation.document) {
    open();
    writeln('<html><head><link href="css/stylesheet.css" rel="stylesheet" type="text/css"></head><body class="quiz-explanation">\n', thisExplanation, '</body></html>');
    close();
  }

  // Scroll it to the right explanation
  ScrollToAnchor(newWindow.Explanation, Anchor);
}

