// JavaScript Document

var updateWishlistComments = function() {

	var request = '/popups/actions/wishlistcomments.asp?type=ajax&wl_comment=' + $('WL_Comment').value.replace(/\n/g, "<br/>");

	var ajax = new Ajax.Request(request, {

		method: 'get',
		requestHeaders: {Accept: 'application/json'},
		onSuccess: function(transport) {

			$('divWishlistComments').update($('WL_Comment').value.replace(/\n/g, "<br/>"));
			hideIbox();

		},

	  	onFailure: function(transport){ alert('There was an error sending the data') }

	});

}

var updateWishlistItemComments = function(WLI_ID) {

	var request = '/popups/actions/wishlistitemcomments.asp?type=ajax&wli_id=' + WLI_ID + '&wli_comment=' + $('WLI_Comment').value.replace(/\n/g, "<br/>");

	var ajax = new Ajax.Request(request, {

		method: 'get',
		requestHeaders: {Accept: 'application/json'},
		onSuccess: function(transport) {

			var WLI_Comment = $('WLI_Comment').value.replace(/\n/g, " ");

			if (WLI_Comment.length > 85) WLI_Comment = WLI_Comment.substring(0, 85) + '...';

			$('spaWishlistItemComments_' + WLI_ID).update(WLI_Comment);
			hideIbox();

		},

	  	onFailure: function(transport){ alert('There was an error sending the data') }

	});

}

var updateWishlist = function() { 

	var request = '/actions/wishlist_update.asp?type=ajax&ajaxprocess=update&' + $('frmSubmitWishlist').serialize();

	var ajax = new Ajax.Request(request, {

		method: 'get',
		requestHeaders: {Accept: 'application/json'},
		onSuccess: function(transport) {

			var response = transport.responseText.evalJSON(true);
			
			// Reset the wishlist info
			$('spaItemCount').update(response.wishlist.WL_ItemCount + ' (' + response.wishlist.WL_ItemQuantityCount + ')');
			$('spaTotalPrice').update(parseFloat(response.wishlist.WL_TotalPrice).toFixed(2));
			$('divWishlistComments').update(response.wishlist.WL_Comment.replace(/\n/g, "<br/>"));

			// Reset the wishlist items info
			for (iLoopA = 0; iLoopA < response.wishlist.wl_items.wl_item.length; iLoopA++)
			{
				$('spaItemPrice_' + response.wishlist.wl_items.wl_item[iLoopA].WLI_ID).update(parseFloat(response.wishlist.wl_items.wl_item[iLoopA].WLI_ItemPrice).toFixed(2));
				$('spaLinePrice_' + response.wishlist.wl_items.wl_item[iLoopA].WLI_ID).update(parseFloat(response.wishlist.wl_items.wl_item[iLoopA].WLI_LinePrice).toFixed(2));
				$('spaFabricItemPrice_' + response.wishlist.wl_items.wl_item[iLoopA].WLI_ID).update(parseFloat(response.wishlist.wl_items.wl_item[iLoopA].WLIF_ItemPrice).toFixed(2));
				$('spaFabricLinePrice_' + response.wishlist.wl_items.wl_item[iLoopA].WLI_ID).update(parseFloat(response.wishlist.wl_items.wl_item[iLoopA].WLIF_LinePrice).toFixed(2));
			}

		},

	  	onFailure: function(transport){ alert('There was an error sending the data') }

	});

}

var emptyWishlist = function() { 

	var request = '/actions/wishlist_update.asp?type=ajax&ajaxprocess=empty';

	var ajax = new Ajax.Request(request, {

		method: 'get',
		requestHeaders: {Accept: 'application/json'},
		onSuccess: function(transport) {

			var request = '/templates/sensoryplus/webshop/Wishlist_NoItems.html';

			var ajax = new Ajax.Request(request, {

				method: 'get',
				requestHeaders: {Accept: 'text/html'},
				onSuccess: function(transport) {

					$('divContentContainer').update(transport.responseText);

				},

				onFailure: function(transport){ alert('There was an error sending the data') }
		
			});

		},

	  	onFailure: function(transport){ alert('There was an error sending the data') }

	});

}

var submitWishlist = function() { 

	var request = '/actions/wishlist_update.asp?type=ajax&ajaxprocess=submit';

	var ajax = new Ajax.Request(request, {

		method: 'get',
		requestHeaders: {Accept: 'application/json'},
		onSuccess: function(transport) {

			var response = transport.responseText.evalJSON(true);
			location.href = '/wishlist_submitted_view.asp?WL_ID=' + response.wishlist.WL_ID;

		},

	  	onFailure: function(transport){ alert('There was an error sending the data') }

	});

}

