//-----------------------------------------------------------------
// Licensed Materials - Property of IBM
//
// WebSphere Commerce
//
// (C) Copyright IBM Corp. 2008, 2010 All Rights Reserved.
//
// US Government Users Restricted Rights - Use, duplication or
// disclosure restricted by GSA ADP Schedule Contract with
// IBM Corp.
//-----------------------------------------------------------------

/**
 * @fileOverview This class contains declarations of AJAX services used by the Madisons store pages.
 */

dojo.require("wc.service.common");

/**
 * @class This class stores common parameters needed to make the service call.
 */
ServicesDeclarationJS = {
	langId: "-1", /* language of the  store */
	storeId: "", /*numeric unique identifier of the store */
	catalogId: "", /*catalog of the store that is currently in use */
	cursorEnable:false,
	/**
	 * Sets common parameters used by the services
	 * @param (int) langId The language of the store.
	 * @param (int) storeId The store currently in use.
	 * @param (int) catalogId The catalog of the store currently in use.
	 */
	setCommonParameters:function(langId,storeId,catalogId){
			this.langId = langId;
			this.storeId = storeId;
			this.catalogId = catalogId;
	}
}

	/**
	* Adds an item to to the wishlist and remove the same item from the shopping
	* cart.
	* @constructor
	 */
	wc.service.declare({
		id: "AjaxInterestItemAddAndDeleteFromCart",
		actionId: "AjaxInterestItemAddAndDeleteFromCart",
		url: getAbsoluteURL() + "AjaxInterestItemAdd",
		formId: ""

     /**
     * display a success message
     * @param (object) serviceResponse The service response object, which is the
     * JSON object returned by the service invocation
     */
		,successHandler: function(serviceResponse) {
			//Now delete from cart..
			MessageHelper.hideAndClearMessage();
			requestSubmitted = false;
			CheckoutHelperJS.deleteFromCart(serviceResponse.orderItemId,true);
			MessageHelper.displayStatusMessage(MessageHelper.messages["WISHLIST_ADDED"]);
		}

     /**
     * display an error message
     * @param (object) serviceResponse The service response object, which is the
     * JSON object returned by the service invocation
     */
		,failureHandler: function(serviceResponse) {

			if (serviceResponse.errorMessage) {
				MessageHelper.displayErrorMessage(serviceResponse.errorMessage);
			} 
			else {
				 if (serviceResponse.errorMessageKey) {
					MessageHelper.displayErrorMessage(serviceResponse.errorMessageKey);
				 }
			}
			cursor_clear();
		}

	}),

	/**
	 * Add an item to a shopping cart in Ajax mode. A message is displayed after
	 * the service call.
	 * @constructor
	 */
	wc.service.declare({
		id: "AjaxAddOrderItem",
		actionId: "AjaxAddOrderItem",
		url: getAbsoluteURL() + "AjaxOrderChangeServiceItemAdd",
		formId: ""

     /**
     * display a success message
     * @param (object) serviceResponse The service response object, which is the
     * JSON object returned by the service invocation
     */

		,successHandler: function(serviceResponse) {
			MessageHelper.hideAndClearMessage();
		
			cursor_clear();
			if(categoryDisplayJS){
				
				var attributes = document.getElementsByName("attrValue");
			
				var singleSKU = true;
				
				for(var i=0; i<attributes.length; i++){
					if (attributes[i].options.length > 1)
					{
						singleSKU = false;
					}
				}
				
				if (!singleSKU)
				{
					categoryDisplayJS.selectedAttributes = [];
					for(var i=0; i<attributes.length; i++){
						if(attributes[i] != null){
							attributes[i].value = "";
						}
					}
				}
			}
			if(typeof(ShipmodeSelectionExtJS)!= null && typeof(ShipmodeSelectionExtJS)!='undefined'){
				ShipmodeSelectionExtJS.setOrderItemId(serviceResponse.orderItemId[0]);
			}

//			if(displayInstructionItemId != null && dojo.byId('shippingInstructionsCheckBoxDiv_'+displayInstructionItemId) != undefined) {
//				var itemId = dojo.byId("shippingInstructionsCheckBoxDiv_"+displayInstructionItemId);
//				if(itemId != null){
//					dojo.byId("shippingInstructionsCheckBoxDiv_"+orderItemsId).style.display= "block";
//				}
//			}
		}
     /**
     * display an error message
     * @param (object) serviceResponse The service response object, which is the
     * JSON object returned by the service invocation
     */
		,failureHandler: function(serviceResponse) {

			var ompageName = "";
			var omUserType = "";
			if(document.getElementById("omPageNameId") != null  &&   document.getElementById("omPageNameId") != undefined ){
				ompageName =document.getElementById("omPageNameId").value;
			}
			if(document.getElementById("omUserTypeId") != null  &&   document.getElementById("omUserTypeId") != undefined ){
				omUserType = document.getElementById("omUserTypeId").value;
			}			
			console.log('AjaxAddOrderItem begin ompageName='+ompageName + ' , omUserType='+omUserType);

			if (serviceResponse.errorMessage) {
			 	if(serviceResponse.errorMessageKey == "_ERR_NO_ELIGIBLE_TRADING"){
			 		MessageHelper.displayErrorMessage(MessageHelper.messages["ERROR_CONTRACT_EXPIRED_GOTO_ORDER"]);
 				} else if (serviceResponse.errorMessageKey == "_ERR_RETRIEVE_PRICE") {
 					MessageHelper.displayErrorMessage(MessageHelper.messages["ERROR_RETRIEVE_PRICE"]);
 				} else {
 					MessageHelper.displayErrorMessage(serviceResponse.errorMessage);
 				}
			 	omniturePageViewTag(ompageName,null,null,null,null,omUserType,null,"event49", serviceResponse.errorMessage);
			} 
			else {
				 if (serviceResponse.errorMessageKey) {
					MessageHelper.displayErrorMessage(serviceResponse.errorMessageKey);
					omniturePageViewTag(ompageName,null,null,null,null,omUserType,null,"event49", serviceResponse.errorMessageKey);
				 }
			}
			console.log("call from AjaxAddOrderItem end");
			cursor_clear();
		}

	}),

   /**
   * Add an item to a shopping cart in non-Ajax mode. Upon a successful request,
   * the shopping cart page is loaded. An error message is displayed otherwise.
   * @constructor
   */
	wc.service.declare({
		id: "AjaxAddOrderItem_shopCart",
		actionId: "AjaxAddOrderItem",
		url: getAbsoluteURL() + "AjaxOrderChangeServiceItemAdd",
		formId: ""

     /**
     * redirects to the shopping cart page
     * @param (object) serviceResponse The service response object, which is the
     * JSON object returned by the service invocation
     */
		,successHandler: function(serviceResponse) {
			//Now delete from cart..
			document.location.href = "AjaxOrderItemDisplayView?storeId=" + ServicesDeclarationJS.storeId + "&catalogId=" + ServicesDeclarationJS.catalogId + "&langId=" + ServicesDeclarationJS.langId;
		}
     /**
     * display an error message
     * @param (object) serviceResponse The service response object, which is the
     * JSON object returned by the service invocation
     */
		,failureHandler: function(serviceResponse) {

			if (serviceResponse.errorMessage) {
			 	if(serviceResponse.errorMessageKey == "_ERR_NO_ELIGIBLE_TRADING"){
			 		MessageHelper.displayErrorMessage(MessageHelper.messages["ERROR_CONTRACT_EXPIRED_GOTO_ORDER"]);
			 	} else if (serviceResponse.errorMessageKey == "_ERR_RETRIEVE_PRICE") {
 					MessageHelper.displayErrorMessage(MessageHelper.messages["ERROR_RETRIEVE_PRICE"]);
 				} else {				
 					MessageHelper.displayErrorMessage(serviceResponse.errorMessage);
 				}
			} 
			else {
				 if (serviceResponse.errorMessageKey) {
					MessageHelper.displayErrorMessage(serviceResponse.errorMessageKey);
				 }
			}
			cursor_clear();
		}

	}),

	/**
	 * Remove an item from shopping cart. A message is displayed after the service
	 * call.
	 * @constructor
	 */
	wc.service.declare({
		id: "AjaxDeleteOrderItem",
		actionId: "AjaxDeleteOrderItem",
		url: getAbsoluteURL() + "AjaxOrderChangeServiceItemDelete",
		formId: ""
    /**
     * display a success message
     * @param (object) serviceResponse The service response object, which is the
     * JSON object returned by the service invocation
     */
		,successHandler: function(serviceResponse) {
			MessageHelper.hideAndClearMessage();
			MessageHelper.displayStatusMessage(MessageHelper.messages["SHOPCART_REMOVEITEM"]);
		}
     /**
     * display an error message
     * @param (object) serviceResponse The service response object, which is the
     * JSON object returned by the service invocation
     */
		,failureHandler: function(serviceResponse) {
			var ompageName = "";
			var omUserType = "";
			if(document.getElementById("omPageNameId") != null  &&   document.getElementById("omPageNameId") != undefined ){
				ompageName =document.getElementById("omPageNameId").value;
			}
			if(document.getElementById("omUserTypeId") != null  &&   document.getElementById("omUserTypeId") != undefined ){
				omUserType = document.getElementById("omUserTypeId").value;
			}			
			console.log('AjaxDeleteOrderItem begin ompageName='+ompageName + ' , omUserType='+omUserType);
			
			if (serviceResponse.errorMessage) {
				MessageHelper.displayErrorMessage(serviceResponse.errorMessage);
				omniturePageViewTag(ompageName,null,null,null,null,omUserType,null,"event49", serviceResponse.errorMessage);
			} 
			else {
				 if (serviceResponse.errorMessageKey) {
					MessageHelper.displayErrorMessage(serviceResponse.errorMessageKey);
					omniturePageViewTag(ompageName,null,null,null,null,omUserType,null,"event49", serviceResponse.errorMessageKey);
				 }
			}
			console.log("AjaxDeleteOrderItem end");	
			cursor_clear();
		}

	}),
	
	
	/**
	 * Removes an item from shopping cart on the shipping & billing page. A message is displayed after the service call.
	 * @constructor
	 */
	wc.service.declare({
		id: "AjaxDeleteOrderItemForShippingBillingPage",
		actionId: "AjaxDeleteOrderItemForShippingBillingPage",
		url: getAbsoluteURL() + "AjaxOrderChangeServiceItemDelete",
		formId: ""
		
		/**
		 * display a success message
		 * @param (object) serviceResponse The service response object, which is the
		 * JSON object returned by the service invocation
		 */
		,successHandler: function(serviceResponse) {
			MessageHelper.hideAndClearMessage();
			MessageHelper.displayStatusMessage(MessageHelper.messages["SHOPCART_REMOVEITEM"]);
		}
		
		/**
		 * display an error message
		 * @param (object) serviceResponse The service response object, which is the
		 * JSON object returned by the service invocation
		 */
		,failureHandler: function(serviceResponse) {
			if (serviceResponse.errorMessage) {
				MessageHelper.displayErrorMessage(serviceResponse.errorMessage);
			} 
			else {
				 if (serviceResponse.errorMessageKey) {
					MessageHelper.displayErrorMessage(serviceResponse.errorMessageKey);
				 }
			}
			cursor_clear();
		}
	}),
	

	/**
	 * Remove an item from shopping cart. A message is only displayed if the service
	 * call returns an error Message. It is used to remove an item from the shopping
	 * cart and add the same item to the wish list.
	 * @constructor
	 */
	wc.service.declare({
		id: "AjaxDeleteOrderItemFromCart",
		actionId: "AjaxDeleteOrderItem",
		url: getAbsoluteURL() + "AjaxOrderChangeServiceItemDelete",
		formId: ""
     /**
     * display an error message
     * @param (object) serviceResponse The service response object, which is the
     * JSON object returned by the service invocation
     */
		,failureHandler: function(serviceResponse) {

			if (serviceResponse.errorMessage) {
				MessageHelper.displayErrorMessage(serviceResponse.errorMessage);
			} 
			else {
				 if (serviceResponse.errorMessageKey) {
					MessageHelper.displayErrorMessage(serviceResponse.errorMessageKey);
				 }
			}
			cursor_clear();
		}

	}),

	/**
	 * Remove an item from shopping cart. 
	 * Upon a successful request, this function will load the AjaxOrderItemDisplayView page or the OrderShippingBillingView page depending on what page the service was invoked from. 
	 * An error message will be displayed otherwise.
	 * @constructor
	 */
	wc.service.declare({
		id: "AjaxDeleteOrderItem1",
		actionId: "AjaxDeleteOrderItem",
		url: getAbsoluteURL() + "AjaxOrderChangeServiceItemDelete",
		formId: ""

    /**
     *redirect to the Shopping Cart Page
     * @param (object) serviceResponse The service response object, which is the
     * JSON object returned by the service invocation
     */
		,successHandler: function(serviceResponse) {
			if (!CheckoutHelperJS.pendingOrderDetailsPage)
			{
				if(CheckoutHelperJS.shoppingCartPage){
					
					document.location.href = "AjaxOrderItemDisplayView?storeId=" + ServicesDeclarationJS.storeId + "&catalogId=" + ServicesDeclarationJS.catalogId + "&langId=" + ServicesDeclarationJS.langId;
				}else{
					document.location.href = "OrderDeliveryView?storeId=" + ServicesDeclarationJS.storeId + "&catalogId=" + ServicesDeclarationJS.catalogId + "&langId=" + ServicesDeclarationJS.langId + "&orderId=" + serviceResponse.orderId;
				}
			}
			else
			{
				cursor_clear();
			}
		}

    /**
     * display an error message
     * @param (object) serviceResponse The service response object, which is the
     * JSON object returned by the service invocation
     */
		,failureHandler: function(serviceResponse) {

			var ompageName = "";
			var omUserType = "";
			if(document.getElementById("omPageNameId") != null  &&   document.getElementById("omPageNameId") != undefined ){
				ompageName =document.getElementById("omPageNameId").value;
			}
			if(document.getElementById("omUserTypeId") != null  &&   document.getElementById("omUserTypeId") != undefined ){
				omUserType = document.getElementById("omUserTypeId").value;
			}		
			console.log('AjaxDeleteOrderItem1 begin ompageName='+ompageName + ' , omUserType='+omUserType);
			
			if (serviceResponse.errorMessage) {
				MessageHelper.displayErrorMessage(serviceResponse.errorMessage);
				omniturePageViewTag(ompageName,null,null,null,null,omUserType,null,"event49", serviceResponse.errorMessage);
			} 
			else {
				 if (serviceResponse.errorMessageKey) {
					MessageHelper.displayErrorMessage(serviceResponse.errorMessageKey);
					omniturePageViewTag(ompageName,null,null,null,null,omUserType,null,"event49", serviceResponse.errorMessageKey);
				 }
			}
			console.log("AjaxDeleteOrderItem1 end");			
			cursor_clear();
		}

	}),


	/**
	 * This service updates an order item in the shopping cart.
	 * A message is displayed after the service call.
	 * @constructor
	 */
	wc.service.declare({
		id: "AjaxUpdateOrderItem",
		actionId: "AjaxUpdateOrderItem",
		url: getAbsoluteURL() + "AjaxOrderChangeServiceItemUpdate",
		formId: ""

    /**
     * hides all the messages and the progress bar
     * @param (object) serviceResponse The service response object, which is the
     * JSON object returned by the service invocation
     */
		,successHandler: function(serviceResponse) {
			MessageHelper.hideAndClearMessage();
			var curEnabled = ServicesDeclarationJS.cursorEnable;
			
			if(curEnabled == undefined || curEnabled == null || curEnabled == false){
				cursor_clear();
			} 
		}

    /**
     * display an error message
     * @param (object) serviceResponse The service response object, which is the
     * JSON object returned by the service invocation
     */
		,failureHandler: function(serviceResponse) {

			if (serviceResponse.errorMessage) {
				if (serviceResponse.errorMessageKey == "_ERR_RETRIEVE_PRICE") {
 					MessageHelper.displayErrorMessage(MessageHelper.messages["ERROR_RETRIEVE_PRICE_QTY_UPDATE"]);
				}
				else{
					MessageHelper.displayErrorMessage(serviceResponse.errorMessage);
				}
			} 
			else {
				 if (serviceResponse.errorMessageKey) {
					MessageHelper.displayErrorMessage(serviceResponse.errorMessageKey);
				 }
			}
			cursor_clear();
		}

	}),

	/**
	 * Updates an order item in the shopping cart. 
	 * Upon a successful request, this function will load the AjaxOrderItemDisplayView page  
	 * An error message will be displayed otherwise.
	 * @constructor
	 */
	wc.service.declare({
		id: "AjaxUpdateOrderItem1",
		actionId: "AjaxUpdateOrderItem",
		url: getAbsoluteURL() + "AjaxOrderChangeServiceItemUpdate",
		formId: ""
	/**
     *redirect to the Shopping Cart Page
     * @param (object) serviceResponse The service response object, which is the
     * JSON object returned by the service invocation
     */
		,successHandler: function(serviceResponse) {
			if (!CheckoutHelperJS.pendingOrderDetailsPage)
			{
				if(CheckoutHelperJS.shoppingCartPage){	
					document.location.href = "AjaxOrderItemDisplayView?storeId=" + ServicesDeclarationJS.storeId + "&catalogId=" + ServicesDeclarationJS.catalogId + "&langId=" + ServicesDeclarationJS.langId;
				}
			}
			else
			{
				cursor_clear();
			}
		}

    /**
     * display an error message
     * @param (object) serviceResponse The service response object, which is the
     * JSON object returned by the service invocation
     */
		,failureHandler: function(serviceResponse) {

			if (serviceResponse.errorMessage) {
				if (serviceResponse.errorMessageKey == "_ERR_RETRIEVE_PRICE") {
 					MessageHelper.displayErrorMessage(MessageHelper.messages["ERROR_RETRIEVE_PRICE_QTY_UPDATE"]);
				}
				else{
					MessageHelper.displayErrorMessage(serviceResponse.errorMessage);
				}
			} 
			else {
				 if (serviceResponse.errorMessageKey) {
					MessageHelper.displayErrorMessage(serviceResponse.errorMessageKey);
				 }
			}
			cursor_clear();
		}

	}),

	/**
	 * This service updates shipping information (shipping mode, shipping address)
	 * for a shopping cart. A message is displayed after the service call.
	 * @constructor
	 */
	wc.service.declare({
		id: "AjaxUpdateOrderShippingInfo",
		actionId: "AjaxUpdateOrderShippingInfo",
		url: getAbsoluteURL() + "AjaxOrderChangeServiceShipInfoUpdate",
		formId: ""
    /**
     * hides all the messages and the progress bar	
     * @param (object) serviceResponse The service response object, which is the
     * JSON object returned by the service invocation
     */
		,successHandler: function(serviceResponse) {
			MessageHelper.hideAndClearMessage();
			cursor_clear();
		}

     /**
     * display an error message
     * @param (object) serviceResponse The service response object, which is the
     * JSON object returned by the service invocation
     */
		,failureHandler: function(serviceResponse) {

			if (serviceResponse.errorMessage) {
				MessageHelper.displayErrorMessage(serviceResponse.errorMessage);
			} 
			else {
				 if (serviceResponse.errorMessageKey) {
					MessageHelper.displayErrorMessage(serviceResponse.errorMessageKey);
				 }
			}
			cursor_clear();
		}

	}),


	/**
	 * This service submits the order. Upon success, the order billing confirmation
	 * page is shown. A error message is displayed otherwise.
	 * @constructor
	 */
	wc.service.declare({
		id: "AjaxSubmitOrder",
		actionId: "AjaxSubmitOrder",
		url: getAbsoluteURL() + "AjaxOrderProcessServiceOrderSubmit",
		formId: ""

    /**
     *redirect to the Order Confirmation page
     * @param (object) serviceResponse The service response object, which is the
     * JSON object returned by the service invocation
     */
		,successHandler: function(serviceResponse) {
			var shipmentTypeId = CheckoutHelperJS.getShipmentTypeId();
			document.location.href = "OrderShippingBillingConfirmationView?storeId=" + ServicesDeclarationJS.storeId + "&catalogId=" + ServicesDeclarationJS.catalogId + "&langId=" + ServicesDeclarationJS.langId + "&orderId=" + serviceResponse.orderId + "&shipmentTypeId=" + shipmentTypeId;
		}

    /**
     * display an error message
     * @param (object) serviceResponse The service response object, which is the
     * JSON object returned by the service invocation
     */
		,failureHandler: function(serviceResponse) {
			
			var ompageName = "";
			var omUserType = "";
			var errorMsg = "" ;
			if(document.getElementById("omPageNameId") != null  &&   document.getElementById("omPageNameId") != undefined ){
				ompageName =document.getElementById("omPageNameId").value;
			}
			if(document.getElementById("omUserTypeId") != null  &&   document.getElementById("omUserTypeId") != undefined ){
				omUserType = document.getElementById("omUserTypeId").value;
			}			
			console.log('AjaxSubmitOrder begin ompageName='+ompageName + ' , omUserType='+omUserType);
			if (serviceResponse.errorMessage) {
				errorMsg  = serviceResponse.errorMessage ;
			} else {
				if (serviceResponse.errorMessageKey) {
					errorMsg  = serviceResponse.errorMessageKey ;
				}
			}
			
			//Begin HHG-328 to display error message next to payment instruction on order summary
			var errorMessageShown = false;
			var errorMessageParamArray = serviceResponse['errorMessageParam'];
			if(errorMessageParamArray != undefined && errorMessageParamArray != null && errorMessageParamArray != ''){
				var errorMessageParamValue = new String(errorMessageParamArray);
				if(errorMessageParamValue.indexOf('[') != -1) {
					errorMessageParamValue = errorMessageParamValue.substring(1, errorMessageParamValue.length - 1);
				}
				
				var errorMessageParamValueTokens = errorMessageParamValue.split(",");
				for(index = 0; index < errorMessageParamValueTokens.length; index = index + 1) {
					var paymentInstructionId = errorMessageParamValueTokens[index];
					paymentInstructionId = dojo.string.trim(paymentInstructionId);
					if(document.getElementById('paymentErrorMessage_' + paymentInstructionId) != undefined) {
						if (serviceResponse.errorMessage == '_ERR_INVALID_CARD') {
							document.getElementById('paymentErrorMessage_' + paymentInstructionId).innerHTML = 'Credit Card Processing Failed';
						} else {
							document.getElementById('paymentErrorMessage_' + paymentInstructionId).innerHTML = serviceResponse.errorMessage;
						}
						errorMessageShown = true;
					}
				}
			}

			omniturePageViewTag(ompageName,null,null,null,null,omUserType,null,"event49", errorMsg);
			
			if (serviceResponse.errorMessageKey == '_ERR_INVALID_SHIPMODE') {
				document.location.href = "OrderDeliveryView?errorMessage=ERROR_SHIPMODE_NOT_SELECTED&storeId=" + ServicesDeclarationJS.storeId + "&catalogId=" + ServicesDeclarationJS.catalogId + "&langId=" + ServicesDeclarationJS.langId ;
			}else if(serviceResponse.errorMessageKey == '_ERR_MISSING_PAYMENT_INFO') {
				document.location.href = "OrderBillingView?storeId=" + ServicesDeclarationJS.storeId + "&catalogId=" + ServicesDeclarationJS.catalogId + "&langId=" + ServicesDeclarationJS.langId + "&orderId=.&shipmentType=single"  ;
			}else{
				
				if(!errorMessageShown){
					if (serviceResponse.errorMessage) {
						MessageHelper.displayErrorMessage(serviceResponse.errorMessage);
					} 
					else {
						 if (serviceResponse.errorMessageKey) {
							MessageHelper.displayErrorMessage(serviceResponse.errorMessageKey);
						 }
					}
				}
			}
			cursor_clear();
		}

	}),

	/**
	 * This service submits the quote. Upon success, the quote  confirmation
	 * page is shown. A error message is displayed otherwise.
	 * @constructor
	 */
	wc.service.declare({
		id: "AjaxSubmitQuote",
		actionId: "AjaxSubmitQuote",
		url: getAbsoluteURL() + "AjaxSubmitQuote",
		formId: ""

   /**
    *redirect to the Quote Confirmation page
    * @param (object) serviceResponse The service response object, which is the
    * JSON object returned by the service invocation
    */
		,successHandler: function(serviceResponse) {
			var redirectURL = "OrderShippingBillingConfirmationView?storeId=" + ServicesDeclarationJS.storeId 
			+ "&catalogId=" + ServicesDeclarationJS.catalogId 
			+ "&langId=" + ServicesDeclarationJS.langId 
			+ "&orderId=" + CheckoutHelperJS.getOrderId() 
			+ "&shipmentTypeId=" + CheckoutHelperJS.getShipmentTypeId()
			+ "&isQuote=true"
			+ "&quoteId=" + serviceResponse.outOrderId// outOrderId is the id of the new quote created.
			
			if(serviceResponse.outExternalQuoteId != undefined && serviceResponse.outExternalQuoteId != null){
				redirectURL += redirectURL + "&externalQuoteId=" + serviceResponse.outExternalQuoteId; 
			}
			document.location.href = redirectURL;
		}

   /**
    * display an error message
    * @param (object) serviceResponse The service response object, which is the
    * JSON object returned by the service invocation
    */
		,failureHandler: function(serviceResponse) {

			if (serviceResponse.errorMessage) {
				MessageHelper.displayErrorMessage(serviceResponse.errorMessage);
			} 
			else {
				 if (serviceResponse.errorMessageKey) {
					MessageHelper.displayErrorMessage(serviceResponse.errorMessageKey);
				 }
			}
			cursor_clear();
		}

	}),

	/**
	 * This service adds an address for the person. An error message is displayed
	 * if the service failed.
	 * @constructor
	 */
	wc.service.declare({
		id: "AjaxAddAddressForPerson",
		actionId: "AjaxAddAddressForPerson",
		url: getAbsoluteURL() + "AjaxPersonChangeServiceAddressAdd",
		formId: ""

    /**
     * hides all the messages and the progress bar
     * @param (object) serviceResponse The service response object, which is the
     * JSON object returned by the service invocation
     */
		,successHandler: function(serviceResponse) {
    		AddressHelper.sAddressId = serviceResponse.addressId;
			AddressHelper.updateOrderAfterAddressUpdate();
			MessageHelper.hideAndClearMessage();
			
			//HHG-1226 Updating the address id in the billingAddressDropDownBoxContext so that it will be refreshed in the dropdown
			var renderContext = wc.render.getContextById("billingAddressDropDownBoxContext");
			renderContext.properties["billingAddress1"] = serviceResponse.addressId;
			
			CheckoutPayments.updateBillingAddressForAll(serviceResponse.addressId);
			
			cursor_clear();
		}

     /**
     * display an error message
     * @param (object) serviceResponse The service response object, which is the
     * JSON object returned by the service invocation
     */
		,failureHandler: function(serviceResponse) {

			var ompageName = "";
			var omUserType = "";
			if(document.getElementById("omPageNameId") != null  &&   document.getElementById("omPageNameId") != undefined ){
				ompageName =document.getElementById("omPageNameId").value;
			}
			if(document.getElementById("omUserTypeId") != null  &&   document.getElementById("omUserTypeId") != undefined ){
				omUserType = document.getElementById("omUserTypeId").value;
			}			
			console.log('AjaxAddAddressForPerson begin ompageName='+ompageName + ' , omUserType='+omUserType);

			if(ompageName == 'OrderBillingView'){
				if (serviceResponse.errorMessage) {
					MessageHelper.displayErrorMessage(serviceResponse.errorMessage);
					omniturePageViewTag(ompageName,null,null,null,null,omUserType,null,"event49", serviceResponse.errorMessage);
				} 
				else {
					 if (serviceResponse.errorMessageKey) {
						MessageHelper.displayErrorMessage(serviceResponse.errorMessageKey);
						omniturePageViewTag(ompageName,null,null,null,null,omUserType,null,"event49", serviceResponse.errorMessageKey);
					 }
				}				
			}else{
				if (serviceResponse.errorMessage) {
					//Display alternate address dialog
					var url= "AddressVerificationResponseView?"+serviceResponse.errorMessageParam.toString().replace(/\n/g,"&")+"name=modal shipping_adr_verify";
					if (!secondDlg) {
						ModalJS.init();
					}
					ModalJS.showDialogTwo(url);
					MessageHelper.displayErrorMessage(serviceResponse.errorMessage);
					omniturePageViewTag(ompageName,null,null,null,null,omUserType,null,"event49", serviceResponse.errorMessage);
				} 
				else {
					 if (serviceResponse.errorMessageKey) {
						MessageHelper.displayErrorMessage(serviceResponse.errorMessageKey);
						omniturePageViewTag(ompageName,null,null,null,null,omUserType,null,"event49", serviceResponse.errorMessageKey);
					 }
				}
			}
			console.log("AjaxAddAddressForPerson end");
			cursor_clear();
		}

	}),

	/**
	 * This service adds an address for the person. An error message is displayed
	 * if the service failed.
	 * @constructor
	 */
	wc.service.declare({
		id: "AjaxUpdateAddressForPerson",
		actionId: "AjaxUpdateAddressForPerson",
		url: getAbsoluteURL() + "AjaxPersonChangeServiceAddressUpdate",
		formId: ""

    /**
     * hides all the messages and the progress bar
     * @param (object) serviceResponse The service response object, which is the
     * JSON object returned by the service invocation
     */
		,successHandler: function(serviceResponse) {
			AddressHelper.sAddressId = serviceResponse.addressId;
			AddressHelper.updateOrderAfterAddressUpdate();
			MessageHelper.hideAndClearMessage();
			//HHG-1226 Updating the address id in the billingAddressDropDownBoxContext so that it will be refreshed in the dropdown
			var renderContext = wc.render.getContextById("billingAddressDropDownBoxContext");
			renderContext.properties["billingAddress1"] = serviceResponse.addressId;
			
			CheckoutPayments.updateBillingAddressForAll(serviceResponse.addressId);
			
			cursor_clear();
		}
     /**
     * display an error message
     * @param (object) serviceResponse The service response object, which is the
     * JSON object returned by the service invocation
     */
		,failureHandler: function(serviceResponse) {

			var ompageName = "";
			var omUserType = "";
			if(document.getElementById("omPageNameId") != null  &&   document.getElementById("omPageNameId") != undefined ){
				ompageName =document.getElementById("omPageNameId").value;
			}
			if(document.getElementById("omUserTypeId") != null  &&   document.getElementById("omUserTypeId") != undefined ){
				omUserType = document.getElementById("omUserTypeId").value;
			}			
			console.log('AjaxUpdateAddressForPerson begin ompageName='+ompageName + ' , omUserType='+omUserType);

			if (serviceResponse.errorMessage) {
				//Display alternate address dialog
				var url= "AddressVerificationResponseView?"+serviceResponse.errorMessageParam.toString().replace(/\n/g,"&")+"name=modal shipping_adr_verify";
				if (!secondDlg) {
					ModalJS.init();
				}
				ModalJS.showDialogTwo(url);
				MessageHelper.displayErrorMessage(serviceResponse.errorMessage);
				omniturePageViewTag(ompageName,null,null,null,null,omUserType,null,"event49", serviceResponse.errorMessage);
			} 
			else {
				 if (serviceResponse.errorMessageKey) {
					MessageHelper.displayErrorMessage(serviceResponse.errorMessageKey);
					omniturePageViewTag(ompageName,null,null,null,null,omUserType,null,"event49", serviceResponse.errorMessageKey);
				 }
			}
			
			console.log("AjaxUpdateAddressForPerson end");
			cursor_clear();
		}

	}),

	/**
	 * This service calls AjaxOrderChangeServiceItemUpdate to update order total after shipping address is updated in the order.
	 */
	wc.service.declare({
		id: "AjaxUpdateOrderAfterAddressUpdate",
		actionId: "AjaxUpdateOrderAfterAddressUpdate",
		url: getAbsoluteURL() + "AjaxOrderChangeServiceItemUpdate",
		formId: ""

    /**
     * hides all the messages and the progress bar
     * @param (object) serviceResponse The service response object, which is the
     * JSON object returned by the service invocation
     */
		,successHandler: function(serviceResponse) {
			MessageHelper.hideAndClearMessage();
			cursor_clear();
		}

    /**
     * display an error message
     * @param (object) serviceResponse The service response object, which is the
     * JSON object returned by the service invocation
     */
		,failureHandler: function(serviceResponse) {

			if (serviceResponse.errorMessage) {
				MessageHelper.displayErrorMessage(serviceResponse.errorMessage);
			} 
			else {
				 if (serviceResponse.errorMessageKey) {
					MessageHelper.displayErrorMessage(serviceResponse.errorMessageKey);
				 }
			}
			cursor_clear();
		}

	}),
	
	/**
	 * This service adds an item to the wishlist. This is different from
	 * AjaxInterestItemAddAndDeleteFromCart in that this function does not remove
	 * the item from the shopping cart. It is used mainly in catalog browsing.
	 * @constructor
	 */
	wc.service.declare({
		id: "AjaxInterestItemAdd",
		actionId: "AjaxInterestItemAdd",
		url: getAbsoluteURL() + "AjaxInterestItemAdd",
		formId: ""
    /**
     * hides all the messages and the progress bar
     * @param (object) serviceResponse The service response object, which is the
     * JSON object returned by the service invocation
     */
		,successHandler: function(serviceResponse) {
			MessageHelper.hideAndClearMessage();
			cursor_clear();
			MessageHelper.displayStatusMessage(MessageHelper.messages["WISHLIST_ADDED"]);
			if(categoryDisplayJS)
			categoryDisplayJS.selectedAttributes = [];
		}
     /**
     * display an error message
     * @param (object) serviceResponse The service response object, which is the
     * JSON object returned by the service invocation
     */
		,failureHandler: function(serviceResponse) {

			if (serviceResponse.errorMessage) {
				if(serviceResponse.errorMessageKey == "_ERR_IITEM_ALREADY_EXIST"){
					MessageHelper.displayErrorMessage(MessageHelper.messages["WISHLIST_ALREADY_ADDED_ERROR"]); 
				}
				else{	
					MessageHelper.displayErrorMessage(serviceResponse.errorMessage);
				}
			} 
			else {
				 if (serviceResponse.errorMessageKey) {
					MessageHelper.displayErrorMessage(serviceResponse.errorMessageKey);
				 }
			}
			cursor_clear();
		}

	}),

	/**
	 * This service adds an item to the wishlist in non-Ajax mode. Upon success,
	 * the shopping cart page is displayed. This is different from
	 * AjaxInterestItemAddAndDeleteFromCart in that this function does not remove
	 * the item from the shopping cart. It is used mainly in catalog browsing.
	 * @constructor
	 */
	wc.service.declare({
		id: "AjaxInterestItemAdd_shopCart",
		actionId: "AjaxInterestItemAdd",
		url: getAbsoluteURL() + "AjaxInterestItemAdd",
		formId: ""

    /**
     * hides all the messages and the progress bar
     * @param (object) serviceResponse The service response object, which is the
     * JSON object returned by the service invocation
     */
		,successHandler: function(serviceResponse) {
			document.location.href = "AjaxOrderItemDisplayView?storeId=" + ServicesDeclarationJS.storeId + "&catalogId=" + ServicesDeclarationJS.catalogId + "&langId=" + ServicesDeclarationJS.langId;
		}

     /**
     * display an error message
     * @param (object) serviceResponse The service response object, which is the
     * JSON object returned by the service invocation
     */
		,failureHandler: function(serviceResponse) {

			if (serviceResponse.errorMessage) {
				MessageHelper.displayErrorMessage(serviceResponse.errorMessage);
			} 
			else {
				 if (serviceResponse.errorMessageKey) {
					MessageHelper.displayErrorMessage(serviceResponse.errorMessageKey);
				 }
			}
			cursor_clear();
		}

	}),

  /**
   * This service deletes an item from the wish list. An error message will be
   * displayed if the service call failed. 
   */
	wc.service.declare({
		id: "AjaxInterestItemDelete",
		actionId: "AjaxInterestItemDelete",
		url: getAbsoluteURL() + "AjaxInterestItemDelete",
		formId: ""

    /**
     * hides all the messages and the progress bar
     * @param (object) serviceResponse The service response object, which is the
     * JSON object returned by the service invocation
     */
		,successHandler: function(serviceResponse) {
			MessageHelper.hideAndClearMessage();
		}

     /**
     * display an error message
     * @param (object) serviceResponse The service response object, which is the
     * JSON object returned by the service invocation
     */
		,failureHandler: function(serviceResponse) {

			if (serviceResponse.errorMessage) {
				MessageHelper.displayErrorMessage(serviceResponse.errorMessage);
			} 
			else {
				 if (serviceResponse.errorMessageKey) {
					MessageHelper.displayErrorMessage(serviceResponse.errorMessageKey);
				 }
			}
			cursor_clear();
		}

	}),

	/**
	 * This service sends the wish list to a specified email address.
	 */
	wc.service.declare({
		id: "AjaxInterestItemListMessage",
		actionId: "AjaxInterestItemListMessage",
		url: getAbsoluteURL() + "AjaxInterestItemListMessage",
		formId: ""

    /**
     * hides all the messages and the progress bar
     * @param (object) serviceResponse The service response object, which is the
     * JSON object returned by the service invocation
     */
		,successHandler: function(serviceResponse) {
			MessageHelper.hideAndClearMessage();
		}
     /**
     * display an error message
     * @param (object) serviceResponse The service response object, which is the
     * JSON object returned by the service invocation
     */
		,failureHandler: function(serviceResponse) {

			if (serviceResponse.errorMessage) {
				MessageHelper.displayErrorMessage(serviceResponse.errorMessage);
			} 
			else {
				 if (serviceResponse.errorMessageKey) {
					MessageHelper.displayErrorMessage(serviceResponse.errorMessageKey);
				 }
			}
			cursor_clear();
		}

	}),

	/**
	 * This service applies the promotion code to the order(s).
	 */
	wc.service.declare({
		id: "AjaxPromotionCodeManage",
		actionId: "AjaxPromotionCodeManage",
		url: getAbsoluteURL() + "AjaxPromotionCodeManage",
		formId: ""

    /**
     * hides all the messages and the progress bar
     * @param (object) serviceResponse The service response object, which is the
     * JSON object returned by the service invocation
     */
		,successHandler: function(serviceResponse) {
			MessageHelper.hideAndClearMessage();

			var params = [];
			
			params.storeId		= this.storeId;
			params.catalogId	= this.catalogId;
			params.langId		= this.langId;
			
			params.orderId = ".";
			params.calculationUsage = "-1,-2,-5,-6,-7";
			
			wc.service.invoke("AjaxUpdateOrderItem",params);
			
		}

     /**
     * display an error message
     * @param (object) serviceResponse The service response object, which is the
     * JSON object returned by the service invocation
     */
		,failureHandler: function(serviceResponse) {

			if (serviceResponse.errorMessage) {
				addInlineError("promoCode", serviceResponse.errorMessage);
			} 
			else {
				 if (serviceResponse.errorMessageKey) {
					addInlineError("promoCode", serviceResponse.errorMessageKey);
				 }
			}
			cursor_clear();
		}

	}),

  /**
   * This services adds or removes a coupon from the order(s).
   */
	wc.service.declare({
		id: "AjaxCouponsAddRemove",
		actionId: "AjaxCouponsAddRemove",
		url: getAbsoluteURL() + "AjaxCouponsAddRemove",
		formId: ""

    /**
     * Hides all the messages and the progress bar. It will then called the
     * AjaxOrderChangeServiceItemUpdate service
     * @param (object) serviceResponse The service response object, which is the
     * JSON object returned by the service invocation
      */
		,successHandler: function(serviceResponse) {
			MessageHelper.hideAndClearMessage();
			
			var params = [];
			
			params.storeId		= this.storeId;
			params.catalogId	= this.catalogId;
			params.langId		= this.langId;
			
			params.orderId = serviceResponse.orderId;
			params.calculationUsage = "-1,-2,-5,-6,-7";
			
			wc.service.invoke("AjaxUpdateOrderItem",params);

		}

     /**
     * display an error message
     * @param (object) serviceResponse The service response object, which is the
     * JSON object returned by the service invocation
     */
		,failureHandler: function(serviceResponse) {

			if (serviceResponse.errorMessage) {
				MessageHelper.displayErrorMessage(serviceResponse.errorMessage);
			} 
			else {
				 if (serviceResponse.errorMessageKey) {
					MessageHelper.displayErrorMessage(serviceResponse.errorMessageKey);
				 }
			}
			cursor_clear();
		}

	}),

	/**
	 * This service adds a billing address to the order(s).
	 */
	wc.service.declare({
		id: "AddBillingAddress",
		actionId: "AddBillingAddress",
		url: getAbsoluteURL() + "AjaxPersonChangeServiceAddressAdd",
		formId: ""

    /**
     * hides all the messages and the progress bar
     * @param (object) serviceResponse The service response object, which is the
     * JSON object returned by the service invocation
     */
		,successHandler: function(serviceResponse) {
			MessageHelper.hideAndClearMessage();
		}

     /**
     * display an error message
     * @param (object) serviceResponse The service response object, which is the
     * JSON object returned by the service invocation
     */
		,failureHandler: function(serviceResponse) {

			if (serviceResponse.errorMessage) {
				MessageHelper.displayErrorMessage(serviceResponse.errorMessage);
			} 
			else {
				 if (serviceResponse.errorMessageKey) {
					MessageHelper.displayErrorMessage(serviceResponse.errorMessageKey);
				 }
			}
			cursor_clear();
		}

	}),
	
