$(document).ready(function() {
	// Send product
	$("#sendProductForm").live("submit", function(e) {
		try {
			var formData = $(this).serializeObject();
			jQuery.post('/pal/json/sendProductAJAXResponse.jsp', formData, function(data){
				if (data.success == "true") {
					// Show confirmation message and close
					$("#sendProductFormDiv").hide();
					$("#sendProductFormMessage").show();
					setTimeout(function () { $(".sendProductLink").colorbox.close(); }, 1700);
				}
				else {
					$("#sendProductFormErrors").html(errorsToHTML(data.errors)).show();
					$(".sendProductLink").colorbox.resize();
				}
			}, 'json');
		} catch(e) {
			alert (e);
		}
		
		return false;
	});
	
	
	// Public wishlist
	$("#sendWishlistForm").live("submit", function(e) {
		try {
			var formData = $(this).serializeObject();
			jQuery.post('/pal/json/sendPublicWishlistAJAXResponse.jsp', formData, function(data){
				if (data.success == "true") {
					// Show confirmation message and close
					$("#sendWishlistFormDiv").hide();
					$("#sendWishlistFormMessage").show();
					setTimeout(function () { $(".sendWishlistLink").colorbox.close(); }, 1700);
				}
				else {
					$("#sendWishlistFormErrors").html(errorsToHTML(data.errors)).show();
					$(".sendWishlistLink").colorbox.resize();
				}
			}, 'json');
		} catch(e) {
			alert (e);
		}
		
		return false;
	});
	
	// Private wishlist
	$("#sendPrivateWishlistForm").live("submit", function(e) {
		try {
			var formData = $(this).serializeObject();
			jQuery.post('/pal/json/sendPrivateWishlistAJAXResponse.jsp', formData, function(data){
				if (data.success == "true") {
					// Show confirmation message and close
					$("#sendPrivateWishlistFormDiv").hide();
					$("#sendPrivateWishlistFormMessage").show();
					setTimeout(function () { $(".sendPrivateWishlistLink").colorbox.close(); }, 1700);
				}
				else {
					$("#sendPrivateWishlistFormErrors").html(errorsToHTML(data.errors)).show();
					$(".sendPrivateWishlistLink").colorbox.resize();
				}
			}, 'json');
		} catch(e) {
			alert (e);
		}
		
		return false;
	});
});

function errorsToHTML(errors) {
	var message = "";
	var i;
	for (i = 0; i < errors.length; i++) {
		message += errors[i] + "<br />";
	}
	
	return message;
}

