﻿/*----------------------------------------------------------------------------
 Project:			OnlineShopDotNet
 Source File:		ProductContainer.js
 Company:     		Ascent Technology Limited
 Author:      		John Mills
 Date:        		29/11/2007
 Purpose:     		This file contains the AJAX client side code for the
						ProductContainer classes.
 Notes:       		
------------------------------------------------------------------------------
 John Mills     	Created.
 29/11/2007        
------------------------------------------------------------------------------
 John Mills     	Refactored code to have a ProductContainer base class and
 24/12/2007         added the QuickCompare class.
------------------------------------------------------------------------------
 John Mills     	Completed and tidied the ProductContainer classes.
 28/12/2007
------------------------------------------------------------------------------
 John Mills     	Fixed incompatibilities between IE, Firefox and Opera.
 08/01/2007         It now works properly in all three browsers.
------------------------------------------------------------------------------
 John Mills     	Fixed another incompatibility between IE, Firefox and 
 09/01/2007         Opera. The Items link now works properly in all three 
                    browsers.
------------------------------------------------------------------------------
 John Mills     	Fixed a bug that prevented the shopping cart from updating
 09/01/2007         the Sub Total and GST fields when in GST Exclusive mode.
----------------------------------------------------------------------------*/

Type.registerNamespace('AscentTechnologyLimited.OnlineShop.Controls');

AscentTechnologyLimited.OnlineShop.Controls.ProductContainer = function(element) { 
    AscentTechnologyLimited.OnlineShop.Controls.ProductContainer.initializeBase(this, [element]);
	
	this._cart = null;
	this._emptyCart = null;
	this._itemsPanel = null;
	this._itemsLink = null;
	this._showHideButton = null;
	this._totalQuantity = null;
	this.__itemsLinkClickHandler = null;
	
}

AscentTechnologyLimited.OnlineShop.Controls.ProductContainer.prototype = {

	initialize : function() {
		
		AscentTechnologyLimited.OnlineShop.Controls.ProductContainer.callBaseMethod(this, 'initialize');
		
		this.__itemsLinkClickHandler = Function.createDelegate(this, this._itemsLinkClickHandler);
		$addHandler(this._itemsLink, 'click', this.__itemsLinkClickHandler);
		
	},
    
   dispose : function() {
		       
		AscentTechnologyLimited.OnlineShop.Controls.ProductContainer.callBaseMethod(this, 'dispose');
		
		$clearHandlers(this.get_itemsLink());
		
		if (this.__itemsLinkClickHandler) 
		{
			delete this.__itemsLinkClickHandler;
			this.__itemsLinkClickHandler = null;
		}
	
	},

	get_itemsPanel : function()
	{
		return this._itemsPanel;
	},

	set_itemsPanel : function(value)
	{
		this._itemsPanel = value;
	},

	get_cart : function()
	{
		return this._cart;
	},

	set_cart : function(value)
	{
		this._cart = value;
	},

	get_emptyCart : function()
	{
		return this._emptyCart;
	},

	set_emptyCart : function(value)
	{
		this._emptyCart = value;
	},

	get_itemsLink : function()
	{
		return this._itemsLink;
	},

	set_itemsLink : function(value)
	{
		this._itemsLink = value;
	},

	get_showHideButton : function()
	{
		return this._showHideButton;
	},

	set_showHideButton : function(value)
	{
		this._showHideButton = value;
	},

	get_totalQuantity : function()
	{
		return this._totalQuantity;
	},

	set_totalQuantity : function(value)
	{
		this._totalQuantity = value;
	},

	_itemsLinkClickHandler : function(sender)
	{

		this._showHideButton.onclick();
		
		return false;
		
	},
	
	_clearItems : function(table)
	{
				
		for (counter = table.rows.length, counter > 0; counter--;)
		{
			table.deleteRow(counter);
		}

	},
		
	_refreshItems : function(items)
	{
	
	   var nodes = this._itemsPanel.childNodes;
		var table;

		for (counter = 0; counter < nodes.length; counter++)
		{
		    if (nodes[counter].tagName == 'TABLE')
		    {
		        table = nodes[counter];
		        break;
		    }
		}

		if (table) 
		{
		    this._clearItems(table);	
	
		    for (counter = 0; counter < items.length; counter++)
		    {
			    this._createItem(table, items[counter]);
		    }
		}
	
	},
	
	_createItem : function(table, item)
	{
			
	},
	
	_showHideElement : function(element, show)
	{
		
		var visibility;
		var display;
		
		if (show)
		{
			visibility = 'visible';
			display = 'block';
		}
		else
		{
			visibility = 'hidden';
			display = 'none';
		}
		
		element.style.visibility = visibility;
		element.style.display = display;
		
	},
	
	_showHideItemsPanel : function(show)
	{
		
		this._showHideElement(this._itemsPanel, show);
		
	},

	_showHideCart : function(show)
	{
		
		this._showHideElement(this._cart, show);
		
	},
	
	_showHideEmptyCart : function(show)
	{
		
		if (this._emptyCart) this._showHideElement(this._emptyCart, show);
		
	},
	
	_showHideShowHideButton : function(show)
	{
		
		this._showHideElement(this._showHideButton, show);
		
	},
	
	add_requestEnded : function(handler)
	{
		this.get_events().addHandler('requestEnded', handler);
	},
	
	remove_requestEnded : function(handler)
	{
		this.get_events().removeHandler('requestEnded', handler);
	},

	_onRequestEnded : function()
	{
		var handler = this.get_events().getHandler('requestEnded');
      if (handler) handler(this, Sys.EventArgs.Empty);
	}
	
}

