/* C12907 - Dec 31 2009  Easy Reorder Task */
/** Q03767 - 01/21/2011 - UAT#637 - Pre-Order Future Changes- Removing "Error Message Is:" text 
*  Q04175 - 04/07/2011 - UAT#650 - Add To Cart Accessory Layer changes.
*  Q05081 - 01/05/2012 - UAT#1079 - QuickLook View vulnerable to Blind SQL Injection.
* Added a new method AjaxAddAccessories2ShopCart method and modified, this method is specifically called
* from Add to Cart Accessory Layer.
* Modified Method AjaxAddAccs2ShopCart to load accessory layer when add to cart is successful. 
*/
// ***
// * This javascript function is used by the 'Add to cart' button for creating the loading iamge layer upon 
// * completeion of AJAX call to the backend WebSphere Commerce AJAX controller command
// ** Cross Site Scripting code added in AjaxAdd2ShopCart method
// ** added null value for params[find] to that method.
// ***
function addtoCartLoadLayer(){
				//Begin layer code
						var defaultContent = "<div id='addcartloading-template' class='ie6png'><div class='loadingimage'><img src='/wcsstore/eStore/images/loading.gif' alt='Loading...' /></div></div>";
						//var thisRow = form; 
	               		//while( !dojo.hasClass(thisRow, "row") ) { 	
	              	 	//thisRow = thisRow.parentNode; 
	              		 //} 
						
						//var x = lib.utils.getPosition(thisRow)[0][0];
						//var y = lib.utils.getPosition(thisRow)[0][1];
						
						lib.layer.create("#addCartLayer", {
							defaultContent : defaultContent,
							closeSelector : ".js-layerClose",
							  //xPos : x,
							  //yPos : y,
							  keepCentered : true,
							callback : null,
							nonModal: false
						});//End layer code				
}

/*
 * This javascript function is used by the 'Add to cart' button for creating the loading iamge layer . 
 * Upon completeion of AJAX call to the backend WebSphere Commerce AJAX controller command
 * This loading image layer will be closed and the accessory layer will be loaded.
 * added null value for params[find] to that method.
*/

function addtoCartLoadLayerAcc(){
				
						var defaultContent = "<div id='addcartloading-template' class='ie6png'><div class='loadingimage'><img src='/wcsstore/eStore/images/loading.gif' alt='Loading...' /></div></div>";
					
						lib.layer.create("#addCartLayerProduct", {
							defaultContent : defaultContent,
							closeSelector : ".js-layerClose",
							keepCentered : true,							
							nonModal: false,
	// Changes for the Defect # 1079  Start
							callback : function() {
							dojo.query("#addCartLayerProduct").fadeOut({ 
							delay: 5000, 
							onEnd: function(){
							dojo.query("#addCartLayerProduct").orphan();
							}
							}).play();
							}
	// Changes for the Defect # 1079  End
						});
}

/*
* This function will hide the layer that is passed in as parameter to the function and also will remove the page overlay.
*/

function hidelayer(layer) {

					document.getElementById(layer).style.display='none';
					document.getElementById('pageOverlayLayer').style.display='none';
}

function addtoCartLoadLayerProduct(form){
				//Begin layer code
						var defaultContent = "<div id='addcartloadingProduct-template' class='ie6png'><div class='loadingimage'><img src='/wcsstore/eStore/images/loading.gif' alt='Loading...' /></div></div>";
						var thisRow = form;
						//alert("form value :"+thisRow); 
	               		while( !dojo.hasClass(thisRow, "pricing-panel") ) { 	
	              	 		thisRow = thisRow.parentNode; 
	              		} 
						//alert("step1");
						var x = lib.utils.getPosition(thisRow)[0][0];
						var y = lib.utils.getPosition(thisRow)[0][1];
						//alert("step2");
						lib.layer.create("#addCartLayerProduct", {
							defaultContent : defaultContent,
							closeSelector : ".js-layerClose",
							//keepCentered : true,
							  xPos : x,
							  yPos : y,
							callback : null,
							nonModal: false
						});//End layer code				
}
		
// ***
// * This javascript function is used by the 'Add to cart' button for creating the overlay(layer) upon 
// * completeion of AJAX call to the backend WebSphere Commerce AJAX controller command
// ***
function addtoCartLayer(defaultContent, storeId, catalogId){
				//Update header totals
				updateCount(storeId, catalogId);				
				//Begin layer code
						var defaultContent = defaultContent;
						//var thisRow = form; 
	    	           	//	while( !dojo.hasClass(thisRow, "row") ) { 	
	     	         	// 	thisRow = thisRow.parentNode; 
	      	        	//	 } 
						
						//var x = lib.utils.getPosition(thisRow)[0][0];
						//var y = lib.utils.getPosition(thisRow)[0][1];
						
						lib.layer.create("#addCartLayer", {
							defaultContent : defaultContent,
							closeSelector : ".js-layerClose",
						  	keepCentered : true,
							//xPos : x,
							//yPos : y,
							nonModal: false,
							callback : function() {
								dojo.query("#addCartLayer").fadeOut({ 
									delay: 5000, 
									onEnd: function(){ dojo.query("#addCartLayer").orphan(); }
								}).play();
							}
						});//End layer code			
}

// ***
// * This javascript function is used by the 'Add to cart' button for creating the overlay(layer) upon 
// * completeion of AJAX call to the backend WebSphere Commerce AJAX controller command
// ***
function addtoShoppingCartLayer(defaultContent,storeId , catalogId, langId){
		//Update header totals
		updateCount(storeId, catalogId);				
		//Begin layer code
		var defaultContent = defaultContent;
				
		lib.layer.create("#addCartLayer", {
			defaultContent : defaultContent,
			closeSelector : ".js-layerClose",
		  	keepCentered : true,
			//xPos : x,
			//yPos : y,
			nonModal: false,
			callback : function() {
				dojo.query("#addCartLayer").fadeOut({ 
					delay: 5000, 
					onEnd: function(){
					updateShopCart(storeId , catalogId, langId);	
					dojo.query("#addCartLayer").orphan();
					}
				}).play();
			}
		});//End layer code			
}

function updateShopCart(storeId , catalogId, langId) {
 	location.href= "CusaApplyBestShippingMode?URL=OrderItemDisplay&updatePrices=1&calculationUsageId=-1&calculationUsageId=-2&calculationUsageId=-3&calculationUsageId=-4&calculationUsageId=-7&orderId=.&errorViewName=OrderItemDisplay"+"&storeId="+storeId+"&catalogId="+catalogId+"&langId="+langId;
}
	
	

