/**
 * Controller for the cart.
 * 
 * 
 */
function CartController() {}

// Subclass.

CartController.prototype = new Controller;

/**
 * Run on onload.
 * 
 * 
 * 
 */
CartController.prototype.onload = function() {
    
    this.configureQtyInputs();

    this.configureControlLinks();
    
    // Notify customer that they have entered a coupon, but don't have any
    // qualifying products.
    if ($('btn-checkout')) {
        var obj = this;
        $('btn-checkout').onclick = function () {
            if (!obj.coupon_no_discount) {
                return;
            }
            var msg = 'You have entered a coupon, but you have not chosen ' +
                      'any qualifying products. Continue?';
            if (!confirm(msg)) {
                return false;
            }
        };
    }
};


/**
 * Sets a flag when customer has entered a coupon code, but is not receiving
 * a discount.
 * 
 * 
 */
CartController.prototype.setCouponNoDiscount = function(coupon_no_discount) {
    
    this.coupon_no_discount = coupon_no_discount;
    
};


/**
 * Adds handlers to the qty text inputs.
 * 
 * 
 */
CartController.prototype.configureQtyInputs = function() {

    var qty_inputs = $$('form_qty');

    for (var i = 0; i < qty_inputs.length; i++) {
        
        EventCache.add(
            qty_inputs[i],
            'click',
            this.selectText.bind(this, qty_inputs[i])
        );
    }
};


/**
 * Selects input text.
 * 
 * 
 */
CartController.prototype.selectText = function(evt, input) {
    
    input.select();
    
};


/**
 * Adds handlers to the cart "control" links.
 * 
 * 
 */
CartController.prototype.configureControlLinks = function() {

    var msg = 'Are you sure you want to delete this item?';

    new ConfirmLinks('remove', 'cart', msg);

};
