﻿var CartHelper = Class.create();
CartHelper.prototype = 
{
	// Constructor
	initialize: function()
	{
		this.reloadAfterUpdate = false;
	},
	
	// Post Ajax request to handler to save the item details in the basket
	add: function( type, itemKey, quantity )
	{
		this._containerDiv = $( "AddToCartContainer_" + type + ":" + itemKey );
		this._addContainer = this._containerDiv.getElementsByTagName( "div" )[0];
		this._loadingContainer = this._containerDiv.getElementsByTagName( "div" )[1];
		this._completeContainer = this._containerDiv.getElementsByTagName( "div" )[2];
		
		// Hide add options & show loading
		Element.hide( this._addContainer );
		Element.show( this._loadingContainer );
		
		// Toggle Icons
		var addImage = this._addContainer.getElementsByTagName( "img" )[0];
		var removeImage = this._addContainer.getElementsByTagName( "img" )[1];
		
		if( quantity == 0 )
		{
			$( "Quantity_" + itemKey ).value = 1;
			addImage.src = "/Lib/Img/AddToCart.gif";
			removeImage.src = "/Lib/Img/RemoveFromCart_Hidden.gif";
			addImage.alt = "Add"
			addImage.title = "Add this item to your cart"
		}
		else
		{
			addImage.src = "/Lib/Img/UpdateQuantity.gif";
			removeImage.src = "/Lib/Img/RemoveFromCart.gif";
			addImage.alt = "Update"
			addImage.title = "Update the item within your cart"
		}
		
		this.update(type, itemKey, quantity);		
	},
	
	// Execute request to handle cart item
	update: function( type, itemKey, quantity )
	{
		var request = new Ajax.Request(
			'/AjaxHandlers/CartManager.ashx', {
			method: 'get',
			parameters: 'Type=' + type + '&ItemKey=' + itemKey + '&Quantity=' + quantity,
			onComplete: this._updateCart
		});
		
		var location = window.location.href;
		
		if (quantity > 0 || 
		    location.indexOf("/Products/") > 0 || 
		    location.indexOf("/Accessories/") > 0 ||
		    location.indexOf("/Ribbons/") > 0 ||
		    location.indexOf("/Labels/") > 0 ||
		    location.indexOf("self-adhesive-label-finder") > 0 ||
		    location.indexOf("thermal-transfer-ribbons") > 0)
		    
		{
			this._requestComplete();
		}
		
		var miniCartDiv = document.getElementById('miniCartDiv');
		if (miniCartDiv != null)
		{
			setTimeout('blink("miniCartDiv", 0);', 100);
		}
	},
	
	_updateCart: function(response)
	{
		var xml = response.responseXML;
		
		var Count, TotalCartCostExcludingVat, TotalCartCostIncludingVat, TotalCartVat, DeliveryCharge;
	
		if (navigator.appVersion.indexOf("MSIE") != -1)
		{
		    Count = xml.childNodes[0].childNodes[0].childNodes[0].firstChild.nodeValue;
			TotalCartCostExcludingVat = xml.childNodes[0].childNodes[0].childNodes[1].firstChild.nodeValue;
			TotalCartCostIncludingVat = xml.childNodes[0].childNodes[0].childNodes[2].firstChild.nodeValue;
			TotalCartVat = xml.childNodes[0].childNodes[0].childNodes[3].firstChild.nodeValue;
			DeliveryCharge = xml.childNodes[0].childNodes[0].childNodes[4].firstChild.nodeValue;
		}
		else
		{
		    Count = xml.childNodes[0].childNodes[1].childNodes[1].firstChild.nodeValue;
			TotalCartCostExcludingVat = xml.childNodes[0].childNodes[1].childNodes[3].firstChild.nodeValue;
			TotalCartCostIncludingVat = xml.childNodes[0].childNodes[1].childNodes[5].firstChild.nodeValue;
			TotalCartVat = xml.childNodes[0].childNodes[1].childNodes[7].firstChild.nodeValue;
			DeliveryCharge = xml.childNodes[0].childNodes[1].childNodes[9].firstChild.nodeValue;
		}	
		
		document.getElementById('BasketItemCount').innerHTML = Count;
		
		//-- Right about here will cause an error if you are on the product screen, however the broswer wont show this.
		
		document.getElementById('TotalCartCostExcludingVat').innerHTML = TotalCartCostExcludingVat;
		document.getElementById('DeliveryCharge').innerHTML = DeliveryCharge;
		document.getElementById('TotalCartVat').innerHTML = TotalCartVat;
		document.getElementById('TotalCartCostIncludingVat').innerHTML = TotalCartCostIncludingVat;

		if ((TotalCartCostExcludingVat == "£0.00"))
		{
		    document.getElementById('totals').style.display = 'none';
			document.getElementById('totals').style.visibility = 'hidden';
			document.getElementById('basketbuttons').style.display = 'none';
			document.getElementById('basketbuttons').style.visibility = 'hidden';
			
			document.getElementById('emptymessage').style.display = 'block';
			document.getElementById('emptymessage').style.visibility = 'visible';
			
			var googleCheckoutButton = document.getElementById('GoogleCheckoutButton');
			if (googleCheckoutButton != null)
			    googleCheckoutButton.style.display = 'none';
		}		
	},
	
	removeItem: function( type, itemKey, quantity, container )
	{
		if( confirm( "Are you sure you want to remove this item from your basket?" ) )
		{
		    RemoveElement( container );
			Cart.update( type, itemKey, quantity );
		}
	},
	
	_requestComplete: function()
	{
		// Hide loading container and show complete container
		Element.hide( this._loadingContainer );
		Element.show( this._completeContainer );
		
		// Hide complete container after 1 second
		window.setTimeout( "Cart.reset( '" + Cart._containerDiv.getAttribute( "id" ) + "' )", 500 );
	},
	
	// Reset - Show add options & hide everything else 
	reset: function( containerId )
	{
		var containerDiv = $( containerId );
		var addContainer = containerDiv.getElementsByTagName( "div" )[0];
		var loadingContainer = containerDiv.getElementsByTagName( "div" )[1];
		var completeContainer = containerDiv.getElementsByTagName( "div" )[2];
		
		Element.hide( loadingContainer, completeContainer );
		Element.show( addContainer );
	},
	
	updateMiniBasket: function()
	{
		var request = new Ajax.Updater(
			'BasketItemCount',
			'/AjaxHandlers/CartHelper.ashx', {
			method: 'get',
			parameters: 'Request=Count'
		});
	}
};

function add( type, itemKey, quantity )
{
    if (quantity > 1)
    {
        var img = document.getElementById('addupdate');
        img.src = '/Lib/Img/UpdateQuantity.gif';
        Cart.add( type, itemKey, quantity );
    }
}

function blink(elementId, count)
{
	if (count < 6)
	{
		document.getElementById("miniCartDiv").style.backgroundColor = count % 2 == 0 ? '#ffc05f' : '#ffffff';
		setTimeout('blink("' + elementId + '", ' + (count + 1) + ');', 300);
	}
}