// AddtoWishList.js


// ***
// * This javascript function is used by the 'Add to Wish List' button for creating the loading iamge layer during 
// *  the duration of AJAX call to the backend WebSphere Commerce AJAX controller command
// ***
function add2WishListLoadLayer(){
				//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
						});//End layer code				
}
		
// ***
// * This javascript function is used by the 'AAdd to Wish List' button for creating the overlay(layer) upon 
// * completeion of AJAX call to the backend WebSphere Commerce AJAX controller command
// ***
function add2WishListLayer(defaultContent){
				//Update header totals
				//		updateCount();
				//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",
							//xPos : x,
							//yPos : y,
							keepCentered : true,
							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 Wish List' button for submitting AJAX request to backend
// * WebSphere Commerce AJAX controller command
// ***
function AjaxAdd2WishList(catEntryId,catalogId,landId,storeId,prodname) { 
		
			
            add2WishListLoadLayer();
            var prodname= prodname;
			// Set up parameters needed to make the call
			var params = [];
			params.URL = "SuccessfulAJAXRequest";
			params.catEntryId =catEntryId;
			params.catalogId = catalogId;
			params.landId = landId;
			params.storeId = storeId;
			dojo.xhrGet({
				url: "AjaxInterestItemAdd",
				handleAs: "json",
				content: params,
				load: function(response,ioArgs) {
			    //alert("Inside load");
				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
						var defaultContent = "<div id='addcart-template' class='ie6png'><div class='content'><div id='information-box'><h2>Your Item " + prodname + " has been added to the wish list.</h2><a class='js-layerClose' href='#close'>close</a><br /></div></div></div>";
						 add2WishListLayer(defaultContent);
					} //End success scenario
				
				
	            //Failure scenario 
					if (errorMessage) {
						// Launch add to cart layer with errorMessage
						var defaultContent = "<div id='addcart-template' class='ie6png'><div class='content'><div id='information-box'><h2>Error message is:" + errorMessage + " </h2><a class='js-layerClose' href='#close'>close</a><br /><br /></div></div></div>";						   
						 add2WishListLayer(defaultContent);
					}
					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>Error message is:" + errorMessageKey  + " </h2><a class='js-layerClose' href='#close'>close</a></div></div></div>";
							 add2WishListLayer(defaultContent);
							 }

					}
				

					} , //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>Service request error.</h2><a class='js-layerClose' href='#close'>close</a></div></div></div>";
							 add2WishListLayer(defaultContent);							
						

					    } //End of error
				
					}); //End dojoxhrGet call
								  
				  
              
       }
       
       
// ***
// * This javascript function is used by the 'Add to My Products' button for submitting AJAX request to backend
// * WebSphere Commerce AJAX controller command
// ***
function AjaxAdd2MyProducts(catEntryId,catalogId,landId,storeId,prodname,field1) { 
		
		    add2WishListLoadLayer();
            var prodname= prodname;
			// Set up parameters needed to make the call
			var params = [];
			params.URL = "SuccessfulAJAXRequest";
			params.catEntryId =catEntryId;
			params.catalogId = catalogId;
			params.landId = landId;
			params.storeId = storeId;
			params.field1 = field1;
			dojo.xhrGet({
				url: "AjaxInterestItemAdd",
				handleAs: "json",
				content: params,
				load: function(response,ioArgs) {
			    //alert("Inside load");
				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
						var defaultContent = "<div id='addcart-template' class='ie6png'><div class='content'><div id='information-box'><h2>Your Item " + prodname + " has been added to your my Products list.</h2><a class='js-layerClose' href='#close'>close</a><br /></div></div></div>";
						 add2WishListLayer(defaultContent);
					} //End success scenario
				
				
	            //Failure scenario 
					if (errorMessage) {
						// Launch add to cart layer with errorMessage
						var defaultContent = "<div id='addcart-template' class='ie6png'><div class='content'><div id='information-box'><h2>Error message is:" + errorMessage + " </h2><a class='js-layerClose' href='#close'>close</a><br /><br /></div></div></div>";						   
						 add2WishListLayer(defaultContent);
					}
					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>Error message is:" + errorMessageKey  + " </h2><a class='js-layerClose' href='#close'>close</a></div></div></div>";
							 add2WishListLayer(defaultContent);
							 }

					}
				

					} , //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>Service request error.</h2><a class='js-layerClose' href='#close'>close</a></div></div></div>";
							 add2WishListLayer(defaultContent);							
						

					    } //End of error
				
					}); //End dojoxhrGet call
								  
				  
              
       }
