__DiscountCoupon = function(){
	if(SmartCheckout.useDiscountCoupon !== 'Y') return;
	this.container = $_('#OSCDiscountCoupon');
	if(!this.container) return;
	this.valueField = $_('#coupon');
	this.statusField = $_('#OSCCouponState')
	this.URLSubmitCoupon = SmartCheckout.APIURL+'?ax=1&function=add_discount_coupon&coupon=';
	var self = this;
	this.valueField.attach('keypress', function(e){
		if(e.keyCode == 13){
			self.submit();
		}
	})
	
}
__DiscountCoupon.prototype.discard = function(){
	if(SmartCheckout.useDiscountCoupon !== 'Y') return;
	var self = this;
	if(!this.container) return;
	this.container.mutate({opacity:1},{opacity:0}, 40, function(){self.container.style.display = 'none'});
}
__DiscountCoupon.prototype.enable = function(){
	if(SmartCheckout.useDiscountCoupon !== 'Y') return;
	var self = this;
	if(!this.container) return;
	this.container.style.display='';
	$_('#OSCCouponUpdateControl').style.display='';
	this.valueField.value = this.statusField.innerHTML = '';
	this.container.mutate({opacity:0},{opacity:1}, 40);
}

__DiscountCoupon.prototype.submit = function(){
	if(SmartCheckout.useDiscountCoupon !== 'Y') return;
	var self = this;
	if(!this.container) return;
	$_('#OSCCouponIndicator').style.display='';
	$_('#OSCCouponUpdateControl').style.display='none';
	$_(this.URLSubmitCoupon + this.valueField.value).GET(function(data){
		if(self.statusField){
			$_('#OSCCouponIndicator').style.display='none';
			self.statusField.innerHTML = data.stdout || 'Coupon accepted';
			if(!data.stdout){
				
				self.discard();
				Totals.getContents();
				
			}else
				$_('#OSCCouponUpdateControl').style.display='';
			//$_('#OSCCouponUpdateControl').loading(0)
		}
	})
}

__DiscountCoupon.prototype.$TEST = function(){
	if(SmartCheckout.useDiscountCoupon !== 'Y') return true;
	if(!this.container) return true;
	return !(this.valueField.value && this.container.offsetHeight);
}

