

function showReviewSummary() {

if (numRatings > 0) {
	document.writeln("<div><span style=\"font-weight:bold;\">");
	document.writeln("Average Rating: ");
	var starName = getStarName(avgRating);	
	if (starName.length>1) {
		document.writeln("<img src=\"/images/public/ratings_stars/stars-" + starName + ".gif\" align=\"absmiddle\"><\/span>&nbsp;" + avgRating + "&nbsp;<a href=\"#reviews\">(" + numRatings + " reviews)<\/a>");
	}
	document.writeln("<\/div>");
	document.writeln("<div><a href=\"/popup/addreview.html?objid=" + objid + "\" rel=\"facebox\">Click here</a> to add or update a review for this product.<\/div>");
} else {
	document.writeln("<div><span style=\"font-weight:normal;\">");
	document.writeln("No reviews for this item yet.  Be the first to review - <a href=\"/popup/addreview.html\" rel=\"facebox\">click here</a>!");
	document.writeln("<\/div>");

}


}




function getStarName(ratingVal) {
var starName = "";
if (ratingVal > .99) {
	starName = "1-0";
}
if (ratingVal > 1.49) {
	starName = "1-5";
}
if (ratingVal > 1.99) {
	starName = "2-0";
}
if (ratingVal > 2.49) {
	starName = "2-5";
}
if (ratingVal > 2.99) {
	starName = "3-0";
}
if (ratingVal > 3.49) {
	starName = "3-5";
}
if (ratingVal > 3.99) {
	starName = "4-0";
}
if (ratingVal > 4.49) {
	starName = "4-5";
}
if (ratingVal == 5) {
	starName = "5-0";
}
return(starName);
}




function updateReviewSampleStar(imageElementId, ratingElement) {
var idx = ratingElement.selectedIndex;
var ratingVal = ratingElement.options[idx].value;
var starName = getStarName(ratingVal);
var imageElement = document.getElementById(imageElementId);

if (starName.length>1) {
	imageElement.src = ("/images/public/ratings_stars/stars-" + starName + ".gif");
}

}





function addReview(theForm) {
var ratingIdx = theForm.elements['productRating'].selectedIndex;
var ratingVal = theForm.elements['productRating'].options[ratingIdx].value;
var ratingReviewText = theForm.elements['productReview'].value;
var productObjid = theForm.elements['objid'].value;

var url = ("/cgi-bin/catch_product_review.cgi");
var vars = ("objid=" + productObjid + "&rating=" + ratingVal + "&review=" + ratingReviewText);
alert("Sending " + vars + " to " + url);
sendAjax(url, vars, catchReviews);

setTimeout("updateReviewSummary()", 1000);

jQuery(document).trigger('close.facebox');

//alert("Thanks for adding a review.");
var addReviewCanvas = document.getElementById('canvasAddReview');
addReviewCanvas.innerHTML = ("<div>Thanks for adding a review.<\/div>");

}



function catchReviews(theHTML) {
//alert("Getting Review HTML: " + theHTML.length + " chars. in length");

var reviewsCanvas = document.getElementById('canvasReviews');
reviewsCanvas.innerHTML = theHTML;

//document.location.href = (originalURL + "#reviews");
}




function catchReviewSummary(theHTML) {
//alert("Getting Review HTML: " + theHTML.length + " chars. in length");

var reviewsCanvas = document.getElementById('canvasReviewSummary');
reviewsCanvas.innerHTML = theHTML;

//document.location.href = (originalURL + "#reviewSummary");
}




function sendAjax(url, vars, callbackFunction) {
//alert("Sending review to: " + url);

var request = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("MSXML2.XMLHTTP.3.0");

request.open("POST", url, true);
request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 

request.onreadystatechange = function(){

		if (request.readyState == 4 && request.status == 200) {

				if (request.responseText){

						callbackFunction(request.responseText);
				}
		}
}
request.send(vars);

}



function sendReviewFeedback(reviewid, ratingVal) {
var url = ("/cgi-bin/catch_product_review_feedback.cgi");
var vars = ("objid=" + objid + "&rating=" + ratingVal + "&id=" + reviewid);
sendAjax(url, vars, catchReviews);

}





function updateReviewSummary() {
var url = ("/cgi-bin/get_product_review_summary.cgi");
var vars = ("objid=" + objid);
sendAjax(url, vars, catchReviewSummary);
}



