function myPopupRelocate() {
 var scrolledX, scrolledY;
 if( self.pageYOffset ) {
   scrolledX = self.pageXOffset;
   scrolledY = self.pageYOffset;
 } else if( document.documentElement && document.documentElement.scrollTop ) {
   scrolledX = document.documentElement.scrollLeft;
   scrolledY = document.documentElement.scrollTop;
 } else if( document.body ) {
   scrolledX = document.body.scrollLeft;
   scrolledY = document.body.scrollTop;
 }

 var centerX, centerY;
 if( self.innerHeight ) {
   centerX = self.innerWidth;
   centerY = self.innerHeight;
 } else if( document.documentElement && document.documentElement.clientHeight ) {
   centerX = document.documentElement.clientWidth;
   centerY = document.documentElement.clientHeight;
 } else if( document.body ) {
   centerX = document.body.clientWidth;
   centerY = document.body.clientHeight;
 }

 var leftOffset = scrolledX + (centerX - 572) / 2;
 var topOffset = scrolledY + (centerY - 300) / 2;

 document.getElementById("mypopup").style.top = topOffset + "px";
 document.getElementById("mypopup").style.left = leftOffset + "px";
}

function fireMyPopup() {
 myPopupRelocate();
 document.getElementById("check1").checked = false;
 document.getElementById("check2").checked = false;
 document.getElementById("check3").checked = false;
 document.getElementById("check4").checked = false;
 document.getElementById("check5").checked = false;
 document.getElementById("check6").checked = false;
 document.getElementById("mypopup").style.display = "block";
 document.getElementById("thequiz").style.display="block";
 document.getElementById("theresult").style.display="none";
 document.body.onscroll = myPopupRelocate;
 window.onscroll = myPopupRelocate;
}
function doquiz() {
  var ta='script error!';

  document.getElementById("thequiz").style.display="none";
  document.getElementById("theresult").style.display="block";

  c1 = document.getElementById("check1").checked;
  c2 = document.getElementById("check2").checked;
  c3 = document.getElementById("check3").checked;
  c4 = document.getElementById("check4").checked;
  c5 = document.getElementById("check5").checked;
  c6 = document.getElementById("check6").checked;

  var count = 0;
  if (c1) count++;
  if (c2) count++;
  if (c3) count++;
  if (c4) count++;
  if (c5) count++;
  if (c6) count++;

  ta = 'script error: 003407-' + count;

  if (count == 1) {
    if (c1) ta = '!Based on your input, you have a sleep problem. Since you have trouble falling asleep, we would recommend using <a href="http://www.sleepproductsreview.com/reposanex.htm" style="color:#830000;"> Reposanex </a>';
    else if (c2) ta = 'Based on your input, you have a sleep problem. Since you have trouble staying asleep, we would recommend using <a href="http://www.sleepproductsreview.com/reposanex.htm" style="color:#830000;"> Reposanex </a>';
    else if (c3) ta = 'Based on your input, you have a sleep problem. Since you awake several times during the night, we would recommend that you try <a style="color:#840000;" href="http://www.sleepproductsreview.com/melotrex.htm"> Melotrex </a>';
    else if (c4) ta = 'Based on your input, you have a sleep problem. Since you have trouble resuming sleep once awakened, we would recommend that you try <a style="color:#840000;" href="http://www.sleepproductsreview.com/sleep2night.htm"> Sleep-2-Night </a>';
    else if (c5) ta = 'Based on your input, you have a sleep problem. Since you seldom awake feeling refreshed, we would recommend that you try <a style="color:#840000;" href="http://www.sleepproductsreview.com/7hourslumber.htm"> 7-Hour Slumber </a>';
    else ta = 'Based on your input, you have a sleep problem. Since you have trouble sleeping long enough, we would recommend that you try <a style="color:#840000;" href="http://www.sleepproductsreview.com/sleep2night.htm"> Sleep-2-Night </a>';
  }
  else if (count == 5) {
    ta = 'Based on your input, you have a sleep problem. Since you have trouble getting to sleep and multiple other sleep issues, we would recommend that you try <a style="color:#840000;" href="http://www.sleepproductsreview.com/reposanex.htm"> Reposanex. </a>'
  }
  else if (count == 6) {
   ta = 'Based on your input, you have a severe sleep problem. We would recommend that you first consult your physician regarding some of the prescription medication options.  You could also try <a style="color:#840000;" href="http://www.sleepproductsreview.com/reposanex.htm"> Reposanex. </a>'
  }
  else if (count == 0) {
   ta='Congratulations! Based on your input, you do not have a sleep problem.';
  }
  else if ((c5 && count == 2) && (c3 || c4 || c6)) {
     ta = 'Based on your input, you have a sleep problem. Since you seldom awake feeling refreshed, and are not getting enough sleep, we would recommend that you try <a style="color:#840000;" href="http://www.sleepproductsreview.com/7hourslumber.htm"> 7-Hour Slumber </a>';
  }
  else if (c4 && c6 && count == 2) {
    ta = 'Based on your input, you have a sleep problem. Since you have trouble sleeping long enough, and resuming sleep if awakened, we would recommend that you try <a style="color:#840000;" href="http://www.sleepproductsreview.com/sleep2night.htm"> Sleep-2-Night </a>';
  }
  else if (c1 && c2 && count >= 2 && !c5 && !c6) {
    ta = 'Based on your input, you have a sleep problem. Since you have trouble falling asleep and staying asleep, we would recommend using <a href="http://www.sleepproductsreview.com/reposanex.htm" style="color:#830000;"> Reposanex </a>';
  }
  else if (c4 && c6 && count > 2) {
    ta = 'Based on your input, you have a sleep problem. Since you have trouble resuming sleep once awakened and usually awake too soon, we would recommend that you try <a style="color:#840000;" href="http://www.sleepproductsreview.com/sleep2night.htm"> Sleep-2-Night </a>';
  }
  else if (c2 && count == 3) {
    ta = 'Based on your input, you have a sleep problem. Since you have trouble staying asleep, we would recommend using <a href="http://www.sleepproductsreview.com/reposanex.htm" style="color:#830000;"> Reposanex </a>';
  }
  else if (c3 && count == 2) {
    ta ='Based on your input, you have a sleep problem. Since you awake several times during the night, we would recommend that you try <a style="color:#840000;" href="http://www.sleepproductsreview.com/melotrex.htm"> Melotrex </a>';
  }
  else {
    ta = 'Based on your input, you have a sleep problem. Since you have multiple, but not severe sleep issues, we would recommend using <a href="http://www.sleepproductsreview.com/reposanex.htm" style="color:#830000;"> Reposanex </a>';
  }

  document.getElementById("theanswer").innerHTML=ta;

}