function addtoCartLayerProduct(defaultContent,form,storeId,catalogId){
				//Update header totals
				updateCount(storeId, catalogId);
				//Begin layer code
						var defaultContent = defaultContent;
						var thisRow = form; 
	    	           		//while( !dojo.hasClass(thisRow, "row") ) { 	
	     	         	 	//thisRow = thisRow.parentNode; 
	      	        		// } 
						//var x = lib.utils.getPosition(thisRow)[0][0];
						//var y = lib.utils.getPosition(thisRow)[0][1];
						lib.layer.create("#addCartLayer", {
							defaultContent : defaultContent,
							closeSelector : ".js-layerClose",
							keepCentered : true,
							//xPos : x,
							//yPos : y,
							nonModal: false,
							callback : function() {
								dojo.query("#addCartLayerProduct").fadeOut({ 
									delay: 0, 
									onEnd: function(){ dojo.query("#addCartLayerProduct").orphan(); }
								}).play();
								dojo.query("#addCartLayer").fadeOut({ 
									delay: 5000, 
									onEnd: function(){ dojo.query("#addCartLayer").orphan(); }
								}).play();	
							}
						});//End layer code				
}
// ***
// * This function accepts params catentryId and quantity and adds that catentry id to the cart.
// * catalogId, storeId, langId needs to be passed to construct view cart and checkout urls
// * This function is used only for category and accessory listing pages
// * WebSphere Commerce AJAX controller command
// ***
function AjaxAdd2ShopCart(catEntryId, count, prodname, catalogId, storeId, langId, field2, prdType,isShoppingCart,availableReccAssociations,typeOfProduct) { 
		 
           var qtyStr = "quantity" + count;
           var qty = document.getElementById(qtyStr).value;
           var tncUrl = "";
           addtoCartLoadLayerAcc();
           var params = [];
			params["orderId"] = ".";
			params["catEntryId"] = catEntryId;
			params["quantity"] = qty;
			// check for opening tnc page
			//specifically making value of 'find' null to counter the X Site Scripting
			params['find'] =  "";
			if(prdType>0){
				//alert("inside field2 NOT empty :: "+field2);
				params["field2"] = field2;
				if(prdType=="2"){
					tncUrl = "CusaCarePAKTermsConditionsDisplay?storeId="+storeId+"&catalogId="+catalogId+"&langId="+langId+"&flag=detail&cptype=2";
				}else if (prdType=="1"){
					tncUrl = "CusaCarePAKTermsConditionsDisplay?storeId="+storeId+"&catalogId="+catalogId+"&langId="+langId+"&flag=detail&cptype=1";
				}else if (prdType=="3"){
					tncUrl = "CusaRecycleTermsConditionsDisplay?storeId="+storeId+"&catalogId="+catalogId+"&langId="+langId+"&flag=detail";
				}
				//alert("inside field2 NOT empty URL :: "+tncUrl);
				/* CarePAK Terms and Conditions Layer */
					lib.layer.create("#carePAKTermsLayer", {
						url : tncUrl,
						keepCentered : true,
						nonModal: false,
						callback : function() {
							dojo.query("#addCartLayerProduct").fadeOut({ 
								delay: 0, 
								onEnd: function(){ dojo.query("#addCartLayerProduct").orphan(); }
							}).play();	
							/* Setup the Print Link */
							lib.link.popupWindow("#js-printCarePAKTermsAndConditions", { width: 600, height: 600, scrollbars : 1 });
						
							/* Setup the Page Overlay */
							var overlay = new lib.obj.pageOverlay({ selector : "#pageOverlayLayer" });
						
							/* Setup teh "decline" button */
							dojo.query(".js-layerClose").connect("click", function(evt) {
								dojo.stopEvent(evt);
								overlay.removeOverlay();
								lib.layer.remove("#carePAKTermsLayer");
							});
							
							/* Setup teh "Accept button */
							dojo.query(".js-acceptTerms").connect("click", function(evt) {
								dojo.stopEvent(evt);
								overlay.removeOverlay();
								lib.layer.remove("#carePAKTermsLayer");
								addtoCartLoadLayer();
								//alert("Accept, do something TBD by Engineering Approach");
								dojo.xhrGet({
									url: "CusaAjaxOrderItemAdd",
									handleAs: "json",
									content: params,
									load: function(response,ioArgs) {
								    	console.log("Inside load of Ajax Call");
										//Parse JSON response for errors
										var errorMessageKey =response.errorMessageKey;
									    var errorMessage =response.errorMessage;
										//Success scenario
										if(!errorMessageKey && ! errorMessage){
											//Get Order id from JSON reponse
										    var orderId = response.orderId;		
											//Launch post success code 
											//Launch add to cart layer  with internationalized success text( e.g.successful add to cart) from the resource bundle
											//Begin layer code
											var defaultContent = "";
											var defaultContent = "<div id='addcart-template' class='ie6png'><div class='content'><div id='information-box'><h2>Your Product Selection Has Been Added to the Cart.</h2><a class='js-layerClose' href='#close'>close</a><br /><br /><a href='CusaApplyBestShippingMode?updatePrices=1&calculationUsageId=-1&calculationUsageId=-2&calculationUsageId=-3&calculationUsageId=-4&calculationUsageId=-7&orderItemId*=&quantity*=&storeId="+storeId+"&catalogId="+catalogId+"&langId="+langId+"&orderId="+orderId+"&URL=OrderItemDisplay' class='right-arrow'> View Cart & Begin Checkout</a></div></div></div>";
											addtoCartLayer(defaultContent, storeId, catalogId);
										} //End success scenario
										//Failure scenario
										var defaultContent = ""; 
										if (errorMessage) {
											// Launch add to cart layer with errorMessage
											if(errorMessageKey=='_API_BAD_INV'){
												defaultContent = "<div id='addcart-template' class='ie6png'><div class='content'><div id='information-box'><h2>There is not sufficient inventory for this purchase, and we are not accepting backorders on this product.</h2><a class='js-layerClose' href='#close'>close</a><br /><br /><a href='CusaApplyBestShippingMode?updatePrices=1&calculationUsageId=-1&calculationUsageId=-2&calculationUsageId=-3&calculationUsageId=-4&calculationUsageId=-7&orderItemId*=&quantity*=&storeId="+storeId+"&catalogId="+catalogId+"&langId="+langId+"&orderId="+orderId+"&URL=OrderItemDisplay' class='right-arrow'> View Cart & Begin Checkout</a></div></div></div>";						   
											}else{
												defaultContent = "<div id='addcart-template' class='ie6png'><div class='content'><div id='information-box'><h2>" + errorMessage  + " </h2><a class='js-layerClose' href='#close'>close</a><br /><br /><a href='CusaApplyBestShippingMode?updatePrices=1&calculationUsageId=-1&calculationUsageId=-2&calculationUsageId=-3&calculationUsageId=-4&calculationUsageId=-7&orderItemId*=&quantity*=&storeId="+storeId+"&catalogId="+catalogId+"&langId="+langId+"&orderId="+orderId+"&URL=OrderItemDisplay' class='right-arrow'> View Cart & Begin Checkout</a></div></div></div>";						   
											}
											addtoCartLayer(defaultContent, storeId, catalogId);
										} else {
											if (errorMessageKey) {
												// Launch add to cart layer with errorMessageKey
												//Begin layer code
												var defaultContent = "<div id='addcart-template' class='ie6png'><div class='content'><div id='information-box'><h2>" + errorMessageKey  + " </h2><a class='js-layerClose' href='#close'>close</a><br /><br /><a href='CusaApplyBestShippingMode?updatePrices=1&calculationUsageId=-1&calculationUsageId=-2&calculationUsageId=-3&calculationUsageId=-4&calculationUsageId=-7&orderItemId*=&quantity*=&storeId="+storeId+"&catalogId="+catalogId+"&langId="+langId+"&orderId="+orderId+"&URL=OrderItemDisplay' class='right-arrow'> View Cart & Begin Checkout</a></div></div></div>";
												addtoCartLayer(defaultContent, storeId, catalogId);
											}
										}
									}, //End of load 
									//This code gets invoked on an error in AJAX communication
									error: function(error, args) {
										//log to console will log to the debug console
										//console.log("error!",error);
										// Launch add to cart layer with message ?Communication Failure?
									    var defaultContent = "<div id='addcart-template' class='ie6png'><div class='content'><div id='information-box'><h2>"+ prodname +" could not be added to the cart. Please try again.</h2><a class='js-layerClose' href='#close'>close</a><br /><br /><a href='CusaApplyBestShippingMode?updatePrices=1&calculationUsageId=-1&orderItemId*=&quantity*=&storeId="+storeId+"&catalogId="+catalogId+"&langId="+langId+"&orderId="+orderId+"&URL=OrderItemDisplay' class='right-arrow'> View Cart & Begin Checkout</a></div></div></div>";
										addtoCartLayer(defaultContent, storeId, catalogId);							
									} //End of error
									
								}); //End dojoxhrGet call																
							});
						}
					});
				/* END -- CarePAK Terms and Conditions Layer */								
			}else{			

				dojo.xhrGet({
					url: "CusaAjaxOrderItemAdd",
					handleAs: "json",
					content: params,
					load: function(response,ioArgs) {
				    	console.log("Inside load of Ajax Call");
						//Parse JSON response for errors
						var errorMessageKey =response.errorMessageKey;
					    var errorMessage =response.errorMessage;
						//Success scenario
						if(!errorMessageKey && ! errorMessage){
							
							if(availableReccAssociations == 'YES' && typeOfProduct == 'HARDWARE') {
							 
							 if (document.getElementById("quicklook") != null) {
							 	document.getElementById('quicklook').style.display='none';
							 } 
					
							 tncUrl = "CusaAccessoryLayerDisplay?storeId="+storeId+"&catalogId="+catalogId+"&langId="+langId+"&productId="+catEntryId;
								lib.layer.create("#addToCartAccessoryLayer", {
								url : tncUrl,
								keepCentered : true,
								nonModal: false,
								callback : function() {
										dojo.query("#addCartLayerProduct").fadeOut({ 
										delay: 0, 
										onEnd: function(){ dojo.query("#addCartLayerProduct").orphan(); }
									}).play();	
								
									/* Setup the Page Overlay */
									var overlay = new lib.obj.pageOverlay({ selector : "#pageOverlayLayer" });
								
									/* Setup the "decline" button */
									dojo.query(".js-layerClose").connect("click", function(evt) {
										dojo.stopEvent(evt);
										overlay.removeOverlay();
										lib.layer.remove("#addToCartAccessoryLayer");
									});
								}
								});
							 }

							 else {
							//Get Order id from JSON reponse
						    var orderId = response.orderId;		
							//Launch post success code 
							//Launch add to cart layer  with internationalized success text( e.g.successful add to cart) from the resource bundle
							//Begin layer code
							var defaultContent = "<div id='addcart-template' class='ie6png'><div class='content'><div id='information-box'><h2>Your Product Selection Has Been Added to the Cart.</h2><a class='js-layerClose' href='#close'>close</a><br /><br /><a href='CusaApplyBestShippingMode?updatePrices=1&calculationUsageId=-1&calculationUsageId=-2&calculationUsageId=-3&calculationUsageId=-4&calculationUsageId=-7&orderItemId*=&quantity*=&storeId="+storeId+"&catalogId="+catalogId+"&langId="+langId+"&orderId="+orderId+"&URL=OrderItemDisplay' class='right-arrow'> View Cart & Begin Checkout</a></div></div></div>";
							document.getElementById('addCartLayerProduct').style.display='none';
							addtoCartLayer(defaultContent, storeId, catalogId);
							}
				
							if(isShoppingCart == 'true'){
								//Do not display add to cart overlay UAT 426
								//addtoShoppingCartLayer(defaultContent,storeId , catalogId, langId);
								updateShopCart(storeId , catalogId, langId);
								
							}else{
								//addtoCartLayer(defaultContent, storeId, catalogId);
								 updateCount(storeId, catalogId);
							}
						
						} //End success scenario
						//Failure scenario
						var defaultContent = ""; 
						if (errorMessage) {
							// Launch add to cart layer with errorMessage
							if(errorMessageKey=='_API_BAD_INV'){
								defaultContent = "<div id='addcart-template' class='ie6png'><div class='content'><div id='information-box'><h2>There is not sufficient inventory for this purchase, and we are not accepting backorders on this product.</h2><a class='js-layerClose' href='#close'>close</a><br /><br /><a href='CusaApplyBestShippingMode?updatePrices=1&calculationUsageId=-1&calculationUsageId=-2&calculationUsageId=-3&calculationUsageId=-4&calculationUsageId=-7&orderItemId*=&quantity*=&storeId="+storeId+"&catalogId="+catalogId+"&langId="+langId+"&orderId="+orderId+"&URL=OrderItemDisplay' class='right-arrow'> View Cart & Begin Checkout</a></div></div></div>";						   
							} else if(errorMessageKey=='_ERR_INVALID_INPUT'){
								defaultContent = "<div id='addcart-template' class='ie6png'><div class='content'><div id='information-box'><h2>Please enter valid value in quantity field.</h2><a class='js-layerClose' href='#close'>close</a><br /><br /></div></div></div>";
							} else {
								defaultContent = "<div id='addcart-template' class='ie6png'><div class='content'><div id='information-box'><h2>"+errorMessage+"</h2><a class='js-layerClose' href='#close'>close</a><br /><br /><a href='CusaApplyBestShippingMode?updatePrices=1&calculationUsageId=-1&calculationUsageId=-2&calculationUsageId=-3&calculationUsageId=-4&calculationUsageId=-7&orderItemId*=&quantity*=&storeId="+storeId+"&catalogId="+catalogId+"&langId="+langId+"&orderId="+orderId+"&URL=OrderItemDisplay' class='right-arrow'> View Cart & Begin Checkout</a></div></div></div>";						   
							}
							addtoCartLayer(defaultContent, storeId, catalogId);
						} else {
							if (errorMessageKey) {
								// Launch add to cart layer with errorMessageKey
								//Begin layer code
								var defaultContent = "<div id='addcart-template' class='ie6png'><div class='content'><div id='information-box'><h2>" + errorMessageKey  + " </h2><a class='js-layerClose' href='#close'>close</a><br /><br /><a href='CusaApplyBestShippingMode?updatePrices=1&calculationUsageId=-1&calculationUsageId=-2&calculationUsageId=-3&calculationUsageId=-4&calculationUsageId=-7&orderItemId*=&quantity*=&storeId="+storeId+"&catalogId="+catalogId+"&langId="+langId+"&orderId="+orderId+"&URL=OrderItemDisplay' class='right-arrow'> View Cart & Begin Checkout</a></div></div></div>";
								addtoCartLayer(defaultContent, storeId, catalogId);
							}
						}
					}, //End of load 
					//This code gets invoked on an error in AJAX communication
					error: function(error, args) {
						//log to console will log to the debug console
						//console.log("error!",error);
						// Launch add to cart layer with message ?Communication Failure?
					    var defaultContent = "<div id='addcart-template' class='ie6png'><div class='content'><div id='information-box'><h2>"+ prodname +" could not be added to the cart. Please try again.</h2><a class='js-layerClose' href='#close'>close</a><br /><br /><a href='CusaApplyBestShippingMode?updatePrices=1&calculationUsageId=-1&orderItemId*=&quantity*=&storeId="+storeId+"&catalogId="+catalogId+"&langId="+langId+"&orderId="+orderId+"&URL=OrderItemDisplay' class='right-arrow'> View Cart & Begin Checkout</a></div></div></div>";
						addtoCartLayer(defaultContent, storeId, catalogId);							
					} //End of error
					
				}); //End dojoxhrGet call
			} //else of field2 check ends
}
// ***
// * This function accepts params product name and form and get all the elements of the form 
// * and add to the cart. There can be multiple recommended supplies associated to that product.
// * catalogId, storeId, langId needs to be passed to construct view cart and checkout urls
// * This function is used only for Product detail pages
// * WebSphere Commerce AJAX controller command
// ***
		function AjaxAddAccs2ShopCart(form, prodname, catalogId, storeId, langId,productId) {
			
			addtoCartLoadLayerProduct(form);
            var orderId = ".";

			// Set up parameters needed to make the call
			var params = [];
			var validQuantityFlg = "1";
			var validCP = "0";
			var tncUrl = "";
			var cpType = "";
			var field2ConcatArray = new Array();
			var field2Concat = new String();
			n = document.addtocart.elements.length;
			var elementNeeded = new Array();
			for (i=0; i<n; i++){
			
				if(document.addtocart.elements[i].value<0){
					var defaultContent = "<div id='addcart-template' class='ie6png'><div class='content'><div id='information-box'><h2>Invalid Quantity - Please enter valid value in quantity field (0 - 99)</h2><a class='js-layerClose' href='#close'>close</a><br /><br /></div></div></div>";
					addtoCartLayerProduct(defaultContent,form,storeId,catalogId);
					validQuantityFlg = "0";
					break;
				}
				if(document.addtocart.elements[i].value>0){
					var makeCatEntry = "";
					var count=0;						
					sName = document.addtocart.elements[i].name;
					var temp = new Array();
					temp = sName.split('_');
					
					if(temp[0]=="quantity"){								
						count++;
						qtyElement = sName.substring(9,15);
						params[sName] =  document.addtocart.elements[i].value;
						elementNeeded[count]=qtyElement;							
					}
					if(temp[0]=="catEntryId"){						
						for(a=0; a<elementNeeded.length; a++){
							var makeCatEntry = "catEntryId_"+elementNeeded[a];
							if(document.addtocart.elements[i].name==makeCatEntry){
								params[document.addtocart.elements[i].name] =  document.addtocart.elements[i].value;					
							}
						}	
					}					
					if(temp[0]=="field2"){						
						for(a=0; a<elementNeeded.length; a++){
							var makeField2 = "field2_"+elementNeeded[a];
							if(document.addtocart.elements[i].name==makeField2){
								params[document.addtocart.elements[i].name] =  document.addtocart.elements[i].value;
								validCP = "1";
							}
						}	
					}
					if(temp[0]=="prdType"){						
						for(a=0; a<elementNeeded.length; a++){
							var makePrdType = "prdType_"+elementNeeded[a];
							if(document.addtocart.elements[i].name == makePrdType){
								cpType =  document.addtocart.elements[i].value;
							}
						}	
					}					
				}// end if							
			} // end for
			params['find'] =  "";	
					
			if(cpType=="2"){
				tncUrl = "CusaCarePAKTermsConditionsDisplay?storeId="+storeId+"&catalogId="+catalogId+"&langId="+langId+"&flag=detail&cptype=2";
			}else if (cpType=="1"){
				tncUrl = "CusaCarePAKTermsConditionsDisplay?storeId="+storeId+"&catalogId="+catalogId+"&langId="+langId+"&flag=detail&cptype=1";
			}else if (cpType=="3"){
				tncUrl = "CusaRecycleTermsConditionsDisplay?storeId="+storeId+"&catalogId="+catalogId+"&langId="+langId+"&flag=detail";
			}
			if(validCP=="1"){
				/* CarePAK Terms and Conditions Layer */
					lib.layer.create("#carePAKTermsLayer", {
						url : tncUrl,
						keepCentered : true,
						nonModal: false,
						callback : function() {
							dojo.query("#addCartLayerProduct").fadeOut({ 
								delay: 0, 
								onEnd: function(){ dojo.query("#addCartLayerProduct").orphan(); }
							}).play();	
							/* Setup the Print Link */
							lib.link.popupWindow("#js-printCarePAKTermsAndConditions", { width: 600, height: 600, scrollbars : 1 });
						
							/* Setup the Page Overlay */
							var overlay = new lib.obj.pageOverlay({ selector : "#pageOverlayLayer" });
						
							/* Setup teh "decline" button */
							dojo.query(".js-layerClose").connect("click", function(evt) {
								dojo.stopEvent(evt);
								overlay.removeOverlay();
								lib.layer.remove("#carePAKTermsLayer");
							});
							
							/* Setup teh "Accept button */
							dojo.query(".js-acceptTerms").connect("click", function(evt) {
								dojo.stopEvent(evt);
								overlay.removeOverlay();
								lib.layer.remove("#carePAKTermsLayer");
								addtoCartLoadLayerProduct(form);
								//alert("Accept, do something TBD by Engineering Approach");
								dojo.xhrGet({
									url: "CusaAjaxOrderItemAdd",
									handleAs: "json",
									content: params,
									load: function(response,ioArgs) {									   
										console.log("Inside load of Ajax Call");						
										//Parse JSON response for errors
											var errorMessageKey =response.errorMessageKey;
										    var errorMessage =response.errorMessage;
									    	//Success scenario
											if(!errorMessageKey && ! errorMessage){   
											//Launch post success code 
											//Launch add to cart layer  with internationalized success text( e.g.successful add to cart) from the resource bundle
											//Begin layer code
													//alert("one");
													var defaultContent = "<div id='addcart-template' class='ie6png'><div class='content'><div id='information-box'><h2>Your Product Selection Has Been Added to the Cart.</h2><a class='js-layerClose' href='#close'>close</a><br /><br /><a href='CusaApplyBestShippingMode?updatePrices=1&calculationUsageId=-1&calculationUsageId=-2&calculationUsageId=-3&calculationUsageId=-4&calculationUsageId=-7&orderItemId*=&quantity*=&storeId="+storeId+"&catalogId="+catalogId+"&langId="+langId+"&orderId="+orderId+"&URL=OrderItemDisplay' class='right-arrow'> View Cart & Begin Checkout</a></div></div></div>";
													addtoCartLayerProduct(defaultContent,form,storeId,catalogId);
											} //End success scenario										
											//Failure scenario 
											if (errorMessage) {
													// Launch add to cart layer with errorMessage
													//alert("2");
													var defaultContent = "";
													if(errorMessageKey=='_API_BAD_INV'){
														defaultContent = "<div id='addcart-template' class='ie6png'><div class='content'><div id='information-box'><h2>There is not sufficient inventory for this purchase, and we are not accepting backorders on this product.</h2><a class='js-layerClose' href='#close'>close</a><br /><br /><a href='CusaApplyBestShippingMode?updatePrices=1&calculationUsageId=-1&calculationUsageId=-2&calculationUsageId=-3&calculationUsageId=-4&calculationUsageId=-7&orderItemId*=&quantity*=&storeId="+storeId+"&catalogId="+catalogId+"&langId="+langId+"&orderId="+orderId+"&URL=OrderItemDisplay' class='right-arrow'> View Cart & Begin Checkout</a></div></div></div>";						   
													}else{
														defaultContent = "<div id='addcart-template' class='ie6png'><div class='content'><div id='information-box'><h2>" + errorMessage+ " </h2><a class='js-layerClose' href='#close'>close</a><br /><br /><a href='CusaApplyBestShippingMode?updatePrices=1&calculationUsageId=-1&calculationUsageId=-2&calculationUsageId=-3&calculationUsageId=-4&calculationUsageId=-7&orderItemId*=&quantity*=&storeId="+storeId+"&catalogId="+catalogId+"&langId="+langId+"&orderId="+orderId+"&URL=OrderItemDisplay' class='right-arrow'> View Cart & Begin Checkout</a></div></div></div>";						   
													}
													addtoCartLayerProduct(defaultContent,form,storeId,catalogId);
											}else {
												if (errorMessageKey) {
													// Launch add to cart layer with errorMessageKey
													//Begin layer code
													//alert("3");
													var defaultContent = "<div id='addcart-template' class='ie6png'><div class='content'><div id='information-box'><h2>" + errorMessageKey  + " </h2><a class='js-layerClose' href='#close'>close</a><br /><br /><a href='CusaApplyBestShippingMode?updatePrices=1&calculationUsageId=-1&calculationUsageId=-2&calculationUsageId=-3&calculationUsageId=-4&calculationUsageId=-7&orderItemId*=&quantity*=&storeId="+storeId+"&catalogId="+catalogId+"&langId="+langId+"&orderId="+orderId+"&URL=OrderItemDisplay' class='right-arrow'> View Cart & Begin Checkout</a></div></div></div>";
													addtoCartLayerProduct(defaultContent,form,storeId,catalogId);
												}
											}
									}, //End of load 									
										//This code gets invoked on an error in AJAX communication
										error: function(error, args) {
											//log to console will log to the debug console
											//console.log("error!",error);
											// Launch add to cart layer with message ?Communication Failure?
									        	//alert("4");
									        	var defaultContent = "<div id='addcart-template' class='ie6png'><div class='content'><div id='information-box'><h2>"+ prodname +" could not be added to the cart. Please try again.</h2><a class='js-layerClose' href='#close'>close</a><br /><br /><a href='CusaApplyBestShippingMode?updatePrices=1&calculationUsageId=-1&calculationUsageId=-2&calculationUsageId=-3&calculationUsageId=-4&calculationUsageId=-7&orderItemId*=&quantity*=&storeId="+storeId+"&catalogId="+catalogId+"&langId="+langId+"&orderId="+orderId+"&URL=OrderItemDisplay' class='right-arrow'> View Cart & Begin Checkout</a></div></div></div>";
												addtoCartLayerProduct(defaultContent,form,storeId,catalogId);							
									    } //End of error
									
								}); //End dojoxhrGet call
																
							});
						}
					});
				/* END -- CarePAK Terms and Conditions Layer */
				
			}else{
				validQuantityFlg="4";
			}
			if(validQuantityFlg=="4"){
			dojo.xhrGet({
				url: "CusaAjaxOrderItemAdd",
				handleAs: "json",
				content: params,
				load: function(response,ioArgs) {
				
					//Parse JSON response for errors
						var errorMessageKey =response.errorMessageKey;
					    var errorMessage =response.errorMessage;
					
					
				    //Success scenario
					if(!errorMessageKey && ! errorMessage){
				
							 if((document.getElementById("typeOfProduct") != null) 
							 && (document.getElementById("typeOfProduct").value.indexOf('HARDWARE') != -1)
							 &&	(document.getElementById("availableReccAssociations") != null) 
							 && (document.getElementById("availableReccAssociations").value.indexOf('YES') != -1)) {
	
								tncUrl = "CusaAccessoryLayerDisplay?storeId="+storeId+"&catalogId="+catalogId+"&langId="+langId+"&productId="+productId;
								lib.layer.create("#addToCartAccessoryLayer", {
								url : tncUrl,
								keepCentered : true,
								nonModal: false,
								callback : function() {
										dojo.query("#addCartLayerProduct").fadeOut({ 
										delay: 0, 
										onEnd: function(){ dojo.query("#addCartLayerProduct").orphan(); }
									}).play();	
								
									/* Setup the Page Overlay */
									var overlay = new lib.obj.pageOverlay({ selector : "#pageOverlayLayer" });
								
									/* Setup the "decline" button */
									dojo.query(".js-layerClose").connect("click", function(evt) {
										dojo.stopEvent(evt);
										overlay.removeOverlay();
										lib.layer.remove("#addToCartAccessoryLayer");
									});
								}
								});
						  updateCount(storeId, catalogId);							
						  }else {
					  		var defaultContent = "<div id='addcart-template' class='ie6png'><div class='content'><div id='information-box'><h2>Your Product Selection Has Been Added to the Cart.</h2><a class='js-layerClose' href='#close'>close</a><br /><br /><a href='CusaApplyBestShippingMode?updatePrices=1&calculationUsageId=-1&calculationUsageId=-2&calculationUsageId=-3&calculationUsageId=-4&calculationUsageId=-7&orderItemId*=&quantity*=&storeId="+storeId+"&catalogId="+catalogId+"&langId="+langId+"&orderId="+orderId+"&URL=OrderItemDisplay' class='right-arrow'> View Cart & Begin Checkout</a></div></div></div>";
							addtoCartLayerProduct(defaultContent,form,storeId,catalogId);
					  	  } 
								
								/* END Add To Cart Accessory Layer */
					} //End success scenario
					
					//Failure scenario 
					if (errorMessage) {
							// Launch add to cart layer with errorMessage
							//alert("2");
							var defaultContent = "";
							if(errorMessageKey=='_API_BAD_INV') {
								defaultContent = "<div id='addcart-template' class='ie6png'><div class='content'><div id='information-box'><h2>There is not sufficient inventory for this purchase, and we are not accepting backorders on this product</h2><a class='js-layerClose' href='#close'>close</a><br /><br /><a href='CusaApplyBestShippingMode?updatePrices=1&calculationUsageId=-1&calculationUsageId=-2&calculationUsageId=-3&calculationUsageId=-4&calculationUsageId=-7&orderItemId*=&quantity*=&storeId="+storeId+"&catalogId="+catalogId+"&langId="+langId+"&orderId="+orderId+"&URL=OrderItemDisplay' class='right-arrow'> View Cart & Begin Checkout</a></div></div></div>";						   
							} else if(errorMessageKey== '_ERR_MISSING_COMMAND_PARAMETERS') {
								defaultContent = "<div id='addcart-template' class='ie6png'><div class='content'><div id='information-box'><h2>Please enter valid value in quantity field.</h2><a class='js-layerClose' href='#close'>close</a><br /><br /></div></div></div>";						   							
							} else {
								defaultContent = "<div id='addcart-template' class='ie6png'><div class='content'><div id='information-box'><h2>" + errorMessageKey + " </h2><a class='js-layerClose' href='#close'>close</a><br /><br /><a href='CusaApplyBestShippingMode?updatePrices=1&calculationUsageId=-1&calculationUsageId=-2&calculationUsageId=-3&calculationUsageId=-4&calculationUsageId=-7&orderItemId*=&quantity*=&storeId="+storeId+"&catalogId="+catalogId+"&langId="+langId+"&orderId="+orderId+"&URL=OrderItemDisplay' class='right-arrow'> View Cart & Begin Checkout</a></div></div></div>";						   
							}
							addtoCartLayerProduct(defaultContent,form,storeId,catalogId);
					}else {
						if (errorMessageKey) {
							// Launch add to cart layer with errorMessageKey
							//Begin layer code
							//alert("3");
							var defaultContent = "<div id='addcart-template' class='ie6png'><div class='content'><div id='information-box'><h2>" + errorMessageKey  + " </h2><a class='js-layerClose' href='#close'>close</a><br /><br /><a href='CusaApplyBestShippingMode?updatePrices=1&calculationUsageId=-1&calculationUsageId=-2&calculationUsageId=-3&calculationUsageId=-4&calculationUsageId=-7&orderItemId*=&quantity*=&storeId="+storeId+"&catalogId="+catalogId+"&langId="+langId+"&orderId="+orderId+"&URL=OrderItemDisplay' class='right-arrow'> View Cart & Begin Checkout</a></div></div></div>";
							addtoCartLayerProduct(defaultContent,form,storeId,catalogId);
						}
					}
				} , //End of load 
				
				//This code gets invoked on an error in AJAX communication
				error: function(error, args) {
					//log to console will log to the debug console
					//console.log("error!",error);
					// Launch add to cart layer with message ?Communication Failure?
			        	//alert("4");
			        	var defaultContent = "<div id='addcart-template' class='ie6png'><div class='content'><div id='information-box'><h2>"+ prodname +" could not be added to the cart. Please try again.</h2><a class='js-layerClose' href='#close'>close</a><br /><br /><a href='CusaApplyBestShippingMode?updatePrices=1&calculationUsageId=-1&calculationUsageId=-2&calculationUsageId=-3&calculationUsageId=-4&calculationUsageId=-7&orderItemId*=&quantity*=&storeId="+storeId+"&catalogId="+catalogId+"&langId="+langId+"&orderId="+orderId+"&URL=OrderItemDisplay' class='right-arrow'> View Cart & Begin Checkout</a></div></div></div>";
						addtoCartLayerProduct(defaultContent,form,storeId,catalogId);						
			    } //End of error
				
			}); //End dojoxhrGet call
			}
		}
