


function ProductRatingContext(containerId, errorMsg) {
	this.containerId = containerId;
	if (errorMsg) {
		this.errorMessage = errorMsg;
	} else {
		this.errorMessage = "<em>An error occurred processing your rating, please try again later.</em>";
	}
}

var productRatingContext = new ProductRatingContext();
var filledStar = new Image();
var emptyStar = new Image();
filledStar.src = "http://gfx3.gamelink.com/store/white_background/star.gif";
emptyStar.src = "http://gfx3.gamelink.com/store/white_background/star_empty.gif";

function updateStars(imageNamePrefix, rating) {
	var newVal = 0;
	if (rating) { 
		newVal = rating;
	}
	for (var i = 1; i <= 5; i++) {
		var starImg = document.images[imageNamePrefix+i];
		if (starImg) {
			if (newVal >= i) {
				starImg.src = filledStar.src;
			} else {
				starImg.src = emptyStar.src;
			}
		}
	}
}

function onRatingLoad() {
    var reply = this.req.responseText;
    if (reply) {
      var container = document.getElementById(productRatingContext.containerId);
      if (container) {
        container.innerHTML = reply;
      }
    }
}

function showError() {
    var container = document.getElementById(productRatingContext.containerId);
    if (container) {
      container.innerHTML = productRatingContext.errorMessage;
    }
}
  
function submitRating(productId, score, context, showavg, containerId, msgBoxId, formatGroupId ) {
	productRatingContext.containerId = containerId;
	showRatingMessage(msgBoxId, null, "Saving...");
	new net.ContentLoader("/includes/productRating.jhtml?action=score&actionValue=" + score + "&productId=" + productId + "&context=" + context + "&containerId=" + productRatingContext.containerId + "&msgBoxId=" + msgBoxId + "&fgid=" + formatGroupId + "&showavg=" + showavg, onRatingLoad, showError);
}

function submitSceneRating(productId, sceneId, score, context, showavg, containerId, msgBoxId ) {
	productRatingContext.containerId = containerId;
	showRatingMessage(msgBoxId, null, "Saving...");
	new net.ContentLoader("/includes/sceneRating.jhtml?action=score&actionValue=" + score + "&productId=" + productId + 
		"&context=" + context + "&sceneID=" + sceneId + "&containerId=" + productRatingContext.containerId + 
		"&msgBoxId=" + msgBoxId + "&showavg=" + showavg, onRatingLoad, showError);
}

function showRatingMessage(ratingMessageBoxId, rating, defaultMessage) {
  	var messageBox = document.getElementById(ratingMessageBoxId);
  	if (messageBox) {
	  var message = "";
	  if (rating) {
		  switch (rating) {
			  case 1:
				message = "Terrible";
				break;
			  case 2:
				message = "Bad";
				break;
			  case 3:
				message = "Average";
				break;
			  case 4:
				message = "Good";
				break;
			  case 5:
				message = "Great";
					break;
			  };
		  } else if (defaultMessage) {
			  message = defaultMessage;
		  }
		  messageBox.innerHTML = message;
	}
}