if (typeof(eCartPanelObject) == 'undefined') 
{
    eCartPanelObject = function(objectName, ajaxEventUID)
    {
        this.objectName    = objectName;
    	this.container     = '#' + objectName;
    	//this.updateControl = '#' + objectName + '_update';
    	this.updateControl = '.' + objectName + '_update';
    	this.clearControl  = '#' + objectName + '_clear';
    	
    	// --- Coupon section --- //
    	this.applyCouponControl  = '#' + objectName + '_apply_coupon';
    	this.cancelCouponControl = '#' + objectName + '_cancel_coupon';
    	this.couponCodeControl   = '#' + objectName + '_coupon_code';
    	// --- Coupon section --- //
    	
    	// --- Certificate section --- //
    	this.applyCertificateControl = '#' + objectName + '_apply_certificate';
    	this.certificateCodeControl  = '#' + objectName + '_certificate_code';
    	// --- Certificate section --- //
    	
    	this.cartManager = new eCartManagerObject(objectName + '__ajaxEventUID', ajaxEventUID);  
    }
}
eCartPanelObject.prototype.init = function()
{
    var currentObject = this;
    
    $(this.clearControl).bind('click', function() {
        currentObject.clear();
    });
    
    $(this.updateControl).bind('click', function() {
        currentObject.update();        
    });
    
    // --- Coupon section --- //
    $(this.applyCouponControl).bind('click', function() {
        currentObject.applyCoupon();        
    });
    $(this.couponCodeControl).setEnterHandler(function() { currentObject.applyCoupon(); } );
    
    $(this.cancelCouponControl).bind('click', function() {
        currentObject.cancelCoupon();        
    });
    // --- Coupon section --- //
    
    // --- Certificate section --- //
    $(this.applyCertificateControl).bind('click', function() {
        currentObject.applyCertificate();        
    });
    $(this.certificateCodeControl).setEnterHandler(function() { currentObject.applyCertificate(); } );
    // --- Certificate section --- //
    
    $('input[id^="' + this.objectName + '_item_quantity"]').setEnterHandler(function() { currentObject.update() } );		    
    $('input[id^="' + this.objectName + '_certificate_quantity"]').setEnterHandler(function() { currentObject.update() } );		    
}
eCartPanelObject.prototype.setInfoPanel = function(infoPanelObject)
{
	this.cartManager.setInfoPanel(infoPanelObject);
}
eCartPanelObject.prototype.setCartPanel = function(cartPanelObject)
{
	this.cartManager.setCartPanel(cartPanelObject);
}
eCartPanelObject.prototype.loadInfo = function(content)
{
	$(this.container).html(content);
}
eCartPanelObject.prototype.clear = function()
{
	this.cartManager.clear();
}
eCartPanelObject.prototype.remove = function(id)
{
	this.cartManager.removeItem(id);
}
eCartPanelObject.prototype.removeCertificate = function(id)
{
	this.cartManager.removeCertificateItem(id);
}
eCartPanelObject.prototype.update = function()
{
    var currentObject = this;
    
    var item_quantities = {};
    var certificate_quantities = {};
    
    var valid = true;
    
    // --- Item quantities --- //
    $('input[id^="' + this.objectName + '_item_quantity"]').each(function() {

	    if (!valid) return;
	    
	    quantity = $(this).val();

    	var reg = /^[0-9]*$/;
	    if (!reg.test(quantity) || (quantity <= 0))
	    {
	        alert('Please enter a valid quantity!'); 
	        $(this).focus();
	        
	        valid = false;
	    }
        name = $(this).attr('name');
	    item_quantities[name.replace(currentObject.objectName, '')] = quantity;
	});
	
    // --- Certificate quantities --- //
    $('input[id^="' + this.objectName + '_certificate_quantity"]').each(function() {

	    if (!valid) return;
	    
	    quantity = $(this).val();

    	var reg = /^[0-9]*$/;
	    if (!reg.test(quantity) || (quantity <= 0))
	    {
	        alert('Please enter a valid quantity!'); 
	        $(this).focus();
	        
	        valid = false;
	    }
        name = $(this).attr('name');
	    certificate_quantities[name.replace(currentObject.objectName, '')] = quantity;
	});
	
    if (valid)
    {
    	this.cartManager.updateItems(item_quantities, certificate_quantities);
    }
}

// --- Coupon section --- //
eCartPanelObject.prototype.applyCoupon = function()
{
    var coupon = $(this.couponCodeControl).val();
    this.cartManager.applyCoupon(coupon);    
}

eCartPanelObject.prototype.cancelCoupon = function()
{
    this.cartManager.cancelCoupon();    
}
// --- Coupon section --- //

// --- Certificate section --- //
eCartPanelObject.prototype.applyCertificate = function()
{
    var certificate = $(this.certificateCodeControl).val();
    this.cartManager.applyCertificate(certificate);    
}
eCartPanelObject.prototype.cancelCertificate = function(id)
{
    this.cartManager.cancelCertificate(id);    
}
// --- Certificate section --- //