//-----------------------------------------------------------------
// Licensed Materials - Property of IBM
//
// WebSphere Commerce
//
// (C) Copyright IBM Corp. 2008, 2009 All Rights Reserved.
//
// US Government Users Restricted Rights - Use, duplication or
// disclosure restricted by GSA ADP Schedule Contract with
// IBM Corp.
//-----------------------------------------------------------------


PersonalBarHelperJS = {
		/* Global variable declarations */
		
		isLoadedBrowserHistory:false,
		isLoadedWishList:false,
		isLoadedShoppingCart:false,
		
		
		// Init the JavaScript object
		init: function(){
			dojo.subscribe("StackPop", PersonalBarHelperJS, "solveStackPop");
			dojo.subscribe("StackBack",PersonalBarHelperJS, "solveStackBack");
			
			//Subscribe the Event of Click Tab
			dojo.subscribe("WC_Cart_content-selectChild", PersonalBarHelperJS,"clickTab");
			
		},
		
		clickTab:function(args){
		  console.log("ClickTab:"+args);
		   var currentPage = args;
		   if(currentPage.id=="Page3" && !this.isLoadedBrowserHistory){// Load Browser history
		       var recentBrowse = dijit.byId("BrowserHistory");
		       recentBrowse.refresh();
		       this.isLoadedBrowserHistory= true;
		   }
		   if(currentPage.id=="Page2" && !this.isLoadedWishList){// Load Wishlist
		       var recentBrowse = dijit.byId("MiniWishList");
		       recentBrowse.refresh();
		       this.isLoadedWishList= true;
		   }
		},
		
		solveStackPop: function(args){
			var startMenu = dojo.byId("StartMenu");
			var value = startMenu.style.bottom;
			if(value==null||value==""){
				value = "25px";
			}
			var bottom = dojo.number.parse(value.replace("px",""));
			var delta = dojo.number.parse(args.replace("px",""));
			bottom = bottom + delta;
			startMenu.style.bottom = bottom + "px";
			
			dojo.addClass("WC_Cart_button", "WC_Cart_button_opened");
			dojo.removeClass("WC_Cart_button", "WC_Cart_button_closed");
		},
		
		solveStackBack: function(args){
			var startMenu = dojo.byId("StartMenu");
			var value = startMenu.style.bottom;
			if(value==null||value==""){
				value = "145px";
			}
			var bottom = dojo.number.parse(value.replace("px",""));
			var delta = dojo.number.parse(args.replace("px",""));
			bottom = bottom - delta;
			startMenu.style.bottom = bottom + "px";
			
			dojo.addClass("WC_Cart_button", "WC_Cart_button_closed");
			dojo.removeClass("WC_Cart_button", "WC_Cart_button_opened");
		},
		/** 
		 * This variable stores the ID of the language that the store currently uses. Its default value is set to -1, which corresponds to United States English.
		 * @private
		 */
		langId: "-1",
		
		/** 
		 * This variable stores the ID of the current store. Its default value is empty.
		 * @private
		 */
		storeId: "",
		
		/** 
		 * This variable stores the ID of the catalog. Its default value is empty.
		 * @private
		 */
		catalogId: "",
		
		/** 
		 * This variable stores the shipment type, either 1 for single shipment or 2 for multiple shipment. Its default value is set to empty.
		 * @private
		 */
		shipmentTypeId:"",
		
		
		/** 
		 * This array stores the item IDs in an order. It is used to save the item IDs before an address is edited or created during the order check-out.
		 * @private
		 */
		orderItemIds:[],
		
		/** 
		 * This constant stores the amount of time in milliseconds that the <code>updateCartWait</code> function needs to wait before updating the shopping cart.
		 * @private
		 * @constant
		 * @see CheckoutHelperJS.updateCartWait
		 */
		updateWaitTimeOut:1500,
		
		/** 
		 * This array stores the number of key press events after a shopper has modified the quantity of an item in the shopping cart.
		 * @private
		 * @see CheckoutHelperJS.updateCartWait
		 */
		keyPressCount:{},
		
		/** 
		 * This variable stores the true/false value that indicates if the 'AjaxCheckout' feature is enabled.
		 * When it is true, virtually all information on the order check-out pages is updated using services.
		 * It is set to true by default.
		 * 
		 * @private
		 */
		ajaxCheckOut:true,
		
		/**
		 * This array stores the address ID before it is updated by the user during the order check-out.
		 * @private
		 */
		selectedAddressesArray:{},
		
		/**
		 * This variable stores the value that indicates if the payment method input has been changed by the user.
		 *
		 * @private
		 */
		dataDirty:false,
		
		/**
		 * This variable stores a value that indicates if the value of a shipping information related input field or the quantity of an item has been changed by the user.
		 * Its default value is set to false.
		 *
		 * @private
		 */
		fieldDirtyFlag:false,
		
		/** 
		 * This constant stores the String value representing a date that the service understands to reset a requested shipping date.
		 * @private
		 * @constant
		 */
		resetRequestedShipDateValue:"1970-01-01T00:00:00.000Z",
		
		/**
		 * This variable stores the value that indicates if the current page is the Shopping Cart page. It is used to determine what page to redirect to after a service call has been successfully performed.
		 *
		 * @private
		 */
		shoppingCartPage:false,
		
		/**
		 * This variable stores the value that indicates if the requested shipping date has recently been updated. It is used to determine if certain area of the page needs to be refreshed.
		 */
		RequestShippingDateAction:false,
		
		
		/**
		 * This function deletes an order item from the bar.
		 * If forWishlist is true, then the item is added to the wish list subsequently by calling the <code>AjaxDeleteOrderItemFromCart</code> service.
		 *
		 * @param {Integer} orderItemId The ID of the order item to delete.
		 * @param {Boolean} forWishlist If the value is true, then the item is added to the wish list.
		 */
		deleteFromCart:function(orderItemId,forWishlist){ 
			
			var params = [];
			params.storeId = this.storeId;
			params.catalogId = this.catalogId;
			params.langId = this.langId;
			params.orderId = ".";
			params.orderItemId = orderItemId;
			
			if(!submitRequest()){
				return;
			}  
			cursor_wait();

			if(forWishlist)
				 wc.service.invoke("AjaxDeleteOrderItemFromCart", params);
			else
				 wc.service.invoke("AjaxDeleteOrderItem", params);
		},
		
		/**
		 * This function sets the common parameters for the current page, i.e. language ID, store ID and catalog ID.
		 *
		 * @param {Integer} langId The ID of the language that the store currently uses.
		 * @param {Integer} storeId The ID of the current store.
		 * @param {Integer} catalogId The ID of the catalog.
		 */
		setCommonParameters:function(langId,storeId,catalogId){
			this.langId = langId;
			this.storeId = storeId;
			this.catalogId = catalogId;
		},
		
		 /**
			* This function is used to invoke AjaxInterestItemDelete service. It prepares the parameters and then invokes the service.
			* @param {string} cateEntryId  The ID fo the catalog entry.
			*/	
		deleteInterestItem:function(catEntryId) {
				var params = [];
				params.storeId = this.storeId;
				params.catalogId = this.catalogId;
				params.langId = this.langId;
				params.catEntryId = catEntryId;
				
				/* Handles  multiple clicks */
				if(!submitRequest()){
					return;
				}   			
				cursor_wait();		
				wc.service.invoke( "AjaxInterestItemDelete", params);				
		},
		
		/**
		  * This function is used to redirect to the shopping cart. 
		  * @param {string} url The URL of the page.
		  */
		goToCart: function(url){
		   window.location.href=url;
		 }
}