/*
 * AJAX form post function 
 */
function voteHelpful(guid, isHelpful){
	if ($.cookie("vrRevHelpSub"+guid)) {
		handleVoteReSubmit(guid);
	} else {
		$.ajax({
			url : '/reviews/vote.json',
			data : ({review : guid, helpful : isHelpful}),
			type : 'POST',
			dataType : 'json',
			cache : false,
			timeout : 10000,
			success : handleVoteData,
			error : handleVoteError 
		});
	}
	//return false;
}

/*
 * Handle the retrieve data from the AJAX request
 */
function handleVoteData(data, textState) {		
	$.cookie("vrRevHelpSub"+data._guid, 0);
	if ($('.review-helpful').is(':hidden')) {
		$('.review-helpful').show();
	}
	$('#'+data._guid+'_helpfulLinks').text('Your vote has been accepted.');
	$('#'+data._guid+'_helpfulCount').text(data._helpfulCount);
	$('#'+data._guid+'_totalHelpfulCount').text(data._totalCount);
	return false;
}

/*
 * Handle the error data from the AJAX request
 */
function handleVoteError(XMLHttpRequest, textStatus, errorThrown) {
	if (textStatus == 'timeout') {
		alert('The request took too long. Please try again.');
	} else {
		alert('An error occured. Please try again or contact customer support.');
	}
}

/*
 * Handle resubmission of a vote
 */
function handleVoteReSubmit(guid) {
	$('#'+guid+'_helpfulLinks').text('You have already voted on this review.');
}

