//
//-------------------------------------------------------------------
// 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:[],
	selectedAttributes:new Object(),
	errorMessages: new Object(),
	langId: "-1",
	storeId: "",
	catalogId: "",

	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;
	},

	getCatalogEntryId : function(){
		var attributeArray = [];
		for(attribute in this.selectedAttributes){
			attributeArray.push(attribute + "_" + this.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"));
		}
	},

	
	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;
	}
}