// ***
// * This function accepts params product name and form and get all the elements of the form 
// * and add to the cart. There can be multiple Ink paper products associated to that product.
// * catalogId, storeId, langId needs to be passed to construct view cart and checkout urls
// * This function is used only for Product detail pages
// * WebSphere Commerce AJAX controller command
// ***		

		function AjaxEIDAdd2ShopCart(form, prodname, catalogId, storeId, langId) {
		 
		var addToCartChk;
		
		addToCartChk=addtoCartEasyInkFinder(form);
			 
		 if(addToCartChk)
		 {
		 	document.getElementById("quantity_error").className="";		
			document.getElementById("error_id").style.display="none";
		 	addtoCartLoadLayer();
            var orderId = ".";

			// Set up parameters needed to make the call
			var params = [];
			var validQuantityFlg = "1";	
			n = form.elements.length;
			var elementNeeded = new Array();
			//alert("number of form elments :: "+n);
			for (i=0; i<n; i++){			
				if(form.elements[i].value<0){
					var defaultContent = "<div id='addcart-template' class='ie6png'><div class='content'><div id='information-box'><h2>Invalid Quantity - Please enter valid value in quantity field (0 - 99)</h2><a class='js-layerClose' href='#close'>close</a><br /><br /></div></div></div>";
					addtoCartLayerProduct(defaultContent,form,storeId,catalogId);
					validQuantityFlg = "0";
					break;
				}
				if(form.elements[i].value>0){
					var makeCatEntry = "";
					var count=0;						
					sName = form.elements[i].name;
					//alert("sName :: "+sName);
					var temp = new Array();
					temp = sName.split('_');
					
					if(temp[0]=="quantity"){								
						count++;
						qtyElement = sName.substring(9,15);
						params[sName] =  form.elements[i].value;
						elementNeeded[count]=qtyElement;							
					}
					if(temp[0]=="catEntryId"){						
						for(a=0; a<elementNeeded.length; a++){
							var makeCatEntry = "catEntryId_"+elementNeeded[a];
							if(form.elements[i].name==makeCatEntry){
								params[form.elements[i].name] =  form.elements[i].value;					
							}
						}	
					}					
				}// end if							
			} // end for
			//alert(params);
			if(validQuantityFlg=="1"){
			dojo.xhrGet({
				url: "CusaAjaxOrderItemAdd",
				handleAs: "json",
				content: params,
				load: function(response,ioArgs) {
				   
					console.log("Inside load of Ajax Call");
	
					//Parse JSON response for errors
						var errorMessageKey =response.errorMessageKey;
					    var errorMessage =response.errorMessage;
					
					
				    //Success scenario
					if(!errorMessageKey && ! errorMessage){   
					//Launch post success code 
					//Launch add to cart layer  with internationalized success text( e.g.successful add to cart) from the resource bundle
					//Begin layer code
							//alert("one");
							var defaultContent = "<div id='addcart-template' class='ie6png'><div class='content'><div id='information-box'><h2>Your Product Selection Has Been Added to the Cart.</h2><a class='js-layerClose' href='#close'>close</a><br /><br /><a href='CusaApplyBestShippingMode?updatePrices=1&calculationUsageId=-1&calculationUsageId=-2&calculationUsageId=-3&calculationUsageId=-4&calculationUsageId=-7&orderItemId*=&quantity*=&storeId="+storeId+"&catalogId="+catalogId+"&langId="+langId+"&orderId="+orderId+"&URL=OrderItemDisplay' class='right-arrow'> View Cart & Begin Checkout</a></div></div></div>";
							addtoCartLayerProduct(defaultContent,form,storeId,catalogId);
					} //End success scenario
					
					//Failure scenario 
					if (errorMessage) {
							// Launch add to cart layer with errorMessage
							//alert("2");
							var defaultContent = "";
							if(errorMessageKey=='_API_BAD_INV'){
								defaultContent = "<div id='addcart-template' class='ie6png'><div class='content'><div id='information-box'><h2>There is not sufficient inventory for this purchase, and we are not accepting backorders on this product</h2><a class='js-layerClose' href='#close'>close</a><br /><br /><a href='CusaApplyBestShippingMode?updatePrices=1&calculationUsageId=-1&calculationUsageId=-2&calculationUsageId=-3&calculationUsageId=-4&calculationUsageId=-7&orderItemId*=&quantity*=&storeId="+storeId+"&catalogId="+catalogId+"&langId="+langId+"&orderId="+orderId+"&URL=OrderItemDisplay' class='right-arrow'> View Cart & Begin Checkout</a></div></div></div>";						   
							}else{
								defaultContent = "<div id='addcart-template' class='ie6png'><div class='content'><div id='information-box'><h2>ERROR: Invalid Quantity or Invalid Params. Please try again with the right values. </h2><a class='js-layerClose' href='#close'>close</a><br /><br /><a href='CusaApplyBestShippingMode?updatePrices=1&calculationUsageId=-1&calculationUsageId=-2&calculationUsageId=-3&calculationUsageId=-4&calculationUsageId=-7&orderItemId*=&quantity*=&storeId="+storeId+"&catalogId="+catalogId+"&langId="+langId+"&orderId="+orderId+"&URL=OrderItemDisplay' class='right-arrow'> View Cart & Begin Checkout</a></div></div></div>";						   
							}
							addtoCartLayerProduct(defaultContent,form,storeId,catalogId);
					}else {
						if (errorMessageKey) {
							// Launch add to cart layer with errorMessageKey
							//Begin layer code
							//alert("3");
							var defaultContent = "<div id='addcart-template' class='ie6png'><div class='content'><div id='information-box'><h2>" + errorMessageKey  + " </h2><a class='js-layerClose' href='#close'>close</a><br /><br /><a href='CusaApplyBestShippingMode?updatePrices=1&calculationUsageId=-1&calculationUsageId=-2&calculationUsageId=-3&calculationUsageId=-4&calculationUsageId=-7&orderItemId*=&quantity*=&storeId="+storeId+"&catalogId="+catalogId+"&langId="+langId+"&orderId="+orderId+"&URL=OrderItemDisplay' class='right-arrow'> View Cart & Begin Checkout</a></div></div></div>";
							addtoCartLayerProduct(defaultContent,form,storeId,catalogId);
						}
					}
				} , //End of load 
				
				//This code gets invoked on an error in AJAX communication
				error: function(error, args) {
					//log to console will log to the debug console
					//console.log("error!",error);
					// Launch add to cart layer with message ?Communication Failure?
			        	//alert("4");
			        	var defaultContent = "<div id='addcart-template' class='ie6png'><div class='content'><div id='information-box'><h2>"+ prodname +" could not be added to the cart. Please try again.</h2><a class='js-layerClose' href='#close'>close</a><br /><br /><a href='CusaApplyBestShippingMode?updatePrices=1&calculationUsageId=-1&calculationUsageId=-2&calculationUsageId=-3&calculationUsageId=-4&calculationUsageId=-7&orderItemId*=&quantity*=&storeId="+storeId+"&catalogId="+catalogId+"&langId="+langId+"&orderId="+orderId+"&URL=OrderItemDisplay' class='right-arrow'> View Cart & Begin Checkout</a></div></div></div>";
						addtoCartLayerProduct(defaultContent,form,storeId,catalogId);						
			    } //End of error
				
			}); //End dojoxhrGet call
			}
		  }
		  else
		  {
				//var defaultContent = "<div id='addcart-template' class='ie6png'><div class='content'><div id='information-box'><h2>Invalid Quantity - Please select atleast one product.</h2><a class='js-layerClose' href='#close'>close</a><br /><br /><a href='CusaApplyBestShippingMode?updatePrices=1&calculationUsageId=-1&orderItemId*=&quantity*=&storeId="+storeId+"&catalogId="+catalogId+"&langId="+langId+"&orderId="+orderId+"&URL=OrderItemDisplay' class='right-arrow'> View Cart & Begin Checkout</a></div></div></div>";
				//addtoCartLayer(defaultContent);
				document.getElementById("quantity_error").className="ErrorNotification ErrorText";		
				document.getElementById("error_id").style.display="";  			  }	
		}
		function addtoCartEasyInkFinder(form)
		{
			var addToCartChk = false;
			var n = form.elements.length;
			for (i=0; i<n; i++){	
				if(form.elements[i].checked == 1)
				{
					addToCartChk = true;
					break;
				}
			}
			return addToCartChk;
		}		
		
		function AjaxAdd2ShopCartRecycle(catEntryId, comment, catalogId, storeId, langId, field2) { 

			//evt.preventDefault();
			new lib.obj.pageOverlay({ selector : "#pageOverlayLayer" }).removeOverlay();
			lib.layer.remove("#layerTerms");
			//		alert("Accept, do something TBD by Engineering Approach");
			addtoCartLoadLayer();	
			var params = [];
			params["orderId"] = ".";
			params["catEntryId"] = catEntryId;
			params["quantity"] = 1;
			params["comment"] = comment;
			params["field2"] = field2;
			
				dojo.xhrGet({
				url: "CusaAjaxOrderItemAdd",
				handleAs: "json",
				content: params,
				load: function(response,ioArgs) {
				   
					console.log("Inside load of Ajax Call");
	
					//Parse JSON response for errors
						var errorMessageKey =response.errorMessageKey;
					    var errorMessage =response.errorMessage;
					
					
				    //Success scenario
					if(!errorMessageKey && ! errorMessage){   
					//Launch post success code 
					//Launch add to cart layer  with internationalized success text( e.g.successful add to cart) from the resource bundle
					//Begin layer code
					//		alert("one");		
							var defaultContent = "";						
							var defaultContent = "<div id='addcart-template' class='ie6png'><div class='content'><div id='information-box'><h2>Your Product Selection Has Been Added to the Cart.</h2><a class='js-layerClose' href='#close'>close</a><br /><br /><a href='CusaApplyBestShippingMode?updatePrices=1&calculationUsageId=-1&quantity=1&orderId=.&storeId="+storeId+"&catalogId="+catalogId+"&langId="+langId+"&URL=OrderItemDisplay' class='right-arrow'> View Cart & Begin Checkout</a></div></div></div>";
					//		var defaultContent = "<div id='addcart-template' class='ie6png'><div class='content'><div id='information-box'><h2>Your Product Selection Has Been Added to the Cart.</h2><a class='js-layerClose' href='#close'>close</a> View Cart & Begin Checkout</div></div></div>";
					//		alert("two");
							addtoCartLayer(defaultContent, storeId, catalogId);
					//		alert("333");
					} //End success scenario
					
					//Failure scenario 
					if (errorMessage) {
							// Launch add to cart layer with errorMessage
							//alert("2");
							var defaultContent = "";
							if(errorMessageKey=='_API_BAD_INV'){
								defaultContent = "<div id='addcart-template' class='ie6png'><div class='content'><div id='information-box'><h2>There is not sufficient inventory for this purchase, and we are not accepting backorders on this product.</h2><a class='js-layerClose' href='#close'>close</a><br /><br /><a href='CusaApplyBestShippingMode?updatePrices=1&calculationUsageId=-1&calculationUsageId=-2&calculationUsageId=-3&calculationUsageId=-4&calculationUsageId=-7&orderItemId*=&quantity*=&storeId="+storeId+"&catalogId="+catalogId+"&langId="+langId+"&orderId="+orderId+"&URL=OrderItemDisplay' class='right-arrow'> View Cart & Begin Checkout</a></div></div></div>";						   
							}else{
								defaultContent = "<div id='addcart-template' class='ie6png'><div class='content'><div id='information-box'><h2>" + errorMessage + " </h2><a class='js-layerClose' href='#close'>close</a><br /><br /><a href='CusaApplyBestShippingMode?updatePrices=1&calculationUsageId=-1&calculationUsageId=-2&calculationUsageId=-3&calculationUsageId=-4&calculationUsageId=-7&orderItemId*=&quantity*=&storeId="+storeId+"&catalogId="+catalogId+"&langId="+langId+"&orderId="+orderId+"&URL=OrderItemDisplay' class='right-arrow'> View Cart & Begin Checkout</a></div></div></div>";						   
							}
							addtoCartLayer(defaultContent, storeId, catalogId);
					}else {
						if (errorMessageKey) {
							// Launch add to cart layer with errorMessageKey
							//Begin layer code
							//alert("3");
							var defaultContent = "<div id='addcart-template' class='ie6png'><div class='content'><div id='information-box'><h2>" + errorMessageKey  + " </h2><a class='js-layerClose' href='#close'>close</a><br /><br /><a href='CusaApplyBestShippingMode?updatePrices=1&calculationUsageId=-1&calculationUsageId=-2&calculationUsageId=-3&calculationUsageId=-4&calculationUsageId=-7&orderItemId*=&quantity*=&storeId="+storeId+"&catalogId="+catalogId+"&langId="+langId+"&orderId="+orderId+"&URL=OrderItemDisplay' class='right-arrow'> View Cart & Begin Checkout</a></div></div></div>";
							addtoCartLayer(defaultContent, storeId, catalogId);
						}
					}
				} , //End of load 
				
				//This code gets invoked on an error in AJAX communication
				error: function(error, args) {
					//log to console will log to the debug console
					//console.log("error!",error);
					// Launch add to cart layer with message ?Communication Failure?
			        	//alert("4");
			        	var defaultContent = "<div id='addcart-template' class='ie6png'><div class='content'><div id='information-box'><h2>Service request error.</h2><a class='js-layerClose' href='#close'>close</a><br /><br /><a href='CusaApplyBestShippingMode?updatePrices=1&calculationUsageId=-1&calculationUsageId=-2&calculationUsageId=-3&calculationUsageId=-4&calculationUsageId=-7&orderItemId*=&quantity*=&storeId="+storeId+"&catalogId="+catalogId+"&langId="+langId+"&orderId="+orderId+"&URL=OrderItemDisplay' class='right-arrow'> View Cart & Begin Checkout</a></div></div></div>";
						addtoCartLayer(defaultContent, storeId, catalogId);
			    } //End of error
				
			}); //End dojoxhrGet call
			
			
		}  // end of AjaxAdd2ShopCartRecycle
		
		
		
		function AjaxAdd2ShopCartCarePAK(catEntryId, catalogId, storeId, langId, field2) { 

			//evt.preventDefault();
			new lib.obj.pageOverlay({ selector : "#pageOverlayLayer" }).removeOverlay();
			lib.layer.remove("#layerTerms");
			//		alert("Accept, do something TBD by Engineering Approach");
			addtoCartLoadLayer();	
			var params = [];
			params["orderId"] = ".";
			params["catEntryId"] = catEntryId;
			params["quantity"] = 1;
			params["field2"] = field2;
			
				dojo.xhrGet({
				url: "CusaAjaxOrderItemAdd",
				handleAs: "json",
				content: params,
				load: function(response,ioArgs) {
				   
					console.log("Inside load of Ajax Call");
	
					//Parse JSON response for errors
						var errorMessageKey =response.errorMessageKey;
					    var errorMessage =response.errorMessage;
					
					
				    //Success scenario
					if(!errorMessageKey && ! errorMessage){   
					//Launch post success code 
					//Launch add to cart layer  with internationalized success text( e.g.successful add to cart) from the resource bundle
					//Begin layer code
					//		alert("one");	
							var defaultContent = "";						
							var defaultContent = "<div id='addcart-template' class='ie6png'><div class='content'><div id='information-box'><h2>Your Product Selection Has Been Added to the Cart.</h2><a class='js-layerClose' href='#close'>close</a><br /><br /><a href='CusaApplyBestShippingMode?updatePrices=1&calculationUsageId=-1&quantity=1&orderId=.&storeId="+storeId+"&catalogId="+catalogId+"&langId="+langId+"&URL=OrderItemDisplay' class='right-arrow'> View Cart & Begin Checkout</a></div></div></div>";
					//		alert("two");
							addtoCartLayer(defaultContent, storeId, catalogId);
					//		alert("333");
					} //End success scenario
					
					//Failure scenario 
					if (errorMessage) {
							// Launch add to cart layer with errorMessage
							//alert("2");
							var defaultContent = "";
							if(errorMessageKey=='_API_BAD_INV'){
								defaultContent = "<div id='addcart-template' class='ie6png'><div class='content'><div id='information-box'><h2>There is not sufficient inventory for this purchase, and we are not accepting backorders on this product</h2><a class='js-layerClose' href='#close'>close</a><br /><br /><a href='CusaApplyBestShippingMode?updatePrices=1&calculationUsageId=-1&calculationUsageId=-2&calculationUsageId=-3&calculationUsageId=-4&calculationUsageId=-7&orderItemId*=&quantity*=&storeId="+storeId+"&catalogId="+catalogId+"&langId="+langId+"&orderId="+orderId+"&URL=OrderItemDisplay' class='right-arrow'> View Cart & Begin Checkout</a></div></div></div>";						   
							}else{
								defaultContent = "<div id='addcart-template' class='ie6png'><div class='content'><div id='information-box'><h2>" + errorMessage + " </h2><a class='js-layerClose' href='#close'>close</a><br /><br /><a href='CusaApplyBestShippingMode?updatePrices=1&calculationUsageId=-1&calculationUsageId=-2&calculationUsageId=-3&calculationUsageId=-4&calculationUsageId=-7&orderItemId*=&quantity*=&storeId="+storeId+"&catalogId="+catalogId+"&langId="+langId+"&orderId="+orderId+"&URL=OrderItemDisplay' class='right-arrow'> View Cart & Begin Checkout</a></div></div></div>";						   
							}
							addtoCartLayer(defaultContent, storeId, catalogId);
					}else {
						if (errorMessageKey) {
							// Launch add to cart layer with errorMessageKey
							//Begin layer code
							//alert("3");
							var defaultContent = "<div id='addcart-template' class='ie6png'><div class='content'><div id='information-box'><h2>" + errorMessageKey  + " </h2><a class='js-layerClose' href='#close'>close</a><br /><br /><a href='CusaApplyBestShippingMode?updatePrices=1&calculationUsageId=-1&calculationUsageId=-2&calculationUsageId=-3&calculationUsageId=-4&calculationUsageId=-7&orderItemId*=&quantity*=&storeId="+storeId+"&catalogId="+catalogId+"&langId="+langId+"&orderId="+orderId+"&URL=OrderItemDisplay' class='right-arrow'> View Cart & Begin Checkout</a></div></div></div>";
							addtoCartLayer(defaultContent, storeId, catalogId);
						}
					}
				} , //End of load 
				
				//This code gets invoked on an error in AJAX communication
				error: function(error, args) {
					//log to console will log to the debug console
					//console.log("error!",error); 
					// Launch add to cart layer with message ?Communication Failure?
			        	//alert("4");
			        	// Error Message modified for shipping address where products can't be shipped
			    //    	var defaultContent = "<div id='addcart-template' class='ie6png'><div class='content'><div id='information-box'><h2>Service request error.</h2><a class='js-layerClose' href='#close'>close</a><br /><br /><a href='CusaApplyBestShippingMode?updatePrices=1&calculationUsageId=-1&calculationUsageId=-2&calculationUsageId=-3&calculationUsageId=-4&calculationUsageId=-7&orderItemId*=&quantity*=&storeId="+storeId+"&catalogId="+catalogId+"&langId="+langId+"&orderId="+orderId+"&URL=OrderItemDisplay' class='right-arrow'> View Cart & Begin Checkout</a></div></div></div>";
			    		var defaultContent = "<div id='addcart-template' class='ie6png'><div class='content'><div id='information-box'><h2>Service request error. The reason could be - this product is not available for shipping to your State. Please change the address.</h2><a class='js-layerClose' href='#close'>close</a><br /><br /></div></div></div>";
						addtoCartLayer(defaultContent, storeId, catalogId);
			    } //End of error
				
			}); //End dojoxhrGet call
			
			
		}  // end of AjaxAdd2ShopCartCarePAK
		
