$(document).ready(function(){
	$('#review_form').submit(function(){
		if( $('input[name=prod_id]').val() != undefined && ($('input.star:checked:last').rating().attr('value') != undefined || $('textarea[name=review-text]').val() != '') ){
			var sent = {
				prod_id: $('input[name=prod_id]').val(),
				rating: $('input.star:checked:last').rating().attr('value'),
				review: $('textarea[name=review-text]').val(),
				json: true
			};

			$.getJSON('/reviews.php', sent, function(received){
				if(received == true){
					// Disable the input fields:
					$('input[name=review-rating]').rating('readOnly',true); 
					$('#review-text').attr('disabled', true);
					$('#review-submit input').attr('disabled', true);

					// Show a confirmation message:
					alert('Thank you. Your review has been submitted and is pending moderation.');
				} else{
					// Show an error message:
					alert('Sorry, there was an error submitting your review. Please try again later.');
				}
			});
		} else{
			alert('Please rate or comment on this product to submit a review.');
		}

		return false;
	}); 

	$('.review-partial a, .review-full a').click(function(){
		// Toggle the visibility of the previews:
		$(this).parent().parent().children('.review-partial').toggle();
		$(this).parent().parent().children('.review-full').toggle();

		return false;
	});
});
