/*

	@Class: CartInterface
	
	@Brief: 
			
	@Notes: This class requires MooTools!
	
*/

var CartInterface = new Class({
	options: {
		CartWrapperID:		Class.empty,	// ID of Cart Content Wrapper - Update AJAX content here
		RemoveButtonClass:	Class.empty, 	// Add To Cart Button
		UpdateTotalsID:	Class.empty,	// ID of the Update Totals Button
		ApplyCodeID:		Class.empty,	// ID of the Apply Promo Code Button
		WebRoot:			Class.empty	// The web root for this site
	}, 
	
	initialize: function(options) {
		// Start-Up Code	
		this.setOptions(options);
				
		// Find Instances
		this.CartWrapper 		=  $( this.options.CartWrapperID );
		this.RemoveButtons 		= $$( this.options.RemoveButtonClass );
		this.UpdateTotalsButton	=  $( this.options.UpdateTotalsID );
		this.ApplyCodeButton	=  $( this.options.ApplyCodeID );
		this.WebRoot			=     this.options.WebRoot;
		
		if( this.RemoveButtons )
			for( var i = 0; i < this.RemoveButtons.length; i++ )
				this.RemoveButtons[i].addEvent( 'click', this.RemoveItem.bindWithEvent( this, this.RemoveButtons[i].id.split('_')[1] ) );
				
		//	if( this.UpdateTotalsButton )
		//		this.UpdateTotalsButton.addEvent( 'click', this.UpdateTotals.bindWithEvent( this ) );
			
		if( this.ApplyCodeButton )
			this.ApplyCodeButton.addEvent( 'click', this.ApplyCode.bindWithEvent( this ), true );

	},
	
	InterfaceSetup: function( x ) {
		// Find Instances
		
		if( x ) {
			if( $( 'hidden_cart_count' ).value > 0 ) {
				var bagCount = $( 'hidden_cart_count' ).value - 1;

				if( bagCount == 1 )
					var countTag = ' Item';
				else
					var countTag = ' Items';
				
				$( 'ShoppingBagQuantity' ).innerHTML = bagCount + countTag;
			}
		}
	
		this.RemoveButtons 	= $$( this.options.RemoveButtonClass );
		
		if( this.RemoveButtons )
			for( var i = 0; i < this.RemoveButtons.length; i++ )
				this.RemoveButtons[i].addEvent( 'click', this.RemoveItem.bindWithEvent( this, this.RemoveButtons[i].id.split('_')[1] ) );
	},

	RemoveItem: function( event, itemID ) {
		
		var url = this.WebRoot + 'Cart.cfm?AJAXRequest=true&RemoveItem=true&ProductID=' + itemID;
		new Ajax( url, { method: 'get', update: this.CartWrapper, onComplete: this.InterfaceSetup.bind( this, true ) } ).request();

	},

	UpdateTotals: function( event ) {
		
		var allForms = $$( '.CartForm' );
		var allNumeric = true;
		
		for( var i = 0; i < allForms.length; i++ )
			if( !(/(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/.test( allForms[i].Quantity.value.replace(/^\s+|\s+$/g,"") )) )
				allNumeric = false;
		
		if( allNumeric ) {
			for( var i = 0; i < allForms.length; i++ ) {
				if( allForms[i].Quantity.value.replace(/^\s+|\s+$/g,"") == 0 ) {
					// if this quantity is 0, send the delete call
					var url = this.WebRoot + 'Cart.cfm?AJAXRequest=true&RemoveItem=true&ProductID=' + allForms[i].ProductID.value;
					new Ajax( url, { method: 'get', onComplete: this.CompleteUpdateTotals.bind( this, [ allForms.length, i ] ) } ).request();
				
				} else {
					// otherwise send the update call
					var url = this.WebRoot + 'Cart.cfm?AJAXRequest=true&UpdateQuantities=true&ProductID=' + allForms[i].ProductID.value + '&Quantity=' + allForms[i].Quantity.value.replace(/^\s+|\s+$/g,"");
					new Ajax( url, { method: 'get', onComplete: this.CompleteUpdateTotals.bind( this, [ allForms.length, i ] ) } ).request();
				}
			}
		} else {
			alert( 'All quantities must be numeric values.' );
		}
		
	},

	CompleteUpdateTotals: function( total, current ) {
		
		if( total == current + 1 ){
			location.href = this.WebRoot + 'Cart.cfm';
		}
			
	},
	
	ApplyCode: function( event ) {
		
		if($('promo_code_value').value == "") {
			alert('Please enter a valid promo code.');
		} else {
			var url = this.WebRoot + 'Cart.cfm?AJAXRequest=true&ApplyCode=true&PromoCode=' + $('promo_code_value').value + '&total=' + $('subT').value;
			//var url = 'Cart.cfm?AJAXRequest=true&ApplyCode=true&PromoCode=' + $('promo_code_value').value + '&total=' + $('subT').value;
			new Ajax( url, { method: 'get', onComplete: this.CompleteApplyCode.bind( this, true ) } ).request();
		}
		
	},
	
	CompleteApplyCode: function( ) {
		
		location.href = this.WebRoot + 'Cart.cfm';
		
	},

	StripSlash: function( sMessage ) {
		return sMessage.replace( /\//g, "slash_" );	
	},
	
	Echo: function( sMessage ) {
		alert( sMessage ); 
	}

});

//	//////////////////////////////////////////////////////////////////////////////////////////////// //
//	//////////////////////////////////////////////////////////////////////////////////////////////// //

function ValidateUpdateCart( x ) {
	
	var IsValid = true;
	var QuantityInputs = $$( '.Quantity_Input' );
	
	for( var i = 0; i < QuantityInputs.length; i++ ) {
		if( !(/(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/.test( QuantityInputs[i].value.replace(/^\s+|\s+$/g,"") )) ) {
			alert( 'Quantities may only contain numeric values.' );
			QuantityInputs[i].focus();
			var IsValid = false;
		}
	}
	
	if( IsValid )
		x.submit();
}

//	//////////////////////////////////////////////////////////////////////////////////////////////// //
//	//////////////////////////////////////////////////////////////////////////////////////////////// //


CartInterface.implement(new Options);

window.addEvent('load', function() {
	var MyCart = new CartInterface({
		CartWrapperID:		'CartWrapper',
		RemoveButtonClass:	'.removeItem',
		UpdateTotalsID:	'UpdateTotals',
		ApplyCodeID:		'ApplyCode',
		WebRoot:			'/'
	});
});

