$(document).ready(function() {
    $("#coupon_code").bind("click", function() {
        form = $(document.createElement('form'));
        $(form).html("<span class='error' style='color:red'></span><label>Coupon code:</label><input type='text' name='couponcode' /><input type='submit' value='Ok' />");
        $(form).submit(function() {
            $.ajax({
                url: '/coupon_fix.php',
                data: $(this).serialize(),
                type: 'POST',
                success: function(data) {
			$('span#coupon_info').hide();
			if (data.substr(0, 5) == 'valid') {
				$(form).html("<span>The coupon was accepted.</span>");
				$('span#coupon_info').html('New coupon active: ' + data.substr(6));
				$('span#coupon_info').fadeIn();
			} else {
				$(form).children('.error').hide();
				$(form).children('.error').html('The coupon was rejected:<br /><b>' + data + '</b><br />'); 
				$(form).children('.error').fadeIn();
			}
                }
            });
            return false;
        });
        $(form).dialog();
        return false;
    });
});