/**
 * This service schedules an order based on the input order date and order interval parameters. 
 */
wc.service.declare({
	id: "ScheduleOrder",
	actionId: "ScheduleOrder",
	url: getAbsoluteURL() + "AjaxOrderProcessServiceOrderSchedule",
	formId: ""

	/**
	 * Hides all the messages and the progress bar.
	 * Constructs a URL that deletes the current order and forward to the order confirmation page.
	 * @param (object) serviceResponse The service response object, which is the JSON object returned by the service invocation
	 */
	,successHandler: function(serviceResponse) {
		MessageHelper.hideAndClearMessage();
		var originalOrderId = document.getElementById("orderIdToSchedule").value;
		var newOrderId = serviceResponse.orderId;
		var shipmentTypeId = CheckoutHelperJS.getShipmentTypeId();
		var purchaseOrderNumber = "";
		if(document.forms["purchaseOrderNumberInfo"].purchase_order_number.value != null){
			purchaseOrderNumber = document.forms["purchaseOrderNumberInfo"].purchase_order_number.value;
		}
		var url = "OrderProcessServiceOrderCancel?orderId=" + originalOrderId + "&storeId="  + ServicesDeclarationJS.storeId + "&catalogId=" + ServicesDeclarationJS.catalogId + "&langId=" + ServicesDeclarationJS.langId + "&URL=OrderShippingBillingConfirmationView%3ForderId%3D" + newOrderId + "%26originalOrderId%3D" + originalOrderId + "%26shipmentTypeId%3D" + shipmentTypeId + "%26purchaseOrderNumber%3D" + purchaseOrderNumber;
		document.location.href = url;
	}
	
	/**
	 * Displays an error message if the the service call failed.
	 * @param (object) serviceResponse The service response object, which is the
	 * JSON object returned by the service invocation
	 */
	,failureHandler: function(serviceResponse) {
		if (serviceResponse.errorMessage) {
			MessageHelper.displayErrorMessage(serviceResponse.errorMessage);
		} else {
			 if (serviceResponse.errorMessageKey) {
				MessageHelper.displayErrorMessage(serviceResponse.errorMessageKey);
			 }
		}
		cursor_clear();
	}
}), 

