// ----------------------------------------------
// File:		AddSavedItemService.js
// Author:		Ryan Ma
// Description:	A class that holds the available service methods, along with response handlers.
// Example:
// AddSavedItemService.getInstance().processAddToBag(sku,quantity);
// ----------------------------------------------


// ----------------------------------------------
// Function:	AddSavedItemService
// Author:		Ryan Ma
// Description:	Base class
// Inputs:		<none>
// Returns:		<nothing>
// ----------------------------------------------
function AddSavedItemService(){}

// ----------------------------------------------
// Function:	AddSavedItemService.processAddToBag
// Author:		Ryan Ma
// Description:	Calls the service to add a sku to the shopping bag
// Inputs:		<String> argSku - the sku number of the item to add
//				<int> argQuantity - the number of items to add
// Returns:		<nothing>
// ----------------------------------------------
AddSavedItemService.prototype.processSavedItem = function(argSku, argSelectedSku, argCId, argMCId, argCtx)
{
	confirmationChangeSignOut();
	try
	{
//		Debug.windowTrace("processSavedItem("+argSku+", "+argSelectedSku+", "+argCId+", "+argMCId+")");
		//PageMethods.AddSavedItem= function(sku,selectedSku,cid,mcid,onSuccess,onFailed,userContext)
		PageMethods.AddSavedItem(argSku, argSelectedSku, argCId, argMCId, this.onResult, this.onError, argCtx);
	}
	catch (err)
	{
		Debug.error(err);
	}
};
AddSavedItemService.prototype.processSavedEngagementItem = function(argSku, argSelectedSku, argCId, argMCId, argCtx)
{
	try
	{
		//PageMethods.AddToEngagementSavedItems(sku,selectedSku,cid,mcid,onSuccess,onFailed,userContext)
		PageMethods.AddToEngagementSavedItems(argSku, argSelectedSku, argCId, argMCId, this.onResult, this.onError, argCtx);
	}
	catch (err)
	{
		Debug.error(err);
	}
};
AddSavedItemService.prototype.processSavedCatalogueItem = function(argSku, argSelectedSku, argCId, argMCId, argCat_id, argCat_item_id, argP_cat_id, argP_cat_item_id, argCtx)
{
	try
	{
		//PageMethods.AddToEngagementSavedItems(sku,selectedSku,cid,mcid,onSuccess,onFailed,userContext)
		PageMethods.AddSavedItem(argSku, argSelectedSku, argCId, argMCId, argCat_id, argCat_item_id, argP_cat_id, argP_cat_item_id, this.onResult, this.onError, argCtx);
	}
	catch (err)
	{
		Debug.error(err);
	}
};
AddSavedItemService.prototype.processMultipleSavedItems = function(argSku, argSkuArray, argSelectedSku, argCId, argMCId, argCtx)
{
	try
	{
		var skuArrayAsString = argSkuArray.join(":");
		PageMethods.AddMultipleSavedItems(argSku, skuArrayAsString, argSelectedSku, argCId, argMCId, this.onResult, this.onError, argCtx);
	}
	catch (err)
	{
		Debug.error(err);
	}
}
AddSavedItemService.prototype.processMultipleSavedCatalogueItems = function(argSku, argSkuArray, argSelectedSku, argCId, argMCId, argCat_id, argCat_item_id, argP_cat_id, argP_cat_item_id, argCtx)
{
	try
	{
		var skuArrayAsString = argSkuArray.join(":");
		PageMethods.AddMultipleSavedItems(argSku, skuArrayAsString, argSelectedSku, argCId, argMCId, argCat_id, argCat_item_id, argP_cat_id, argP_cat_item_id, this.onResult, this.onError, argCtx);
	}
	catch (err)
	{
		Debug.error(err);
	}
}

