//-----------------------------------------------------------------
// 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.
//-----------------------------------------------------------------

/** 
 * @fileOverview This file is to be included in all full pages that use the CatalogEntryThumbnailDisplay.jspf.
 * It prepares all the code required for the Product Quick Info pop-up reveal.
 */

	//Make the Product Quick Info pop-up window movable.
	dojo.require("dojo.dnd.move");
	dojo.require("dijit.Dialog");
	dojo.require("dojo._base.event");
	dojo.require("dojo.parser");
    dojo.require("dijit.Tooltip");
	
    QuickInfo = {
    	       /**
    	        * This variable stores the ID of the language that the store is currently using.
    	        * @private
    	        */
    	       langId: "-1",
    	       
    	       /**
    	        * This variable stores the ID of the current store.
    	        * @private
    	        */       
    	       storeId: "",
    	       
    	       /**
    	        * This variable stores the ID of the catalog that is used in the store.
    	        * @private
    	        */           
    	       catalogId: "",
    	       
    	       productId: "",
    	       
   	           event: null,
   	           
   	           targetId: "",
   	           
   	           nodeId: "",
   	           
   	           productActionList: null,
   	           
   	           popUpQty: "",
   	           
   	        catEntryDisplayUrl:"",
    	       
    	       /**
    	        * Sets the common ids used in the store - language id, store id, and catalog id.
    	        * 
    	        * @param {string} langId The id of the store language.
    	        * @param {string} storeId The id of the store.
    	        * @param {string} langId The id of the catalog used in the store.
    	        */
    	       setCommonParameters:function(productId,storeId,langId, catalogId, event,targetId,nodeId,productActionList,popUpQty,catEntryDisplayUrl){
    	              this.langId = langId;
    	              this.storeId = storeId;
    	              this.catalogId = catalogId;
    	              this.productId = productId;
    	              this.event = event;
    	              this.targetId = targetId;
    	              this.nodeId = nodeId;
    	              this.productActionList = productActionList;
    	              this.popUpQty = popUpQty;
    	              this.catEntryDisplayUrl=catEntryDisplayUrl;
    	       }
    	}
    
	/** The variable stores the Product Quick Info pop-up window. */
	var m1;
	
	/** The variable stores the identifier of the order item to be replaced */
	var replaceOrderItemId;
	
	/** 
	 * Initializes the Product Quick Info pop-up window to a movable dialog.
	 */	
	var initPopup = function(){
		m1 = new dojo.dnd.Moveable("second_level_category_popup", {handle: "popupHeader"});
	};
	var detailedURL;
	dojo.addOnLoad(initPopup);

	/** 
	 * Displays the Product Quick Info button.
	 * 
	 * @param {string} id The id of the div area to show.
	 */
	 
	function showPopupButton(id){
		
		if(document.getElementById("popupButton_"+id)!=null && document.getElementById("popupButton_"+id)!='undefined'){
			var popupButton = document.getElementById("popupButton_"+id);
			popupButton.style.visibility="visible";
		
		}
	}

	/** 
	 * Displays the Product Quick Info button.
	 * 
	 * @param {string} id The id of the div area to show.
	 */
	 
	function fastFinderShowPopupButton(id,content){
		if(document.getElementById("popupButton_"+id)!=null && document.getElementById("popupButton_"+id)!='undefined'){
			var popupButton = document.getElementById("popupButton_"+id);
			popupButton.style.visibility="visible";
			
		
		}
	}

	/** 
	 * Hides the Product Quick Info button.
	 * 
	 * @param {string} id The id of the div area to hide. 
	 */	
	function hidePopupButton(id){
		if(document.getElementById("popupButton_"+id)!=null && document.getElementById("popupButton_"+id)!='undefined') {
			var popupButton = document.getElementById("popupButton_"+id);
			popupButton.style.visibility="hidden";
		}
	}
	 
	 /** 
		 * Overrides the hidePopupButton function above by also checking to see if the user clicks shift+tab.
		 * 
		 * @param {string} id The id of the div area to hide. 
		 * @param {event} event The keystroke event entered by the user. 
		 */	
	 function shiftTabHidePopupButton(id, e){
		 if ((e.shiftKey) && (dojo.keys.TAB)){
				 
				 hidePopupButton(id);
			 } 
			 
		}
		 
	 

	/** 
	 * Displays the Product Quick Info pop-up containing product information.
	 * Retrieves product information in JSON format via an Ajax call.
	 * 
	 * @param {string} productId The id of the product to display.
	 * @param {string} storeId The id of the current store.
	 * @param {string} langId The id of the language used in the store.
	 * @param {string} catalogId The id of the current store catalog.
	 * @param {string} event The event triggered from user actions.
	 * @param {string} targetId The id of the dojo element to display the pop-up next to.
	 * @param {string} nodeId The id of element to display the pop-up around on <Enter> key click.
	 * @param {object} productActionList The object containing Product Quick Info pop-up action list settings.
	 * @param {string} popUpQty The quantity to be displayed for this product.
	 */		
	 function showPopupPreLoader(type,productId,storeId,langId, catalogId, event,targetId,nodeId,productActionList,popUpQty,catEntryDisplayUrl){
		 resetPopUp();
		 dijit.byId('second_level_category_popup').closeButtonNode.style.display='none';
		 dijit.byId('second_level_category_popup').show();
		 preLoaderDiv= dojo.byId("preLoaderImageQuickInfo");
		 var url=getAbsoluteURL()+"/AjaxQuickInfoPopulateView?";
		 url=url+"catalogId="+catalogId+"&langId="+langId+"&type="+type;
		 url=url+"&productId="+productId+"&storeId="+storeId;
		 QuickInfo.setCommonParameters(productId,storeId,langId, catalogId, null,targetId,nodeId,productActionList,popUpQty,catEntryDisplayUrl);
		 loadQuickInfoURL('QuickInfo_Controller',url);
		 
		 
	 }
	 
	function showPopup(productId,storeId,langId, catalogId, event,targetId,nodeId,productActionList,popUpQty,catEntryDisplayUrl){
		 
		if(event == null || (event != null && event.type!="keypress") || (event != null && event.type=="keypress" && event.keyCode==13)){
			
			resetPopUp();
			detailedURL=catEntryDisplayUrl;
			//Default action list is used if it is not passed into this method
			if(productActionList == null){
				productActionList = new popupActionProperties();
			}
			
			//From the productActionList object properties
			//hide/show the action links from the Quick Info 		
			if(productActionList.showAddToCart){
				if(document.getElementById('addToCartAjaxButton')){
					document.getElementById('addToCartAjaxButton').style.visibility='visible';
				} else if(document.getElementById('addToCartButton')){
					document.getElementById('addToCartButton').style.visibility='visible';
				}			
			} else {
				if(document.getElementById('addToCartAjaxButton')){
					document.getElementById('addToCartAjaxButton').style.visibility='hidden';
				} else if(document.getElementById('addToCartButton')){
					document.getElementById('addToCartButton').style.visibility='hidden';
				}			
			}
	
			if(productActionList.showWishList){
				if(document.getElementById('addToWishListLinkAjax')){
					document.getElementById('addToWishListLinkAjax').style.display='block';
				} else if(document.getElementById('addToWishListLink')){
					document.getElementById('addToWishListLink').style.display='block';
				}
			} else {
				if(document.getElementById('addToWishListLinkAjax')){
					document.getElementById('addToWishListBtn').style.display='none';
				} else if(document.getElementById('addToWishListLink')){
					document.getElementById('addToWishListBtn').style.display='none';
				}
			}
					
/*			if(productActionList.showProductCompare && document.getElementById('compare')){
				if(document.getElementById('addToCompareLink')){
					document.getElementById('addToCompareLink').style.display='block';
				}
			} else {
				if(document.getElementById('addToCompareLink')){
					document.getElementById('addToCompareLink').style.display='block';
				}
			}		
			
	*/	
			
			if(productActionList.showReplaceCartItem){
				if(document.getElementById('replaceCartItemAjax')){
					document.getElementById('replaceCartItemAjax').style.display = 'block';
				} else if(document.getElementById('replaceCartItemNonAjax')){
					document.getElementById('replaceCartItemNonAjax').style.display = 'block';
				}		

				//When showing the quick info pop-up for replace item flow
				//Don't show the requisition list links
				if(document.getElementById('addToNewRequisitionList')){
					document.getElementById('addToNewRequisitionList').style.display = 'none';
				}
				if(document.getElementById('addToExistingRequisitionList')){
					document.getElementById('addToExistingRequisitionList').style.display = 'none';
				}				
			} else {
				if(document.getElementById('replaceCartItemAjax')){
					document.getElementById('replaceCartItemAjax').style.display = 'none';
				} else if(document.getElementById('replaceCartItemNonAjax')){
					document.getElementById('replaceCartItemNonAjax').style.display = 'none';
				}		
			}		
			
			//Do not display the Quantity field if the product is not buyable
			if(!productActionList.showAddToCart && !productActionList.showWishList){
				if(document.getElementById('productPopUpQty')){
					document.getElementById('productPopUpQty').disabled = true;
				}
			} else {
				if(document.getElementById('productPopUpQty')){
					document.getElementById('productPopUpQty').disabled = false;
				}
			}
			//Set the quantity to Quantity field
			if(popUpQty == null)
			{
				document.getElementById('productPopUpQty').value = 1;
			}else {
				document.getElementById('productPopUpQty').value = popUpQty;
			}
		
			
			
			// hides the DialogUnderlayWrapper component, the component that grays out the screen behind,
			// as we do not want the background to be greyed out
			dojo.query('.dijitDialogUnderlayWrapper', document).forEach(function(tag) {		
				tag.style.display='none';
			});		
			
			var parameters = {};
			parameters.storeId = storeId;
			parameters.langId=langId;
			parameters.catalogId=catalogId;
			parameters.productId=productId;	
	
			dojo.xhrPost({
					url: getAbsoluteURL() + "GetCatalogEntryDetailsByID",				
					handleAs: "json-comment-filtered",
					content: parameters,
					service: this,
					load: populatePopUp,
					error: function(errObj,ioArgs) {
						console.debug("CatalogEntryThumbnailDisplay.showPopup: Unexpected error occurred during an xhrPost request.");
					}
				});
		}
	}

	/** 
	 * Populates all the contents of the Product Quick Info pop-up with the JSON returned from the server.
	 * 
	 * @param {object} serviceRepsonse The JSON response from the service.
	 * @param {object} ioArgs The arguments from the service call.
	 */		
	function populatePopUp(serviceResponse, ioArgs) {
		 //populate the entitledItemJsonObject
		 
		categoryDisplayQuickInfoJS.setEntitledItemJsonObject(serviceResponse.productAttributes);
		 
		var catEntryID = serviceResponse.catalogEntry.catalogEntryIdentifier.uniqueID;
		var isProductBean = false;
		if(serviceResponse.catalogEntry.catalogEntryTypeCode=='ProductBean') {
			isProductBean = true;
		}
		var isBundleBean = false;
		if(serviceResponse.catalogEntry.catalogEntryTypeCode=='BundleBean') {
			isBundleBean = true;
		}
		var isItemBean = false;
		if(serviceResponse.catalogEntry.catalogEntryTypeCode=='ItemBean') {
			isItemBean = true;
		}
		
		var isPackageBean = false;
		if(serviceResponse.catalogEntry.catalogEntryTypeCode=='PackageBean') {
			isPackageBean = true;
		}
		document.getElementById('productIdQuickInfo').innerHTML = catEntryID;
		var descAttributesHTML = "";
		for (var i in serviceResponse.catalogEntryAttributes.attributes) {
			if (serviceResponse.catalogEntryAttributes.attributes[i].usage == "Descriptive" ) 
			{				
				descAttributesHTML = descAttributesHTML + serviceResponse.catalogEntryAttributes.attributes[i].name + ' : ';
				if (serviceResponse.catalogEntryAttributes.attributes[i].value != null )
				{
					descAttributesHTML = descAttributesHTML + serviceResponse.catalogEntryAttributes.attributes[i].value.value + '<br />';
				}
				else
				{
					descAttributesHTML = descAttributesHTML + '<br />';
				}			
			}
		}
		document.getElementById('productDescriptiveAttributes').innerHTML = descAttributesHTML;
		
		document.getElementById('productSKUValue').innerHTML = serviceResponse.catalogEntry.catalogEntryIdentifier.externalIdentifier.partNumber;
		//document.getElementById('productMoreInfoLink').href = serviceResponse.catalogEntryURL;
		
		for (var i in serviceResponse.catalogEntryPromotions) {
			document.getElementById('productPromotions').innerHTML = serviceResponse.catalogEntryPromotions[i] + '<br />';
		}
		if(document.getElementById("selectedAttr_"+replaceOrderItemId) !=null)
		var selectedAttributeArray = document.getElementById("selectedAttr_"+replaceOrderItemId).value.toString().split("|");
		//categoryDisplayQuickInfoJS.moreInfoUrl='ProductDisplay?storeId='+storeId+'&catalogId='+catalogId+'&langId='+langId+'&productId='+catEntryID;

		var attributesHTML = "";
		var orderAttributes = []; 
		for (var i in serviceResponse.catalogEntryAttributes.attributes) {
			if (serviceResponse.catalogEntryAttributes.attributes[i].usage == "Defining") {
				if (isProductBean) {
					orderAttributes.push(serviceResponse.catalogEntryAttributes.attributes[i].name.toUpperCase());
				}
			}
		}
		
		for (var i in serviceResponse.catalogEntryAttributes.attributes) {
			if (serviceResponse.catalogEntryAttributes.attributes[i].usage == "Defining") {
				if (isProductBean) {
				
					if(serviceResponse.catalogEntryAttributes.attributes[i].name.toUpperCase()===orderAttributes[0]){
						attributesHTML = attributesHTML + '<label for="qattr_'+ i + '" class="nodisplay">'+serviceResponse.catalogEntryAttributes.attributes[i].name+document.getElementById('requiredFieldText').innerHTML+'</label><span class="required-field">* </span><span>' + serviceResponse.catalogEntryAttributes.attributes[i].name + ' : </span>';
						attributesHTML = attributesHTML + '<select name="qattr_' + i + '" class="drop_down" id="qattr_' + i + '" onChange="JavaScript:categoryDisplayQuickInfoJS.setSelectedAttributeForQuickInfo(\'' + (serviceResponse.catalogEntryAttributes.attributes[i].name).replace("'","\\'") + '\',this.options[this.selectedIndex].value,\''+catEntryID+ '\');categoryDisplayQuickInfoJS.updateDropDownForQuickInfo(\'' +(serviceResponse.catalogEntryAttributes.attributes[i].name).replace("'","\\'")+ '\',\''+catEntryID+ '\');categoryDisplayQuickInfoJS.showData(\''+catEntryID+ '\');updateMoreInfoUrl();">';
												
						
						
					for (var j in serviceResponse.catalogEntryAttributes.attributes[i].allowedValue) {
						
						attributesHTML = attributesHTML + '<option value="' + serviceResponse.catalogEntryAttributes.attributes[i].allowedValue[j].value +'">' + serviceResponse.catalogEntryAttributes.attributes[i].allowedValue[j].value + '</option>';	
					}
						
					}else{
					
						attributesHTML = attributesHTML + '<label for="qattr_'+ serviceResponse.catalogEntryAttributes.attributes[i].name + '" class="nodisplay">'+serviceResponse.catalogEntryAttributes.attributes[i].name+document.getElementById('requiredFieldText').innerHTML+'</label><span class="required-field">* </span><span>' + serviceResponse.catalogEntryAttributes.attributes[i].name + ' : </span>';
						attributesHTML = attributesHTML + '<select name="qattr_' + serviceResponse.catalogEntryAttributes.attributes[i].name + '" class="drop_down" id="qattr_' + serviceResponse.catalogEntryAttributes.attributes[i].name + '" onChange="JavaScript:categoryDisplayQuickInfoJS.setSelectedAttributeForQuickInfo(\'' + (serviceResponse.catalogEntryAttributes.attributes[i].name).replace("'","\\'") + '\',this.options[this.selectedIndex].value,\''+catEntryID+ '\');categoryDisplayQuickInfoJS.updateDropDownForQuickInfo(\'' +(serviceResponse.catalogEntryAttributes.attributes[i].name).replace("'","\\'")+ '\',\''+catEntryID+ '\');categoryDisplayQuickInfoJS.showData(\''+catEntryID+ '\');updateMoreInfoUrl();">';
				}
					
					
					attributesHTML = attributesHTML + '</select><br />';
				} else {
					attributesHTML = attributesHTML + serviceResponse.catalogEntryAttributes.attributes[i].name + ' : ' + serviceResponse.catalogEntryAttributes.attributes[i].value.value + '<br />';
				}
			}
		}
		if (attributesHTML != '') {
			attributesHTML = attributesHTML + '<br />';
		}
		document.getElementById('productAttributes').innerHTML = attributesHTML;
		for (var i in serviceResponse.catalogEntryAttributes.attributes) {
			if (serviceResponse.catalogEntryAttributes.attributes[i].usage == "Defining") {
				if (isProductBean) {
		
					if(serviceResponse.catalogEntryAttributes.attributes[i].name.toUpperCase()===orderAttributes[0]){
						categoryDisplayQuickInfoJS.setSelectedAttributeForQuickInfo((serviceResponse.catalogEntryAttributes.attributes[i].name).replace("'","\\'"),serviceResponse.catalogEntryAttributes.attributes[i].allowedValue[0].value,catEntryID);
						categoryDisplayQuickInfoJS.updateDropDownForQuickInfo((serviceResponse.catalogEntryAttributes.attributes[i].name).replace("'","\\'"),catEntryID);
						categoryDisplayQuickInfoJS.showData(catEntryID);
					}
				}
			}
		
		}
		if(isItemBean){
			document.getElementById('productName').innerHTML = serviceResponse.catalogEntry.description[0].name;
			var baseImageUrl = dojo.byId('baseUrlImageDir').innerHTML;
			if ( serviceResponse.catalogEntry.description[0].fullImage != serviceResponse.catalogEntry.objectPath){
				document.getElementById('productFullImage').src = serviceResponse.catalogEntry.description[0].fullImage;
			}
			else{
				document.getElementById('productFullImage').src = baseImageUrl + "images/trasparent.gif";
			}
			
			if (serviceResponse.catalogEntry.offerPrice) {
				document.getElementById('productPrice').innerHTML = '<span style="font-size: 10px;">'+serviceResponse.catalogEntry.currencyFormat+'</span>&nbsp;&nbsp;'+serviceResponse.catalogEntry.offerPrice;
			}
			document.getElementById('productShortDescription').innerHTML = serviceResponse.catalogEntry.description[0].shortDescription + '<br />';
			document.getElementById('productLongDescription').innerHTML = serviceResponse.catalogEntry.description[0].longDescription + '<br />';
		}
		if(isBundleBean){
			if(document.getElementById('productPopUpQty')){
				document.getElementById('productPopUpQty').disabled = true;
			}
			document.getElementById('productActions').style.display = 'none';
			//document.getElementById('moreinfo').style.display = 'block';
		}
		else{
			document.getElementById('productActions').style.display = 'block';
			//document.getElementById('moreinfo').style.display = 'none';
			
			categoryDisplayQuickInfoJS.setCurrentCatalogEntryId(catEntryID);
			
			//Populate the links for adding items/products/packages to requisition list
			if(isProductBean){
				if(document.getElementById('addToNewRequisitionList')){
					categoryDisplayQuickInfoJS.setCurrentPageType("product");	
					document.getElementById('addToNewRequisitionList').href = "javascript:categoryDisplayQuickInfoJS.addToNewListFromProductDetail('entitledItem_"+catEntryID+"', 'productPopUpQty', document.location.href);";
					document.getElementById('addToExistingRequisitionList').href = "javascript:RequisitionList.showReqListPopupForItem('entitledItem_"+catEntryID+"', 'productPopUpQty');";
				}				
			} else if(isItemBean || isPackageBean){
				if(document.getElementById('addToNewRequisitionList')){
					if(isItemBean){
						categoryDisplayQuickInfoJS.setCurrentPageType("item");
					} else {
						categoryDisplayQuickInfoJS.setCurrentPageType("package");
					}
					document.getElementById('addToNewRequisitionList').href = "javascript:categoryDisplayQuickInfoJS.addItemToNewListFromProductDetail('" + catEntryID + "', 'productPopUpQty', document.location.href);";
					document.getElementById('addToExistingRequisitionList').href = "javascript:RequisitionList.showReqListPopup('productPopUpQty');";
				}		
			}
		}
		
		// Setup addToCart button
		if(isBundleBean){
			// Disable add to cart for Bundles in all cases
			if(document.getElementById('addToCartAjaxButton')){
				document.getElementById('addToCartAjaxButton').style.visibility='hidden';
			} else if(document.getElementById('addToCartButton')){
				document.getElementById('addToCartButton').style.visibility='hidden';
			}
		} else {
			var addtoCart;
			if(document.getElementById('addToCartLinkAjax')){
				if(isProductBean) {
					addtoCart = document.getElementById('addToCartLinkAjax');
					addtoCart.href = "JavaScript:categoryDisplayQuickInfoJS.Add2ShopCartAjax('entitledItem_"+catEntryID+"',document.getElementById('productPopUpQty').value, true);";
				} else {
					addtoCart = document.getElementById('addToCartLinkAjax');
					addtoCart.href = "JavaScript:categoryDisplayQuickInfoJS.AddItem2ShopCartAjax('"+catEntryID+"',document.getElementById('productPopUpQty').value); hidePopup('second_level_category_popup');";
				}
			}
			if(document.getElementById('addToCartLink')){
				if(isProductBean) {
					addtoCart = document.getElementById('addToCartLink');
					addtoCart.href = "#";
					//Must dynamically set the onclick event as follows for IE6
					addtoCart.onclick = new Function("categoryDisplayQuickInfoJS.Add2ShopCart('entitledItem_"+catEntryID+"',document.getElementById('OrderItemAddForm_"+catEntryID+"'),document.getElementById('productPopUpQty').value, true); return false;");
				} else {
					addtoCart = document.getElementById('addToCartLink');
					addtoCart.href = "#";
					//Must dynamically set the onclick event as follows for IE6
					addtoCart.onclick = new Function("categoryDisplayQuickInfoJS.AddItem2ShopCart(document.getElementById('OrderItemAddForm_"+catEntryID+"'),document.getElementById('productPopUpQty').value); hidePopup('second_level_category_popup');return false;");
				}
			}
		}

		// Setup new button
		
			var toProductPage;
			if(document.getElementById('toProductPage')){				
					toProductPage = document.getElementById('toProductPage');
					toProductPage.href = detailedURL;				
			}
			
		

		
		if (document.getElementById('addToWishListLinkAjax')) {
			if(isItemBean || isPackageBean){
			document.getElementById('addToWishListLinkAjax').href = "JavaScript:categoryDisplayQuickInfoJS.AddItem2WishListAjax("+catEntryID+"); hidePopup('second_level_category_popup');";
			}
			else{
			document.getElementById('addToWishListLinkAjax').href = "JavaScript:categoryDisplayQuickInfoJS.Add2WishListAjax('entitledItem_"+catEntryID+"',true);";
			}
		} else if(document.getElementById('addToWishListLink')) {
			document.getElementById('addToWishListLink').href = "#";
			//Must dynamically set the onclick event as follows for IE6
			if(isItemBean || isPackageBean){
			document.getElementById('addToWishListLink').onclick = new Function("categoryDisplayQuickInfoJS.AddItem2WishList(document.getElementById('OrderItemAddForm_"+catEntryID+"'));hidePopup('second_level_category_popup');return false;");
			}
			else{
			document.getElementById('addToWishListLink').onclick = new Function("categoryDisplayQuickInfoJS.Add2WishList('entitledItem_"+catEntryID+"',document.getElementById('OrderItemAddForm_"+catEntryID+"')); hidePopup('second_level_category_popup');return false;");
			}
		}
		if (document.getElementById('addToCompareLink')) {
			document.getElementById('addToCompareLink').href = "JavaScript:categoryDisplayQuickInfoJS.Add2CompareAjax('"+catEntryID+"', '" + serviceResponse.productCompareImagePath +"', '" + serviceResponse.catalogEntryURL+ "','"+serviceResponse.compareImageDescription+"'); delayHidePopup();";
		}
		if (document.getElementById('replaceCartItemAjax')) {
			document.getElementById('replaceCartItemAjax').href = "JavaScript:categoryDisplayQuickInfoJS.ReplaceItemAjax('entitledItem_"+catEntryID+"',document.getElementById('productPopUpQty').value); hidePopup('second_level_category_popup');";
		}
		if (document.getElementById('replaceCartItemNonAjax')) {
			document.getElementById('replaceCartItemNonAjax').href = "#";
			//Must dynamically set the onclick event as follows for IE6			
			document.getElementById('replaceCartItemNonAjax').onclick = new Function("categoryDisplayQuickInfoJS.ReplaceItemNonAjax('entitledItem_"+catEntryID+"',document.getElementById('productPopUpQty').value,document.getElementById('ReplaceItemForm')); hidePopup('second_level_category_popup');return false;");
		}
		dojo.style(preLoaderDiv,"display","none");
		gobackFocus();//set the default focus to the Close button
	}
	
	/**
	 *  This function is used to dynamically update the more info link url based on the selection of attributes in the 
	 *  Quickinfo popup.
	 */

	function updateMoreInfoUrl() {
			//document.getElementById('productMoreInfoLink').href =categoryDisplayQuickInfoJS.moreInfoUrl;
	}
	/** 
	 * Reset all contents of the Product Quick Info pop-up.
	 * This dialog will be re-used across all products on the page.
	 */			
	function resetPopUp() {
		document.getElementById('productName').innerHTML = "";
		document.getElementById('productPrice').innerHTML = "";
		document.getElementById('productShortDescription').innerHTML = "";
		document.getElementById('productLongDescription').innerHTML = "";
		document.getElementById('productDescriptiveAttributes').innerHTML = "";
		document.getElementById('productSKUValue').innerHTML = "";
		//document.getElementById('productMoreInfoLink').href = "";
		document.getElementById('productPromotions').innerHTML = "";
		if(document.getElementById('productPopUpQty')){
			document.getElementById('productPopUpQty').disabled = false;
			document.getElementById('productPopUpQty').value = "1";
		}
		
		document.getElementById('productAttributes').innerHTML = "";
		if (document.getElementById('addToCartLinkAjax')) {
			document.getElementById('addToCartLinkAjax').href = "";
		} else if (document.getElementById('addToCartLink')) {
			document.getElementById('addToCartLink').href = "";
		}
		
		if (document.getElementById('addToWishListLinkAjax')) {
			document.getElementById('addToWishListLinkAjax').href = "";
		} else if (document.getElementById('addToWishListLink')) {
			document.getElementById('addToWishListLink').href = "";
		}
		
		if (document.getElementById('addToCompareLink')) {
			document.getElementById('addToCompareLink').href = "";
		}
		if(document.getElementById('replaceCartItemAjax')){
			document.getElementById('replaceCartItemAjax').href= "";
		} else if(document.getElementById('replaceCartItemNonAjax')){
			document.getElementById('replaceCartItemNonAjax').href = "";
		}
		//set the default product image - NoImageIcon.jpg
		if (dojo.byId('baseUrlImageDir')){
		    var baseImageUrl = dojo.byId('baseUrlImageDir').innerHTML;
		    dojo.byId('productFullImage').src = baseImageUrl + "images/trasparent.gif";
		}		
		categoryDisplayQuickInfoJS.selectedAttributes = new Object();
		categoryDisplayQuickInfoJS.selectedProducts = new Object();
	}
	
	/** 
	 * Hides the Product Quick Info pop-up.
	 * 
	 * @param {string} id The id of the Product Quick Info pop-up to hide.
	 * @param {object} event The event triggered from user actions.
	 */		
	function hidePopup(id,event){
	if(event!=null && event.type=="keypress" && event.keyCode!="27"){
			return;
		}else{		
			var quickInfo = dijit.byId(id);
			if(quickInfo != null){
				quickInfo.hide();
			}
		}
	}

	/** 
	 * Stores the identifier of the order item which is the candidate for replacement.
	 * 
	 * @param {string} changeOrderItemId The id of the order item to be replaced.
	 */	
	function saveChangeOrderItemId(changeOrderItemId){
		replaceOrderItemId = changeOrderItemId;
	}

	/** 
	 * Defines the list of actions that show up in the Product Quick Info pop-up.
	 * Each property corresponds to an action.
	 * Default settings show the first 3 links.
	 */	
	function popupActionProperties() {
		this.showAddToCart = true;
		this.showWishList = true;
		this.showProductCompare = true;
		this.showReplaceCartItem = false;
	}

	/** 
	 * Transfers the focus to the "Close" button,
	 * when pressing the <Tab> key on the last focusable element in the Product Quick Info pop-up.
	 */	
	function gobackFocus() {
		document.getElementById('closeLink').focus();
	}

	/** 
	 * Transfers the focus to the last focusable element present in the Product Quick Info pop-up,
	 * when pressing the <Shift+Tab> keys on the "Close" button.
	 */		
	function setbackFocus(event) {
		if(event.shiftKey && event.keyCode == dojo.keys.TAB)
		{
			if(document.getElementById('replaceCartItemNonAjax') && document.getElementById('replaceCartItemNonAjax').style.display!="none") {
				document.getElementById('replaceCartItemNonAjax').focus();
			} else if(document.getElementById('replaceCartItemAjax') && document.getElementById('replaceCartItemAjax').style.display!="none") {
				document.getElementById('replaceCartItemAjax').focus();
			} else if(document.getElementById('addToCompareLink') && document.getElementById('addToCompareLink').style.display!="none") {
				document.getElementById('addToCompareLink').focus();
			} else if(document.getElementById('addToWishListLink') && document.getElementById('addToWishListLink').style.display!="none") {
				document.getElementById('addToWishListLink').focus();
			} else if(document.getElementById('addToWishListLinkAjax') && document.getElementById('addToWishListLinkAjax').style.display!="none") {
				document.getElementById('addToWishListLinkAjax').focus();
			} else {
				//document.getElementById('productMoreInfoLink').focus();
			}
			
			dojo.stopEvent(event);
		}
	}
	
	/** 
	 * Triggers a call to the hidePopup() function after a delay of a certain amount of time.
	 */			
	function delayHidePopup() {
		setTimeout(dojo.hitch(this,"hidePopup",'second_level_category_popup'),200);
	}

	 
	 /**
	 Added for Blouse Style Quick Info
	 **/
	 
	 function showAssociationPopup(productId,storeId,langId, catalogId, event,targetId,nodeId,productActionList,popUpQty){
		 
			if(event == null || (event != null && event.type!="keypress") || (event != null && event.type=="keypress" && event.keyCode==13)){
				
				resetPopUp();
				
				//Default action list is used if it is not passed into this method
				if(productActionList == null){
					productActionList = new popupActionProperties();
				}
				
				//hide/show the action links from the Quick Info 		
				
					if(document.getElementById('sp_addToCartAjaxButton')){
						document.getElementById('sp_addToCartAjaxButton').style.visibility='hidden';
					} else if(document.getElementById('sp_addToCartButton')){
						document.getElementById('sp_addToCartButton').style.visibility='hidden';
					}			
				
				
					if(document.getElementById('sp_addToWishListLinkAjax')){
						document.getElementById('sp_addToWishListLinkAjax').style.display='none';
					} else if(document.getElementById('sp_addToWishListLink')){
						document.getElementById('sp_addToWishListLink').style.display='none';
					}
				
					if(document.getElementById('sp_addToCompareLink')){
						document.getElementById('sp_addToCompareLink').style.display='none';
					}
				    if(document.getElementById('sp_quick_info_blousestyle')){
						document.getElementById('sp_quick_info_blousestyle').style.display='block';
					}
					
					if(document.getElementById('sp_quick_info_newMessage')){
						document.getElementById('sp_quick_info_newMessage').style.display='block';
					}		
					if(document.getElementById('sp_MessageDiv')){
						document.getElementById('sp_MessageDiv').style.display = 'none';
					}
				
				

					//When showing the quick info pop-up for replace item flow
					//Don't show the requisition list links
					if(document.getElementById('sp_addToNewRequisitionList')){
						document.getElementById('sp_addToNewRequisitionList').style.display = 'none';
					}
					if(document.getElementById('sp_addToExistingRequisitionList')){
						document.getElementById('sp_addToExistingRequisitionList').style.display = 'none';
					}				
				
					if(document.getElementById('sp_replaceCartItemAjax')){
						document.getElementById('sp_replaceCartItemAjax').style.display = 'none';
					} else if(document.getElementById('sp_replaceCartItemNonAjax')){
						document.getElementById('sp_replaceCartItemNonAjax').style.display = 'none';
					}		
						
				
				//Do not display the Quantity field if the product is not buyable
				
				
					document.getElementById('sp_productQuantity').style.display = 'none';
					document.getElementById('sp_productPopUpQty').disabled = true;
					//document.getElementById('productMoreInfoLink').style.display = 'none';
				
				
				//Set the quantity to Quantity field
				if(popUpQty == null)
				{
					document.getElementById('sp_productPopUpQty').value = 1;
				}
			
				 dijit.byId('sp_second_level_category_popup').closeButtonNode.style.display='none';
				 dijit.byId('sp_second_level_category_popup').show();
				
				// hides the DialogUnderlayWrapper component, the component that grays out the screen behind,
				// as we do not want the background to be greyed out
				dojo.query('.dijitDialogUnderlayWrapper', document).forEach(function(tag) {		
					tag.style.display='none';
				});		
				
				var parameters = {};
				parameters.storeId = storeId;
				parameters.langId=langId;
				parameters.catalogId=catalogId;
				parameters.productId=productId;	
		
				dojo.xhrPost({
						url: getAbsoluteURL() + "GetCatalogEntryDetailsByID",				
						handleAs: "json-comment-filtered",
						content: parameters,
						service: this,
						load: populateAssociationPopUp,
						error: function(errObj,ioArgs) {
							console.debug("CatalogEntryThumbnailDisplay.showPopup: Unexpected error occurred during an xhrPost request.");
						}
					});
			}
		}

		/** 
		 * Populates all the contents of the Product Quick Info pop-up with the JSON returned from the server.
		 * 
		 * @param {object} serviceRepsonse The JSON response from the service.
		 * @param {object} ioArgs The arguments from the service call.
		 */		
		function populateAssociationPopUp(serviceResponse, ioArgs) {
			 //populate the entitledItemJsonObject
categoryDisplayQuickInfoJS.setEntitledItemJsonObject(serviceResponse.productAttributes);
			
			var catEntryID = serviceResponse.catalogEntry.catalogEntryIdentifier.uniqueID;
			var isProductBean = false;
			if(serviceResponse.catalogEntry.catalogEntryTypeCode=='ProductBean') {
				isProductBean = true;
			}
			var isBundleBean = false;
			if(serviceResponse.catalogEntry.catalogEntryTypeCode=='BundleBean') {
				isBundleBean = true;
			}
			var isItemBean = false;
			if(serviceResponse.catalogEntry.catalogEntryTypeCode=='ItemBean') {
				isItemBean = true;
			}
			
			var isPackageBean = false;
			if(serviceResponse.catalogEntry.catalogEntryTypeCode=='PackageBean') {
				isPackageBean = true;
			}
			document.getElementById('sp_productIdQuickInfo').innerHTML = catEntryID;
			document.getElementById('sp_productName').innerHTML = serviceResponse.catalogEntry.description[0].name;
			var baseImageUrl = dojo.byId('sp_baseUrlImageDir').innerHTML;
			if ( serviceResponse.catalogEntry.description[0].fullImage != serviceResponse.catalogEntry.objectPath){
				document.getElementById('sp_productFullImage').src = serviceResponse.catalogEntry.description[0].fullImage;
			}
			else{
				document.getElementById('sp_productFullImage').src = baseImageUrl + "images/trasparent.gif";
			}
			
			if (serviceResponse.catalogEntry.offerPrice) {
				
			var myString=serviceResponse.catalogEntry.offerPrice;
			var mySplitResult = myString.split(" ");
			if(mySplitResult.length==2){			
				document.getElementById('sp_productPrice').innerHTML = '<span style="font-size:10px;">'+mySplitResult[0]+'</span>'+'&nbsp;&nbsp;'+mySplitResult[1];
			}else{
				document.getElementById('sp_productPrice').innerHTML = '<span style="font-size:10px;">'+mySplitResult[0]+'</span>';
			}
		
			}
			document.getElementById('sp_productShortDescription').innerHTML = serviceResponse.catalogEntry.description[0].shortDescription + '<br />';
			document.getElementById('sp_productLongDescription').innerHTML = serviceResponse.catalogEntry.description[0].longDescription + '<br />';
			
			var descAttributesHTML = "";
			for (var i in serviceResponse.catalogEntryAttributes.attributes) {
				if (serviceResponse.catalogEntryAttributes.attributes[i].usage == "Descriptive" ) 
				{				
					descAttributesHTML = descAttributesHTML + serviceResponse.catalogEntryAttributes.attributes[i].name + ' : ';
					if (serviceResponse.catalogEntryAttributes.attributes[i].value != null )
					{
						descAttributesHTML = descAttributesHTML + serviceResponse.catalogEntryAttributes.attributes[i].value.value + '<br />';
					}
					else
					{
						descAttributesHTML = descAttributesHTML + '<br />';
					}			
				}
			}
			document.getElementById('sp_productDescriptiveAttributes').innerHTML = descAttributesHTML;
			
			document.getElementById('sp_productSKUValue').innerHTML = serviceResponse.catalogEntry.catalogEntryIdentifier.externalIdentifier.partNumber;
			//document.getElementById('productMoreInfoLink').href = serviceResponse.catalogEntryURL;
			
			for (var i in serviceResponse.catalogEntryPromotions) {
				document.getElementById('sp_productPromotions').innerHTML = serviceResponse.catalogEntryPromotions[i] + '<br />';
			}
			if(document.getElementById("sp_selectedAttr_"+replaceOrderItemId) !=null)
			var selectedAttributeArray = document.getElementById("sp_selectedAttr_"+replaceOrderItemId).value.toString().split("|");
			//categoryDisplayQuickInfoJS.moreInfoUrl='ProductDisplay?storeId='+storeId+'&catalogId='+catalogId+'&langId='+langId+'&productId='+catEntryID;

			var attributesHTML = "";
			for (var i in serviceResponse.catalogEntryAttributes.attributes) {
				if (serviceResponse.catalogEntryAttributes.attributes[i].usage == "Defining") {
					if (isProductBean) {
						attributesHTML = attributesHTML + '<label for="attrValue_'+ i + '" class="nodisplay">'+serviceResponse.catalogEntryAttributes.attributes[i].name+document.getElementById('sp_requiredFieldText').innerHTML+'</label><span class="required-field">* </span><span>' + serviceResponse.catalogEntryAttributes.attributes[i].name + ' : </span>';
						attributesHTML = attributesHTML + '<select name="attrValue" class="drop_down" id="attrValue_' + i + '" onChange="JavaScript:categoryDisplayQuickInfoJS.setSelectedAttribute(\'' + (serviceResponse.catalogEntryAttributes.attributes[i].name).replace("'","\\'") + '\',this.options[this.selectedIndex].value);updateMoreInfoUrl();">';
						attributesHTML = attributesHTML + '<option value="">'+document.getElementById("selectText").innerHTML+'</option>';
						for (var j in serviceResponse.catalogEntryAttributes.attributes[i].allowedValue) {
							if(document.getElementById("sp_selectedAttr_"+replaceOrderItemId) !=null && serviceResponse.catalogEntryAttributes.attributes[i].allowedValue[j].value == selectedAttributeArray[i]){
								var selected = 'selected';
								categoryDisplayQuickInfoJS.setSelectedAttribute(serviceResponse.catalogEntryAttributes.attributes[i].name,selectedAttributeArray[i]);
								updateMoreInfoUrl();
								attributesHTML = attributesHTML + '<option value="' + serviceResponse.catalogEntryAttributes.attributes[i].allowedValue[j].value +'"selected='+selected+'>' + serviceResponse.catalogEntryAttributes.attributes[i].allowedValue[j].value + '</option>';
							}
							else{
							attributesHTML = attributesHTML + '<option value="' + serviceResponse.catalogEntryAttributes.attributes[i].allowedValue[j].value +'">' + serviceResponse.catalogEntryAttributes.attributes[i].allowedValue[j].value + '</option>';	
						}
					}
						attributesHTML = attributesHTML + '</select><br />';
					} else {
						attributesHTML = attributesHTML + serviceResponse.catalogEntryAttributes.attributes[i].name + ' : ' + serviceResponse.catalogEntryAttributes.attributes[i].value.value + '<br />';
					}
				}
			}
			if (attributesHTML != '') {
				attributesHTML = attributesHTML + '<br />';
			}
			document.getElementById('sp_productAttributes').innerHTML = attributesHTML;
			
			
			if(isBundleBean){
				if(document.getElementById('sp_productPopUpQty')){
					document.getElementById('sp_productPopUpQty').disabled = true;
				}
				document.getElementById('sp_productActions').style.display = 'none';
				//document.getElementById('moreinfo').style.display = 'block';
			}
			else{
				document.getElementById('sp_productActions').style.display = 'block';
				//document.getElementById('moreinfo').style.display = 'none';
				
				categoryDisplayQuickInfoJS.setCurrentCatalogEntryId(catEntryID);
				
				//Populate the links for adding items/products/packages to requisition list
				if(isProductBean){
					if(document.getElementById('sp_addToNewRequisitionList')){
						categoryDisplayQuickInfoJS.setCurrentPageType("product");	
						document.getElementById('sp_addToNewRequisitionList').href = "javascript:categoryDisplayQuickInfoJS.addToNewListFromProductDetail('entitledItem_"+catEntryID+"', 'sp_productPopUpQty', document.location.href);";
						document.getElementById('sp_addToExistingRequisitionList').href = "javascript:RequisitionList.showReqListPopupForItem('entitledItem_"+catEntryID+"', 'sp_productPopUpQty');";
					}				
				} else if(isItemBean || isPackageBean){
					if(document.getElementById('sp_addToNewRequisitionList')){
						if(isItemBean){
							categoryDisplayQuickInfoJS.setCurrentPageType("item");
						} else {
							categoryDisplayQuickInfoJS.setCurrentPageType("package");
						}
						document.getElementById('sp_addToNewRequisitionList').href = "javascript:categoryDisplayQuickInfoJS.addItemToNewListFromProductDetail('" + catEntryID + "', 'sp_productPopUpQty', document.location.href);";
						document.getElementById('sp_addToExistingRequisitionList').href = "javascript:RequisitionList.showReqListPopup('sp_productPopUpQty');";
					}		
				}
			}
			
			// Setup addToCart button
			if(isBundleBean){
				// Disable add to cart for Bundles in all cases
				if(document.getElementById('sp_addToCartAjaxButton')){
					document.getElementById('sp_addToCartAjaxButton').style.visibility='hidden';
				} else if(document.getElementById('sp_addToCartButton')){
					document.getElementById('sp_addToCartButton').style.visibility='hidden';
				}
			} else {
				var addtoCart;
				if(document.getElementById('sp_addToCartLinkAjax')){
					if(isProductBean) {
						addtoCart = document.getElementById('sp_addToCartLinkAjax');
						addtoCart.href = "JavaScript:categoryDisplayQuickInfoJS.Add2ShopCartAjax('entitledItem_"+catEntryID+"',document.getElementById('sp_productPopUpQty').value, true);";
					} else {
						addtoCart = document.getElementById('sp_addToCartLinkAjax');
						addtoCart.href = "JavaScript:categoryDisplayQuickInfoJS.AddItem2ShopCartAjax('"+catEntryID+"',document.getElementById('sp_productPopUpQty').value); hidePopup('sp_second_level_category_popup');";
					}
				}
				if(document.getElementById('sp_addToCartLink')){
					if(isProductBean) {
						addtoCart = document.getElementById('sp_addToCartLink');
						addtoCart.href = "#";
						//Must dynamically set the onclick event as follows for IE6
						addtoCart.onclick = new Function("categoryDisplayQuickInfoJS.Add2ShopCart('entitledItem_"+catEntryID+"',document.getElementById('sp_OrderItemAddForm_"+catEntryID+"'),document.getElementById('sp_productPopUpQty').value, true); return false;");
					} else {
						addtoCart = document.getElementById('addToCartLink');
						addtoCart.href = "#";
						//Must dynamically set the onclick event as follows for IE6
						addtoCart.onclick = new Function("categoryDisplayQuickInfoJS.AddItem2ShopCart(document.getElementById('sp_OrderItemAddForm_"+catEntryID+"'),document.getElementById('sp_productPopUpQty').value); hidePopup('sp_second_level_category_popup');return false;");
					}
				}
			}

			
			if (document.getElementById('sp_addToWishListLinkAjax')) {
				if(isItemBean || isPackageBean){
				document.getElementById('sp_addToWishListLinkAjax').href = "JavaScript:categoryDisplayQuickInfoJS.AddItem2WishListAjax("+catEntryID+"); hidePopup('sp_second_level_category_popup');";
				}
				else{
				document.getElementById('sp_addToWishListLinkAjax').href = "JavaScript:categoryDisplayQuickInfoJS.Add2WishListAjax('entitledItem_"+catEntryID+"'); hidePopup('sp_second_level_category_popup');";
				}
			} else if(document.getElementById('sp_addToWishListLink')) {
				document.getElementById('sp_addToWishListLink').href = "#";
				//Must dynamically set the onclick event as follows for IE6
				if(isItemBean || isPackageBean){
				document.getElementById('sp_addToWishListLink').onclick = new Function("categoryDisplayQuickInfoJS.AddItem2WishList(document.getElementById('sp_OrderItemAddForm_"+catEntryID+"'));hidePopup('sp_second_level_category_popup');return false;");
				}
				else{
				document.getElementById('sp_addToWishListLink').onclick = new Function("categoryDisplayQuickInfoJS.Add2WishList('entitledItem_"+catEntryID+"',document.getElementById('sp_OrderItemAddForm_"+catEntryID+"')); hidePopup('sp_second_level_category_popup');return false;");
				}
			}
			if (document.getElementById('sp_addToCompareLink')) {
				document.getElementById('sp_addToCompareLink').href = "JavaScript:categoryDisplayQuickInfoJS.Add2CompareAjax('"+catEntryID+"', '" + serviceResponse.productCompareImagePath +"', '" + serviceResponse.catalogEntryURL+ "','"+serviceResponse.compareImageDescription+"'); delayHidePopup();";
			}
			if (document.getElementById('sp_replaceCartItemAjax')) {
				document.getElementById('sp_replaceCartItemAjax').href = "JavaScript:categoryDisplayQuickInfoJS.ReplaceItemAjax('entitledItem_"+catEntryID+"',document.getElementById('sp_productPopUpQty').value); hidePopup('sp_second_level_category_popup');";
			}
			if (document.getElementById('sp_replaceCartItemNonAjax')) {
				document.getElementById('sp_replaceCartItemNonAjax').href = "#";
				//Must dynamically set the onclick event as follows for IE6			
				document.getElementById('sp_replaceCartItemNonAjax').onclick = new Function("categoryDisplayQuickInfoJS.ReplaceItemNonAjax('entitledItem_"+catEntryID+"',document.getElementById('sp_productPopUpQty').value,document.getElementById('sp_ReplaceItemForm')); hidePopup('sp_second_level_category_popup');return false;");
			}
			if(document.getElementById('sp_productActions')){
				
				document.getElementById('sp_productActions').style.display='none';
			}
			gobackFocus();//set the default focus to the Close button
		}
		 
		 function loadQuickInfoURL(controllerId,changeUrl){
			wc.render.getRefreshControllerById("QuickInfo_Controller").url = changeUrl;				
			wc.render.updateContext("QuickInfo_Context");

		}
