
//Rating

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 = "star.gif";
emptyStar.src = "star_empty.gif";//"greystar.gif";; 


//Called from mouseover stars //what about multiple rates on page! use prefix!
function updateStars(prefix,rating) {
	var newVal = 0;
	if (rating) { 
		newVal = rating;
	}

	for (var i = 1; i <= 5; i++) {
		var starImg = document.images[prefix+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;
    }
}
 
//called when star is clicked
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);
}
  

//onmouseover calls this
function showRatingMessage(msgid,rating) {
	  var messageBox = document.getElementById(msgid);
	  if (messageBox) {
		  var message = "";
		  if (rating) {
			  switch (rating) {
				  case 1:
					message = "Not Recommended";
					break;
				  case 2:
					message = "Below Average";
					break;
				  case 3:
					message = "Average";
					break;
				  case 4:
					message = "Good";
					break;
				  case 5:
					message = "Excellent";
					break;
			  };
		  } else  {
			  message = "Rate this restaurant";
		  }
		  messageBox.innerHTML = message;
	  }
  }
