﻿/*----------------------------------------------------------------------------
 Project:			OnlineShopDotNet
 Source File:		AddToShoppingCartButton.js
 Company:     		Ascent Technology Limited
 Author:      		John Mills
 Date:        		24/12/2007
 Purpose:     		This file contains the AJAX client side code for the
						AddToShoppingCartButton class.
 Notes:       		
------------------------------------------------------------------------------
 John Mills     	Created.
 24/12/2007        
------------------------------------------------------------------------------
 John Mills     	Added ability for specifying the extended warranty.
 28/12/2007        
----------------------------------------------------------------------------*/

function addToShoppingCartClick(moniqueId, quantity, extendedWarrantyYearCount, element)
{

	var shoppingCart;
	var style = _getAddToCartStyle(element);
	var handler = Function.createDelegate(element, _shoppingCartRequestEndedHandler);
	
	if (_shoppingCartExists())
		shoppingCart = AscentTechnologyLimited$OnlineShop$Controls$ShoppingCart$get_Current();
	//alert(style)
	if (style == 0)
	{
		element.className = 'AddToCartInProgressStd';
	}
	
	if (style == 1)
	{
	   element.className = 'AddToCartInProgressLarge';
	}
	
	if (style == 2)
	{
	   element.className = 'AddToCartInProgressSmall';
	}

	if (style == 3)
	{
	   //alert('here');
	   element.className = 'AddToCartInProgressMed';
	}		
		//alert(element.className)
	element.disabled = true;
	element.removeAttribute('href');
	element.onclick = null;
	element.removeAttribute('onclick');
	
	if (shoppingCart)
	{
		element._shoppingCartRequestEndedHandler = handler;
		shoppingCart.add_requestEnded(element._shoppingCartRequestEndedHandler);
		shoppingCart.add(moniqueId, quantity, extendedWarrantyYearCount);
	}
	else
	{
		AscentTechnologyLimited$OnlineShop$Controls$add(moniqueId, quantity, 
			extendedWarrantyYearCount, handler, handler);
	}
	 
	return false;
	
}

// Returns True if a shopping cart exists on the page.
function _shoppingCartExists()
{
	return (typeof AscentTechnologyLimited$OnlineShop$Controls$ShoppingCart$get_Current != 'undefined' &&
		AscentTechnologyLimited$OnlineShop$Controls$ShoppingCart$get_Current() != null);
}

function _shoppingCartRequestEndedHandler(sender, arguments)
{
	
	var style = _getAddToCartStyle(this);
	
	if (style == 0)
	{
		this.className = 'AddToCartStd AddToCartAddedStd';
   }
   
	if (style == 1)
	{
		this.className = 'AddToCartLarge AddToCartAddedLarge';
	}
	
	if (style == 2)
	{
	   this.className = 'AddToCartSmall AddToCartAddedSmall';
	}

	if (style == 3)
	{
	   this.className = 'AddToCartMed AddToCartAddedMed';
	}
	
	//var location = Sys.UI.DomElement.getLocation(obj);

//var mpe = $find("ctl00_ltnPageHeader_ModalPopupExtender");
//mpe.set_X(screen.width-230);
//mpe.show();

	if (_shoppingCartExists())
		AscentTechnologyLimited$OnlineShop$Controls$ShoppingCart$get_Current().remove_requestEnded( 
			this._shoppingCartRequestEndedHandler);
	this._shoppingCartRequestEndedHandler = null;
}

// Adds an item to the Shopping Cart
function AscentTechnologyLimited$OnlineShop$Controls$add(moniqueId, quantity, 
	extendedWarrantyYearCount,	completedHandler)
{

	Services.ShoppingCart.Add(moniqueId, quantity, extendedWarrantyYearCount, completedHandler, 
		completedHandler, this);
		
}
	
function _getAddToCartStyle(element)
{
   if (element.className.indexOf('Std') == -1)
		// Standard
	   {
	      if (element.className.indexOf('Large') == -1)
		   {
		      if (element.className.indexOf('Small') == -1)
		      {
		         return 3;
		      }
		      else
		      {
		         return 2;
		      }
		   }
		   else
		   {
		      return 1;
		   }
		}
   else
	   {
		   return 0;
	   }
}

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
