// 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  that contains all the global variables and JavaScript functions needed by the compare product page and compare zone. 
	 * This JavaScript is used by CompareProductDisplay.jsp.
	 * @version 1.0
	 */

	/**
	 * @class The functions defined in the class are used for comparing the product's attributes. 
	 *
	 * This CompareProducts class defines all the variables and functions for the page that uses the compare functionality in the store.
	 * The compare zone in the right side bar is a place holder that accepts a maximum of 4 products to compare.
	 * The compare product display page compares the various product's attribute and displays the results side by side.
	 *
	 */

var CompareProducts = {
					
					/** The compareCounter is an integer to store the number of items allowed in the compare page or the compare zone. */
					compareCounter:0,
					
					/** The compareCounter is an integer to store the number of items that needs to be displayed in a row in the compare zone. */
					compareRowCounter:0,
		
					/** The compareItemCatEntry is an object to store the catentry items. */
					compareItemCatEntry:new Object(),
				
					/** The langId is a string to store the current language identifier of the store. */
					langId: "-1",
		
					/** The storeId is a string to store the current store identifier of the store. */
					storeId: "",
		
					/** The catalogId is a string to store the current catalog identifier of the store. */
					catalogId: "",
					
					/** The contentURL is a string to store the current URL of refresh area. */
					contentURL: "",
					
					/** The quickViewURL is a string to store the Quick View URL for add to cart functionality. */
					quickViewURL: "",
					
					/** The isCompare is a boolean to store whether compare has already been applied */
					isCompare: false,
					
					/** The isCompareButtonClicked is a boolean which is used to identify if compare button is clicked or not */
					isCompareButtonClicked: false,					
					/**
					* This function sets the common parameters used in all service calls like langId, storeId and catalogId.
					* @param {string} langId the langugae identifier to use.
					* @param {string} storeId the store identifier to use.
					* @param {string} catalogId the catalog identifier to use.
					*/
					setCommonParameters:function(langId,storeId,catalogId,contentURL,quickViewURL){
						this.langId = langId;
						this.storeId = storeId;
						this.catalogId = catalogId;
						
						contentURL = contentURL.replace('http:',window.location.protocol);
						contentURL = contentURL.replace('https:',window.location.protocol);
						
						quickViewURL = quickViewURL.replace('http:',window.location.protocol);
						quickViewURL = quickViewURL.replace('https:',window.location.protocol);
					
						this.contentURL = contentURL;
						this.quickViewURL = quickViewURL;
					},
					/**
					* This Function does check if cookie already exist for comparison and loads the page accordingly	
					*/
					init:function() {
						CompareProducts.compareCounter=0;
						if(dojo.cookie("compareProd")!=undefined){
							var compareItems = dojo.fromJson(dojo.cookie("compareProd"));
							dojo.forEach(compareItems,
							function(item) {
								CompareProducts.compareCounter++;
								var quickViewURL2 = CompareProducts.getQuickViewURL(item.catentryId);
								document.getElementById('product'+CompareProducts.compareCounter).innerHTML = CompareProducts.generateInnerHtml(item.catentryId,item.brand,item.imgPath,item.desc,item.price,item.productURL,quickViewURL2);
								var compareElement = dojo.byId('product'+CompareProducts.compareCounter);
								dojo.removeClass(compareElement, 'productCompare placeholder'+CompareProducts.compareCounter);
								dojo.addClass(compareElement, 'productCompare');
								var chkelement = null;
								if(document.forms['chkProduct_'+item.catentryId]!=null){
									chkelement = document.forms['chkProduct_'+item.catentryId][0];
								}
								if(chkelement!=null){
									chkelement.checked=true;
								}
								var chkelement1 = null;
								if(document.forms['chkProductPopup_'+item.catentryId]!=null){
									chkelement1 = document.forms['chkProductPopup_'+item.catentryId][0];
								}
								if(chkelement1!=null){
									chkelement1.checked=true;
								}
								var prodelement = dojo.byId('product_'+item.catentryId);
								if(prodelement != null){
									dojo.removeClass(prodelement, 'product');
									dojo.removeClass(prodelement, 'compare');
									dojo.addClass(prodelement, 'product compare');
								}
							}
							);
							document.getElementById('compareText').innerHTML='<strong>'+CompareProducts.compareCounter+' products</strong> added for comparison';
							var compareTextElement = dojo.byId('compareText');
							dojo.removeClass(compareTextElement, 'compare0');
							dojo.addClass(compareTextElement, 'compare'+CompareProducts.compareCounter);
		                }
					},
					
					/**
					* This Function generates the HTML for the display of product in comparator tool	
					*/	
					generateInnerHtml:function(catentryId,brand,imgPath,desc,price,productURL,quickViewURL2){
						var displayHtml = '';
						//displayHtml = displayHtml + '<div class="brand"><h2><a href="#">'+ brand +'</a></h2>';
						displayHtml = displayHtml + '<div class="photoCompare"><a href="'+productURL+'"><img src="'+ imgPath +'" width="140" height="140" alt="Product" /></a></div>';
						displayHtml = displayHtml + '<div class="info"><div class="description"><a href="'+productURL+'">'+ desc +'</a></div></div>';
						displayHtml = displayHtml + '<div class="purchase"><div class="price"><sup>$</sup>'+ price +'</div>';
						displayHtml = displayHtml + '<div class="add"><a href="javascript:setCurrentId(\'WC_CatalogEntryDBThumbnailDisplayJSPF_' + catentryId ;
						displayHtml = displayHtml + '_links_5\'); categoryDisplayJS.setEntitledItemsId(\'entitledItem_'+catentryId+'\');categoryDisplayJS.showProductQuickView(\''+quickViewURL2+'\');" id="WC_CatalogEntryDBThumbnailDisplayJSPF_'+ catentryId +'_links_5" waistate:controls="MiniShoppingCart" class="button addtocart">Add to Cart</a></div>';
						displayHtml = displayHtml + '</div>';
		                displayHtml = displayHtml + '<div class="remove"><a href="javascript:CompareProducts.removeProductToCompare('+catentryId+');"><img src="'+removeImgPath+'" width="12" height="12" alt="Remove" /></a></div>';
		                return displayHtml;
					},
					
					/**
					* This Function adds or removes a product to cookie and displays the same on HTML	
					*/
					addOrRemoveProductToCompare:function(elementId){
						var element = document.forms[elementId][0];
						MessageHelper.hideAndClearMessage();
						if(element != null){
							var catentryId = [];
	       		  			catentryId = elementId.split("_");
							if(!element.checked){
								this.removeProductToCompare(catentryId[1]);
							}
							if(element.checked){
								this.addProductToCompare(catentryId[1]);
							}
						}
					},
					
					/**
					* This Function adds a product to cookie and displays the same on HTML	
					*/
					addProductToCompare:function(catentryId){
						if(this.compareCounter<4){
							var prodExists = false;
							var compareProdArray = new Array(this.compareCounter+1);
							if(dojo.cookie("compareProd")!=undefined){
								var temp = dojo.fromJson(dojo.cookie("compareProd"));
								var i=0;
								for(i=0;i<temp.length;i++){
									if(temp[i].catentryId==catentryId){
										prodExists = true;
									}
								}
								if(!prodExists){
									for(i=0;i<compareProdArray.length;i++){
										compareProdArray[i]=temp[i];
									}
								}
							}
							if(!prodExists){
								this.reset();
								var prodJSON = {};
								dojo.forEach(eval(dojo.byId('comp_product_'+catentryId).innerHTML), function(item) {
									prodJSON = item;
								});
								compareProdArray[this.compareCounter]= prodJSON ;
								dojo.cookie("compareProd",dojo.toJson(compareProdArray));
								this.compareCounter++;
								this.compare(this.isCompare);
								if(dojo.cookie("cmRS")!=undefined){
									if(dojo.isIE == '6' || dojo.isIE == '7' || dojo.isIE == '8') {
										var quickViewElement = dojo.byId('productQuickviewWrapper');
										if(quickViewElement!=null){
											if(quickViewElement.style.display == 'none') {
												location.reload(true);
											}
										}else{
											location.reload(true);
										}
			  						}	
			  					}
							}
						}else{
							var chkelement = null;
							if(document.forms['chkProduct_'+catentryId]!=null){
								chkelement = document.forms['chkProduct_'+catentryId][0];
							}
							chkelement.checked = false ;
							Util.hidePageErrorMsgs();
							dojo.create("div", {"id": "errors"}, "contentHeader" , "last");
							var compareToolbarErrVar = MessageHelper.messages["COMPARE_PRODUCT_TITLE"];
							var innerDiv = dojo.create("div", {innerHTML:"<p>"+compareToolbarErrVar+"</p>"}, "errors");
							dojo.attr(innerDiv, {"id": "errorsInner","tabindex": "1"});
							dojo.byId("errorsInner").focus();
							//MessageHelper.displayErrorMessage(MessageHelper.messages['COMPARE_PRODUCT_TITLE'],'errorsInner');
						}
					},
					
					/**
					* This Function removes the product from cookie and removes the display of the same on HTML	
					*/
					removeProductToCompare:function(catentryId){
						var prodExists = false;
						var compareProdArray = new Array(this.compareCounter-1);
						if(dojo.cookie("compareProd")!=undefined){
							var temp = dojo.fromJson(dojo.cookie("compareProd"));
							var i=0;
							var j=0;
							for(i=0;i<temp.length;i++){
								if(temp[i].catentryId!=catentryId){
									compareProdArray[j]=temp[i];
									j++;
								}
							}
							this.reset();
							var chkelement = null;
							if(document.forms['chkProduct_'+catentryId]!=null){
								chkelement = document.forms['chkProduct_'+catentryId][0];
							}
							if(chkelement!=null){
								chkelement.checked=false;
							}
							var prodelement = dojo.byId('product_'+catentryId);
							if(prodelement != null){
								dojo.removeClass(prodelement, 'product compare');
								dojo.addClass(prodelement, 'product');
							}
							if(temp.length==1){
								dojo.cookie("compareProd",null,{expires:-1});
							}
							if(temp.length>1){
								dojo.cookie("compareProd",dojo.toJson(compareProdArray));
							}
							this.compareCounter--; 
							this.compare(this.isCompare);
						}
					},
					
					/**
					* This Function resets only the HTML view with default display	
					*/
					reset:function(){
						var i=1;
						for(i=1;i<(this.compareCounter+1);i++){
							document.getElementById('product'+i).innerHTML = '';
							var compareElement = dojo.byId('product'+i);
							if(compareElement != null){
								dojo.removeClass(compareElement, 'productCompare');
								dojo.addClass(compareElement, 'productCompare placeholder'+i);
							}
						}
						document.getElementById('compareText').innerHTML='<strong>No products</strong> added for comparison';
						var compareTextElement = dojo.byId('compareText');
						dojo.removeClass(compareTextElement, 'compare'+this.compareCounter);
						dojo.addClass(compareTextElement, 'compare0');
					},
					/**
					* This Function used to clear all the products in the comparator tool	
					*/
					clearCompare:function(){
						if(dojo.cookie("compareProd")!=undefined){
							var compareItems = dojo.fromJson(dojo.cookie("compareProd"));
							dojo.forEach(compareItems,
							function(item) {
								CompareProducts.removeProductToCompare(item.catentryId);
							}
							);
						}
					},
					/**
					* This Function used to populate the Quick View URL for add to cart	
					*/
					getQuickViewURL:function(catentryId){
						var quickViewURLTemp = this.quickViewURL + '&catentryId=' + catentryId;
						return quickViewURLTemp;
					},
					/**
					* loadContentURL Sets the URL of the page to load into CompareProducts_Controller which in turn is used to display 
					* Product Comparision of minimum 2 to 4 products
					*
					* @param {String} contentURL The URL to load contents from.
					*
					**/
					compare:function(isCompare){
						
						//For Handling multiple clicks
						/*if(!submitRequest()){
							return;
						} */
						var contentURL = CompareProducts.contentURL;
						if(isCompare){
							this.isCompareButtonClicked = true;
							if(this.compareCounter > 1){
								cursor_wait();
								if(dojo.cookie("compareProd")!=undefined){
									var compareItems = dojo.fromJson(dojo.cookie("compareProd"));
									dojo.forEach(compareItems,
									function(item) {
										contentURL = contentURL + '&catentryId=' + item.catentryId;
									}
									);
								}
								this.isCompare = isCompare;
							}
						}
							CommonControllersDeclarationJS.setControllerURL('CompareProductsController',contentURL);
							wc.render.updateContext("CompareProducts_Context");
					}
	}