AscentTechnologyLimited.OnlineShop.Controls.ProductContainer.registerClass('AscentTechnologyLimited.OnlineShop.Controls.ProductContainer', Sys.UI.Control);

AscentTechnologyLimited.OnlineShop.Controls.ShoppingCart = function(element) { 
    AscentTechnologyLimited.OnlineShop.Controls.ShoppingCart.initializeBase(this, [element]);
	
	this._cart = null;
	this._emptyCart = null;
	this._itemsLink = null;
	this._showHideButton = null;
}

AscentTechnologyLimited.OnlineShop.Controls.ShoppingCart.prototype = {

	initialize : function() {
		
		AscentTechnologyLimited.OnlineShop.Controls.ShoppingCart.callBaseMethod(this, 'initialize');
		
		this._ServiceSucceededHandler = Function.createDelegate(this, this.ServiceSucceededHandler);
		this._ServiceFailedHandler = Function.createDelegate(this, this.ServiceFailedHandler);

	},
    
   dispose : function() {
		       
		AscentTechnologyLimited.OnlineShop.Controls.ShoppingCart.callBaseMethod(this, 'dispose');
	
	},

	get_subTotal : function()
	{
		return this._subTotal;
	},

	set_subTotal : function(value)
	{
		this._subTotal = value;
	},

	get_gst : function()
	{
		return this._gst;
	},

	set_gst : function(value)
	{
		this._gst = value;
	},

	get_total : function()
	{
		return this._total;
	},

	set_total : function(value)
	{
		this._total = value;
	},

	add : function(moniqueId, quantity, extendedWarrantyYearCount)
	{

		this._beginRequest(this);
		Services.ShoppingCart.Add(moniqueId, quantity, extendedWarrantyYearCount, 
			this._ServiceSucceededHandler, this._ServiceFailedHandler, this);
	},

	refresh : function()
	{

		this._beginRequest(control);
		Services.ShoppingCart.GetItems(this._ServiceSucceededHandler, 
			this._ServiceFailedHandler, this);
	},

	_refresh : function(result)
	{
	
		var hasItems = (result.Items.length !== 0);
	
		this._totalQuantity.innerHTML = result.TotalQuantity;
		this._subTotal.innerHTML = result.SubTotal;
		this._gst.innerHTML = result.Gst;
		this._total.innerHTML = result.Total;
		
		this._showHideShowHideButton(hasItems);
		this._showHideEmptyCart(!hasItems);
		this._showHideCart(hasItems);
		this._showHideSubTotal(hasItems & !result.ShowGst);
		this._showHideGst(hasItems & !result.ShowGst);
		this._showHideTotal(hasItems);
		
		this._refreshItems(result.Items);
	},
	
	_refreshItems : function(items)
	{
	   var nodes = this._itemsPanel.childNodes;
		var table;


		for (counter = 0; counter < nodes.length; counter++)
		{
		    if (nodes[counter].tagName == 'TABLE')
		    {
		         table = nodes[counter];
		         break;
		    }
		}

		if (table) 
		{

		    this._clearItems(table);	
 table.style.width = "200px"
		    for (counter = 0; counter < items.length; counter++)
		    {
			    this._createItem(table, items[counter]);
		    }
	    }
	},
	
	_createItem : function(table, item)
	{

		var row;
		var cell;
		var link;

		row = table.insertRow(-1);
		cell = row.insertCell(-1);
		link = document.createElement('a');
		cell.appendChild(link);
		if (item.MoniqueID !== null)
			link.href = 'productspecification.aspx?ItemID=' + item.MoniqueID;
		else					
			link.href = 'pcconfigs.aspx?ConfigID=' + item.ProductContainerID;
		link.innerHTML = item.Name;
		if (item.HasQuantityDiscount || item.HasCouponDiscount || item.HasDailyDealDiscount) this._createDiscounts(cell, item);
		cell = row.insertCell(-1);
		cell.innerText = item.Quantity;
			
	},
	
	_createDiscounts : function(cell, item)
	{
			
		var table = document.createElement('table');
		
		table.cellspacing = 0;
		table.cellpadding = 0;
		table.className = 'ShoppingCartItemDiscount';
		cell.appendChild(table);

		if (item.HasCouponDiscount)
		{
			this._createDiscount(table, 'Coupon', item.CouponDiscount, 'media/ui/icon_coupon_discount.gif');
		}

		if (item.HasDailyDealDiscount)
		{
			this._createDiscount(table, 'Daily Deal', item.DailyDealDiscount, 'media/ui/icon_coupon_discount.gif');
		}

		if (item.HasQuantityDiscount)
		{
			this._createDiscount(table, 'Quantity', item.QuantityDiscount, 'media/ui/icon_qty_discount.gif');
		}
	
	},
	
	_createDiscount : function(table, discountType, amount, imageUrl)
	{

		var row;
		var image;

		row = table.insertRow(-1);

		cell = row.insertCell(-1);
		image = document.createElement('img');
		image.src = imageUrl;
		cell.appendChild(image);

		cell = row.insertCell(-1);
		cell.innerHTML = discountType + ' discount of ' + amount + ' applies to this item';

	},
	
	_showHideSubTotal : function(show)
	{
		
		this._showHideElement(this._subTotal.parentNode.parentNode, show);
		
	},

	_showHideGst : function(show)
	{
		
		this._showHideElement(this._gst.parentNode.parentNode, show);
		
	},

	_showHideTotal : function(show)
	{
		
		this._showHideElement(this._total.parentNode.parentNode, show);
		
	},

	_beginRequest : function(shoppingCart)
	{
	
		var element = shoppingCart.get_element();
	
		element.disabled = true;	
		
	},
	
	_endRequest : function(shoppingCart)
	{
		
		var element = shoppingCart.get_element();
	
		element.disabled = false;
		this._onRequestEnded();
		
	},

	ServiceSucceededHandler : function (result, shoppingCart)
	{
		 
		if (result.Success)
		{
			this._refresh(result);
		}
		this._endRequest(shoppingCart);
		
	},

	ServiceFailedHandler : function (result, shoppingCart)
	{
	
		this._endRequest(shoppingCart);
	
	}
	
}