// ***  Easy Reorder Add to Cart Ajax Call
// * This function accepts params product name and form and get all the elements of the form 
// * and add to the cart. There can be multiple lineitems associated to this USER
// * catalogId, storeId, langId needs to be passed to construct view cart and checkout urls
// * This function is used only for Easy Reorder page.
// * WebSphere Commerce AJAX controller command
// ***
		function AjaxAddEasyRO2ShopCart(form, catalogId, storeId, langId) { 
			// Easy Reorder Product selection Validation - Tapan
		
			if(checkProductSelection() == undefined ) { 
				document.getElementById("easyreorderSelectArea").style.display="block";
		   // 	document.easyreorder.easyreorderId.focus();
		     }
			
		if(checkProductSelection()!= undefined ) { 	
			document.getElementById("easyreorderSelectArea").style.display="none";
			addtoCartLoadLayer();
            var orderId = ".";
		//	alert('Call2');
			// Set up parameters needed to make the call
			var params = [];
			var validQuantityFlg = "1";
			var validCP = "0";
			var tncUrl = "";
			var cpType = "";
			var field2ConcatArray = new Array();
			var field2Concat = new String();
			n = document.easyreorder.elements.length;
			var elementNeeded = new Array();
		//	alert("number of form elments :: "+n);
			for (i=0; i<n; i++){			
				if(document.easyreorder.elements[i].value<0){
					var defaultContent = "<div id='addcart-template' class='ie6png'><div class='content'><div id='information-box'><h2>Invalid Quantity - Please enter valid value in quantity field (0 - 99)</h2><a class='js-layerClose' href='#close'>close</a><br /><br /></div></div></div>";
					addtoCartLayerProduct(defaultContent,form,storeId,catalogId);
					validQuantityFlg = "0";
					break;
				}
				if(document.easyreorder.elements[i].value>0){
					var makeCatEntry = "";
					var count=0;						
					sName = document.easyreorder.elements[i].name;
			//		alert("sName :: "+sName);
					var temp = new Array();
					temp = sName.split('_');
					
					if(temp[0]=="quantity"){		
			//		alert('inside quantity')	;					
						count++;
						qtyElement = sName.substring(9,15);
				//		alert("qtyElement :: "+qtyElement);
						params[sName] =  document.easyreorder.elements[i].value;
						elementNeeded[count]=qtyElement;							
					}
					if(temp[0]=="catEntryId"){			
			//		alert('inside catEntryId')	;			
						for(a=0; a<elementNeeded.length; a++){
							var makeCatEntry = "catEntryId_"+elementNeeded[a];
							if(document.easyreorder.elements[i].name==makeCatEntry){
								params[document.easyreorder.elements[i].name] =  document.easyreorder.elements[i].value;	
							}
						}	
					}					
					if(temp[0]=="field2"){						
						for(a=0; a<elementNeeded.length; a++){
							var makeField2 = "field2_"+elementNeeded[a];
							if(document.easyreorder.elements[i].name==makeField2){
								params[document.easyreorder.elements[i].name] =  document.easyreorder.elements[i].value;
								validCP = "1";
							}
						}	
					}
					if(temp[0]=="prdType"){						
						for(a=0; a<elementNeeded.length; a++){
							var makePrdType = "prdType_"+elementNeeded[a];
							if(document.easyreorder.elements[i].name == makePrdType){
								cpType =  document.easyreorder.elements[i].value;
							}
						}	
					}					
				}// end if							
			} // end for
			params['find'] =  "";			
			if(cpType=="2"){
				tncUrl = "CusaCarePAKTermsConditionsDisplay?storeId="+storeId+"&catalogId="+catalogId+"&langId="+langId+"&flag=detail&cptype=2";
			}else if (cpType=="1"){
				tncUrl = "CusaCarePAKTermsConditionsDisplay?storeId="+storeId+"&catalogId="+catalogId+"&langId="+langId+"&flag=detail&cptype=1";
			}else if (cpType=="3"){
				tncUrl = "CusaRecycleTermsConditionsDisplay?storeId="+storeId+"&catalogId="+catalogId+"&langId="+langId+"&flag=detail";
			}
			if(validCP=="1"){
				/* CarePAK Terms and Conditions Layer */
					lib.layer.create("#carePAKTermsLayer", {
						url : tncUrl,
						keepCentered : true,
						nonModal: false,
						callback : function() {
							dojo.query("#addCartLayerProduct").fadeOut({ 
								delay: 0, 
								onEnd: function(){ dojo.query("#addCartLayerProduct").orphan(); }
							}).play();	
							/* Setup the Print Link */
							lib.link.popupWindow("#js-printCarePAKTermsAndConditions", { width: 600, height: 600, scrollbars : 1 });
						
							/* Setup the Page Overlay */
							var overlay = new lib.obj.pageOverlay({ selector : "#pageOverlayLayer" });
						
							/* Setup teh "decline" button */
							dojo.query(".js-layerClose").connect("click", function(evt) {
								dojo.stopEvent(evt);
								overlay.removeOverlay();
								lib.layer.remove("#carePAKTermsLayer");
							});
							
							/* Setup teh "Accept button */
							dojo.query(".js-acceptTerms").connect("click", function(evt) {
								dojo.stopEvent(evt);
								overlay.removeOverlay();
								lib.layer.remove("#carePAKTermsLayer");
								addtoCartLoadLayerProduct(form);
								//alert("Accept, do something TBD by Engineering Approach");
								dojo.xhrGet({
									url: "CusaAjaxOrderItemAdd",
									handleAs: "json",
									content: params,
									load: function(response,ioArgs) {									   
										console.log("Inside load of Ajax Call");						
										//Parse JSON response for errors
											var errorMessageKey =response.errorMessageKey;
										    var errorMessage =response.errorMessage;
									    	//Success scenario
											if(!errorMessageKey && ! errorMessage){   
											//Launch post success code 
											//Launch add to cart layer  with internationalized success text( e.g.successful add to cart) from the resource bundle
											//Begin layer code
													//alert("one");
													var defaultContent = "<div id='addcart-template' class='ie6png'><div class='content'><div id='information-box'><h2>Your Product Selection Has Been Added to the Cart.</h2><a class='js-layerClose' href='#close'>close</a><br /><br /><a href='CusaApplyBestShippingMode?updatePrices=1&calculationUsageId=-1&calculationUsageId=-2&calculationUsageId=-3&calculationUsageId=-4&calculationUsageId=-7&orderItemId*=&quantity*=&storeId="+storeId+"&catalogId="+catalogId+"&langId="+langId+"&orderId="+orderId+"&URL=OrderItemDisplay' class='right-arrow'> View Cart & Begin Checkout</a></div></div></div>";
													addtoCartLayerProduct(defaultContent,form,storeId,catalogId);
											} //End success scenario										
											//Failure scenario 
											if (errorMessage) {
													// Launch add to cart layer with errorMessage
													//alert("2");
													var defaultContent = "";
													if(errorMessageKey=='_API_BAD_INV'){
														defaultContent = "<div id='addcart-template' class='ie6png'><div class='content'><div id='information-box'><h2>There is not sufficient inventory for this purchase, and we are not accepting backorders on this product.</h2><a class='js-layerClose' href='#close'>close</a><br /><br /><a href='CusaApplyBestShippingMode?updatePrices=1&calculationUsageId=-1&calculationUsageId=-2&calculationUsageId=-3&calculationUsageId=-4&calculationUsageId=-7&orderItemId*=&quantity*=&storeId="+storeId+"&catalogId="+catalogId+"&langId="+langId+"&orderId="+orderId+"&URL=OrderItemDisplay' class='right-arrow'> View Cart & Begin Checkout</a></div></div></div>";						   
													}else{
														defaultContent = "<div id='addcart-template' class='ie6png'><div class='content'><div id='information-box'><h2>" + errorMessage + " Please see that a product is selected. </h2><a class='js-layerClose' href='#close'>close</a><br /><br /><a href='CusaApplyBestShippingMode?updatePrices=1&calculationUsageId=-1&calculationUsageId=-2&calculationUsageId=-3&calculationUsageId=-4&calculationUsageId=-7&orderItemId*=&quantity*=&storeId="+storeId+"&catalogId="+catalogId+"&langId="+langId+"&orderId="+orderId+"&URL=OrderItemDisplay' class='right-arrow'> View Cart & Begin Checkout</a></div></div></div>";						   
													}
													addtoCartLayerProduct(defaultContent,form,storeId,catalogId);
											}else {
												if (errorMessageKey) {
													// Launch add to cart layer with errorMessageKey
													//Begin layer code
													//alert("3");
													var defaultContent = "<div id='addcart-template' class='ie6png'><div class='content'><div id='information-box'><h2>" + errorMessageKey  + " Please see that a product is selected. </h2><a class='js-layerClose' href='#close'>close</a><br /><br /><a href='CusaApplyBestShippingMode?updatePrices=1&calculationUsageId=-1&calculationUsageId=-2&calculationUsageId=-3&calculationUsageId=-4&calculationUsageId=-7&orderItemId*=&quantity*=&storeId="+storeId+"&catalogId="+catalogId+"&langId="+langId+"&orderId="+orderId+"&URL=OrderItemDisplay' class='right-arrow'> View Cart & Begin Checkout</a></div></div></div>";
													addtoCartLayerProduct(defaultContent,form,storeId,catalogId);
												}
											}
									}, //End of load 									
										//This code gets invoked on an error in AJAX communication
										error: function(error, args) {
											//log to console will log to the debug console
											//console.log("error!",error);
											// Launch add to cart layer with message ?Communication Failure?
									        	//alert("4");
									        	var defaultContent = "<div id='addcart-template' class='ie6png'><div class='content'><div id='information-box'><h2>"+ prodname +" could not be added to the cart. Please try again.</h2><a class='js-layerClose' href='#close'>close</a><br /><br /><a href='CusaApplyBestShippingMode?updatePrices=1&calculationUsageId=-1&calculationUsageId=-2&calculationUsageId=-3&calculationUsageId=-4&calculationUsageId=-7&orderItemId*=&quantity*=&storeId="+storeId+"&catalogId="+catalogId+"&langId="+langId+"&orderId="+orderId+"&URL=OrderItemDisplay' class='right-arrow'> View Cart & Begin Checkout</a></div></div></div>";
												addtoCartLayerProduct(defaultContent,form,storeId,catalogId);						
									    } //End of error
									
								}); //End dojoxhrGet call
																
							});
						}
					});
				/* END -- CarePAK Terms and Conditions Layer */
				
			}else{
				validQuantityFlg="4";
			}
			if(validQuantityFlg=="4"){
			dojo.xhrGet({
				url: "CusaAjaxOrderItemAdd",
				handleAs: "json",
				content: params,
				load: function(response,ioArgs) {
				   
					console.log("Inside load of Ajax Call");
	
					//Parse JSON response for errors
						var errorMessageKey =response.errorMessageKey;
					    var errorMessage =response.errorMessage;
					
					
				    //Success scenario
					if(!errorMessageKey && ! errorMessage){   
					//Launch post success code 
					//Launch add to cart layer  with internationalized success text( e.g.successful add to cart) from the resource bundle
					//Begin layer code
							//alert("one");
							var defaultContent = "<div id='addcart-template' class='ie6png'><div class='content'><div id='information-box'><h2>Your Product Selection Has Been Added to the Cart.</h2><a class='js-layerClose' href='#close'>close</a><br /><br /><a href='CusaApplyBestShippingMode?updatePrices=1&calculationUsageId=-1&calculationUsageId=-2&calculationUsageId=-3&calculationUsageId=-4&calculationUsageId=-7&orderItemId*=&quantity*=&storeId="+storeId+"&catalogId="+catalogId+"&langId="+langId+"&orderId="+orderId+"&URL=OrderItemDisplay' class='right-arrow'> View Cart & Begin Checkout</a></div></div></div>";
							addtoCartLayerProduct(defaultContent,form,storeId,catalogId);
					} //End success scenario
					
					//Failure scenario 
					if (errorMessage) {
							// Launch add to cart layer with errorMessage
							//alert("2");
							var defaultContent = "";
							if(errorMessageKey=='_API_BAD_INV'){
								defaultContent = "<div id='addcart-template' class='ie6png'><div class='content'><div id='information-box'><h2>There is not sufficient inventory for this purchase, and we are not accepting backorders on this product</h2><a class='js-layerClose' href='#close'>close</a><br /><br /><a href='CusaApplyBestShippingMode?updatePrices=1&calculationUsageId=-1&calculationUsageId=-2&calculationUsageId=-3&calculationUsageId=-4&calculationUsageId=-7&orderItemId*=&quantity*=&storeId="+storeId+"&catalogId="+catalogId+"&langId="+langId+"&orderId="+orderId+"&URL=OrderItemDisplay' class='right-arrow'> View Cart & Begin Checkout</a></div></div></div>";						   
							}else{
								defaultContent = "<div id='addcart-template' class='ie6png'><div class='content'><div id='information-box'><h2>" + errorMessage + "Please see that a product is selected. </h2><a class='js-layerClose' href='#close'>close</a><br /><br /><a href='CusaApplyBestShippingMode?updatePrices=1&calculationUsageId=-1&calculationUsageId=-2&calculationUsageId=-3&calculationUsageId=-4&calculationUsageId=-7&orderItemId*=&quantity*=&storeId="+storeId+"&catalogId="+catalogId+"&langId="+langId+"&orderId="+orderId+"&URL=OrderItemDisplay' class='right-arrow'> View Cart & Begin Checkout</a></div></div></div>";						   
							}
							addtoCartLayerProduct(defaultContent,form,storeId,catalogId);
					}else {
						if (errorMessageKey) {
							// Launch add to cart layer with errorMessageKey
							//Begin layer code
							//alert("3");
							var defaultContent = "<div id='addcart-template' class='ie6png'><div class='content'><div id='information-box'><h2>" + errorMessageKey  + " Please see that a product is selected.</h2><a class='js-layerClose' href='#close'>close</a><br /><br /><a href='CusaApplyBestShippingMode?updatePrices=1&calculationUsageId=-1&calculationUsageId=-2&calculationUsageId=-3&calculationUsageId=-4&calculationUsageId=-7&orderItemId*=&quantity*=&storeId="+storeId+"&catalogId="+catalogId+"&langId="+langId+"&orderId="+orderId+"&URL=OrderItemDisplay' class='right-arrow'> View Cart & Begin Checkout</a></div></div></div>";
							addtoCartLayerProduct(defaultContent,form,storeId,catalogId);
						}
					}
				} , //End of load 
				
				//This code gets invoked on an error in AJAX communication
				error: function(error, args) {
					//log to console will log to the debug console
					//console.log("error!",error);
					// Launch add to cart layer with message ?Communication Failure?
			        	//alert("4");
			        	var defaultContent = "<div id='addcart-template' class='ie6png'><div class='content'><div id='information-box'><h2>"+ prodname +" could not be added to the cart. Please try again.</h2><a class='js-layerClose' href='#close'>close</a><br /><br /><a href='CusaApplyBestShippingMode?updatePrices=1&calculationUsageId=-1&calculationUsageId=-2&calculationUsageId=-3&calculationUsageId=-4&calculationUsageId=-7&orderItemId*=&quantity*=&storeId="+storeId+"&catalogId="+catalogId+"&langId="+langId+"&orderId="+orderId+"&URL=OrderItemDisplay' class='right-arrow'> View Cart & Begin Checkout</a></div></div></div>";
						addtoCartLayerProduct(defaultContent,form,storeId,catalogId);							
			    } //End of error
				
			}); //End dojoxhrGet call
			}
		}	// if(checkProductSelection()!= undefined )	
	}
	
