/* START ADD BOOKMARK DEAL FUNCTIONS */

function bookmarkDealHandleSuccess(response, status) {
	// Handle Response

	/*
	Return Values:
	> 0 - Success, number returned is entry_id
	-1 - Already Bookmarked
	-2 - Not Logged In
	*/

	if (response == '-2') {
		// User not logged in
		$('#overlay1').show().center();
		return;
	}
	if (response == '-1') {
		// Already bookmarked
		$('#overlay2').show().center();
		return;
	}
	
	// If we reach here, Bookmark was successful
	
	if(window.location.toString().indexOf('garagesales') != -1) {
		$('#bookmarkDealButton-'+response).html('<a href="/deals/main.php/mygaragesales/">Saved to list!</a>');
	} else {
		$('#bookmarkDealButton-'+response).html('Saved to list!');
	}
}

function bookmarkDealHandleFailure(request, status) {
	alert('Unable to fulfill request. Please try again later:\nHTTP status:  ' + status);
}

function bookmarkDeal(ACT, entry_id) {
	$.ajax({
		type : 'POST',
		url : '/deals/main.php',
		data : 'ACT=' + ACT + '&entry_id=' + entry_id,
		success : bookmarkDealHandleSuccess,
		error : bookmarkDealHandleFailure
	});
}

/* END ADD BOOKMARK DEAL FUNCTIONS */

/* START REMOVE BOOKMARK DEAL FUNCTIONS */

function removeBookmarkHandleSuccess(response, status) {
	// Handle Response

	/*
	n > 0 	- Success, number returned is entry_id
	-1 		- Not Bookmarked
	-2 		- Not Logged In
	-3		- Missing entry_id
	*/
	
	if (response == '-2') {
		// User not logged in
		$('#overlay1').show().center();
		return;
	}
	if (response == '-1') {
		// Not bookmarked
		$('#overlay2').show().center();
		return;
	}
	if (response == '-1') {
		// No Entry Id passed
		alert('Entry ID Missing!');
		return;
	}

	// If we reach here, Bookmark Delete was successful
	
	var strs = response.split(":");
	$('#bookmarkListing #bookmarks-'+strs[0]).remove();
	$('#bookmarkListingTotal').html(strs[1]);
};

function removeBookmarkHandleFailure(request, status) {
	alert('Unable to fulfill request. Please try again later:\nHTTP status:  ' + status);
}

function removeBookmark(ACT, entry_id){
	$.ajax({
		type : 'POST',
		url : '/deals/main.php',
		data : 'ACT=' + ACT + '&entry_id=' + entry_id,
		success : removeBookmarkHandleSuccess,
		error : removeBookmarkHandleFailure
	});
}
/* END REMOVE BOOKMARK DEAL FUNCTIONS */