﻿//-----------------------------------------------------------------
// 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");
	
	/** 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"});
	};
	
	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";
		}
	}

	/** 
	 * 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 showPopup(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();
			}
			
			//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';
				}			
			}
			var viewWishCompare = false;
			if(productActionList.showWishList){
				if(document.getElementById('addToWishListLinkAjax')){
					document.getElementById('addToWishListLinkAjax').style.display='block';
					viewWishCompare=true;
				} else if(document.getElementById('addToWishListLink')){
					document.getElementById('addToWishListLink').style.display='block';
					viewWishCompare=true;
				}
			} else {
				if(document.getElementById('addToWishListLinkAjax')){
					document.getElementById('addToWishListLinkAjax').style.display='none';
				} else if(document.getElementById('addToWishListLink')){
					document.getElementById('addToWishListLink').style.display='none';
				}
			}
					
			if(productActionList.showProductCompare && document.getElementById('compare')){
				if(document.getElementById('addToCompareLink')){
					document.getElementById('addToCompareLink').style.display='block';
					viewWishCompare=true;
				}
			} else {
				if(document.getElementById('addToCompareLink')){
					document.getElementById('addToCompareLink').style.display='none';
				}
			}		
			if( viewWishCompare ) {
				document.getElementById('wishcompareholder').style.display='block';
				document.getElementById('wishcompare').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;
			}
		
			dijit.byId('second_level_category_popup').closeButtonNode.style.display='none';
			dijit.byId('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;	
	
			console.debug("Paramerters >> storeId: "+storeId+" >> langId: "+langId+" >> catalogId:" +catalogId + " >> productId: " + productId + " >> url: " + getAbsoluteURL() + "GetCatalogEntryDetailsByID");
			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
		 
		// PLN@Fiwe - remove ,00 from prices
		 var decimalsToRemove = ',00';
		 
		 categoryDisplayJS.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;
		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/NoImageIcon.jpg";
		}
		// Fiwe add Brand img to quickinfo
		
		if ( serviceResponse.catalogEntry.manufacturerName != ""){
			var strManufacturername;
			strManufacturername="";
			strManufacturername = "" + serviceResponse.catalogEntry.manufacturerName;
			 
			strManufacturername = replaceChar(strManufacturername, "\u00C4", "ae");
			strManufacturername = replaceChar(strManufacturername, "\u00C5", "aa");
			strManufacturername = replaceChar(strManufacturername, "\u00D6", "oe");
			
			strManufacturername = replaceChar(strManufacturername, "\u00E4", "ae"); //ä
			strManufacturername = replaceChar(strManufacturername, "\u00E5", "aa"); //å
			strManufacturername = replaceChar(strManufacturername, "\u00F6", "oe"); //ö
			

			strManufacturername = replaceChar(strManufacturername, " ", "_");
			strManufacturername= strManufacturername.toLowerCase();  			
			strManufacturername+=".jpg";
			
			document.getElementById('brandImg_popup').src = serviceResponse.catalogEntry.objectPath + "images/brands/"+strManufacturername;
			document.getElementById('brandImg_popup').style.display = 'block';
			document.getElementById('brandImg_popup').style.visibility="visible";
		}else{
			//document.getElementById('brandImageDiv').style.display = 'none';
			//document.getElementById('brandImg_popup').style.display = 'none';
			document.getElementById('brandImg_popup').style.display = 'block';
			document.getElementById('brandImg_popup').visibility="hidden";
		}
		
		if (serviceResponse.catalogEntry.offerPrice) {
			if (serviceResponse.catalogEntry.compOfferPrice && serviceResponse.catalogEntry.compUnit) {
				$('#productPrice').html(serviceResponse.catalogEntry.compOfferPrice.replace(decimalsToRemove,'') );				
				document.getElementById('productUnit').innerHTML = ' per ' + serviceResponse.catalogEntry.compUnit;
				
				// Set Offerprice to calcProductPrice and unit to calcProductUnit
				$('#calcProductPrice').html(serviceResponse.catalogEntry.offerPrice.replace(decimalsToRemove,'') );				
				if (serviceResponse.catalogEntry.unit) {
					document.getElementById('calcProductUnit').innerHTML = ' per ' + serviceResponse.catalogEntry.unit;
				}
				document.getElementById('unitcalculation').style.display = 'block';

			} else {
				// PLN@Fiwe - remove ,00 from prices
				$('#productPrice').html(serviceResponse.catalogEntry.offerPrice.replace(decimalsToRemove,'') );				
				if (serviceResponse.catalogEntry.unit) {
					document.getElementById('productUnit').innerHTML = ' per ' + serviceResponse.catalogEntry.unit;
				}
				document.getElementById('unitcalculation').style.display = 'none';
			}
		}
		if (serviceResponse.catalogEntry.priceClass) {
			// PLN@Fiwe - remove ,00 from prices
			$('#productPrice').removeClass().addClass(serviceResponse.catalogEntry.priceClass);
		}
		
		if (serviceResponse.catalogEntry.listPrice) {
			if (serviceResponse.catalogEntry.compListPrice && serviceResponse.catalogEntry.compUnit) {
				$('#productListPrice').html(serviceResponse.catalogEntry.compListPrice.replace(decimalsToRemove,''));				
				document.getElementById('productUnitListPrice').innerHTML = serviceResponse.catalogEntry.compUnit + ' per ' + serviceResponse.catalogEntry.unit;
			} else {
				$('#productListPrice').html(serviceResponse.catalogEntry.listPrice.replace(decimalsToRemove,''));				
				if (serviceResponse.catalogEntry.unit) {
					document.getElementById('productUnitListPrice').innerHTML = serviceResponse.catalogEntry.unit + ' per ' + serviceResponse.catalogEntry.unit;
				}
			}
		}else{
			$('#productListPrice').html('');				
		}
		
		$('#productPopM2factor').val(serviceResponse.catalogEntry.field3);
		
		$('#unitcalculation_field3').html(serviceResponse.catalogEntry.field3 + serviceResponse.catalogEntry.compUnit + ' per ' + serviceResponse.catalogEntry.unit );
		
		if (serviceResponse.catalogEntry.description[0].longDescription.length > 250)
			document.getElementById('productLongDescription').innerHTML = serviceResponse.catalogEntry.description[0].longDescription.substring(0,250) + '...' + '<br />';
		else
			document.getElementById('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('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("|");
		//categoryDisplayJS.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 + '<div class="defAttr"><label for="attrValue_'+ serviceResponse.catalogEntryAttributes.attributes[i].attributeIdentifier.uniqueID + '" class="nodisplay">'+serviceResponse.catalogEntryAttributes.attributes[i].name+document.getElementById('requiredFieldText').innerHTML+'</label><span class="required-field">* </span><span>' + serviceResponse.catalogEntryAttributes.attributes[i].name + ' : </span><span id="spanAttr_'+serviceResponse.catalogEntryAttributes.attributes[i].attributeIdentifier.uniqueID+'" class="attrChangedMessage" style="display:none">(ÄNDRAD)</span>';
					attributesHTML = attributesHTML + '<div class="defAttrDropDown"><select name="attrValue" class="drop_down" id="attrValue_' + serviceResponse.catalogEntryAttributes.attributes[i].attributeIdentifier.uniqueID + '" '
						+'onchange="JavaScript:categoryDisplayJS.setSelectedAttribute(\'' + (serviceResponse.catalogEntryAttributes.attributes[i].name).replace("'","\\'") + '\',this.options[this.selectedIndex].value);updateEntryDetails(storeId, langId, catalogId);" '
						+'onkeyup="JavaScript:categoryDisplayJS.setSelectedAttribute(\'' + (serviceResponse.catalogEntryAttributes.attributes[i].name).replace("'","\\'") + '\',this.options[this.selectedIndex].value);updateEntryDetails(storeId, langId, catalogId);">';
//					attributesHTML = attributesHTML + '<option value="">'+document.getElementById("selectText").innerHTML+'</option>';
					//dumpProps(serviceResponse.catalogEntryAttributes.attributes[i].allowedValue);
					for (var j in serviceResponse.catalogEntryAttributes.attributes[i].allowedValue) {
						if(document.getElementById("selectedAttr_"+replaceOrderItemId) !=null && serviceResponse.catalogEntryAttributes.attributes[i].allowedValue[j].value == selectedAttributeArray[i]){
							var selected = 'selected';
							categoryDisplayJS.setSelectedAttribute(serviceResponse.catalogEntryAttributes.attributes[i].name,selectedAttributeArray[i]);
							updateEntryDetails(storeId, langId, catalogId);
							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></div></div>';
				} else {
					attributesHTML = attributesHTML + serviceResponse.catalogEntryAttributes.attributes[i].name + ' : ' + serviceResponse.catalogEntryAttributes.attributes[i].value.value + '<br />';
				}
			}
		}
		document.getElementById('productAttributes').innerHTML = attributesHTML;


		if (isProductBean) {
			for (var i in serviceResponse.catalogEntryAttributes.attributes) {
				if (serviceResponse.catalogEntryAttributes.attributes[i].usage == "Defining") {
					categoryDisplayJS.setSelectedAttribute((serviceResponse.catalogEntryAttributes.attributes[i].name).replace("'","\\'"), document.getElementById('attrValue_' + serviceResponse.catalogEntryAttributes.attributes[i].attributeIdentifier.uniqueID).value);
				}
			}			
			updateEntryDetails(storeId, langId, catalogId);
		}
		
		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';
			
			categoryDisplayJS.setCurrentCatalogEntryId(catEntryID);
		
			
			//Populate the links for adding items/products/packages to requisition list
			if(isProductBean){
				if(document.getElementById('addToNewRequisitionList')){
					categoryDisplayJS.setCurrentPageType("product");	
					document.getElementById('addToNewRequisitionList').href = "javascript:categoryDisplayJS.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){
						categoryDisplayJS.setCurrentPageType("item");
					} else {
						categoryDisplayJS.setCurrentPageType("package");
					}
					document.getElementById('addToNewRequisitionList').href = "javascript:categoryDisplayJS.addItemToNewListFromProductDetail('" + catEntryID + "', 'productPopUpQty', document.location.href);";
					document.getElementById('addToExistingRequisitionList').href = "javascript:RequisitionList.showReqListPopup('productPopUpQty');";
				}		
			}
		}
		
		if (serviceResponse.catalogEntry.field5 == 'm2') {
			document.getElementById('purchase_buttonrow').style.visibility = 'visible';
			document.getElementById('unit').style.display = 'block';
			document.getElementById('fpk').style.display = 'block';
			document.getElementById('fpk').value = '(1 fpk)';
			document.getElementById('productPopUpM2').style.display = 'block';
			document.getElementById('productPopUpQty').style.display = 'none';
		}
		
		
		// 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 if(document.getElementById('addToCartLinkAjax')){
				document.getElementById('addToCartLinkAjax').style.visibility='hidden';
				  if (document.getElementById('quantityField'))
					  document.getElementById('quantityField').style.visibility='hidden';
			}
		} else {
			var addtoCart;
			if(document.getElementById('addToCartLinkAjax')){
				if(isProductBean) {
					addtoCart = document.getElementById('addToCartLinkAjax');
					addtoCart.href = "JavaScript:categoryDisplayJS.Add2ShopCartAjax('entitledItem_"+catEntryID+"',document.getElementById('productPopUpQty').value, true);";
				} else {
					addtoCart = document.getElementById('addToCartLinkAjax');
					addtoCart.href = "JavaScript:categoryDisplayJS.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("categoryDisplayJS.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("categoryDisplayJS.AddItem2ShopCart(document.getElementById('OrderItemAddForm_"+catEntryID+"'),document.getElementById('productPopUpQty').value); hidePopup('second_level_category_popup');return false;");
				}
			}
		}

		
		if (document.getElementById('addToWishListLinkAjax')) {
			if(isItemBean || isPackageBean){
			document.getElementById('addToWishListLinkAjax').href = "JavaScript:categoryDisplayJS.AddItem2WishListAjax("+catEntryID+"); hidePopup('second_level_category_popup');";
			}
			else{
			document.getElementById('addToWishListLinkAjax').href = "JavaScript:categoryDisplayJS.Add2WishListAjax('entitledItem_"+catEntryID+"'); hidePopup('second_level_category_popup');";
			}
		} 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("categoryDisplayJS.AddItem2WishList(document.getElementById('OrderItemAddForm_"+catEntryID+"'));hidePopup('second_level_category_popup');return false;");
			}
			else{
			document.getElementById('addToWishListLink').onclick = new Function("categoryDisplayJS.Add2WishList('entitledItem_"+catEntryID+"',document.getElementById('OrderItemAddForm_"+catEntryID+"')); hidePopup('second_level_category_popup');return false;");
			}
		}
		if (document.getElementById('addToCompareLink')) {
			document.getElementById('addToCompareLink').href = "JavaScript:categoryDisplayJS.Add2CompareAjax('"+catEntryID+"', '" + serviceResponse.productCompareImagePath +"', '" + serviceResponse.catalogEntryURL+ "','"+serviceResponse.compareImageDescription+"'); delayHidePopup();";
		}
		if (document.getElementById('replaceCartItemAjax')) {
			document.getElementById('replaceCartItemAjax').href = "JavaScript:categoryDisplayJS.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("categoryDisplayJS.ReplaceItemNonAjax('entitledItem_"+catEntryID+"',document.getElementById('productPopUpQty').value,document.getElementById('ReplaceItemForm')); hidePopup('second_level_category_popup');return false;");
		}		
		
		if(serviceResponse.catalogEntry.buyable == '1'){
			document.getElementById('purchase_buttonrow').style.visibility = 'visible';
		}else{
			document.getElementById('purchase_buttonrow').style.visibility = 'hidden';				
		}
		
		gobackFocus();//set the default focus to the Close button
	}
	
	/**
	 * Debugfunction, display an objects properties in a confirm box
	 * @param obj
	 * @param parent
	 * @return nothing
	 */
	function dumpProps(obj, parent) {
	   // Go through all the properties of the passed-in object
	   if(obj==null||obj=="") alert("object is NULL");
	   for (var i in obj) {
	      // if a parent (2nd parameter) was passed in, then use that to
	      // build the message. Message includes i (the object's property name)
	      // then the object's property value on a new line
	      if (parent) { var msg = parent + "." + i + "\n" + obj[i]; } else { var msg = i + "\n" + obj[i]; }
	      // Display the message. If the user clicks "OK", then continue. If they
	      // click "CANCEL" then quit this level of recursion
	      if (!confirm(msg)) { return; }
	      // If this property (i) is an object, then recursively process the object
	      if (typeof obj[i] == "object") {
	         if (parent) { dumpProps(obj[i], parent + "." + i); } else { dumpProps(obj[i], i); }
	      }
	   }
	}
	
	/**
	 *  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 = categoryDisplayJS.moreInfoUrl;
	}

		/**
		 * This function adds functionallity to get more info and replace current info in quickinfo
		 * popUp.
		 * 
		 * @param productId
		 * @param storeId
		 * @param langId
		 * @param catalogId
		 * @return
		 */
		function updateEntryDetails(storeId,langId, catalogId){
			var entitledItemJSON = categoryDisplayJS.getEntitledItemJsonObject(); 
			categoryDisplayJS.setEntitledItems(entitledItemJSON);
			 
			var parameters = {};
			parameters.storeId = storeId;
			parameters.langId=langId;
			parameters.catalogId=catalogId;
			parameters.productId=categoryDisplayJS.getCatalogEntryId();	
			console.debug("Paramerters >> CatalogId: "+catalogId+" >> ProductId: "+parameters.productId);
			dojo.xhrPost({
					url: getAbsoluteURL() + "GetCatalogEntryDetailsByID",				
					handleAs: "json-comment-filtered",
					content: parameters,
					service: this,
					load: populateCatentryPopUp,
					error: function(errObj,ioArgs) {
						console.debug("CatalogEntryThumbnailDisplay.showPopup: Unexpected error occurred during an xhrPost request.");
					}
				});
		}
		
		/**
		 * This is the callback that updates the popup window.
		 * 
		 * @param serviceResponse
		 * @param ioArgs
		 * @return
		 */
		function populateCatentryPopUp(serviceResponse, ioArgs) {
			 // PLN Fiwe
			 var decimalsToRemove = ',00';
			var catEntryID = serviceResponse.catalogEntry.catalogEntryIdentifier.uniqueID;
			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/NoImageIcon.jpg";
			}
			
			if (serviceResponse.catalogEntry.offerPrice) {
				if (serviceResponse.catalogEntry.compOfferPrice && serviceResponse.catalogEntry.compUnit) {
					$('#productPrice').html(serviceResponse.catalogEntry.compOfferPrice.replace(decimalsToRemove,'') );				
					document.getElementById('productUnit').innerHTML = ' per ' + serviceResponse.catalogEntry.compUnit;
					// Set Offerprice to calcProductPrice and unit to calcProductUnit
					$('#calcProductPrice').html(serviceResponse.catalogEntry.offerPrice.replace(decimalsToRemove,'') );				
					if (serviceResponse.catalogEntry.unit) {
						document.getElementById('calcProductUnit').innerHTML = ' per ' + serviceResponse.catalogEntry.unit;
					}
					document.getElementById('unitcalculation').style.display = 'block';

				} else {
					// PLN@Fiwe - remove ,00 from prices
					$('#productPrice').html(serviceResponse.catalogEntry.offerPrice.replace(decimalsToRemove,'') );				
					if (serviceResponse.catalogEntry.unit) {
						document.getElementById('productUnit').innerHTML = ' per ' + serviceResponse.catalogEntry.unit;
					}
					document.getElementById('unitcalculation').style.display = 'none';
				}
			}
			if (serviceResponse.catalogEntry.priceClass) {
				// PLN@Fiwe - remove ,00 from prices
				$('#productPrice').removeClass().addClass(serviceResponse.catalogEntry.priceClass);
			}
			
			if (serviceResponse.catalogEntry.listPrice) {
				if (serviceResponse.catalogEntry.compListPrice && serviceResponse.catalogEntry.compUnit) {
					$('#productListPrice').html(serviceResponse.catalogEntry.compListPrice.replace(decimalsToRemove,''));				
					document.getElementById('productUnitListPrice').innerHTML = serviceResponse.catalogEntry.compUnit + ' per fpk';
				} else {
					$('#productListPrice').html(serviceResponse.catalogEntry.listPrice.replace(decimalsToRemove,''));				
					if (serviceResponse.catalogEntry.unit) {
						document.getElementById('productUnitListPrice').innerHTML = serviceResponse.catalogEntry.unit + ' per fpk';
					}
				}
			}else{
				$('#productListPrice').html('');				
			}
			
			if(serviceResponse.catalogEntry.buyable == '1'){
				document.getElementById('purchase_buttonrow').style.visibility = 'visible';
			}else{
				document.getElementById('purchase_buttonrow').style.visibility = 'hidden';				
			}
			
			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;
			
			for (var i in serviceResponse.catalogEntryPromotions) {
				document.getElementById('productPromotions').innerHTML = serviceResponse.catalogEntryPromotions[i] + '<br />';
			}
			// categoryDisplayJS.moreInfoUrl='ProductDisplay?storeId='+storeId+'&catalogId='+catalogId+'&langId='+langId+'&productId='+document.getElementById('productIdQuickInfo').innerHTML;
			// document.getElementById('moreinfo').style.display = 'none';
			document.getElementById('productMoreInfoLink').href = serviceResponse.catalogEntryURL;
			
			categoryDisplayJS.setCurrentCatalogEntryId(catEntryID);
								
			var addtoCart;
			if(document.getElementById('addToCartLinkAjax')){
				addtoCart = document.getElementById('addToCartLinkAjax');
				addtoCart.href = "JavaScript:categoryDisplayJS.AddItem2ShopCartAjax('"+catEntryID+"',document.getElementById('productPopUpQty').value); hidePopup('second_level_category_popup');";
			}
			
			if(document.getElementById('addToCartLink')){
				addtoCart = document.getElementById('addToCartLink');
				addtoCart.href = "#";
				//Must dynamically set the onclick event as follows for IE6
				addtoCart.onclick = new Function("categoryDisplayJS.AddItem2ShopCart(document.getElementById('OrderItemAddForm_"+catEntryID+"'),document.getElementById('productPopUpQty').value); hidePopup('second_level_category_popup');return false;");
			}
			
			if (document.getElementById('addToWishListLinkAjax')) {
				document.getElementById('addToWishListLinkAjax').href = "JavaScript:categoryDisplayJS.AddItem2WishListAjax("+catEntryID+"); hidePopup('second_level_category_popup');";
			} else if(document.getElementById('addToWishListLink')) {
				document.getElementById('addToWishListLink').href = "#";
				//Must dynamically set the onclick event as follows for IE6
				document.getElementById('addToWishListLink').onclick = new Function("categoryDisplayJS.AddItem2WishList(document.getElementById('OrderItemAddForm_"+catEntryID+"'));hidePopup('second_level_category_popup');return false;");
			}
			if (document.getElementById('addToCompareLink')) {
				document.getElementById('addToCompareLink').href = "JavaScript:categoryDisplayJS.Add2CompareAjax('"+catEntryID+"', '" + serviceResponse.productCompareImagePath +"', '" + serviceResponse.catalogEntryURL+ "','"+serviceResponse.compareImageDescription+"'); delayHidePopup();";
			}
			if (document.getElementById('replaceCartItemAjax')) {
				document.getElementById('replaceCartItemAjax').href = "JavaScript:categoryDisplayJS.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("categoryDisplayJS.ReplaceItemNonAjax('entitledItem_"+catEntryID+"',document.getElementById('productPopUpQty').value,document.getElementById('ReplaceItemForm')); hidePopup('second_level_category_popup');return false;");
			}

			// Added by Klas Axelsson
			var isItemBean = false;
			if(serviceResponse.catalogEntry.catalogEntryTypeCode=='ItemBean') {
				isItemBean = true;
			}
			console.debug("EntryType is ItemBean: "+isItemBean);
			if(isItemBean) {
				for (var i in serviceResponse.catalogEntryAttributes.attributes) {
					if (serviceResponse.catalogEntryAttributes.attributes[i].usage == "Defining") {
						categoryDisplayJS.setSelectedAttribute((serviceResponse.catalogEntryAttributes.attributes[i].name).replace("'","\\'"),serviceResponse.catalogEntryAttributes.attributes[i].value.value);
					}
				}
				for (var i in serviceResponse.catalogEntryAttributes.attributes) {
					if (serviceResponse.catalogEntryAttributes.attributes[i].usage == "Defining") {
						// Need ID for get select element tag.
						var uniqueID = serviceResponse.catalogEntryAttributes.attributes[i].attributeIdentifier.uniqueID;
						// attrValue_
						var selectElement = document.getElementById('attrValue_' + uniqueID);
						//console.debug("Select element value: "+selectElement.value+" Response value: "+serviceResponse.catalogEntryAttributes.attributes[i].value.value);
						(document.getElementById('spanAttr_' + uniqueID)).style.display="none";
						for (var idx=0;idx<selectElement.options.length;idx++) { // Iterate the options
							if(selectElement.options[idx].value == serviceResponse.catalogEntryAttributes.attributes[i].value.value) {
								if(selectElement.value != serviceResponse.catalogEntryAttributes.attributes[i].value.value) {
									selectElement.options[idx].selected = true;
									console.debug("Change selectbox value based on resolved SKU value. Attributename: "+(serviceResponse.catalogEntryAttributes.attributes[i].name).replace("'","\\'"));
									(document.getElementById('spanAttr_' + uniqueID)).style.display="inline";
									//alert((serviceResponse.catalogEntryAttributes.attributes[i].name).replace("'","\\'")+" är ändrat till: "+serviceResponse.catalogEntryAttributes.attributes[i].value.value);
								}
								selectElement.options[idx].style.color="#000000";
							} else {
								// If SKU not exist, set gray else black.
								if(categoryDisplayJS.isSkuExisting((serviceResponse.catalogEntryAttributes.attributes[i].name).replace("'","\\'"),selectElement.options[idx].value)) {
									// SKU exist
									selectElement.options[idx].style.color="#000000";
								} else {
									selectElement.options[idx].style.color="#cccccc";
									//console.debug((serviceResponse.catalogEntryAttributes.attributes[i].name).replace("'","\\'")+" with value "+selectElement.options[idx].value+" do not exist!");
								}
							}
						}
					}
				}
			}

		}
	 
	 
	 /** 
	 * 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('productUnit').innerHTML = "";
		document.getElementById('productListPrice').innerHTML = "";
		document.getElementById('productUnitListPrice').innerHTML = "";
		document.getElementById('calcProductPrice').innerHTML = "";
		document.getElementById('calcProductUnit').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/NoImageIcon.jpg";
		}	
		document.getElementById('brandImg_popup').style.display = 'none';
		document.getElementById('unitcalculation').style.display = 'none';  
		categoryDisplayJS.selectedAttributes = new Object();
		categoryDisplayJS.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);
	}
	 function replaceChar(strText, replaceStr, substituteStr)
	 {
		 
		 var strReplaceAll = strText;
		 var intIndexOfMatch = strReplaceAll.indexOf( replaceStr );		
	
		 // Loop over the string value replacing out each matching
		 // substring.
		 while (intIndexOfMatch != -1){
			 // Relace out the current instance.
			 strReplaceAll = strReplaceAll.replace( replaceStr, substituteStr )
			  
		
			 // Get the index of any next matching substring.
			 intIndexOfMatch = strReplaceAll.indexOf( replaceStr );
		 }
		 
		 return (strReplaceAll);
	 }