// ***  This method is specifically called when Add to Cart button is clicked on Add To Cart Accessory Layer
// * This function accepts params product name and form and get all the elements of the form 
// * and add to the cart. There can be multiple lineitems associated to this USER
// * catalogId, storeId, langId needs to be passed to construct view cart and checkout urls
// * This function is used only for Add To Cart Accessory layer.
// * WebSphere Commerce AJAX controller command
// ***

	function AjaxAddAccessories2ShopCart(form, catalogId, storeId, langId) {
		
		if(checkAccessorySelection(false,true)!= undefined ) {
			
			//addtoCartLoadLayer();

		    var orderId = ".";
			var params = [];
			var validQuantityFlg = "1";
			n = document.addAccessoriesForm.elements.length;
			var elementNeeded = new Array();
			
			for (i=0; i<n; i++){
				if(document.addAccessoriesForm.elements[i].value<0){
					var defaultContent = "<div id='addcart-template' class='ie6png'><div class='content'><div id='information-box'><h2>Invalid Quantity - Please enter valid value in quantity field (0 - 99)</h2><a class='js-layerClose' href='#close'>close</a><br /><br /></div></div></div>";
					addtoCartLayerProduct(defaultContent,form,storeId,catalogId);
					validQuantityFlg = "0";
					break;
				}
				if(document.addAccessoriesForm.elements[i].value>0){
					var makeCatEntry = "";
					var count=0;						
					sName = document.addAccessoriesForm.elements[i].name;
					var temp = new Array();
					temp = sName.split('_');
					
					if(temp[0]=="quantity"){
						count++;
						qtyElement = sName.substring(9,15);
						params[sName] =  document.addAccessoriesForm.elements[i].value;
						elementNeeded[count]=qtyElement;							
					}
					if(temp[0]=="catEntryId"){			
						for(a=0; a<elementNeeded.length; a++){
							var makeCatEntry = "catEntryId_"+elementNeeded[a];
							if(document.addAccessoriesForm.elements[i].name==makeCatEntry){
								params[document.addAccessoriesForm.elements[i].name] =  document.addAccessoriesForm.elements[i].value;
							}
						}	
					}					
				}// end if							
			} // end for
		
			addtoCartLoadLayer(); 
			params['find'] =  "";
			
			dojo.xhrGet({
				url: "CusaAjaxOrderItemAdd",
				handleAs: "json",
				content: params,
				load: function(response,ioArgs) {
	
					//Parse JSON response for errors
						var errorMessageKey =response.errorMessageKey;
					    var errorMessage =response.errorMessage;
					
					
				    //Success scenario
					if(!errorMessageKey && ! errorMessage){ 
						 
					//Launch post success code 
					//Launch add to cart layer  with internationalized success text( e.g.successful add to cart) from the resource bundle
					// Commented out the code to launch the add to cart with success text, instead just close the layers.
							
						//var defaultContent = "<div id='addcart-template' class='ie6png'><div class='content'><div id='information-box'><h2>Your Product Selection Has Been Added to the Cart.</h2><a class='js-layerClose' href='#close'>close</a><br /><br /><a href='CusaApplyBestShippingMode?updatePrices=1&calculationUsageId=-1&calculationUsageId=-2&calculationUsageId=-3&calculationUsageId=-4&calculationUsageId=-7&orderItemId*=&quantity*=&storeId="+storeId+"&catalogId="+catalogId+"&langId="+langId+"&orderId="+orderId+"&URL=OrderItemDisplay' class='right-arrow'> View Cart & Begin Checkout</a></div></div></div>";
						//addtoCartLayerProduct(defaultContent,form,storeId,catalogId);
						updateCount(storeId, catalogId);
						//document.getElementById('addCartLayer').style.display='none';
						dojo.query("#addCartLayer").fadeOut({ 
									delay: 0, 
									onEnd: function(){ dojo.query("#addCartLayer").orphan(); }
								}).play();
						dojo.query("#addCartLayerProduct").fadeOut({ 
									delay: 0, 
									onEnd: function(){ dojo.query("#addCartLayerProduct").orphan(); }
								}).play();		
						document.getElementById('pageOverlayLayer').style.display='none';
						document.getElementById('addToCartAccessoryLayer').style.display='none';
					} //End success scenario
					
					//Failure scenario 
					if (errorMessage) {
							// Launch add to cart layer with errorMessage
						
							var defaultContent = "";
							if(errorMessageKey=='_API_BAD_INV'){
								defaultContent = "<div id='addcart-template' class='ie6png'><div class='content'><div id='information-box'><h2>There is not sufficient inventory for this purchase, and we are not accepting backorders on this product</h2><a class='js-layerClose' href='#close'>close</a><br /><br /><a href='CusaApplyBestShippingMode?updatePrices=1&calculationUsageId=-1&calculationUsageId=-2&calculationUsageId=-3&calculationUsageId=-4&calculationUsageId=-7&orderItemId*=&quantity*=&storeId="+storeId+"&catalogId="+catalogId+"&langId="+langId+"&orderId="+orderId+"&URL=OrderItemDisplay' class='right-arrow'> View Cart & Begin Checkout</a></div></div></div>";						   
							}else{
								defaultContent = "<div id='addcart-template' class='ie6png'><div class='content'><div id='information-box'><h2>" + errorMessage + "Please see that a product is selected. </h2><a class='js-layerClose' href='#close'>close</a><br /><br /><a href='CusaApplyBestShippingMode?updatePrices=1&calculationUsageId=-1&calculationUsageId=-2&calculationUsageId=-3&calculationUsageId=-4&calculationUsageId=-7&orderItemId*=&quantity*=&storeId="+storeId+"&catalogId="+catalogId+"&langId="+langId+"&orderId="+orderId+"&URL=OrderItemDisplay' class='right-arrow'> View Cart & Begin Checkout</a></div></div></div>";						   
							}
							
							addtoCartLayerProduct(defaultContent,form,storeId,catalogId);
							document.getElementById('pageOverlayLayer').style.display='none';
							document.getElementById('addToCartAccessoryLayer').style.display='none';
					}else {
						if (errorMessageKey) {
							// Launch add to cart layer with errorMessageKey
							
							var defaultContent = "<div id='addcart-template' class='ie6png'><div class='content'><div id='information-box'><h2>" + errorMessageKey  + " Please see that a product is selected.</h2><a class='js-layerClose' href='#close'>close</a><br /><br /><a href='CusaApplyBestShippingMode?updatePrices=1&calculationUsageId=-1&calculationUsageId=-2&calculationUsageId=-3&calculationUsageId=-4&calculationUsageId=-7&orderItemId*=&quantity*=&storeId="+storeId+"&catalogId="+catalogId+"&langId="+langId+"&orderId="+orderId+"&URL=OrderItemDisplay' class='right-arrow'> View Cart & Begin Checkout</a></div></div></div>";
							addtoCartLayerProduct(defaultContent,form,storeId,catalogId);
							document.getElementById('pageOverlayLayer').style.display='none';
							document.getElementById('addToCartAccessoryLayer').style.display='none';
							
						}
					}
				} , //End of load 
				
				//This code gets invoked on an error in AJAX communication
				
				error: function(error, args) {
				    var defaultContent = "<div id='addcart-template' class='ie6png'><div class='content'><div id='information-box'><h2>"+ prodname +" could not be added to the cart. Please try again.</h2><a class='js-layerClose' href='#close'>close</a><br /><br /><a href='CusaApplyBestShippingMode?updatePrices=1&calculationUsageId=-1&calculationUsageId=-2&calculationUsageId=-3&calculationUsageId=-4&calculationUsageId=-7&orderItemId*=&quantity*=&storeId="+storeId+"&catalogId="+catalogId+"&langId="+langId+"&orderId="+orderId+"&URL=OrderItemDisplay' class='right-arrow'> View Cart & Begin Checkout</a></div></div></div>";
					addtoCartLayerProduct(defaultContent,form,storeId,catalogId);							
			    } //End of error
				
			}); //End dojoxhrGet call
		}	
	}
	
