/*jslint white: true, browser: true, devel: true, onevar: true, undef: true, nomen: true, eqeqeq: true, plusplus: true, bitwise: true, regexp: true, newcap: true, immed: true */

/**
	@fileOverview Shop list JavaScript functions
	@version 11.5
*/

/**
	@description Handles the XHR response when adding an item to a list.
	@since Pre v11.5
	@param {XHR Response Object} req The XHR response object.
	@requires prototype.js
	@see ajaxShopListAddItem
*/
var ajaxShopListAddItemResponse = function (req) {
	var success = req.responseXML.getElementsByTagName('success').item(0).firstChild.nodeValue,
		linkType = req.responseXML.getElementsByTagName('linkType').item(0).firstChild.nodeValue,
		itemId = req.responseXML.getElementsByTagName('itemId').item(0).firstChild.nodeValue;

	if (success === 'true') {
		$('list_response_' + linkType + "_" + itemId).innerHTML = 'This item has been added to your wishlist';
	}
};

/**
	@description Adds an Item to the user's wish-list via XHR.
	@author Revised by John Arthur (v11.5)
	@since v11.5
	@param {STRING} itemId The ID of the item being added to the list.
	@param {NUMBER} linkType The link type of the list the item is being added to.
	@param {NUMBER} sequence The sequence under which the list should fall.
	@param {NUMBER} [quantity = 1] The quantity of items being added to the list.
	@param {STRING} [listname = ''] The (optional) name of the list being added to.
	@param {STRING} [responseFunction = 'ajaxShopListAddItemResponse'] The name of the function to be used for the XHR response.
	@requires eval()
	@requires prototype.js
	@example &lt;a href="javascript:ajaxShopListAddItem(document.getElementById('recipeBoxIID').value, 400, document.getElementById('recipeBoxId').value, 1, document.getElementById('recipeBoxName').value, 'ajaxSwkShopListAddItemResponse')"><img src="/images/content/recipe_section/recipe_save.gif" alt="" /&gt; Save to Recipe Box&lt;/a&gt;
*/
var ajaxShopListAddItem = function (itemId, linkType, sequence, quantity, listName, responseFunction) {
	var url = '/ajax/com.eonegroup.eonecommerce.ajax.AJAXShopListAddItem',
		parms = "linkType=" + linkType + "&itemId=" + escape(itemId) + "&itemseq=" + 0 + "&sequence=" + sequence,
		myAjax;

	parms += '&quantity=' + (!quantity ? 1 : quantity);
	parms += '&listName=' + (!listName ? '' : escape(listName));

	responseFunction = responseFunction || 'ajaxShopListAddItemResponse';

	myAjax = new Ajax.Request(url, {postBody: parms, onComplete: eval(responseFunction)});
};
