/*
 * Author: Kent Heberling
 * Date: June 2009
 */
	var xmlhttp;
	
	// Add/remove cart item
	// First change the cart image and alt text accordingly
	// Make an xmlhttp call to addcart.do or cartRemove.do
	// Use this function if the sidebar cart needs to be updated
	function ajaxAddToCart(itemId, imageTag, siteUrl,itemName, itemPrice){
		$("#loading-" + itemId).show();
		if (imageTag.src == siteUrl + "/images/removefromcart.png"){
			var url="/ecom/cartRemove.do?itemId=" + itemId + "&forward=/ecom/ajaxResponse.jsp";	
		} else {
			var url="/ecom/cartAdd.do?itemId=" + itemId + "&forward=/ecom/ajaxResponse.jsp";
		}
		// Send request	
		sendXmlHttp(url,itemId, imageTag, siteUrl,itemName,itemPrice);	
	}
	
	// Add/remove cart item
	// First change the cart image and alt text accordingly
	// Make an xmlhttp call to addcart.do or cartRemove.do
	// Use this function on the search results pages, or anywhere the sidebar cart does not need to be updated
	function ajaxAddToCartSearchResults(itemId, imageTag, siteUrl){
		$("#loading-" + itemId).show();
		if (imageTag.src == siteUrl + "/images/removefromcart.png"){
			var url="/ecom/cartRemove.do?itemId=" + itemId + "&forward=/ecom/ajaxResponse.jsp";		
		} else {
			var url="/ecom/cartAdd.do?itemId=" + itemId + "&forward=/ecom/ajaxResponse.jsp";	
		}
		// Send request
		// Call sendXmlHttp with empty strings for item name and price - they are not needed but Javascript methods cannot be overloaded	
		sendXmlHttp(url,itemId, imageTag, siteUrl,'','');	
	}	
	
	// ==================================================================
	// Add item to wishlist
	// Change the wishlist image and alt text accordingly
	function ajaxAddToWishList(itemId, imageTag, siteUrl){
		$("#loading-" + itemId).show();
		
		if (imageTag.src == siteUrl + "/images/removefromwishlist.png"){
			var url="/ecom/removeItemFromWishList.do?itemid=" + itemId + "&forward=/ecom/ajaxResponse.jsp?";	
		} else {
			var url="/ecom/addItemToWishList.do?itemid=" + itemId + "&forward=/ecom/ajaxResponse.jsp?";
		}	
		//alert(url);				
		// Send request
		var requestFinished = false;		
		sendXmlHttp(url,itemId, imageTag, siteUrl,'','');			
	}
	
	// ==================================================================
	// Update the sidebar cart to show the newly added item
	// This is only used on Product Detail and Song Listing
	var hide;
	function updateSideBarCart(imageTag, siteUrl, itemId, itemName, itemPrice){	
    		// If the item is not already in the sidebar cart...
    		var cartHeader = document.getElementById("cartSidebarTop").innerHTML;
			cartHeader = cartHeader.replace(/(^\s*)/g, "");
			cartHeader = cartHeader.replace(/(\s*$)/g, "");
    		if (document.getElementById(itemId) == null){
    			// If removing the item from the sidebar cart...
    			if (hide == true){
					document.getElementById("newlyAddedItem").innerHTML = "";	
					// Decrement the items in cart counter
					var currentCount = parseInt(document.getElementById("sidebarCount").innerHTML);
					document.getElementById("sidebarCount").innerHTML = currentCount - 1;	
					hide = false;	
				} else {
					// If adding the item to the sidebar cart					    			
					document.getElementById("newlyAddedItem").innerHTML = '<a href="/product/viewproduct.do?itemid=' + itemId + '">' + unescape(itemName) + '- US $' + itemPrice + '</a>';
					// Check to see if the header reads "No items in cart"
					// Change this message to the link message if an item is added
					if ( cartHeader == "No items in cart"){
						document.getElementById("cartSidebarTop").innerHTML = '<a href="#" onclick="hideDisplayOptions(); return false;"><span id="sidebarCount">1</span> <span id="sidebarCountWord">ITEM</span> IN CART<br /><span id="cartSidebarTopClickText" style="display: inline;">(click to view)</span></a>';
					} else {
						// Increment the items in cart counter
						var currentCount = parseInt(document.getElementById("sidebarCount").innerHTML);
						document.getElementById("sidebarCount").innerHTML = currentCount + 1;	
					}
					hide = true;
				}	
    		} 
    		// If the item is already in the sidebar cart...
    		else {	
	    		if (imageTag.src == siteUrl + "/images/removefromcart.png"){
	    			document.getElementById(itemId).style.display = "inline";
					// Increment the items in cart counter				
					if ( cartHeader == "No items in cart"){
						document.getElementById("cartSidebarTop").innerHTML = '<a href="#" onclick="hideDisplayOptions(); return false;"><span id="sidebarCount">1</span> <span id="sidebarCountWord">ITEM</span> IN CART<br /><span id="cartSidebarTopClickText" style="display: inline;">(click to view)</span></a>';
					} else {
						// Increment the items in cart counter
						var currentCount = parseInt(document.getElementById("sidebarCount").innerHTML);
						document.getElementById("sidebarCount").innerHTML = currentCount + 1;	
					}					  
				} else {
					document.getElementById(itemId).style.display = "none";
					// Decrement the items in cart counter
					var currentCount = parseInt(document.getElementById("sidebarCount").innerHTML);
					document.getElementById("sidebarCount").innerHTML = currentCount - 1;					
				}
			}	
			
			// Make sure the correct messages are showing	
			if (parseInt(document.getElementById("sidebarCount").innerHTML) == 0){
				document.getElementById("cartSidebarTop").innerHTML = "No items in cart";
				// Fade the sidebar cart up if it is currently down and the cart now contains no items
				if (show == false){
					// show variable comes from productDetail.jsp and is toggled by the toggle function
					$("#cartSidebarTable").animate({ height: 'hide' }, 'slow');
					show = true;
				}
			} else {
				if (parseInt(document.getElementById("sidebarCount").innerHTML) == 1){
					document.getElementById("sidebarCountWord").innerHTML = "ITEM";
				} else {
					document.getElementById("sidebarCountWord").innerHTML = "ITEMS";
				}	
			}
		}	
		
	// ==================================================================
	// Send an XML-HTTP request given a URL
	function sendXmlHttp(url,itemId, imageTag, siteUrl,itemName, itemPrice){
		xmlhttp=GetXmlHttpObject();
		if (xmlhttp==null){
		  alert ("Your browser does not support XMLHTTP!");
		  return;
		}		
		url=url+"&sid="+Math.random();
		//xmlhttp.onreadystatechange=stateChanged;
		xmlhttp.onreadystatechange=function () {
            stateChanged(itemId, imageTag, siteUrl,itemName,itemPrice);
        };
		xmlhttp.open("GET",url,true);
		xmlhttp.send(null);	
	}		
	
	// ==================================================================
	// Track changes on the AJAX call
	function stateChanged(itemId, imageTag, siteUrl,itemName, itemPrice){
		if (xmlhttp.readyState==4){ 
		  var response = xmlhttp.responseText;
		  //alert(response);
		  // If a successful cart action took place
		  if (response.indexOf("CARTSUCCESS") != -1){
			    if (imageTag.src == siteUrl + "/images/removefromcart.png"){
					imageTag.src = "/images/addtocart.png";
					imageTag.alt = "Add to cart";
					
				} else {
					imageTag.src = "/images/removefromcart.png";
					imageTag.alt = "Remove from cart";		
			 	}
				// Inititialize the unitip pop up again to get the updated alt text
				init();					 	
			 	// Update the sidebar cart if it is present
				if (document.getElementById("cartSidebarTop") != null ){
					updateSideBarCart(imageTag, siteUrl, itemId, itemName, itemPrice);
				}		  	
		  } 	  	
		  
		  // If a successful wishlist action took place
		  if (response.indexOf("WISHLISTSUCCESS") != -1){
			if (imageTag.src == siteUrl + "/images/removefromwishlist.png"){
				imageTag.src = "/images/addtowishlist.png";
				imageTag.alt = "Add to wishlist"; 
			} else {
				imageTag.src = "/images/removefromwishlist.png";
				imageTag.alt = "Remove from wishlist"; 
			}
			// Inititialize the unitip pop up again to get the updated alt text
			init();
		  }
		  $("#loading-" + itemId).hide();
		}
	}
	
	// ==================================================================
	// Generate an XML-HTTP object
	function GetXmlHttpObject(){
	if (window.XMLHttpRequest)
	  {
	  // code for IE7+, Firefox, Chrome, Opera, Safari
	  return new XMLHttpRequest();
	  }
	if (window.ActiveXObject)
	  {
	  // code for IE6, IE5
	  return new ActiveXObject("Microsoft.XMLHTTP");
	  }
	return null;
	}	