
var writer = new Ext.data.JsonWriter({
	 	encode: true
})


var editor = new Ext.ux.grid.RowEditor({
   	saveText: 'Update',
     	listeners: {
        afteredit: function(object, changes, r, rowIndex) {
        		setTimeout ("ProductTypesEditDataStore.load()", 300); 
        		 editor.startEditing(1);
			 	
			}
		}
});


var proxyPorductTypesEdit = new Ext.data.HttpProxy({
    url: 'php/addDeleteUpdateProductTypes.php'
});


ProductTypesEditDataStore = new Ext.data.Store({
	id: 'ProductTypesEditDataStore',
	proxy: proxyPorductTypesEdit, 
	restful: false,
	reader: new Ext.data.JsonReader({  
		root: 'types',
		id: 'ProductTypeID'		
	}, [
		{name: 'ProductTypeID', type: 'string', mapping: 'ProductTypeID'},
		{name: 'ProductTypeName', type: 'string', mapping: 'ProductTypeName'}
	]),
	writer: writer
});


ProductTypesEditColumnModel = new Ext.grid.ColumnModel([
	{
   	header: 'ID',
      readOnly: true,
      dataIndex: 'ProductTypeID', // this is where the mapped name is important!
      width: 90,
      hidden: true
   },
   {
      header: 'ProductTypeName',
      dataIndex: 'ProductTypeName',
      width: 180,
	   hidden: false,
      editor: {
                xtype: 'textfield'                
            	}
   }
]);
     

var ProductTypesEditEditorGrid =  new Ext.grid.EditorGridPanel({
      id: 'ProductTypesEditEditorGrid',
      store: ProductTypesEditDataStore,     // the datastore is defined here
      cm: ProductTypesEditColumnModel,      // the columnmodel is defined here
      enableColLock:false,
      clicksToEdit:1,
      selModel: new Ext.grid.RowSelectionModel({singleSelect:true}),
      plugins: [editor],
      tbar: [{
            text: 'Add',
            iconCls: 'silk-add',
            handler: onAdd
        }, 
        '-',
        {
            text: 'Delete',
            iconCls: 'silk-delete',
            handler: onDelete
        }
        ]
});


var ProductType = Ext.data.Record.create([
												{name: 'ProductTypeID', type: 'int'},
												{name: 'ProductTypeName', type: 'string'}
												]);
												

ProductTypesEditWindow = new Ext.Window({
      id: 'ProductTypesEditWindow',
      title: 'Categories',
      closable:false,
      collapsible: true,
      width:200,
      height:450,
      x: 40,
      y: 70,
		plain:true,
      layout: 'fit',
      items: ProductTypesEditEditorGrid  // We'll just put the grid in for now...
});


function onAdd(){
	          var ProductTypeStruct = new  ProductType({
                    ProductTypeName: ''
                });
        editor.stopEditing();
        ProductTypesEditEditorGrid.store.insert(0, ProductTypeStruct);
        editor.startEditing(0)
        
}

function onDelete(){
	     var rec =  ProductTypesEditEditorGrid.getSelectionModel().getSelected();
        if (!rec) {
            return false;
        }
        ProductTypesEditEditorGrid.store.remove(rec);
        setTimeout ("ProductTypesEditDataStore.load()", 350); 
}