/**
 * This service schedules an order based on the input order date and order interval parameters. 
 */
wc.service.declare({
	id: "SubmitRecurringOrder",
	actionId: "SubmitRecurringOrder",
	url: getAbsoluteURL() + "AjaxOrderProcessServiceRecurringOrderSubmit",
	formId: ""

	/**
	 * Hides all the messages and the progress bar.
	 * Constructs a URL that deletes the current order and forward to the order confirmation page.
	 * @param (object) serviceResponse The service response object, which is the JSON object returned by the service invocation
	 */
	,successHandler: function(serviceResponse) {
		MessageHelper.hideAndClearMessage();
		var shipmentTypeId = CheckoutHelperJS.getShipmentTypeId();
		var url = "OrderShippingBillingConfirmationView?storeId=" + ServicesDeclarationJS.storeId + "&catalogId=" + ServicesDeclarationJS.catalogId + "&langId=" + ServicesDeclarationJS.langId + "&orderId=" + serviceResponse.orderId + "&shipmentTypeId=" + shipmentTypeId;
		document.location.href = url;
		cursor_clear();
	}
	
	/**
	 * Displays an error message if the the service call failed.
	 * @param (object) serviceResponse The service response object, which is the
	 * JSON object returned by the service invocation
	 */
	,failureHandler: function(serviceResponse) {
		if (serviceResponse.errorMessage) {
			MessageHelper.displayErrorMessage(serviceResponse.errorMessage);
		} else {
			 if (serviceResponse.errorMessageKey) {
				MessageHelper.displayErrorMessage(serviceResponse.errorMessageKey);
			 }
		}
		cursor_clear();
	}
}), 

