
Ext.grid.RowCart = function(config){
    Ext.apply(this, config);
 
    Ext.grid.RowCart.superclass.constructor.call(this);

    if(this.tpl){
        if(typeof this.tpl == 'string'){
            this.tpl = new Ext.Template(this.tpl);
        }
        this.tpl.compile();
    }

    this.state = {};
    this.bodyContent = {};
};



Ext.extend(Ext.grid.RowCart, Ext.util.Observable, {
    header: "",
    width: 65,
    sortable: false,
    fixed:true,
    menuDisabled:true,
    dataIndex: '',
    id: 'cart',
    lazyRender : true,
    enableCaching: true,

    getRowClass : function(record, rowIndex, p, ds){ 	
    	record.rowIndex = rowIndex;
        p.cols = p.cols-1;
        var content = this.bodyContent[record.id];
        if(!content && !this.lazyRender){
            content = this.getBodyContent(record, rowIndex);
        }
        if(content){
            p.body = content;
        }
        return this.state[record.id] ? 'x-grid3-row-expanded' : 'x-grid3-row-collapsed';
    },

    init : function(grid){
        this.grid = grid;

        var view = grid.getView();
        view.getRowClass = this.getRowClass.createDelegate(this); // ?

        view.enableRowBody = true;

        grid.on('render', function(){
            view.mainBody.on('mousedown', this.onMouseDown, this);
        }, this);
    },

    getBodyContent : function(record, index){
        if(!this.enableCaching){
            return this.tpl.apply(record.data);
        }
        var content = this.bodyContent[record.id];
        if(!content){
            content = this.tpl.apply(record.data);
            this.bodyContent[record.id] = content;
        }
        return content;
    },

    onMouseDown : function(e, t){
        if(t.className == 'x-grid3-row-cart'){
            e.stopEvent();
            var row = e.getTarget('.x-grid3-row');             
            var cell = e.getTarget('.x-grid3-row-cart');
            this.addToCart(row, cell);
        }
    },

    renderer : function(v, p, record){
    	if(record.get('poa') == '1') {
    		Ext.select('.txtpoa').show();    		
    		 return ''; // no cart for poa
    	}
        return '<div class="x-grid3-row-cart"><input onclick="focus()" onmousedown="focus()" onselect="focus()" type="text" size="2" name="" value="1" style="width: 25px" /> </div>';
    },

   
	addToCart : function(row, cell) {
		if(typeof row == 'number'){
            row = this.grid.view.getRow(row);
        }
	 	var record = this.grid.store.getAt(row.rowIndex);
	 	var el = Ext.DomQuery.selectNode('input', cell);
	 	var quant = el.value;
	 	if(!quant) quant = 1;
	 	cartAction('add',record.get('productid'), record.get('ordercode') , quant , el, this.grid);
	
		// el.value = 1;  // in feedupa

	}

   
});

