var SeenExpTotalStars = 0;

function doclick(planNum,newVal) {
    var pvar;
    var imgNameY;
    var imgNameN;

    pvar = "p" + planNum;

    imgNameY = "py" + planNum;
    imgNameN = "pn" + planNum;

// winndow.status = "doclick: imgNameY = " + imgNameY + " imgNameN = " + imgNameN;

    if( newVal == 1 ) {     // answer is yes
	document[imgNameY].src = "images/yes-on.gif";
	document[imgNameN].src = "images/no-off.gif";
    }
    else {     // answer is no
	document[imgNameY].src = "images/yes-off.gif";
	document[imgNameN].src = "images/no-on.gif";
    }
    PlanChoices[planNum-1] = newVal;

    var x = PlanChoices.join("");

    SetCookie("expchoices", x);
//  window.status = GetCookie("planchoices");

    return;
}



//todo: redo for each plan
function ExpTotalStars(planNum) {
    var imgName, numImage;
    var i,j,totStars;

    for(i=1; i<=NumPlans; i++) {
	imgName = "ps" + i;
	for(j = 0, totStars = 0; j < NumQues; j++)
	    totStars = totStars + ExpStars[i][j];
	numImage = "images/" + totStars + ".gif";  // eg, "images/18.gif"
	document[imgName].src = numImage;
	//window.status = numImage + " " + imgName;
    }
    SetCookie("SeenExpTotalStars",1);
    return;
}

function ShowPlanName(plan) {
  document.write( PlanNames[plan] );
}


function ShowExpUserPlanRow(plan,doextracols) {
  document.write( '<tr>' );
  document.write( '<td bgcolor="#eeeecc"><font size=-1>'
    + PlanNames[plan] + '</font></td>' );

  var i;
  var starImage;

  for(i = 0; i < NumQues; i++) {
    starImage = "c" + ExpStars[plan][i] + "star.gif";  // eg, "c3star.gif"
    document.write( '<td bgcolor="#ffffff"><img src="images/' + starImage + '"></td>' );
  }

  if( doextracols != 1 ) { // don't display columns: total stars and yes/no choices
    document.write( '</tr>' );
    return;
  }

  // after clicking to see total stars, we always show the totals when
  // the page is viewed again
  if( SeenExpTotalStars ) {
    var j,tot;
    for( j=0,tot=0; j<NumQues; j++ ) {
	tot += ExpStars[plan][j];
    }
    var img = "images/" + tot + ".gif";
    document.write( '<td align=center bgcolor="#f8e591">' +
      '<img name=ps' + plan + ' src=' + img + '></a><br></td>'
      );
  }
  else {
    document.write( '<td align=center bgcolor="#f8e591">' +
      '<a href="#" onClick="ExpTotalStars(' + plan + ');return false" ' +
      'onMouseOver="self.status=\'Click to total stars\'">' +
      '<img name=ps' + plan + ' src="images/clickhere.gif" border=0></a><br></td>'
    );
  }

  var yesImage, noImage;
  var x = GetCookie("expchoices");
  if( x != null )
    PlanChoices = x.split("");
  if( PlanChoices[plan-1] == 1 ) {
    yesImage = "images/yes-on.gif";
    noImage = "images/no-off.gif";
  }
  else if( PlanChoices[plan-1] == 2 ) {
    yesImage = "images/yes-off.gif";
    noImage = "images/no-on.gif";
  }
  else {
    yesImage = "images/yes-off.gif";
    noImage = "images/no-off.gif";
  }

  document.write( '<td bgcolor="#eeeecc"><center>' +
    '<a href="#" onClick="doclick(' + plan + ',1);return false" ' +
    'onMouseOver="self.status=\'Click to answer yes\'">' +
    '<img name=py' + plan + ' src="' + yesImage + '" border=0></a><br>' +
    '<a href="#" onClick="doclick(' + plan + ',2);return false" ' +
    'onMouseOver="self.status=\'Click to answer no\'">' +
    '<img name=pn' + plan + ' src="' + noImage + '" border=0></a></center></td>'
  );
  document.write( '</tr>' );
}

function ShowExpUserPlans(xtracols) {
  var i;

  if( xtracols )
    SeenExpTotalStars = GetCookie("SeenExpTotalStars");

//for(i = 1; i <= NumPlans; i++)
//  ShowExpUserPlanRow(i, xtracols);

  var pt, planNo;
  var cSpan = 8;
  for (pt in PlanTypes) {
    if(xtracols) cSpan = 10;
    document.write('<tr><td colspan=' + cSpan + ' bgcolor="#eeeecc"><b>' + pt + 's</b></td></tr>');
    list = PlanTypes[pt].split(",");
    for (i in list) {
      ShowExpUserPlanRow( list[i], xtracols );
    }
  }

}