/**
 * This service updates the free gift choices made by the shopper for the
 * promotion.
 */
wc.service.declare({
	id: "AjaxUpdateRewardOption",
	actionId: "AjaxUpdateRewardOption",
	url: getAbsoluteURL() + "AjaxOrderChangeServiceRewardOptionUpdate",
	formId: ""

/**
 * Hides all the messages and the progress bar.
 * @param (object) serviceResponse The service response object, which is the
 * JSON object returned by the service invocation
 */
	,successHandler: function(serviceResponse) {
		MessageHelper.hideAndClearMessage();
		cursor_clear();
		
	}
 /**
 * Display an error message.
 * @param (object) serviceResponse The service response object, which is the
 * JSON object returned by the service invocation
 */
	,failureHandler: function(serviceResponse) {

		if (serviceResponse.errorMessage) {
			MessageHelper.displayErrorMessage(serviceResponse.errorMessage);				
		} 
		else {
			 if (serviceResponse.errorMessageKey) {
				MessageHelper.displayErrorMessage(serviceResponse.errorMessageKey);					
			 }
		}
		cursor_clear();
	}

}),

	/**
	 * Create a new saved order.
	 * Perform the service or command call.
	 */
	wc.service.declare({
		id: "AjaxOrderCreate",
		actionId: "AjaxOrderCreate",
		url: getAbsoluteURL() + "AjaxOrderCreate",
		formId: ""

     /**
     * display a success message
     * @param (object) serviceResponse The service response object, which is the
     * JSON object returned by the service invocation
     */
		,successHandler: function(serviceResponse) {
			MessageHelper.hideAndClearMessage();
			MessageHelper.displayStatusMessage(MessageHelper.messages["ORDER_CREATED"]);
			
			cursor_clear();
			
		}
	
     /**
     * display an error message
     * @param (object) serviceResponse The service response object, which is the
     * JSON object returned by the service invocation
     */
		,failureHandler: function(serviceResponse) {
    	
			if (serviceResponse.errorMessage) {
			
				 if (serviceResponse.errorCode == "CMN0409E")
				 {
					 MessageHelper.displayErrorMessage(MessageHelper.messages["ORDER_NOT_CREATED"]);
				 }
				 else
				 {
					 MessageHelper.displayErrorMessage(serviceResponse.errorMessage);
				 }
			} 
			else {
				 if (serviceResponse.errorMessageKey) {
					MessageHelper.displayErrorMessage(serviceResponse.errorMessageKey);
				 }
			}
			cursor_clear();
		}
	}),
	
	/**
	 * Cancel a single saved order.
	 * Perform the service or command call.
	 */
	wc.service.declare({
		id: "AjaxSingleOrderCancel",
		actionId: "AjaxSingleOrderCancel",
		url: getAbsoluteURL() + "AjaxOrderProcessServiceOrderCancel",
		formId: ""

     /**
     * display a success message
     * @param (object) serviceResponse The service response object, which is the
     * JSON object returned by the service invocation
     */
		,successHandler: function(serviceResponse) {
			MessageHelper.hideAndClearMessage();
			MessageHelper.displayStatusMessage(MessageHelper.messages["ORDERS_CANCELLED"]);
			cursor_clear();
		}
	
     /**
     * display an error message
     * @param (object) serviceResponse The service response object, which is the
     * JSON object returned by the service invocation
     */
		,failureHandler: function(serviceResponse) {
			
			if (serviceResponse.errorMessage) {
				 if (serviceResponse.errorCode == "CMN0409E")
				 {
					 MessageHelper.displayErrorMessage(MessageHelper.messages["ORDER_NOT_CANCELLED"]);
				 }
				 else
				 {
					 MessageHelper.displayErrorMessage(serviceResponse.errorMessage);
				 }
			} 
			else {
				 if (serviceResponse.errorMessageKey) {
					MessageHelper.displayErrorMessage(serviceResponse.errorMessageKey);
				 }
			}
			cursor_clear();
		}
	}),
	
	/**
	 * Cancel a saved order. This service is used to delete multiple saved orders one at a time.
	 * Perform the service or command call.
	 */
	wc.service.declare({
		id: "AjaxOrderCancel",
		actionId: "AjaxOrderCancel",
		url: getAbsoluteURL() + "AjaxOrderProcessServiceOrderCancel",
		formId: ""

     /**
     * display a success message
     * @param (object) serviceResponse The service response object, which is the
     * JSON object returned by the service invocation
     */
		,successHandler: function(serviceResponse) {
			// Call again to delete any other orders in the list.
			savedOrdersJS.cancelSavedOrder(false);
		}
	
     /**
     * display an error message
     * @param (object) serviceResponse The service response object, which is the
     * JSON object returned by the service invocation
     */
		,failureHandler: function(serviceResponse) {
			
			if (serviceResponse.errorMessage) {
				 if (serviceResponse.errorCode == "CMN0409E")
				 {
					 MessageHelper.displayErrorMessage(MessageHelper.messages["ORDER_NOT_CANCELLED"]);
				 }
				 else
				 {
					 MessageHelper.displayErrorMessage(serviceResponse.errorMessage);
				 }
			} 
			else {
				 if (serviceResponse.errorMessageKey) {
					MessageHelper.displayErrorMessage(serviceResponse.errorMessageKey);
				 }
			}
			cursor_clear();
		}
	}),
	
	/**
	 * Update the description of a single saved order. This service is used to update the description of a saved order.
	 * Perform the service or command call.
	 */
	wc.service.declare({
		id: "AjaxSingleOrderSave",
		actionId: "AjaxSingleOrderSave",
		url: getAbsoluteURL() + "AjaxOrderCopy",
		formId: ""

     /**
     * display a success message
     * @param (object) serviceResponse The service response object, which is the
     * JSON object returned by the service invocation
     */
		,successHandler: function(serviceResponse) {
			MessageHelper.hideAndClearMessage();
			if (savedOrdersJS.isOrderDetailsPageValue)
			{
				MessageHelper.displayStatusMessage(MessageHelper.messages["PENDING_ORDER_SAVED"]);
				
			}
			else
			{
				MessageHelper.displayStatusMessage(MessageHelper.messages["ORDERS_SAVED"]);
				
			}
			
			var inputElement = document.getElementById('OrderDescription_input');
        	if (inputElement != null && inputElement != 'undefined')
        	{
        		dojo.removeClass(inputElement, 'savedOrderDetailsInputBorderWarning'); 
        		dojo.addClass(inputElement, 'savedOrderDetailsInputBorder');
        		document.getElementById('OldOrderDescription').value = inputElement.value;
        	}
			cursor_clear();
			
			///If the rest of the non-ajax pending order details page needs to be updated to it here.
			if (savedOrdersJS.updateCartRequired)
			{
				savedOrdersJS.updateCartRequired = false;
				CheckoutHelperJS.updateShoppingCart(document.ShopCartForm);
			}
		}
	
     /**
     * display an error message
     * @param (object) serviceResponse The service response object, which is the
     * JSON object returned by the service invocation
     */
		,failureHandler: function(serviceResponse) {
			if (serviceResponse.errorMessage) {
				
				 if (serviceResponse.errorCode == "CMN0409E" || serviceResponse.errorCode == "CMN1024E")
				 {
					 if (serviceResponse.errorCode == "CMN1024E" && serviceResponse.systemMessage != "")
					 {
						 MessageHelper.displayErrorMessage(serviceResponse.systemMessage);
					 }
					 else
					 {
						 if (savedOrdersJS.isOrderDetailsPageValue)
						{
							MessageHelper.displayStatusMessage(MessageHelper.messages["PENDING_ORDER_NOT_SAVED"]);
							
						}
						else
						{
							MessageHelper.displayErrorMessage(MessageHelper.messages["ORDER_NOT_SAVED"]);
							
						}  
					 }
				 }
				 else
				 {
					 MessageHelper.displayErrorMessage(serviceResponse.errorMessage);
				 }
			} 
			else {
				 if (serviceResponse.errorMessageKey) {
					MessageHelper.displayErrorMessage(serviceResponse.errorMessageKey);
				 }
			}
			cursor_clear();
		}
	}),
	
	/**
	 * Update the description of a saved order. This service is used to update the description of multiple saved orders one at a time.
	 * Perform the service or command call.
	 */
	wc.service.declare({
		id: "AjaxOrderSave",
		actionId: "AjaxOrderSave",
		url: getAbsoluteURL() + "AjaxOrderCopy",
		formId: ""

     /**
     * display a success message
     * @param (object) serviceResponse The service response object, which is the
     * JSON object returned by the service invocation
     */
		,successHandler: function(serviceResponse) {
			// Call again to delete any other orders in the list.
			savedOrdersJS.saveOrder(false);
		}
	
     /**
     * display an error message
     * @param (object) serviceResponse The service response object, which is the
     * JSON object returned by the service invocation
     */
		,failureHandler: function(serviceResponse) {
			
			if (serviceResponse.errorMessage) {
				 if (serviceResponse.errorCode == "CMN0409E")
				 {
					 MessageHelper.displayErrorMessage(MessageHelper.messages["ORDER_NOT_SAVED"]);
				 }
				 else
				 {
					 MessageHelper.displayErrorMessage(serviceResponse.errorMessage);
				 }
			} 
			else {
				 if (serviceResponse.errorMessageKey) {
					MessageHelper.displayErrorMessage(serviceResponse.errorMessageKey);
				 }
			}
			cursor_clear();
		}
	}),
	
	/**
	 * Set the current order to be that of a saved order.
	 * Perform the service or command call.
	 */
	wc.service.declare({
		id: "AjaxSetPendingOrder",
		actionId: "AjaxSetPendingOrder",
		url: getAbsoluteURL() + "AjaxSetPendingOrder",
		formId: ""

     /**
     * display a success message
     * @param (object) serviceResponse The service response object, which is the
     * JSON object returned by the service invocation
     */

		,successHandler: function(serviceResponse) {
			
			MessageHelper.hideAndClearMessage();
			
			MessageHelper.displayStatusMessage(MessageHelper.messages["ORDER_SET_CURRENT"]);
			
			savedOrdersJS.determinePageForward("AjaxSetPendingOrder");
			
			cursor_clear();
			
		}
     /**
     * display an error message
     * @param (object) serviceResponse The service response object, which is the
     * JSON object returned by the service invocation
     */
		,failureHandler: function(serviceResponse) {
			
			if (serviceResponse.errorMessage) {
				 if (serviceResponse.errorCode == "CMN0409E" || serviceResponse.errorCode == "CMN1024E")
				 {
					 MessageHelper.displayErrorMessage(MessageHelper.messages["ORDER_NOT_SET_CURRENT"]);
				 }
				 else
				 {
					 MessageHelper.displayErrorMessage(serviceResponse.errorMessage);
				 }
			} 
			else {
				 if (serviceResponse.errorMessageKey) {
					MessageHelper.displayErrorMessage(serviceResponse.errorMessageKey);
				 }
			}
			cursor_clear();
		}

	}),
	
	
	/**
	 * Updates the current pending order setting it to the current shopping cart.
	 * This service does not cause a refresh of the ListOrdersDisplay_Controller registered widgets.
	 * The main function of this service is to keep the cpendorder database table in line with the current shopping cart.
	 * Perform the service or command call.
	 * @constructor
	 */
	wc.service.declare({
		id: "AjaxUpdatePendingOrder",
		actionId: "AjaxUpdatePendingOrder",
		url: getAbsoluteURL() + "AjaxSetPendingOrder",
		formId: ""

     /**
     * There is nothing to do in the event of a success of this service since it is executed in the background.
     * @param (object) serviceResponse The service response object, which is the
     * JSON object returned by the service invocation
     */

		,successHandler: function(serviceResponse) {
			
			savedOrdersJS.determinePageForward("AjaxUpdatePendingOrder");
			cursor_clear();
			
		}
     /**
     * display an error message
     * @param (object) serviceResponse The service response object, which is the
     * JSON object returned by the service invocation
     */
		,failureHandler: function(serviceResponse) {
			
			if (serviceResponse.errorMessage) {
				if (serviceResponse.errorCode == "CMN0409E")
				 {
					 MessageHelper.displayErrorMessage(MessageHelper.messages["ORDER_NOT_SET_CURRENT"]);
				 }
				 else
				 {
					 MessageHelper.displayErrorMessage(serviceResponse.errorMessage);
				 }
			} 
			else {
				 if (serviceResponse.errorMessageKey) {
					MessageHelper.displayErrorMessage(serviceResponse.errorMessageKey);
				 }
			}
			cursor_clear();
		}

	}),
	
	/**
	 * Copy a saved order.
	 * Perform the service or command call. 
	 */
	wc.service.declare({
		id: "AjaxSingleOrderCopy",
		actionId: "AjaxSingleOrderCopy",
		url: getAbsoluteURL() + "AjaxOrderCopy",
		formId: ""

     /**
     * display a success message
     * @param (object) serviceResponse The service response object, which is the
     * JSON object returned by the service invocation
     */

		,successHandler: function(serviceResponse) {

		var params = [];
		
		params.storeId		= this.storeId;
		params.catalogId	= this.catalogId;
		params.langId		= this.langId;
		params.URL="";
		params.updatePrices = "1";
		
		params.orderId = serviceResponse.orderId;
		params.calculationUsageId = "-1";
		
		wc.service.invoke("AjaxSingleOrderCalculate", params);
			MessageHelper.hideAndClearMessage();
			
		}
	
     /**
     * display an error message
     * @param (object) serviceResponse The service response object, which is the
     * JSON object returned by the service invocation
     */
		,failureHandler: function(serviceResponse) {
			
			if (serviceResponse.errorMessage) {
				 if (serviceResponse.errorCode == "CMN0409E")
				 {
					 MessageHelper.displayErrorMessage(MessageHelper.messages["ORDER_NOT_COPIED"]);
				 }
				 else
				 {
					 MessageHelper.displayErrorMessage(serviceResponse.errorMessage);
				 }
			} 
			else {
				 if (serviceResponse.errorMessageKey) {
					MessageHelper.displayErrorMessage(serviceResponse.errorMessageKey);
				 }
			}
			cursor_clear();
		}

	}),
	
	/**
	 * Copy a saved order.
	 * Perform the service or command call. 
	 */
	wc.service.declare({
		id: "AjaxOrderCopy",
		actionId: "AjaxOrderCopy",
		url: getAbsoluteURL() + "AjaxOrderCopy",
		formId: ""

    /**
    * display a success message
    * @param (object) serviceResponse The service response object, which is the
    * JSON object returned by the service invocation
    */

		,successHandler: function(serviceResponse) {

		var params = [];
		
		params.storeId		= this.storeId;
		params.catalogId	= this.catalogId;
		params.langId		= this.langId;
		params.URL="";
		params.updatePrices = "1";
		
		params.orderId = serviceResponse.orderId;
		params.calculationUsageId = "-1";
		
		wc.service.invoke("AjaxOrderCalculate", params);
			MessageHelper.hideAndClearMessage();
			
		}
	
    /**
    * display an error message
    * @param (object) serviceResponse The service response object, which is the
    * JSON object returned by the service invocation
    */
		,failureHandler: function(serviceResponse) {
			
			if (serviceResponse.errorMessage) {
				 if (serviceResponse.errorCode == "CMN0409E")
				 {
					 MessageHelper.displayErrorMessage(MessageHelper.messages["ORDER_NOT_COPIED"]);
				 }
				 else
				 {
					 MessageHelper.displayErrorMessage(serviceResponse.errorMessage);
				 }
			} 
			else {
				 if (serviceResponse.errorMessageKey) {
					MessageHelper.displayErrorMessage(serviceResponse.errorMessageKey);
				 }
			}
			cursor_clear();
		}

	}),
	
	/**
	 * Perform the order calculation operations to compute the contract prices for the order items in an order.
	 * Perform the service or command call.
	 */
	wc.service.declare({
		id: "AjaxSingleOrderCalculate",
		actionId: "AjaxSingleOrderCalculate",
		url: getAbsoluteURL() + "AjaxOrderCalculate",
		formId: ""

     /**
     * display a success message
     * @param (object) serviceResponse The service response object, which is the
     * JSON object returned by the service invocation
     */

		,successHandler: function(serviceResponse) {
			
			MessageHelper.hideAndClearMessage();
			MessageHelper.displayStatusMessage(MessageHelper.messages["ORDER_COPIED"]);
			cursor_clear();
		}
	
     /**
     * display an error message
     * @param (object) serviceResponse The service response object, which is the
     * JSON object returned by the service invocation
     */
		,failureHandler: function(serviceResponse) {
			
			if (serviceResponse.errorMessage) {
				if (serviceResponse.errorCode == "CMN0409E")
				 {
					 MessageHelper.displayErrorMessage(MessageHelper.messages["ORDER_NOT_COPIED"]);
				 }
				 else
				 {
					 MessageHelper.displayErrorMessage(serviceResponse.errorMessage);
				 }
			} 
			else {
				 if (serviceResponse.errorMessageKey) {
					MessageHelper.displayErrorMessage(serviceResponse.errorMessageKey);
				 }
			}
			cursor_clear();
		}

	}),
	
	/**
	 * Perform the order calculation operations to compute the contract prices for the order items in an order.
	 * Perform the service or command call.
	 */
	wc.service.declare({
		id: "AjaxCurrentOrderCalculate",
		actionId: "AjaxCurrentOrderCalculate",
		url: getAbsoluteURL() + "AjaxOrderCalculate",
		formId: ""

     /**
     * display a success message
     * @param (object) serviceResponse The service response object, which is the
     * JSON object returned by the service invocation
     */

		,successHandler: function(serviceResponse) {
			
			MessageHelper.hideAndClearMessage();
			MessageHelper.displayStatusMessage(MessageHelper.messages["ORDER_SET_CURRENT"]);
			cursor_clear();
		}
	
     /**
     * display an error message
     * @param (object) serviceResponse The service response object, which is the
     * JSON object returned by the service invocation
     */
		,failureHandler: function(serviceResponse) {
			
			if (serviceResponse.errorMessage) {
				if (serviceResponse.errorCode == "CMN0409E")
				 {
					 MessageHelper.displayErrorMessage(MessageHelper.messages["ORDER_NOT_COPIED"]);
				 }
				 else
				 {
					 MessageHelper.displayErrorMessage(serviceResponse.errorMessage);
				 }
			} 
			else {
				 if (serviceResponse.errorMessageKey) {
					MessageHelper.displayErrorMessage(serviceResponse.errorMessageKey);
				 }
			}
			cursor_clear();
		}

	}),
	
	/**
	 * Perform the order calculation operations to compute the contract prices for the order items in an order.
	 * Perform the service or command call.
	 */
	wc.service.declare({
		id: "AjaxOrderCalculate",
		actionId: "AjaxOrderCalculate",
		url: getAbsoluteURL() + "AjaxOrderCalculate",
		formId: ""

     /**
     * display a success message
     * @param (object) serviceResponse The service response object, which is the
     * JSON object returned by the service invocation
     */

		,successHandler: function(serviceResponse) {
			
			MessageHelper.hideAndClearMessage();
			// Call again to copy any other orders in the list.
			savedOrdersJS.copyOrder(false);
		}
	
     /**
     * display an error message
     * @param (object) serviceResponse The service response object, which is the
     * JSON object returned by the service invocation
     */
		,failureHandler: function(serviceResponse) {
			
			if (serviceResponse.errorMessage) {
				if (serviceResponse.errorCode == "CMN0409E")
				 {
					 MessageHelper.displayErrorMessage(MessageHelper.messages["ORDER_NOT_COPIED"]);
				 }
				 else
				 {
					 MessageHelper.displayErrorMessage(serviceResponse.errorMessage);
				 }
			} 
			else {
				 if (serviceResponse.errorMessageKey) {
					MessageHelper.displayErrorMessage(serviceResponse.errorMessageKey);
				 }
			}
			cursor_clear();
		}

	}),
	

	/**
	 * Processes a punchout payment request.
	 */
	wc.service.declare({
		id: "AjaxPunchoutPay",
		actionId: "AjaxPunchoutPay",
		url: "PunchoutPaymentRepay",
		formId: ""
		
		/**
		 * Calls PunchoutJS.handleResponse to render the punchout payment section on the page.
		 * @param (object) serviceResponse The service response object, which is the JSON object returned by the service invocation
		 */
		,successHandler: function(serviceResponse) {
			PunchoutJS.handleResponse(serviceResponse.orderId);
			MessageHelper.hideAndClearMessage();
			cursor_clear();
		}
		
		/**
		 * Displays an error message on the page if the request failed.
		 * @param (object) serviceResponse The service response object, which is the JSON object returned by the service invocation.
		 */
		,failureHandler: function(serviceResponse) {
			if (serviceResponse.errorMessage) {
				MessageHelper.displayErrorMessage(serviceResponse.errorMessage);
			} else {
				if (serviceResponse.errorMessageKey) {
					MessageHelper.displayErrorMessage(serviceResponse.errorMessageKey);
				}
			}
			cursor_clear();
		}
	}),
	
	
	/**
	 * Subscribes to or unsubscribes from receiving information related to a particular category in the store.
	 */
	wc.service.declare({
		id: "AjaxCategorySubscribe",
		actionId: "AjaxCategorySubscribe",
		url: "AjaxMarketingTriggerProcessServiceEvaluate",
		formId: ""
		
		/**
		 * Clear messages on the page.
		 * @param (object) serviceResponse The service response object, which is the JSON object returned by the service invocation
		 */
		,successHandler: function(serviceResponse) {
			MessageHelper.hideAndClearMessage();
			MessageHelper.displayStatusMessage(MessageHelper.messages["SUBSCRIPTION_UPDATED"]);
		}
		
		/**
		 * Displays an error message on the page if the request failed.
		 * @param (object) serviceResponse The service response object, which is the JSON object returned by the service invocation.
		 */
		,failureHandler: function(serviceResponse) {
			if (serviceResponse.errorMessage) {
				MessageHelper.displayErrorMessage(serviceResponse.errorMessage);
			} else {
				if (serviceResponse.errorMessageKey) {
					MessageHelper.displayErrorMessage(serviceResponse.errorMessageKey);
				}
			}
			cursor_clear();
		}
	}),
	
	
	/**
	 * HHG-469
	 * 
	 */
	wc.service.declare({
		id: "AjaxHHGOrderUpdate",
		actionId: "AjaxHHGOrderUpdate",
		url: getAbsoluteURL() + "AjaxHHGOrderUpdate",
		formId: ""
		
		,successHandler: function(serviceResponse) {
			MessageHelper.hideAndClearMessage();
			//MessageHelper.displayStatusMessage(MessageHelper.messages["SUBSCRIPTION_UPDATED"]);
			var totalProduct = serviceResponse['productTotal'];
			var orderTotal = serviceResponse['orderTotal'];
			var shipCharge = serviceResponse['shipCharge'];
			var deliveryCharge = serviceResponse['deliveryCharge'];
			var shippingType = serviceResponse['shippingType'];
			var orderItemsId  = serviceResponse['orderItemsId'];
			var orderId = serviceResponse['orderId'];
			var physicalStoreListSize  = serviceResponse['physicalStoreListSize'];
			
			var hideElement = document.getElementById("WC_SingleShipmentOrderTotalsSummary_td_2") ;
			if(hideElement == null || hideElement == undefined ){
				
			}else{
				document.getElementById("WC_SingleShipmentOrderTotalsSummary_td_2").innerHTML =  "$"+totalProduct ;
			}
			
			document.getElementById("WC_SingleShipmentOrderTotalsDeliverySummary_td_8").innerHTML =  "$"+deliveryCharge  ;
			document.getElementById("WC_SingleShipmentOrderTotalsSummary_td_8").innerHTML = "$"+shipCharge  ;
			
			hideElement = document.getElementById("WC_SingleShipmentOrderTotalsSummary_td_10");
			if(hideElement == null || hideElement == undefined ){
				
			}else{
				document.getElementById("WC_SingleShipmentOrderTotalsSummary_td_10").innerHTML =  "$"+orderTotal ;	
			}
			
			
			
			
			if(shippingType == "PickupInStore"){
				dojo.byId("MS_ShippingAddress_Section_"+ orderItemsId).style.display= "none";
				dojo.byId("shippingInstructionsCheckBoxDiv_"+orderItemsId).style.display= "none";
				dojo.byId("deliveryDateDiv_"+orderItemsId).style.display= "none";
				/*
				if (!secondDlg) {
					ModalJS.init();
				}
				var url= "PickUpInStoreListingView?"+ "name=modal instore_list";
				ModalJS.showDialogTwo(url);*/
			}else if(shippingType == "Delivery"){
				dojo.byId("MS_ShippingAddress_Section_"+ orderItemsId).style.display= "block";
				dojo.byId("shippingInstructionsCheckBoxDiv_"+orderItemsId).style.display= "block";
				dojo.byId("deliveryDateDiv_"+orderItemsId).style.display= "block";	
				var renderContext = wc.render.getContextById("userAddressDisplayContext");
				renderContext.properties["orderItemId"] = serviceResponse.orderItemsId;
				renderContext.properties["nickName"] = serviceResponse.nickName;
				renderContext.properties["addressId"] = serviceResponse.addressId;
				wc.render.updateContext("userAddressDisplayContext");
			}else{
				dojo.byId("MS_ShippingAddress_Section_"+ orderItemsId).style.display= "block";
				dojo.byId("deliveryDateDiv_"+orderItemsId).style.display= "none";
				dojo.byId("shippingInstructionsCheckBoxDiv_"+orderItemsId).style.display= "none";
				var renderContext = wc.render.getContextById("userAddressDisplayContext");
				renderContext.properties["orderItemId"] = serviceResponse.orderItemsId;
				renderContext.properties["nickName"] = serviceResponse.nickName;
				renderContext.properties["addressId"] = serviceResponse.addressId;
				wc.render.updateContext("userAddressDisplayContext");
			}
			cursor_clear();

			if(shippingType.indexOf("Delivery") != -1 && document.getElementById("InstallKit_"+orderItemsId) != undefined) {
				var catId = document.getElementById("InstallKit_"+orderItemsId).value;
				//var orderId = document.getElementById("currentOrderId").value;
				var shipModeValues = document.getElementsByName("shipModeId_"+orderItemsId);
				for (i=0; i<shipModeValues.length; i++) {

					  if (shipModeValues[i].checked == true) {
						  var shipMode = shipModeValues[i].value;
						  var shipModeValue = shipMode.split("_")[0];
						  var index = shipMode.split("_")[1];
						  var ffmCenterId = document.getElementsByName("ffmCenterId_"+orderItemsId+"_"+index);
						  ffmCenterId = ffmCenterId[0].value;
						  ModalJS.showDialogTwo('InstallKitAddView?orderId='+orderId+'&catentryId='+catId+'&orderItemId='+orderItemsId+'&shipMode='+shipModeValue+'&ffm='+ffmCenterId+'&name=modal installKitModal');
					  }
				}
			}			
		}
		
		,failureHandler: function(serviceResponse) {

			var ompageName = "";
			var omUserType = "";
			if(document.getElementById("omPageNameId") != null  &&   document.getElementById("omPageNameId") != undefined ){
				ompageName =document.getElementById("omPageNameId").value;
			}
			if(document.getElementById("omUserTypeId") != null  &&   document.getElementById("omUserTypeId") != undefined ){
				omUserType = document.getElementById("omUserTypeId").value;
			}			
			console.log('AjaxHHGOrderUpdate begin ompageName='+ompageName + ' , omUserType='+omUserType);

			if (serviceResponse.errorMessage) {
				MessageHelper.displayErrorMessage(serviceResponse.errorMessage);
				omniturePageViewTag(ompageName,null,null,null,null,omUserType,null,"event49", serviceResponse.errorMessage);
			} else {
				if (serviceResponse.errorMessageKey) {
					MessageHelper.displayErrorMessage(serviceResponse.errorMessageKey);
					omniturePageViewTag(ompageName,null,null,null,null,omUserType,null,"event49", serviceResponse.errorMessageKey);
				}
			}
			console.log("AjaxHHGOrderUpdate end");
			cursor_clear();
		}	
	}),
		
	
	
	
	/**
	 * Added for HHG-333 & HHG-331
	 * Makes a fulfillment service call and checks product availability
	 */
	wc.service.declare({
		id: "AjaxCheckProductAvailabilityService",
		actionId: "AjaxCheckProductAvailabilityService",
		url: getAbsoluteURL() + "AjaxCheckProductAvailabilityService",
		formId: ""
		
		/**
		 * Clear messages on the page.
		 * @param (object) serviceResponse The service response object, which is the JSON object returned by the service invocation
		 */
		,successHandler: function(serviceResponse) {
			var pickupAvailable = serviceResponse['pickupAvailable_0'],
				deliveryAvailable = serviceResponse['deliveryAvailable_0'],
				shippingAvailable = serviceResponse['shippingAvailable_0'],
				callFromPDP = document.getElementById('callFromPDP'),
				/* Setup html variable for not available */
				html = '<p class="no-avail">Not Available in any Store.</p>';
			if (callFromPDP != undefined || callFromPDP != null) { callFromPDP = callFromPDP.value; }
			
			if(callFromPDP == 'true') {
				/*Start: HHG-758	Need to add check availability logic to PDP*/
				dojo.byId('check_availability_pdp_zip_heading').innerHTML = dojo.byId('checkProductAvailabilityZipCode').value;
				dojo.query('#check_store_availability').style('display','none');
				dojo.query('#productAvailabilityDetails').style('display','block');
				if(deliveryAvailable == true){
					dojo.query('#deliveryAvailable').style('display','block');
					dojo.query('#noDeliveryAvailable').style('display','none');
				}else{
					dojo.query('#noDeliveryAvailable').style('display','block');
					dojo.query('#deliveryAvailable').style('display','none');
				}
				if(pickupAvailable==true){
					dojo.query('#pickupAvailable').style('display','block');
					dojo.query('#noPickupAvailable').style('display','none');
				}else{
					dojo.query('#noPickupAvailable').style('display','block');
					dojo.query('#pickupAvailable').style('display','none');
				}
				
				if(shippingAvailable == true){
					dojo.query('#shippingAvailable').style('display','block');
					dojo.query('#noShippingAvailable').style('display','none');
				}else{
					dojo.query('#noShippingAvailable').style('display','block');
					dojo.query('#shippingAvailable').style('display','none');
				}
				/*End: HHG-758	Need to add check availability logic to PDP*/
				
				var pickupCount = parseInt(serviceResponse['pickupCount_0']);
				var pickupShipmodeId = parseInt(serviceResponse['shipmodeId_0']);
				document.getElementById('ffmServiceResponse_PickUpCount').value = pickupCount;
				var index = 0;
				
				/* Create the variable to hold the table HTML as we build it */
				var tableInnerHTML = "";
				tableInnerHTML += '<table width="500" cellspacing="0" cellpadding="0" style="border-collapse:separate">';
				tableInnerHTML += "<tr>";
				tableInnerHTML += '<td class="store-header">Store address</td>';
				tableInnerHTML += '<td class="distance-header">Distance</td>';
				tableInnerHTML += '<td class="option-header">Pick-up in-store</td>';
				tableInnerHTML += '<td class="option-header">ship to store</td>';
				tableInnerHTML += '<td class="option-header">ship to home</td>';
				tableInnerHTML += '<td class="option-header">delivery</td>';
                tableInnerHTML += '</tr>';
				
				var numAvailableLocations = 0;
				
				for(index = 0; index < pickupCount; ++index) {
					var pickupMessageFromServiceResponse = serviceResponse['pickupmessage_0_' + index],
						inStock = false;
					if (serviceResponse['inStock_0_' + index] != undefined) {
						if(serviceResponse['inStock_0_' + index] == true || serviceResponse['inStock_0_' + index] == 'true') {
							inStock = true;
						}
					}
					
					if (inStock) numAvailableLocations++;
					
					/* Open <tr> */
					var trid = 'availableStoresList_OutOfStock_'+index;
					if (inStock) { trid = 'availableStoresList_InStock_'+index; }
					tableInnerHTML += '<tr id="'+trid+'">';
					
					console.log(serviceResponse);
					/* Store Address */
					tableInnerHTML += '<td class="store-cell">' + serviceResponse['pickupStoreAddr_0_' + index] + "</td>";
					
					/* Distance */
					var distance = 'N/A';
					if(serviceResponse['distance_0_' + index] != undefined) {
						distance = parseFloat(serviceResponse['distance_0_' + index]);
						distance = distance.toFixed(2);
					}
					tableInnerHTML += '<td class="distance-cell">' + distance + "</td>";
					
					var pickUpMessage = serviceResponse['pickupmessage_0_' + index].toLowerCase();
					
					var availableImgSrc = "/wcsstore/MadisonsStorefrontAssetStore/images/placeholders/checkMark.png";
					var unAvailableISrc = "/wcsstore/MadisonsStorefrontAssetStore/images/placeholders/notAvail.png";
					
					/* Pick-up */
					tableInnerHTML += '<td class="option-cell"><img src="' + (pickUpMessage == "in-stock" ? availableImgSrc : unAvailableISrc) + '" /></td>';
					
					/* Ship to Store */
					tableInnerHTML += '<td class="option-cell"><img src="' + (pickUpMessage == "ship to store" ? availableImgSrc : unAvailableISrc) + '" /></td>';
					
					/* Ship to Home */
					tableInnerHTML += '<td class="option-cell"><img src="' + (shippingAvailable ? availableImgSrc : unAvailableISrc) + '" /></td>';
					
					/* Delivery */
					tableInnerHTML += '<td class="option-cell"><img src="' + (deliveryAvailable ? availableImgSrc : unAvailableISrc) + '" /></td>';
					
					/* Close <tr> */
					tableInnerHTML += "</tr>";
				}
				/* Close <tbody> and <table> */
				tableInnerHTML += "</table>"
				html = tableInnerHTML;
				
				//If none of the returned stores have the item in stock, display "Store Pickup Not Available" text/image
				if (numAvailableLocations == 0) {
					dojo.query('#noPickupAvailable').style('display','block');
					dojo.query('#pickupAvailable').style('display','none');
				}
				else {
					dojo.query('#noPickupAvailable').style('display','none');
					dojo.query('#pickupAvailable').style('display','block');
				}
			}
			else if (pickupAvailable == true) {
				var pickupCount = parseInt(serviceResponse['pickupCount_0']);
				var pickupShipmodeId = parseInt(serviceResponse['shipmodeId_0']);
				document.getElementById('ffmServiceResponse_PickUpCount').value = pickupCount;
				var index = 0;
				/* Create the divValue variable, open our <table> and include the <thead> along with opening the <tbody> */
				var divValue = '<table class="local-pickup">'
				+'<thead>'
					+'<th>Store Address</th>'
					+'<th>Availability</th>'
					+'<th>Distance</th>'
					+'<th><input type="checkbox" onclick="javascript: displayOnlyInStockItems(this);">Only show in stock</th>'
				+'</thead>'
				+'<tbody>';
				
				var numAvailableLocations = 0;
				
				for(index = 0; index < pickupCount; ++index) {
					var pickupMessageFromServiceResponse = serviceResponse['pickupmessage_0_' + index],
						inStock = false;
					if (serviceResponse['inStock_0_' + index] != undefined) {
						if(serviceResponse['inStock_0_' + index] == true || serviceResponse['inStock_0_' + index] == 'true') {
							inStock = true;
						}
					}
					
					if (inStock) numAvailableLocations++;
					
					/* Open <tr> */
					var trid = 'availableStoresList_OutOfStock_'+index;
					if (inStock) { trid = 'availableStoresList_InStock_'+index; }
					divValue = divValue + '<tr id="'+trid+'">';
					
					console.log(serviceResponse);
					/* Store Address */
					divValue = divValue + "<td>" + serviceResponse['pickupStoreAddr_0_' + index] + "</td>";
					
					/* Availablity */
					var pickupMessage = '<td class="no-avail">Not Available</td>';
					if (inStock) {
						pickupMessage = "<td class='avail'>In Stock</td>";
					}
					divValue = divValue + pickupMessage;
					
					/* Distance */
					var distance = 'N/A';
					if(serviceResponse['distance_0_' + index] != undefined) {
						distance = parseFloat(serviceResponse['distance_0_' + index]);
						distance = distance.toFixed(2);
					}
					divValue = divValue + "<td>" + distance + "</td>";
					
					/* Action button */
					if(inStock) {
						if(callFromPDP == 'true') {
							var ffmCenterId = serviceResponse['ffmCenterId_0_' + index];
							var productCatentryId = document.getElementById('productCatentryId').value;
							var productQuantityValue = document.getElementById('productQuantityValue').value;
							document.getElementById('ffmServiceResponse_SaveFFMCenterId').value = 'true';
							divValue = divValue + "<td><a href='javascript: categoryDisplayJS.AddItem2ShopCartAjax(" + productCatentryId + "," + productQuantityValue + "," + ffmCenterId + ",null," +pickupShipmodeId +"); closeCheckAvailabilityModal();'>Add To Cart</a></td>";
						} else {
							var ffmCenterId = serviceResponse['ffmCenterId_0_' + index];
							divValue = divValue + "<td><a href='javascript: selectStore("+ ffmCenterId +"); closeCheckAvailabilityModal();'>Select Store</a></td>";
						}
					} else {
						divValue = divValue + "<td></td>";
					}
					
					/* Close <tr> */
					divValue = divValue + "</tr>";
				}
				/* Close <tbody> and <table> */
				html = divValue + '</tbody></table>';
			}
			
			dojo.query('#OnlyZipCodeTop, #OnlyZipCodeButtons').style('display','none');
			dojo.query('#FFMResultTop, #FFMResultButtons, #WC_CheckProductAvailability_link_1, #checkProductAvailabilityTable').style('display', 'block');
			dojo.byId("checkProductAvailabilityTable").innerHTML = html;
			
			requestSubmitted = false;
		}
		
		/**
		 * Displays an error message on the page if the request failed.
		 * @param (object) serviceResponse The service response object, which is the JSON object returned by the service invocation.
		 */
		,failureHandler: function(serviceResponse) {
			
			var callFromPDP = document.getElementById('callFromPDP');
			if (callFromPDP != undefined || callFromPDP != null) { callFromPDP = callFromPDP.value; }
			if(callFromPDP == 'true') {
				/*Start: HHG-758	Need to add check availability logic to PDP*/
				dojo.query('#check_store_availability').style('display','none');
				dojo.byId('check_availability_pdp_zip_heading').innerHTML = dojo.byId('checkProductAvailabilityZipCode').value;
				dojo.query('#productAvailabilityDetails').style('display','block');
				dojo.query('#noDeliveryAvailable').style('display','block');
				dojo.query('#noPickupAvailable').style('display','block');
				dojo.query('#noShippingAvailable').style('display','block');
				dojo.query('#deliveryAvailable').style('display','none');
				dojo.query('#pickupAvailable').style('display','none');
				dojo.query('#shippingAvailable').style('display','none');
				dojo.query('#pickupAvailableOOS').style('display','none');
				 
			} 
			dojo.query('#OnlyZipCodeTop, #OnlyZipCodeButtons').style('display','none');
			dojo.query('#FFMResultTop, #FFMResultButtons, #WC_CheckProductAvailability_link_1, #checkProductAvailabilityTable').style('display', 'block');
			dojo.byId("checkProductAvailabilityTable").innerHTML = "<p class='no-avail'>Please enter a valid zip code.</p>";
			requestSubmitted = false; 
		}
	}),
	
	/**
	 * Added for HHG-584
	 * CallMe Service
	 */
	wc.service.declare({
		id: "AjaxCallMeService",
		actionId: "AjaxCallMeService",
		url: getAbsoluteURL() + "AjaxCallMeService",
		formId: ""
		
		/**
		 * Clear messages on the page.
		 * @param (object) serviceResponse The service response object, which is the JSON object returned by the service invocation
		 */
		,successHandler: function(serviceResponse) {
			var callMeServiceSuccess = serviceResponse['callMeServiceSuccess'];
			var reCaptchaSuccess = serviceResponse['reCaptchaSuccess'];
			var storePhoneNo = serviceResponse['callMeStorePhoneNo'];
			
			if(!reCaptchaSuccess || reCaptchaSuccess==undefined){
				dojo.byId("CallMereCaptchaErrorMessage").style.display = "block";
			}
			else if(reCaptchaSuccess) {
                dojo.byId("CallMeRequestScreenHeader").style.display = "none";
                dojo.byId("CallMeModalForm").style.display = "none";
                dojo.byId("CallMeCallerPhoneDisplay").value = params.phoneNumber;
                dojo.byId("CallMeCallerZipDisplay").value = params.zipCode;
                dojo.byId("CallMeCallerInfo").style.display = "block";
                callMePoll();
			}
		}
		/**
		 * Displays an error message on the page if the request failed.
		 * @param (object) serviceResponse The service response object, which is the JSON object returned by the service invocation.
		 */
		,failureHandler: function(serviceResponse) {
            dojo.byId("CallMeRequestScreenHeader").style.display = "none";
            dojo.byId("CallMeModalForm").style.display = "none";
            dojo.byId("CallMeCallerPhoneDisplay").value = params.phoneNumber;
            dojo.byId("CallMeCallerZipDisplay").value = params.zipCode;
            dojo.byId("CallMeCallerInfo").style.display = "block";
            dojo.byId("CallMeStoreMessage").style.display = "block";
		}
	}),
	
	/**
	 * CallMe Polling Service
	 */
	wc.service.declare({
		id: "AjaxHHGCallMePoll",
		actionId: "AjaxHHGCallMePoll",
		url: getAbsoluteURL() + "AjaxHHGCallMePoll",
		formId: ""
		
		/**
		 * Clear messages on the page.
		 * @param (object) serviceResponse The service response object, which is the JSON object returned by the service invocation
		 */
		,successHandler: function(serviceResponse) {
			if (serviceResponse.responseMsg.length > 0) {
				dojo.byId("CallMeProduct").style.display = "block";
				dojo.byId("CallMeStoreMessage").style.display = "block";
			}
			else if (serviceResponse.xcallmeId != "expired") {
				callMePoll();
			}
			else {
				dojo.byId("CallMeStoreMessage").style.display = "block";
			}
		}
		/**
		 * Displays an error message on the page if the request failed.
		 * @param (object) serviceResponse The service response object, which is the JSON object returned by the service invocation.
		 */
		,failureHandler: function(serviceResponse) {
            dojo.byId("CallMeStoreMessage").style.display = "block";
		}
	}),
	
	/**
	 * 
	 * Added for HHG-1291
	 * Makes a call to HHGOrderItemFFMCenterUpdate command and updates the FFMCenterID and ShipModeID 
	 */
	wc.service.declare({
		id: "AjaxHHGOrderItemFFMCenterUpdate",
		actionId: "AjaxHHGOrderItemFFMCenterUpdate",
		url: getAbsoluteURL() + "AjaxHHGOrderItemFFMCenterUpdate",
		formId: ""
		
		/**
		 * Clear messages on the page.
		 * @param (object) serviceResponse The service response object, which is the JSON object returned by the service invocation
		 */
		,successHandler: function(serviceResponse) {
			var totalProduct = serviceResponse['productTotal'];
			var orderTotal = serviceResponse['orderTotal'];
			var shipCharge = serviceResponse['shipCharge'];
			var deliveryCharge = serviceResponse['deliveryCharge'];
			
			document.getElementById("WC_SingleShipmentOrderTotalsSummary_td_2").innerHTML =  "$"+totalProduct ;
			document.getElementById("WC_SingleShipmentOrderTotalsDeliverySummary_td_8").innerHTML =  "$"+deliveryCharge  ;
			document.getElementById("WC_SingleShipmentOrderTotalsSummary_td_8").innerHTML = "$"+shipCharge  ;
			document.getElementById("WC_SingleShipmentOrderTotalsSummary_td_10").innerHTML =  "$"+orderTotal ;
		
			//HHG-1338 - Refreshing the physical address area by updating the context with selected ffmcenterid
			var renderContext = wc.render.getContextById("physicalAddressDisplayContext");
			renderContext.properties["orderItemId"] = serviceResponse.orderItemId;
			renderContext.properties["ffmCenterId_"+orderItemId] = serviceResponse.fulfillmentCenterId;
			wc.render.updateContext("physicalAddressDisplayContext");
			
			dojo.byId("deliveryDateDiv_"+serviceResponse.orderItemId).style.display= "none";
			dojo.byId("MS_requestShippingDateDiv_"+serviceResponse.orderItemId).style.display= "none";
			dojo.byId("shippingInstructionsCheckBoxDiv_"+serviceResponse.orderItemId).style.display= "none";
			cursor_clear();
		}
		/**
		 * Displays an error message on the page if the request failed.
		 * @param (object) serviceResponse The service response object, which is the JSON object returned by the service invocation.
		 */
		,failureHandler: function(serviceResponse) {
			
 			var ompageName = "";
			var omUserType = "";
			if(document.getElementById("omPageNameId") != null  &&   document.getElementById("omPageNameId") != undefined ){
				ompageName =document.getElementById("omPageNameId").value;
			}
			if(document.getElementById("omUserTypeId") != null  &&   document.getElementById("omUserTypeId") != undefined ){
				omUserType = document.getElementById("omUserTypeId").value;
			}			
			console.log('HHGOrderItemFFMCenterUpd begin ompageName='+ompageName + ' , omUserType='+omUserType);
			
			if (serviceResponse.errorMessage) {
				MessageHelper.displayErrorMessage(serviceResponse.errorMessage);
				omniturePageViewTag(ompageName,null,null,null,null,omUserType,null,"event49", serviceResponse.errorMessage);
			} else {
				if (serviceResponse.errorMessageKey) {
					MessageHelper.displayErrorMessage(serviceResponse.errorMessageKey);
					omniturePageViewTag(ompageName,null,null,null,null,omUserType,null,"event49", serviceResponse.errorMessageKey);
				}
			}
			
			console.log("HHGOrderItemFFMCenterUpd end");
			cursor_clear();
		}
	})
