//
//-------------------------------------------------------------------
// Licensed Materials - Property of IBM
//
// WebSphere Commerce
//
// (c) Copyright IBM Corp. 2007
//
// US Government Users Restricted Rights - Use, duplication or
// disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
//-------------------------------------------------------------------
//

ProductJS={
	entitledItems:[],
	selectedProducts:new Object(),
	selectedAttributes:new Object(),
	errorMessages: new Object(),
	langId: "-1",
	storeId: "",
	catalogId: "",
	updateParamObject:function(params, key, value, toArray, index){
		// summary		: This function updates the given params object with Key value pair.
		// description	: This function updates the given params object with Key value pair.
		//				  If the toArray value is true, It creates an Array for duplicate entries.
		//				  else, It overwrites the old value.
		//				  It is useful while making a service call which excepts few paramters of type array
		// params		: JavaScript Object
		//				  It could be a JavaScript Object or JavaScript Array. JavaScript treats Array as Object only.
		// key			: String
		// value		: String
		// toArray		: Boolean
		//				  If true, creates an Array for duplicate entries
		//				  If false, does not creat an Array for duplicate entries. It overwrites the old value.
		// assumptions	: None.
		// dojo API		: None.
		// returns		: A JavaScript Object having key - value pair.
	   if(params == null){
		   params = [];
	   }

	   if(params[key] != null && toArray)
	   {
			if(dojo.lang.isArrayLike(params[key]))
			{
				//3rd time onwards
			    if(index != null && index != "")
				{
					//overwrite the old value at specified index
				     params[key][index] = value;
				}
				else
				{
				    params[key].push(value);
			     }
		    }
			else
			{
			     //2nd time
			     var tmpValue = params[key];
			     params[key] = [];
			     params[key].push(tmpValue);
			     params[key].push(value);
		    }
	   }
	   else
	   {
			//1st time
		   if(index != null && index != "" && index != -1)
		   {
		      //overwrite the old value at specified index
		      params[key+"_"+index] = value;
		   }
		   else if(index == -1)
		   {
		      var i = 1;
		      while(params[key + "_" + i] != null)
			  {
			       i++;
		      }
		      params[key + "_" + i] = value;
		   }
		   else
		   {
		      params[key] = value;
		    }
	   }
	   return params;
	 },
	setCommonParameters:function(langId,storeId,catalogId){
		// summary		: This function initializes common parameters used in all service calls
		// description	: This function initializes storeId, catalogId, and langId.
		// langId	: The language id to use.
		// storeId : The store id to use.
		// catalog : The catalog id to use.
		this.langId = langId;
		this.storeId = storeId;
		this.catalogId = catalogId;
	},
	
	setEntitledItems : function(entitledItemArray){
		this.entitledItems = entitledItemArray;
	},

	setSelectedAttribute : function(selectedAttributeName , selectedAttributeValue){
		//alert(selectedAttributeName +" : "+ selectedAttributeValue);
		this.selectedAttributes[selectedAttributeName] = selectedAttributeValue;
	},
	setSelectedAttributeOfProduct : function(productId,selectedAttributeName,selectedAttributeValue){
		
		selectedAttributesForProduct = new Object();

		if(this.selectedProducts[productId]) selectedAttributesForProduct = this.selectedProducts[productId];
		
		selectedAttributesForProduct[selectedAttributeName] = selectedAttributeValue;
		this.selectedProducts[productId] = selectedAttributesForProduct;
		
	},
	getCatalogEntryId : function(){
		var attributeArray = [];
		for(attribute in this.selectedAttributes){
			attributeArray.push(attribute + "_" + this.selectedAttributes[attribute]);
		}
		return this.resolveSKU(attributeArray);
	},
	getCatalogEntryIdforProduct : function(selectedAttributes){
		var attributeArray = [];
		for(attribute in selectedAttributes){
			attributeArray.push(attribute + "_" + selectedAttributes[attribute]);
		}
		return this.resolveSKU(attributeArray);
	},

	resolveSKU : function(attributeArray){
		//alert("Resolving SKU >> " + attributeArray +">>"+ this.entitledItems);
		var catentry_id = "";
		var attributeArrayCount = attributeArray.length;
		
		for(x in this.entitledItems){
			var catentry_id = this.entitledItems[x].catentry_id;
			var Attributes = this.entitledItems[x].Attributes;
			var attributeCount = 0;
			for(index in Attributes)
				attributeCount ++;

			if(attributeArrayCount >= attributeCount){
				var matchedAttributeCount = 0;

				for(attributeName in attributeArray){
					var attributeValue = attributeArray[attributeName];
					//alert(attributeName + ":" + attributeValue);
					if(attributeValue in Attributes){
						matchedAttributeCount ++;
					}
				}
				
				if(attributeCount == matchedAttributeCount){
					//alert("CatEntryId:" + catentry_id + " for Attribute: " + attributeArray);
					return catentry_id;
				}
			}
		}
		return null;
	},
	
	PopUpAdd2ShopCartAjax : function(entitledItemInbutBoxId){
		var params = [];
		var entitledItemJSON = eval('('+ dojo.byId(entitledItemInbutBoxId).innerHTML +')');
		this.setEntitledItems(entitledItemJSON);
		var catalogEntryId = this.getCatalogEntryId();

		if(catalogEntryId != null){
			params.storeId		= this.storeId;
			params.catalogId	= this.catalogId;
			params.langId			= this.langId;
			params.orderId		= ".";
			params.quantity		= "1";
			params.catEntryId	= catalogEntryId;
			wc.service.invoke("AjaxAddOrderItem", params);
			cursor_wait();
		}
		else{
			alert(this.getErrorMessage("ERR_RESOLVING_SKU"));
		}
	},

	PopUpAdd2WishListAjax : function(entitledItemInbutBoxId){
		var params = [];
		var entitledItemJSON = eval('('+ dojo.byId(entitledItemInbutBoxId).innerHTML +')');
		this.setEntitledItems(entitledItemJSON);
		var catalogEntryId = this.getCatalogEntryId();

		if(catalogEntryId != null){
			params.storeId		= this.storeId;
			params.catalogId	= this.catalogId;
			params.langId			= this.langId;
			params.URL = "SuccessfulAJAXRequest";
			params.catEntryId	= catalogEntryId;
			wc.service.invoke("AjaxInterestItemAdd", params);
			cursor_wait();
		}
		else{
			alert(this.getErrorMessage("ERR_RESOLVING_SKU"));
		}
	},

	BundleAdd2ShopCartAjax : function(form){
		
		var params = [];
		var queryString = dojo.io.encodeForm(dojo.byId(form));

		params.storeId		= this.storeId;
		params.catalogId	= this.catalogId;
		params.langId		= this.langId;
		params.orderId		= ".";
			
		var catEntryArray = [];
		catEntryArray = form.catEntryIDS.value.toString().split(",");
		
		for(var i = 0; i < catEntryArray.length; i++){
			var qty = document.getElementById("quantity_" + catEntryArray[i]).value;
			var catEntryId = catEntryArray[i];
			if(this.selectedProducts[catEntryArray[i]])
				catEntryId = this.getCatalogEntryIdforProduct(this.selectedProducts[catEntryArray[i]]);
			
			if(Common.IsNumeric(qty,false) && qty!=null && qty!='' && catEntryId!=null){
				if(qty!=0){
					this.updateParamObject(params,"catEntryId",catEntryId,false,-1);
					this.updateParamObject(params,"quantity",qty,false,-1);
				}
			}
			else{
				alertDialog(this.getErrorMessage("ERR_RESOLVING_SKU"),this.storeId,this.catalogId,this.langId);
				return;
			}
		}
		wc.service.invoke("AjaxAddOrderItem", params);
		cursor_wait();

	},
	
	BundleAdd2WishListAjax : function(form){
		
		var params = [];
		var queryString = dojo.io.encodeForm(dojo.byId(form));

		params.storeId		= this.storeId;
		params.catalogId	= this.catalogId;
		params.langId		= this.langId;
		params.URL          = "SuccessfulAJAXRequest";
			
		var catEntryArray = [];
		catEntryArray = form.catEntryIDS.value.toString().split(",");
		
		for(var i = 0; i < catEntryArray.length; i++){
			var qty = document.getElementById("quantity_" + catEntryArray[i]).value;
			var catEntryId = catEntryArray[i];
			if(this.selectedProducts[catEntryArray[i]])
				catEntryId = this.getCatalogEntryIdforProduct(this.selectedProducts[catEntryArray[i]]);
			
			if(Common.IsNumeric(qty,false) && qty!=null && qty!='' && catEntryId!=null){
				if(qty!=0){
					this.updateParamObject(params,"catEntryId",catEntryId,false,-1);
					this.updateParamObject(params,"quantity",qty,false,-1);
				}
			}
			else{
				alertDialog(this.getErrorMessage("ERR_RESOLVING_SKU"),this.storeId,this.catalogId,this.langId);
				return;
			}
		}
		wc.service.invoke("AjaxInterestItemAdd", params);
		cursor_wait();

	},
	ProductPageAdd2ShopCartAjax : function(formId, productId, resolveSKU)
	{
		var params = [];
		var queryString = dojo.io.encodeForm(dojo.byId(formId));
		var catalogEntryId = "";

		if(resolveSKU)
			catalogEntryId = this.getCatalogEntryId();
		else
			catalogEntryId = productId;

		params = queryToParamObject(queryString, params, false);
		
		if(catalogEntryId != null){
			params.storeId		= this.storeId;
			params.catalogId	= this.catalogId;
			params.langId			= this.langId;
			params.orderId		= ".";
			params.catEntryId	= catalogEntryId;
			wc.service.invoke("AjaxAddOrderItem", params);
			cursor_wait();
		}
		else{
			alert(this.getErrorMessage("ERR_RESOLVING_SKU"));
		}
	},
	
	ProductPageAdd2WishListAjax : function(formId, productId, resolveSKU)
	{
		var params = [];
		var queryString = dojo.io.encodeForm(dojo.byId(formId));
		var catalogEntryId = "";

		if(resolveSKU)
			catalogEntryId = this.getCatalogEntryId();
		else
			catalogEntryId = productId;

		params = queryToParamObject(queryString, params, false);
		
		if(catalogEntryId != null){
			params.storeId		= this.storeId;
			params.catalogId	= this.catalogId;
			params.langId			= this.langId;
			params.URL = "SuccessfulAJAXRequest";
			params.catEntryId	= catalogEntryId;
			wc.service.invoke("AjaxInterestItemAdd", params);
			cursor_wait();
		}
		else{
			alert(this.getErrorMessage("ERR_RESOLVING_SKU"));
		}
	},

	Add2NewsLetterAjax : function(form)
	{
		//alert("<fmt:message key="FUTURE_DIRECTION" bundle="${storeText}" />");
	},

	Add2RSSFeedsAjax : function(form)
	{
		//alert("<fmt:message key="FUTURE_DIRECTION" bundle="${storeText}" />");
	},
	
	setErrorMessage : function(key, value){
		this.errorMessages[key] = value;
	},
	
	getErrorMessage : function(key){
		var value = this.errorMessages[key];
		if(value == null)
			value = "Could not get the message value for specified key " + key;
		
		return value;
	}
}
