var Coupon = Class.create(
{   
	send: function()
	{
		var emailAddress = $('couponEmailAddress_id').value;
		var couponCode = $('couponCode_id').value;
		var landerPageUrl = $('landerPageUrl_id').value;
		var noRefresh = true;
		
		var options = {
		    parameters: {
				CouponEmailAddress : $('couponEmailAddress_id').value,
				CouponCode : $('couponCode_id').value,
				LanderPageUrl : $('landerPageUrl_id').value,
				NoRefresh : true
			},
			
		    method: 'post',
		    
		    onCreate: function(obj) {
                // clear and hide that 'error' message (just in case it's being shown)
                $('emailCouponMessageBlock').update('');
                $('emailCouponMessageBlock').setStyle({ display: 'none' });
                
                // disable the send button and switch the background to the 'sending' image
                $('sendCoupon_id').disable();
                $('sendCoupon_id').setStyle({ background: "url('/i/coupons/green-email-coupon-sending-button.png') no-repeat" });
		    }.bind(this),
		    
		    onComplete: function(obj) {
                var response = obj.responseText.evalJSON(true);
                var results = $H(response).get('results');
                var isCouponSent = results != '' && results == '1';
                
                if (isCouponSent)
                {
			        // remove form elements
	                $('couponEmailAddress_id').remove();
	                $('couponCode_id').remove();
	                $('landerPageUrl_id').remove();
	                $('sendCoupon_id').remove();
	                
	                // switch the background image to 'sent' image
			        $('emailCouponImageBlock').setStyle({ background: "url('/i/coupons/red-sent.png') no-repeat" });
			        
			        // 'show' that close coupon block link
			        $('closeCouponImageBlock').setStyle({ display: 'block' });
                }
                else
                {
                	// otherwise show the 'error' message
                	$('emailCouponMessageBlock').update('Please submit a valid e-mail to receive your code.');
                	$('emailCouponMessageBlock').setStyle({ display: 'block' });
                	
                	// enable and switch back the background of the  send button to the 'send' image
                	$('sendCoupon_id').enable();
                    $('sendCoupon_id').setStyle({ background: "url('/i/coupons/email-coupon-button.png') no-repeat" });
                }
		    }.bind(this),
		    
		    onFailure: function(obj) {
                // show the 'error' message
                $('emailCouponMessageBlock').update('We are unable to send your code at this time.');
                $('emailCouponMessageBlock').setStyle({ display: 'block' });
                    
                // enable and switch back the background of the  send button to the 'send' image
                $('sendCoupon_id').enable();
                $('sendCoupon_id').setStyle({ background: "url('/i/coupons/email-coupon-button.png') no-repeat" });
		    }
		};
		
		new Ajax.Request('/coupon/send/', options);
	},
	
	closeCouponBlock: function()
	{
		$('couponImageBlock_id').setStyle( { display: 'none' });
	}
});

var couponUtil = new Coupon();

document.observe('dom:loaded', function() 
{
	if ($('sendCoupon_id') !== null)
	{
		$('sendCoupon_id').observe(
			'click',
			function() { couponUtil.send(); }
		);
		$('sendCoupon_id').onclick = function() { return false; }
	}
	
	if ($('closeCouponImageBlock') !== null)
	{	
		$('closeCouponImageBlock').observe(
            'click',
            function() { couponUtil.closeCouponBlock(); }
		);
		$('closeCouponImageBlock').onclick = function() { return false; }
	}
});