/* **  This method is specifically called when View Cart and Begin Checkout button is clicked on Add To Cart Accessory Layer
// * This function accepts params product name and form and get all the elements of the form 
// * and add to the cart. There can be multiple lineitems associated to this USER
// * catalogId, storeId, langId needs to be passed to construct view cart and checkout urls
// * This function is used only for Add To Cart Accessory layer.
// * WebSphere Commerce AJAX controller command
*/

	function AjaxAddAccessories2ShopCartAndCheckOut(form, catalogId, storeId, langId) {
		
		if(checkAccessorySelection(true,false)!= undefined ) {
	
			//addtoCartLoadLayer();

		    var orderId = ".";
			var params = [];
			var validQuantityFlg = "1";
			n = document.addAccessoriesForm.elements.length;
			var elementNeeded = new Array();
			var itemSelected = false;
			
			for (i=0; i<n; i++){
				if(document.addAccessoriesForm.elements[i].value<0){
					var defaultContent = "<div id='addcart-template' class='ie6png'><div class='content'><div id='information-box'><h2>Invalid Quantity - Please enter valid value in quantity field (0 - 99)</h2><a class='js-layerClose' href='#close'>close</a><br /><br /></div></div></div>";
					addtoCartLayerProduct(defaultContent,form,storeId,catalogId);
					validQuantityFlg = "0";
					break;
				}
				if(document.addAccessoriesForm.elements[i].value>0){
					var makeCatEntry = "";
					var count=0;						
					sName = document.addAccessoriesForm.elements[i].name;
					var temp = new Array();
					temp = sName.split('_');
					
					if(temp[0]=="quantity"){
						count++;
						qtyElement = sName.substring(9,15);
						params[sName] =  document.addAccessoriesForm.elements[i].value;
						elementNeeded[count]=qtyElement;							
					}
					if(temp[0]=="catEntryId"){			
						for(a=0; a<elementNeeded.length; a++){
							var makeCatEntry = "catEntryId_"+elementNeeded[a];
							if(document.addAccessoriesForm.elements[i].name==makeCatEntry){
								params[document.addAccessoriesForm.elements[i].name] =  document.addAccessoriesForm.elements[i].value;
								itemSelected = true;
							}
						}	
					}					
				}// end if							
			} // end for
			 
			params['find'] =  "";
			
			if(itemSelected == true)
			{
				dojo.xhrGet({
				url: "CusaAjaxOrderItemAdd",
				handleAs: "json",
				content: params,
				load: function(response,ioArgs) {
	
					var errorMessageKey =response.errorMessageKey;
				    var errorMessage =response.errorMessage;
					
					if(!errorMessageKey && ! errorMessage){ 
					
						var defaultContent = "<div id='addcart-template' class='ie6png'><div class='content'><div id='information-box'><h2>Your Accessory Selection Has Been Added to the Cart. Shopping Cart is Loading..</h2><a class='js-layerClose' href='#close'>close</a><br /><br /></div><div class='loadingimage'><img src='/wcsstore/eStore/images/loading.gif' alt='Loading...' /></div></div></div>";    
						//var redirectURL = "'CusaApplyBestShippingMode?updatePrices=1&calculationUsageId=-1&calculationUsageId=-2&calculationUsageId=-3&calculationUsageId=-4&calculationUsageId=-7&orderItemId*=&quantity*=&storeId="+storeId+"&catalogId="+catalogId+"&langId="+langId+"&orderId="+orderId+"&URL=OrderItemDisplay'";
						
						addtoCartLayerProduct(defaultContent,form,storeId,catalogId);
						
						updateShopCart(storeId , catalogId, langId);							
						//window.location.href = "CusaApplyBestShippingMode?updatePrices=1&calculationUsageId=-1&calculationUsageId=-2&calculationUsageId=-3&calculationUsageId=-4&calculationUsageId=-7&orderItemId*=&quantity*=&storeId=10051&catalogId=10051&langId=-1&orderId=.&URL=OrderItemDisplay";
						//window.location.href = redirectURL;
						//window.location.href = "CusaApplyBestShippingMode?updatePrices=1&calculationUsageId=-1&calculationUsageId=-2&calculationUsageId=-3&calculationUsageId=-4&calculationUsageId=-7&orderItemId*=&quantity*=&storeId="+storeId+"&catalogId="+catalogId+"langId="+langId+"&orderId="+orderId+"&URL=OrderItemDisplay";
						
					} 
					
					//Failure scenario 
					if (errorMessage) {

							// Launch add to cart layer with errorMessage
						
							var defaultContent = "";
							if(errorMessageKey=='_API_BAD_INV'){
								//defaultContent = "<div id='addcart-template' class='ie6png'><div class='content'><div id='information-box'><h2>There is not sufficient inventory for this purchase, and we are not accepting backorders on this product</h2><a class='js-layerClose' href='#close'>close</a><br /><br /><a href='CusaApplyBestShippingMode?updatePrices=1&calculationUsageId=-1&calculationUsageId=-2&calculationUsageId=-3&calculationUsageId=-4&calculationUsageId=-7&orderItemId*=&quantity*=&storeId="+storeId+"&catalogId="+catalogId+"&langId="+langId+"&orderId="+orderId+"&URL=OrderItemDisplay' class='right-arrow'> View Cart & Begin Checkout</a></div></div></div>";						   
								defaultContent = "<div id='addcart-template' class='ie6png'><div class='content'><div id='information-box'><h2>The items could not be added to your cart.  Please try again.</h2><a class='js-layerClose' href='#close'>close</a><br /><br /><a href='CusaApplyBestShippingMode?updatePrices=1&calculationUsageId=-1&calculationUsageId=-2&calculationUsageId=-3&calculationUsageId=-4&calculationUsageId=-7&orderItemId*=&quantity*=&storeId="+storeId+"&catalogId="+catalogId+"&langId="+langId+"&orderId="+orderId+"&URL=OrderItemDisplay' class='right-arrow'> View Cart & Begin Checkout</a></div></div></div>";						   
							}else{
								//defaultContent = "<div id='addcart-template' class='ie6png'><div class='content'><div id='information-box'><h2>" + errorMessage + "Please see that a product is selected. </h2><a class='js-layerClose' href='#close'>close</a><br /><br /><a href='CusaApplyBestShippingMode?updatePrices=1&calculationUsageId=-1&calculationUsageId=-2&calculationUsageId=-3&calculationUsageId=-4&calculationUsageId=-7&orderItemId*=&quantity*=&storeId="+storeId+"&catalogId="+catalogId+"&langId="+langId+"&orderId="+orderId+"&URL=OrderItemDisplay' class='right-arrow'> View Cart & Begin Checkout</a></div></div></div>";						   
								defaultContent = "<div id='addcart-template' class='ie6png'><div class='content'><div id='information-box'><h2>" + "The items could not be added to your cart.  Please try again.</h2><a class='js-layerClose' href='#close'>close</a><br /><br /><a href='CusaApplyBestShippingMode?updatePrices=1&calculationUsageId=-1&calculationUsageId=-2&calculationUsageId=-3&calculationUsageId=-4&calculationUsageId=-7&orderItemId*=&quantity*=&storeId="+storeId+"&catalogId="+catalogId+"&langId="+langId+"&orderId="+orderId+"&URL=OrderItemDisplay' class='right-arrow'> View Cart & Begin Checkout</a></div></div></div>";						   
							}
							
							addtoCartLayerProduct(defaultContent,form,storeId,catalogId);
							document.getElementById('pageOverlayLayer').style.display='none';
							document.getElementById('addToCartAccessoryLayer').style.display='none';
					}else {
						if (errorMessageKey) {
							// Launch add to cart layer with errorMessageKey
							
							//var defaultContent = "<div id='addcart-template' class='ie6png'><div class='content'><div id='information-box'><h2>" + errorMessageKey  + " Please see that a product is selected.</h2><a class='js-layerClose' href='#close'>close</a><br /><br /><a href='CusaApplyBestShippingMode?updatePrices=1&calculationUsageId=-1&calculationUsageId=-2&calculationUsageId=-3&calculationUsageId=-4&calculationUsageId=-7&orderItemId*=&quantity*=&storeId="+storeId+"&catalogId="+catalogId+"&langId="+langId+"&orderId="+orderId+"&URL=OrderItemDisplay' class='right-arrow'> View Cart & Begin Checkout</a></div></div></div>";
							var defaultContent = "<div id='addcart-template' class='ie6png'><div class='content'><div id='information-box'><h2>" + " The items could not be added to your cart.  Please try again.</h2><a class='js-layerClose' href='#close'>close</a><br /><br /><a href='CusaApplyBestShippingMode?updatePrices=1&calculationUsageId=-1&calculationUsageId=-2&calculationUsageId=-3&calculationUsageId=-4&calculationUsageId=-7&orderItemId*=&quantity*=&storeId="+storeId+"&catalogId="+catalogId+"&langId="+langId+"&orderId="+orderId+"&URL=OrderItemDisplay' class='right-arrow'> View Cart & Begin Checkout</a></div></div></div>";
							addtoCartLayerProduct(defaultContent,form,storeId,catalogId);
							document.getElementById('pageOverlayLayer').style.display='none';
							document.getElementById('addToCartAccessoryLayer').style.display='none';
						}
					}
				} , //End of load 
				
				//This code gets invoked on an error in AJAX communication
				
				error: function(error, args) {
				    //var defaultContent = "<div id='addcart-template' class='ie6png'><div class='content'><div id='information-box'><h2>"+ prodname +" could not be added to the cart. Please try again.</h2><a class='js-layerClose' href='#close'>close</a><br /><br /><a href='CusaApplyBestShippingMode?updatePrices=1&calculationUsageId=-1&calculationUsageId=-2&calculationUsageId=-3&calculationUsageId=-4&calculationUsageId=-7&orderItemId*=&quantity*=&storeId="+storeId+"&catalogId="+catalogId+"&langId="+langId+"&orderId="+orderId+"&URL=OrderItemDisplay' class='right-arrow'> View Cart & Begin Checkout</a></div></div></div>";
				    var defaultContent = "<div id='addcart-template' class='ie6png'><div class='content'><div id='information-box'><h2>"+ "The items could not be added to your cart.  Please try again.</h2><a class='js-layerClose' href='#close'>close</a><br /><br /><a href='CusaApplyBestShippingMode?updatePrices=1&calculationUsageId=-1&calculationUsageId=-2&calculationUsageId=-3&calculationUsageId=-4&calculationUsageId=-7&orderItemId*=&quantity*=&storeId="+storeId+"&catalogId="+catalogId+"&langId="+langId+"&orderId="+orderId+"&URL=OrderItemDisplay' class='right-arrow'> View Cart & Begin Checkout</a></div></div></div>";
					addtoCartLayerProduct(defaultContent,form,storeId,catalogId);							
			    } //End of error
		
			});
		  } 
		}
		else {
				//window.location.href = "CusaApplyBestShippingMode?updatePrices=1&calculationUsageId=-1&calculationUsageId=-2&calculationUsageId=-3&calculationUsageId=-4&calculationUsageId=-7&orderItemId*=&quantity*=&storeId=10051&catalogId=10051&langId=-1&orderId=.&URL=OrderItemDisplay";
				updateShopCart(storeId , catalogId, langId);	
		}	
	}
		

