 /**
 * CartRequest - Add to Cart Wrapper to handle server side error checking.
 *
 * @author Scott Murphy
 * 
 * Copyright (c) 2008 Pictage Inc.
 * ALL RIGHTS RESERVED
 */
 
var otherEventCartPopup;
var CartRequest = new Class({
    initialize: function(options) {
		this.options = options;
		this.cartRequest = new Request.JSON({ url:'cart.do', async:false, onComplete: function( resp ) {
			if ( $defined(resp) && $defined( resp.otherEventCart ) ) {
				if($defined(resp.showOtherEventCartPopup) && !resp.showOtherEventCartPopup) {
					alert('You can not buy this product because your cart has products that belong to another event.' + 
					      'Please, place an order with this products or remove them from the cart to buy this new product');
					return;
				}
				else if ( !$defined(otherEventCartPopup) )
					otherEventCartPopup = new PopUp({
						title:'Your cart contains items from another event',
						id: 'otherEventContainer',
						content:'otherEventContainer'
					});
				otherEventCartPopup.show();	
				var goToButton = otherEventCartPopup.container.getElementById('goToOtherEvent');
				goToButton.removeEvents('click');
				goToButton.addEvent( 'click', function() {
					top.location = 'event.do?event='+resp.otherEventCart;
				});
				var emptyCart = otherEventCartPopup.container.getElementById('emptyCart');
				emptyCart.removeEvents('click');
				emptyCart.addEvent( 'click', function() {
					this.doRequest( this.params, true );
					otherEventCartPopup.close();
				}.bind(this) );			
			}
			else
				this.options.onComplete( resp );
		}.bind(this)});
	},
	doRequest: function( params, emptyCart ) {
		this.params = params;
		if ( $defined(emptyCart) )
			$extend(params, {'emptyCart':true});
		this.cartRequest.POST(params);
	}
});	
