$(document).ready( function() {
	$(".reviewRow").each( function() {
		var id = $(this).find(".reviewId").text();
        //attach actions if necessary
        $(this).find(".helpfulBtn").click( function() {
                markHelpful(id);
            });
        $(this).find(".notHelpfulBtn").click( function() {
                markNotHelpful(id);
            });
	});
});

function disableHelpfulContent(id){
    $("#helpfuldiv_"+id).html("Thank You.");
}

function markHelpful(id){
    $.ajax({
                type: "POST",
                url: "/ratingsandreviews/markhelpful.tpb",
                data: {
                        id: id
                },
                async: true,
                error: function() { //user doesn't need to know this
                    disableHelpfulContent(id);
                },
                success: function(xml){
                        var errors = new Array();
                        
                        // Get any errors.
                        $("error", xml).each( function() {
                                errors[errors.length] = $(this).text();
                        });
                        
                        // If there are no errors reload the page.
                        if (errors.length == 0) {
                            disableHelpfulContent(id);
                        } else {
                            disableHelpfulContent(id);
                        }
                }
        });
}

function markNotHelpful(id){
    $.ajax({
                type: "POST",
                url: "/ratingsandreviews/marknothelpful.tpb",
                data: {
                        id: id
                },
                async: true,
                error: function() { //user doesn't need to know this
                    disableHelpfulContent(id);
                },
                success: function(xml){
                        var errors = new Array();
                        
                        // Get any errors.
                        $("error", xml).each( function() {
                                errors[errors.length] = $(this).text();
                        });
                        
                        // If there are no errors reload the page.
                        if (errors.length == 0) {
                            disableHelpfulContent(id);
                        } else {
                            disableHelpfulContent(id);
                        }
                }
        });
}