var addToBasket = function() { 

	var request = '/actions/wishlist_update.asp?type=ajax&ajaxprocess=addtobasket';

	var ajax = new Ajax.Request(request, {

		method: 'get',
		requestHeaders: {Accept: 'application/json'},
		onSuccess: function(transport) {

			$('spaInformation').update('Your wishlist has been added to the basket. <a href=\"/basket.asp\">Click here to view your basket</a>');

		},

	  	onFailure: function(transport){ $('spaInformation').update('There was an error adding your wishlist to the basket, please try again') }

	});

}

var removeWishlistItem = function(WLI_ID, WLIT_ID) { 

	var request = '/actions/wishlist_update.asp?type=ajax&ajaxprocess=remove&WLI_ID=' + WLI_ID + '&WLIT_ID=' + WLIT_ID;

	var ajax = new Ajax.Request(request, {

		method: 'get',
		requestHeaders: {Accept: 'application/json'},
		onSuccess: function(transport) {

			var response = transport.responseText.evalJSON(true);

			if (response.wishlist.WL_ItemCount == 0)
			{
				var request = '/templates/sensoryplus/webshop/Wishlist_NoItems.html';

				var ajax = new Ajax.Request(request, {

					method: 'get',
					requestHeaders: {Accept: 'text/html'},
					onSuccess: function(transport) {

						$('divContentContainer').update(transport.responseText);

					},

					onFailure: function(transport){ alert('There was an error sending the data') }
			
				});
			}

			// Reset the wishlist info
			$('spaItemCount').update(response.wishlist.WL_ItemCount + ' (' + response.wishlist.WL_ItemQuantityCount + ')');
			$('spaTotalPrice').update(parseFloat(response.wishlist.WL_TotalPrice).toFixed(2));
			$('divWishlistComments').update(response.wishlist.WL_Comment.replace(/\n/g, "<br/>"));

			// Remove the item from the table
			switch (WLIT_ID)
			{
				case '1':
					$('trRow1_' + WLI_ID).remove();
					$('trRow2_' + WLI_ID).remove();
					$('trRow3_' + WLI_ID).remove();
					break;

				case '2':
					$('trRow1_' + WLI_ID).remove();
					$('trRow2_' + WLI_ID).remove();
					break;
			}

			// Reset the colourings of the remaining rows...
			var bAltItem = true;
			var strAltItemClass = '';

			for (iLoopA = 0; iLoopA < response.wishlist.wl_items.wl_item.length; iLoopA++)
			{
				if (bAltItem == true)
				{
					bAltItem = false;
					strAltItemClass = '';
				}
				else
				{
					bAltItem = true;
					strAltItemClass = 'tblWishlistItems_Alt';
				}

				switch (WLIT_ID)
				{
					case '1':
						$('trRow1_' + response.wishlist.wl_items.wl_item[iLoopA].WLI_ID).removeClassName('tblWishlistItems_Alt');
						$('trRow2_' + response.wishlist.wl_items.wl_item[iLoopA].WLI_ID).removeClassName('tblWishlistItems_Alt');
						$('trRow3_' + response.wishlist.wl_items.wl_item[iLoopA].WLI_ID).removeClassName('tblWishlistItems_Alt');
						$('trRow1_' + response.wishlist.wl_items.wl_item[iLoopA].WLI_ID).addClassName(strAltItemClass);
						$('trRow2_' + response.wishlist.wl_items.wl_item[iLoopA].WLI_ID).addClassName(strAltItemClass);
						$('trRow3_' + response.wishlist.wl_items.wl_item[iLoopA].WLI_ID).addClassName(strAltItemClass);
						break;
	
					case '2':
						$('trRow1_' + response.wishlist.wl_items.wl_item[iLoopA].WLI_ID).removeClassName('tblWishlistItems_Alt');
						$('trRow2_' + response.wishlist.wl_items.wl_item[iLoopA].WLI_ID).removeClassName('tblWishlistItems_Alt');
						$('trRow1_' + response.wishlist.wl_items.wl_item[iLoopA].WLI_ID).addClassName(strAltItemClass);
						$('trRow2_' + response.wishlist.wl_items.wl_item[iLoopA].WLI_ID).addClassName(strAltItemClass);
						break;
				}
			}

		},

	  	onFailure: function(transport){ alert('There was an error sending the data') }

	});

}