// ----------------------------------------------
// Function:	AddSavedItemService.onResult
// Author:		Ryan Ma
// Description:	Callback from the successful completion of the service call
// Inputs:		<XMLElement> result - Handle to the results xml object
// Returns:		<nothing>
// ----------------------------------------------
AddSavedItemService.prototype.onResult = function(result, argCtx)
{
	try
	{
//		Debug.windowTrace("onResult("+result+")");
		var docElementNode = result.documentElement;
		var resultObj = {};
		var skuNodeArray = docElementNode.getElementsByTagName("Sku");
		resultObj.resStatus = XMLUtils.getNodeValue(skuNodeArray[0], "Status");
		resultObj.bagCount = XMLUtils.getNodeValue(docElementNode, "BagCount");
		resultObj.smallImage = XMLUtils.getNodeValue(docElementNode, "Image");

//		Address Mac FF issue with fading in and out layers over flash by making it just pop in and out.
		var enableMassFadeEffect = true;
		if((agt.indexOf('firefox') > 0) && (agt.indexOf('mac') > 0) && typeof(removeMassFadeEffect) != "undefined" ) {enableMassFadeEffect = false;}
//		Debug.windowTrace("resStatus="+resultObj.resStatus);
//		Debug.windowTrace("smallImage="+resultObj.smallImage);
//		Debug.windowTrace("bagCount="+resultObj.bagCount);
		//1. if response is a success message
		if (resultObj.resStatus == "SUCCESS") {
			//a. update wishlist label with total number of items
			updateSavedCount(resultObj.bagCount);

			//b. display success indicator (page 4 of wireframe)
			var confirmWidget = document.getElementById("addConfirmation");
			document.getElementById("confirmImage").src = resultObj.smallImage;
			document.getElementById("confirmMessage").innerHTML = LABEL_CONFIRM_ADD_TO_WISHLIST;
			if(enableMassFadeEffect) {setOpacity(confirmWidget, 100);}
			confirmWidget.className = "confirmSaved";
			confirmWidget.style.display = "block";
			//c. setTimeout for success indicator to go away
			if (enableMassFadeEffect) {
			MassFadeEffect.fadeCompleteHandler = function () {confirmWidget.style.display = "none";};
			setTimeout(function() {massFadeInstance.addTarget("addConfirmation", -20);}, 2000);
			}
			else {
			setTimeout(function() {confirmWidget.style.display = "none";}, 2000);	
			}

			if (argCtx && argCtx.onSuccess) {
				argCtx.onSuccess();
			}
		} else if (resultObj.resStatus == "ERROR_HTTP_500") {
			window.location = "/common/errors/500.aspx";
		} else {
		//2. else if response is an error
			//a. show error message from xml on the screen
			AddSavedItemService.showError(resultObj.resStatus);
		}

	}
	catch (err)
	{
		AddSavedItemService.showError("Error occurred processing results.");
		Debug.error(err);
	}

};

// ----------------------------------------------
// Function:	AddSavedItemService.onError
// Author:		Ryan Ma
// Description:	Callback from the unsuccessful completion of the service call
// Inputs:		<XMLElement> result - Handle to the results xml object
// Returns:		<nothing>
// ----------------------------------------------
AddSavedItemService.prototype.onError = function(result)
{
	try
	{
//  This is a communication failure, not a business logic failure.
//	report error in the normal error space on the page
//		AddSavedItemService.showError("A server error occurred.");
		var error = new Object();
		error.name = "Service 'AddSavedItemService' returned with an error";
		error.message = result.get_message();
		error.fileName = "AddSavedItemService.js";
		error.lineNumber = "";
		Debug.error(error);
		window.location = "/common/errors/500.aspx";
	}
	catch (err)
	{
		Debug.error(err);
	}
};

AddSavedItemService.showError=function(argMessage) {
	var errorDiv = document.getElementById("ajaxError");
	var message = null;
	try{eval("message = "+argMessage+";");}catch(err){}
	if (!message) {
		message = argMessage;
	}
	if (errorDiv) {
		document.getElementById("ajaxError").innerHTML = message;
	} else {
		var confirmWidget = document.getElementById("addConfirmation");
		document.getElementById("confirmMessage").innerHTML = "<div class='error'>"+message+"</div>";
			confirmWidget.style.marginLeft = "740px";
			confirmWidget.style.display = "block";
			//c. setTimeout for success indicator to go away
			setTimeout(function() {massFadeInstance.addTarget("addConfirmation", -20);}, 2000);
	}
	var addMessageDiv = document.getElementById("addMessage");
	if (addMessageDiv) {
		addMessageDiv.innerHTML = "";
		addMessageDiv.style.display = "none";
	}
	
}
