$j(function(){

	$j('form.reviewRating').submit(function(e){
		e.preventDefault();

		var reviewId = $j(this).find("input[name='reviewId']").val(),
			rating = 0,
			ratingCheckboxes = $j(this).find(".ratingResponse"),
			wrapper = $j("#ratingWrapper_" + reviewId),
			msgBox = $j("#ratingMsg_" + reviewId);
			
			if (ratingCheckboxes[0].checked) {
				rateReview(reviewId, ratingCheckboxes[0].value, msgBox, wrapper);
			} else if(ratingCheckboxes[1].checked) {			
				rateReview(reviewId, ratingCheckboxes[1].value, msgBox, wrapper);
			} else {
				msgBox.text("You have not selected whether the review was helpful or not helpful.");
				msgBox.effect("highlight",{color:"#ffffcc"},"slow");
			}
	});
	
	function rateReview(reviewId, rating, msgBox, wrapper){
		$j.ajax({
			type: "POST",
			url: 'HolidayReview/AddRatingToReview',
			dataType: "text",
			data: {
				content: 'rateTheatreReviewAjax',
				rating: rating,
				reviewId: reviewId,
				reviewType: "theatre"
			},
			success: function(data) {
				msgBox.text(data);
				wrapper.show();
			},
			error: function (xhr, ajaxOptions, thrownError){
				msgBox.text("Error: Cannot contact database. Please try again.");
				msgBox.effect("highlight",{color:"#ffffcc"},"slow");
			} 
		});
	}
});
