BT.ns(function() {with(BT) {
	RMA = {
		
		loadTotals: function(totals, tax_multiplier) {
			this.totals = Object.clone(totals);
			this.totals.tax = parseFloat(this.totals.tax);
			this.totals.shipping = parseFloat(this.totals.shipping);
			this.totals.subtotal = parseFloat(this.totals.subtotal);
			this.totals.total = parseFloat(this.totals.total);
			
			this.tax_multiplier = parseFloat(tax_multiplier);
			
			this.current = Object.clone(this.totals);
		},
		
		dispatch: function(action, lineId) {
			switch(action) {
				
				case 'return':
					this.subtract(lineId);
					this.setReturn(lineId);
					
					this.clearExchange(lineId); // remove the exchange box
				break;
				
				case 'exchange':
					this.add(lineId);
					this.loadSizes(lineId);
				break;
				
				case 'nothing':
					this.add(lineId);
					this.clearExchange(lineId); // remove the exchange box
				break;
			}
		},
		
		clearExchange: function(lineId) {
			$('options_'+lineId).update();
			$('alert_'+lineId).update();
		},
		
		add: function(lineId) {
			if($('price_'+lineId).hasClassName('strike')) {
				this.addCoupon(this.totals.coupon_per_item);
				this.addTotal(parseFloat(this.$('price_'+lineId)));
				this.addSubtotal(parseFloat(this.$('price_'+lineId)));
				this.addTax();
			}
		
			this.updateTotal();
			this.updateTotalReturn();
			this.removeReturn(lineId);
		},
		
		subtract: function(lineId) {
			this.subtractCoupon(this.totals.coupon_per_item);
			this.subtractTotal(parseFloat(this.$('price_'+lineId)));
			this.subtractSubtotal(parseFloat(this.$('price_'+lineId)));
			this.subtractTax();
			this.updateTotal();
			this.updateTotalReturn();
		},
		
		setReturn: function(lineId) {
			$('price_'+lineId).addClassName('strike');
		},
		
		removeReturn: function(lineId) {
			$('price_'+lineId).removeClassName('strike');
		},
		
		subtractSubtotal: function(amount) {
			var value = this.current.subtotal - amount;

			if(value >= 0) {
				$('subtotal').innerHTML = this.money(value);
				this.current.subtotal = value;
			}
		},
		
		subtractTax: function() {
			if (this.tax_multiplier > 0) {
				var value = this.totals.tax - ((this.totals.subtotal - this.current.subtotal) * this.tax_multiplier);
				
				if (value >= 0) {
					$('tax').innerHTML = this.money(value);
					this.current.tax = value;
				}
				else {
					$('tax').innerHTML = this.money(0);
					this.current.tax = 0;
				}
			}
		},

		
		addSubtotal: function(amount) {
			var value = this.current.subtotal + amount;

			if(value <= this.totals.subtotal) {
				$('subtotal').innerHTML = this.money(value);
				this.current.subtotal = value;
			}
		},
		
		addTax: function() {
			if (this.tax_multiplier > 0) {
				var value = this.totals.tax - ((this.totals.subtotal - this.current.subtotal) * this.tax_multiplier);
				
				$('tax').innerHTML = this.money(value);
				this.current.tax = value;
			}
		},
		
		addTotal: function(amount) {
			var value = this.current.total + amount;
			
			if(value >= 0) {
				this.current.total = value;
			}
		},
		
		subtractTotal: function(amount) {
			var value = this.current.total - amount;
			
			if(value >= 0) {
				this.current.total = value;
			}
		},
		
		addCoupon: function(amount) {
			var value = this.current.coupon + amount;
		
			if(value >= 0) {
				$('coupon').innerHTML = this.money(value);
				this.current.coupon = value;
			}
		},
		
		subtractCoupon: function(amount) {
			var value = this.current.coupon - amount;
			
			if(value >= 0) {
				$('coupon').innerHTML = this.money(value);
				this.current.coupon = value;
			}
		},
		
		updateTotalReturn: function() {
			var value = (this.totals.tax - this.current.tax) + this.totals.total - this.current.total - this.calculateCouponValue();
			
			$('total_return').innerHTML = this.money(value);
		},
		
		updateTotal: function() {
		
			coupon = this.calculateCouponValue();
	
			var value = this.current.total - (this.totals.tax - this.current.tax) + coupon;
			
			$('total').innerHTML = this.money(value);
		},
		
		calculateCouponValue: function() {
			value = this.totals.coupon - this.current.coupon;

			if(isNaN(value)) {
				value = 0;
			}
			
			return value;
		},
		
		$: function(elm) {
			val = $(elm).innerHTML;
			
			return val.replace(/\$/g, '');
		},
		
		loadSizes: function(lineId) {
			$('options_'+lineId).innerHTML = 'Loading Sizes...';
			
			new Jument.ajax({
					url: '/ajax/rma/options', 
					options: {
						parameters: 'line_id='+lineId+'&model_id='+$('model_'+lineId).value
					}
			});
		},
		
		selectColor: function(lineId) {
			var option = $('exchange_'+lineId);
			var selected = option.options[option.selectedIndex];
			
			lineId = option.getId();
			
			alertDiv = $('alert_'+lineId);
			alertDiv.update();
			
			if(Element.hasClassName(selected,'no_stock')) {
				alertDiv.innerHTML = '<div>Item out of stock. Please allow 1-2 weeks for delivery.</div>';
			}

		},
		
		process: function(form) {
			$('msg').update();
			new Jument.submit_form('ajax/rma/process', form, 'btnProcess');
		},
		
		doDelete: function(rmaId) {
			if(confirm('Are you sure you want to delete this return/exchange?')) {
				new Jument.ajax({
					url: '/ajax/rma/delete', 
					options: {
						parameters: 'rma_id='+rmaId
					}
				});
			}
		},
		
		money: function(value) {
			return '$' + parseFloat(value).toFixed(2);
		}
	}
}});