var _shoppingCartId = null;

function AscentTechnologyLimited$OnlineShop$Controls$ShoppingCart$get_Current()
{
	return $find(_shoppingCartId);
}

function AscentTechnologyLimited$OnlineShop$Controls$ShoppingCart$Register(shoppingCartId)
{
	_shoppingCartId = shoppingCartId;
}

AscentTechnologyLimited.OnlineShop.Controls.ShoppingCart.registerClass('AscentTechnologyLimited.OnlineShop.Controls.ShoppingCart', AscentTechnologyLimited.OnlineShop.Controls.ProductContainer);

AscentTechnologyLimited.OnlineShop.Controls.QuickCompare = function(element) { 
    AscentTechnologyLimited.OnlineShop.Controls.QuickCompare.initializeBase(this, [element]);
	
}

AscentTechnologyLimited.OnlineShop.Controls.QuickCompare.prototype = {

	initialize : function() {
		
		AscentTechnologyLimited.OnlineShop.Controls.QuickCompare.callBaseMethod(this, 'initialize');
		
		this._ServiceSucceededHandler = Function.createDelegate(this, this.ServiceSucceededHandler);
		this._ServiceFailedHandler = Function.createDelegate(this, this.ServiceFailedHandler);

	},
    
   dispose : function() {
		       
		AscentTechnologyLimited.OnlineShop.Controls.QuickCompare.callBaseMethod(this, 'dispose');
	
	},

	add : function(moniqueId)
	{

		this._beginRequest(this);
		Services.QuickCompare.Add(moniqueId, this._ServiceSucceededHandler, 
			this._ServiceFailedHandler, this);
	
	},
	
	remove : function(moniqueId)
	{

		this._beginRequest(this);
		Services.QuickCompare.Remove(moniqueId, this._ServiceSucceededHandler, 
			this._ServiceFailedHandler, this);
	
	},
	
	_beginRequest : function(quickCompare)
	{
	
		var element = quickCompare.get_element();
	
		element.disabled = true;	
		
	},
	
	_endRequest : function(quickCompare)
	{
		
		var element = quickCompare.get_element();
	
		element.disabled = false;
		this._onRequestEnded();
		
	},

	_refresh : function(result)
	{
	
		var hasItems = (result.Items.length !== 0);
	
		this._totalQuantity.innerHTML = result.TotalQuantity;
				
		this._showHideShowHideButton(hasItems);
		this._showHideEmptyCart(!hasItems);
		this._showHideCart(hasItems);
		this._refreshItems(result.Items);
		
	},

	_createItem : function(table, item)
	{

		var row;
		var cell;
		var link;

		row = table.insertRow(-1);
		cell = row.insertCell(-1);
		link = document.createElement('a');
		cell.appendChild(link);
		if (item.MoniqueID !== null)
			link.href = 'productspecification.aspx?ItemID=' + item.MoniqueID;
		else					
			link.href = 'pcconfigs.aspx?ConfigID=' + item.ProductContainerID;
		link.innerHTML = item.Name;

	},

	ServiceSucceededHandler : function (result, quickCompare)
	{
		 
		if (result.Success)
		{
			this._refresh(result);
		}
		this._endRequest(quickCompare);
		
	},

	ServiceFailedHandler : function (result, quickCompare)
	{
	
		this._endRequest(quickCompare);
	
	}

}

var _quickCompareId = null;

function AscentTechnologyLimited$OnlineShop$Controls$QuickCompare$get_Current()
{
	return $find(_quickCompareId);
}

function AscentTechnologyLimited$OnlineShop$Controls$QuickCompare$Register(quickCompareId)
{
	_quickCompareId = quickCompareId;
}

AscentTechnologyLimited.OnlineShop.Controls.QuickCompare.registerClass('AscentTechnologyLimited.OnlineShop.Controls.QuickCompare', AscentTechnologyLimited.OnlineShop.Controls.ProductContainer);

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
