// ----------------------------------------------
// File:		AddShoppingBagItemService.js
// Author:		Ryan Ma
// Description:	A class that holds the available service methods, along with response handlers.
// Example:
// AddShoppingBagItemService.getInstance().processAddToBag(sku,quantity);
// ----------------------------------------------


// ----------------------------------------------
// Function:	AddShoppingBagItemService
// Author:		Ryan Ma
// Description:	Base class
// Inputs:		<none>
// Returns:		<nothing>
// ----------------------------------------------
function AddShoppingBagItemService()
{
}

// ----------------------------------------------
// Function:	AddShoppingBagItemService.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>
// ----------------------------------------------
AddShoppingBagItemService.prototype.processAddToBag = function(argSku, argSelectedSku, argQuantity, argCId, argMCId, argOrigin, argContext)
{
	confirmationChangeSignOut();
	try
	{
//		Debug.windowTrace("processAddToBag("+argSku+", "+argSelectedSku+", "+argQuantity+", "+argCId+", "+argMCId+", "+argOrigin+")");
		//Sys.Net.WebServiceProxy.invoke("", "addToCartResponse.xml", false, {sku:argSku, quantity:argQuantity}, this.onResult, this.onError);
		PageMethods.AddShoppingBagItem(argSku, argSelectedSku, argQuantity, argCId, argMCId, argOrigin, this.onResult, this.onError, argContext);
	}
	catch (err)
	{
		Debug.error(err);
	}
};
// ----------------------------------------------
// Function:	AddShoppingBagItemService.processAddMultipleToBag
// 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>
// ----------------------------------------------
AddShoppingBagItemService.prototype.processAddMultipleToBag = function(argGroupSku, argSkuArray, argQuantityArray, argSelectedSku, argCId, argMCId, argOrigin, argContext)
{
	try
	{
		var pairArray = new Array();
		for (var pairCnt=0;pairCnt<argSkuArray.length;pairCnt++) {
			pairArray.push(argSkuArray[pairCnt]+":"+argQuantityArray[pairCnt]);
		}
		var skuAndQuantity = pairArray.join(";");
//		Debug.windowTrace("processAddMultipleToBag("+skuAndQuantity+", "+argCId+", "+argMCId+")");
		//Sys.Net.WebServiceProxy.invoke("", "addToCartResponse.xml", false, {skuAndQuantity:skuAndQuantity}, this.onResult, this.onError);
		PageMethods.AddMultipleShoppingBagItems(argGroupSku, skuAndQuantity, argSelectedSku, argCId, argMCId, argOrigin, this.onResult, this.onError, argContext)
	}
	catch (err)
	{
		Debug.error(err);
	}
};
AddShoppingBagItemService.prototype.processAddShoppingBagCatalogueItem = function(argSku, argSelectedSku, argQuantity, argCid, argMcid, argOrigin, argCat_id, argCat_item_id, argP_cat_id, argP_cat_item_id, argContext)
{
	try
	{
		//AddShoppingBagCatalogueItem(string sku, string selectedSku, int quantity, int cid, int mcid, int origin, string cat_id, string cat_item_id, string p_cat_id, string p_cat_item_id)
		PageMethods.AddShoppingBagCatalogueItem(argSku, argSelectedSku, argQuantity, argCid, argMcid, argOrigin, argCat_id, argCat_item_id, argP_cat_id, argP_cat_item_id, this.onResult, this.onError, argContext);
	}
	catch (err)
	{
		Debug.error(err);
	}
}
AddShoppingBagItemService.prototype.processAddMultipleShoppingBagCatalogueItems = function(argGroupSku, argSkuArray, argQuantityArray, argSelectedSku, argCId, argMCId, argOrigin, argCat_id, argCat_item_id, argP_cat_id, argP_cat_item_id, argContext)
{
	try
	{
		var pairArray = new Array();
		for (var pairCnt=0;pairCnt<argSkuArray.length;pairCnt++) {
			pairArray.push(argSkuArray[pairCnt]+":"+argQuantityArray[pairCnt]);
		}
		var skuAndQuantity = pairArray.join(";");
		PageMethods.AddMultipleShoppingBagCatalogueItems(argGroupSku, skuAndQuantity, argSelectedSku, argCId, argMCId, argOrigin, argCat_id, argCat_item_id, argP_cat_id, argP_cat_item_id, this.onResult, this.onError, argContext)
	}
	catch (err)
	{
		Debug.error(err);
	}
}

// ----------------------------------------------
// Function:	AddShoppingBagItemService.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>
// ----------------------------------------------
AddShoppingBagItemService.prototype.onResult = function(result, argCtx)
{
	try
	{
//		Debug.windowTrace("onResult("+result+")");
		var docElementNode = result.documentElement;
		var resultObj = {};
		resultObj.skuArray = new Array();
		var skuNodeArray = docElementNode.getElementsByTagName("Sku");
		for (var snCnt=0;snCnt<skuNodeArray.length;snCnt++) {
			var currSkuNode = skuNodeArray[snCnt];
//			Debug.windowTrace(XMLUtils.getNodeAttribute(currSkuNode, "id")+": Status = "+XMLUtils.getNodeValue(currSkuNode, "Status"));
			var currSkuObj = {};
			currSkuObj.id = XMLUtils.getNodeAttribute(currSkuNode, "id");
			currSkuObj.status = XMLUtils.getNodeValue(currSkuNode, "Status");
			resultObj.skuArray.push(currSkuObj);
		}
		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("message="+resultObj.message);
		//1. if response is a success message
		if (resultObj.resStatus == "SUCCESS") {
			//a. update shopping cart label with total number of items
			updateShoppingBagCount(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_BAG;
			if(enableMassFadeEffect) {setOpacity(confirmWidget, 100);}
			confirmWidget.className = "confirmShopBag";
			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) {
				//handleAddSuccess();
				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
			if (argCtx && argCtx.onFailure) {
				//handleAddFault(resultObj.skuArray);
				argCtx.onFailure(resultObj.skuArray);
			}

		}

	}
	catch (err)
	{
		if (argCtx && argCtx.onFailure) {
			//handleAddFault("onResult threw an error");
			argCtx.onFailure("Error occurred processing response.");
		}
		Debug.error(err);
	}

};

// ----------------------------------------------
// Function:	AddShoppingBagItemService.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>
// ----------------------------------------------
AddShoppingBagItemService.prototype.onError = function(result, argCtx)
{
	try
	{
//  This is a communication failure, not a business logic failure.
//	report error in the normal error space on the page
//		if (argCtx && argCtx.onFailure) {
//			argCtx.onFailure("a server error occurred: "+ result.get_message());
//		}
		
		var error = new Object();
		error.name = "Service 'AddShoppingBagItemService' returned with an error:" +result.get_statusCode();
		error.message = result.get_message();
		error.fileName = "AddShoppingBagItemService.js";
		error.lineNumber = "";
		Debug.error(error);
		window.location = "/common/errors/500.aspx";
	}
	catch (err)
	{
		Debug.error(err);
	}
};

