function renderCommentType(value) {
	switch (value) {
		case "blog": return 'Blog';
		case "podcast": return 'Podcast';
		case "content": return 'General Articles';
		case "repository": return 'Repository';
		default: return '(Unknown)';
	}
}

function reqEmail(value){
	switch (value) {
		case "1": return 'Waiting for email authentication';
		case "2": return 'Email confirmed';
		default: return 'No email auth. required';
	}
}

function resetCustomFilters(_site_id,_type) {
	
	// reset comments main store custom ids, like 'blog_id', 'podcast_id', etc...
	var o = Ext.getCmp('commentsMainGrid').store.lastOptions.params;
	o['podcast_id'] = null;
	o['blog_id'] = null;
	
	hideBarItems(Ext.getCmp('com_tb2_id'));
	
	switch(_type) {
		case "repository":
			Ext.getCmp('com_repository_filter').reset();
			Ext.getCmp('com_repository_filter').store.load({params:{'site_id': _site_id}});
			break;
		case "content":
			Ext.getCmp('com_article_filter').reset();
			Ext.getCmp('com_article_filter').store.load({params:{'site_id': _site_id}});
			break;
		case "blog":
			Ext.getCmp('com_blog_filter').reset();
			Ext.getCmp('com_blog_filter').store.load({params:{'site_id': _site_id}});
			break;
		case "podcast":
			Ext.getCmp('com_podcast_filter').reset();
			Ext.getCmp('com_podcast_filter').store.load({params:{'site_id': _site_id}});
			break;
	}
}

function getArticlesDropDown(ds) {
	var item_store = new Ext.data.Store({
	    proxy: new Ext.data.HttpProxy({
			url: '/articles/getAll'
		}),
		baseParams: {isac: 1, aco: 'cms_commenting'},
		autoLoad: false,
	    reader: new Ext.data.JsonReader({
			    root:'results',
				totalProperty: 'total',
				id: 'id'
	    	}, [
				{name: 'id'},
				{name: 'name'}
			]),
	    remoteSort: true,
	    listeners:{
	    	'loadexception':{
	    		fn: function() {
	    			alert('Error loading articles!');
	    		}
	    	},
	    	"load":{
	    		fn: function(thisStore) {
	    			Ext.getCmp('com_article_filter_text').show();
	    			item_dropdown.show();
	    			var rootNode = new sitesDataRecord({
				    	id:'0',
				    	name:'All Articles (Reset Filter)'
				    });
				    thisStore.insert(0,rootNode);
	    		}
	    	}
	    }
	});
	
	var item_dropdown = new Ext.form.ComboBox({
		id: 'com_article_filter',
		typeAhead: false,
		forceSelection: true,
		editable:true,
		store: item_store,
		displayField: 'name',
		valueField: 'id',
		triggerAction: 'all',
		emptyText: 'Select article',
		width:170,
		hidden: true,
		mode: 'local',
		listeners:{
	    	'select':{
	    		fn: function(combo) {
	    			var o = ds.lastOptions.params;
	    			o['item_id'] = combo.value;
	    			ds.load({params:o});
	    		}
	    	}
		}
	});
	return item_dropdown;
}

function getRepositoriesDropDown(ds) {
	var item_store = new Ext.data.Store({
	    proxy: new Ext.data.HttpProxy({
			url: '/repositories/getAll?all=1'
		}),
		baseParams: {isac: 1, aco: 'cms_commenting'},
		autoLoad: false,
	    reader: new Ext.data.JsonReader({
			    root:'results',
				totalProperty: 'total',
				id: 'id'
	    	}, [
				{name: 'id'},
				{name: 'name'},
				{name: 'iconCls'},
				{name: 'parent_id'}
			]),
	    remoteSort: true,
	    listeners:{
	    	'loadexception':{
	    		fn: function() {
	    			alert('Error loading repository items!');
	    		}
	    	},
	    	"load":{
	    		fn: function(thisStore) {
	    			Ext.getCmp('com_repository_filter_text').show();
	    			item_dropdown.show();
	    			if (!thisStore.insertedNode) {
		    			var rootNode = new sitesDataRecord({
					    	id:'0',
					    	name:'All Repository Items (Reset Filter)'
					    });
					    thisStore.insert(0,rootNode);
					    thisStore.insertedNode = true;
	    			}
	    		}
	    	}
	    }
	});
	
//	var item_dropdown = new Ext.form.ComboBox({
	var item_dropdown = new Ext.ux.TreeComboBox({
		id: 'com_repository_filter',
//		typeAhead: false,
//		forceSelection: true,
//		editable:true,
		nodes: { textField: 'name' },
		store: item_store,
		displayField: 'name',
		valueField: 'id',
		triggerAction: 'all',
//		emptyText: 'Select repository item',
		width:210,
		hidden: true,
		mode: 'local',
		listeners:{
	    	'select':{
	    		fn: function(combo) {
	    			var o = ds.lastOptions.params;
	    			o['item_id'] = combo.value;
	    			ds.load({params:o});
	    		}
	    	}
		}
	});
	
	return item_dropdown;
}

function getBlogsDropDown(ds) {
	var item_store = new Ext.data.Store({
	    proxy: new Ext.data.HttpProxy({
			url: '/blogs/getBlogs'
		}),
		baseParams: {isac: 1, aco: 'cms_commenting'},
		autoLoad: false,
	    reader: new Ext.data.JsonReader({
			    root:'results',
				totalProperty: 'total',
				id: 'id'
	    	}, [
				{name: 'id'},
				{name: 'name'}
			]),
	    remoteSort: true,
	    listeners:{
	    	'loadexception':{
	    		fn: function() {
	    			alert('Error loading blogs!');
	    		}
	    	},
	    	"load":{
	    		fn: function(thisStore) {
	    			Ext.getCmp('com_blog_filter_text').show();
	    			item_dropdown.show();
	    			var rootNode = new sitesDataRecord({
				    	id:'0',
				    	name:'All Blogs (Reset Filter)'
				    });
				    thisStore.insert(0,rootNode);
	    		}
	    	}
	    }
	});
	
	var item_dropdown = new Ext.form.ComboBox({
		id: 'com_blog_filter',
		typeAhead: false,
		forceSelection: true,
		editable:true,
		store: item_store,
		displayField: 'name',
		valueField: 'id',
		triggerAction: 'all',
		emptyText: 'Select blog',
		width:170,
		hidden: true,
		mode: 'local',
		listeners:{
	    	'select':{
	    		fn: function(combo) {
	    			var o = ds.lastOptions.params;
	    			o['blog_id'] = combo.value;
	    			ds.load({params:o});
	    			
	    			// load blog entries store
	    			if (combo.value>0) {
		    			Ext.getCmp('com_blog_entries_filter').reset();
		    			Ext.getCmp('com_blog_entries_filter').store.load({params:{'blog_id':combo.value}});
	    			} else {
	    				Ext.getCmp('com_blog_entries_filter').hide();
	    				Ext.getCmp('com_blog_entries_filter_text').hide();
	    			}
	    		}
	    	}
		}
	});
	return item_dropdown;
}

function getBlogEntriesDropDown(ds) {
	var item_store = new Ext.data.Store({
	    proxy: new Ext.data.HttpProxy({
			url: '/blogs/getEntries'
		}),
//		baseParams: {isac: 1, aco: 'cms_commenting'},
		autoLoad: false,
	    reader: new Ext.data.JsonReader({
			    root:'results',
				totalProperty: 'total',
				id: 'id'
	    	}, [
				{name: 'id'},
				{name: 'name'}
			]),
	    remoteSort: true,
	    listeners:{
	    	'loadexception':{
	    		fn: function() {
	    			alert('Error loading blog articles!');
	    		}
	    	},
	    	"load":{
	    		fn: function(thisStore) {
	    			Ext.getCmp('com_blog_entries_filter_text').show();
	    			item_dropdown.show();
	    			var rootNode = new sitesDataRecord({
				    	id:'0',
				    	name:'All Blog Articles (Reset Filter)'
				    });
				    thisStore.insert(0,rootNode);
	    		}
	    	}
	    }
	});
	
	var item_dropdown = new Ext.form.ComboBox({
		id: 'com_blog_entries_filter',
		typeAhead: false,
		forceSelection: true,
		editable:true,
		store: item_store,
		displayField: 'name',
		valueField: 'id',
		triggerAction: 'all',
		emptyText: 'Select blog article',
		width:200,
		hidden: true,
		mode: 'local',
		listeners:{
	    	'select':{
	    		fn: function(combo) {
	    			var o = ds.lastOptions.params;
	    			o['item_id'] = combo.value;
	    			ds.load({params:o});
	    		}
	    	}
		}
	});
	return item_dropdown;
}

function getPodcastsDropDown(ds) {
	var item_store = new Ext.data.Store({
	    proxy: new Ext.data.HttpProxy({
			url: '/podcasts/getPodcasts'
		}),
		baseParams: {isac: 1, aco: 'cms_commenting'},
		autoLoad: false,
	    reader: new Ext.data.JsonReader({
			    root:'results',
				totalProperty: 'total',
				id: 'id'
	    	}, [
				{name: 'id'},
				{name: 'name'}
			]),
	    remoteSort: true,
	    listeners:{
	    	'loadexception':{
	    		fn: function() {
	    			alert('Error loading podcasts!');
	    		}
	    	},
	    	"load":{
	    		fn: function(thisStore) {
	    			Ext.getCmp('com_podcast_filter_text').show();
	    			item_dropdown.show();
	    			var rootNode = new sitesDataRecord({
				    	id:'0',
				    	name:'All Podcasts (Reset Filter)'
				    });
				    thisStore.insert(0,rootNode);
	    		}
	    	}
	    }
	});
	
	var item_dropdown = new Ext.form.ComboBox({
		id: 'com_podcast_filter',
		typeAhead: false,
		forceSelection: true,
		editable:true,
		store: item_store,
		displayField: 'name',
		valueField: 'id',
		triggerAction: 'all',
		emptyText: 'Select podcast',
		width:170,
		hidden: true,
		mode: 'local',
		listeners:{
	    	'select':{
	    		fn: function(combo) {
	    			var o = ds.lastOptions.params;
	    			o['podcast_id'] = combo.value;
	    			ds.load({params:o});
	    			
	    			// load podcast episodes store
	    			if (combo.value>0) {
		    			Ext.getCmp('com_podcast_episodes_filter').reset();
		    			Ext.getCmp('com_podcast_episodes_filter').store.load({params:{'podcast_id':combo.value}});
	    			} else {
	    				Ext.getCmp('com_podcast_episodes_filter').hide();
	    				Ext.getCmp('com_podcast_episodes_filter_text').hide();
	    			}
	    		}
	    	}
		}
	});
	return item_dropdown;
}

function getPodcastEpisodesDropDown(ds) {
	var item_store = new Ext.data.Store({
	    proxy: new Ext.data.HttpProxy({
			url: '/podcasts/getEpisodes'
		}),
//		baseParams: {isac: 1, aco: 'cms_commenting'},
		autoLoad: false,
	    reader: new Ext.data.JsonReader({
			    root:'results',
				totalProperty: 'total',
				id: 'id'
	    	}, [
				{name: 'id'},
				{name: 'name'}
			]),
	    remoteSort: true,
	    listeners:{
	    	'loadexception':{
	    		fn: function() {
	    			alert('Error loading podcast episodes!');
	    		}
	    	},
	    	"load":{
	    		fn: function(thisStore) {
	    			Ext.getCmp('com_podcast_episodes_filter_text').show();
	    			item_dropdown.show();
	    			var rootNode = new sitesDataRecord({
				    	id:'0',
				    	name:'All Podcast Episodes (Reset Filter)'
				    });
				    thisStore.insert(0,rootNode);
	    		}
	    	}
	    }
	});
	
	var item_dropdown = new Ext.form.ComboBox({
		id: 'com_podcast_episodes_filter',
		typeAhead: false,
		forceSelection: true,
		editable:true,
		store: item_store,
		displayField: 'name',
		valueField: 'id',
		triggerAction: 'all',
		emptyText: 'Select podcast episodes',
		width:200,
		hidden: true,
		mode: 'local',
		listeners:{
	    	'select':{
	    		fn: function(combo) {
	    			var o = ds.lastOptions.params;
	    			o['item_id'] = combo.value;
	    			ds.load({params:o});
	    		}
	    	}
		}
	});
	return item_dropdown;
}

function Comments(_type, _itemId, _siteId, _handler,panel) {
	
	if (_type) var _global = false;
	else {
		var _global = true;
		_type = '';
	}
	if (!_itemId) _itemId = 0;
	if (!_siteId) _siteId = 0;
	
	var _changed = false;
	
	// create the Data Store
	var ds = new Ext.data.Store({
		// load using script tags for cross domain, if the data in on the same domain as
		// this page, an HttpProxy would be better
		proxy: new Ext.data.HttpProxy({
			url: '/comments/getAll'
		}),
		baseParams: {aco: 'cms_commenting'},
		// create reader that reads the group records
		reader: new Ext.data.JsonReader({
			root: 'results',
			totalProperty: 'total',
			id: 'id'
		}, [
			{name: 'id'},
			{name: 'type'},
			{name: 'site_id'},
			{name: 'display_name', mapping: 'site_name'},
			{name: 'item_id'},
			{name: 'item_name'},
			{name: 'user_id'},
			{name: 'email'},
			{name: 'website'},
			{name: 'req_email_flag'},
			{name: 'username'},
			{name: 'author'},
			{name: 'content'},
			{name: 'date'},
			{name: 'status'}
		]),
		// turn on remote sorting
		remoteSort: true
	});
	ds.setDefaultSort('id', 'DESC');
	ds.load({params:{start:0, limit: myPageSize, type: _type, item_id: _itemId}});
	
	// row expander
    var expander = new Ext.grid.RowExpander({
        tpl : new Ext.Template(
            '<div style="padding:5px;"><b>Content:</b> {content}</div>'
        )
    });
	
	// the column model has information about grid columns
	var cm = new Ext.grid.ColumnModel([
		expander,
		{
		   id: 'id',
		   header: "Identity",
		   dataIndex: 'id',
		   width: 50,
		   hidden: true
		},{
		   header: "Author",
		   dataIndex: 'author',
		   width: 130,
		   sortable: true
		},{
		   header: "Author Email",
		   dataIndex: 'email',
		   width: 130,
		   sortable: true
		},{
		   header: "Author Website",
		   dataIndex: 'website',
		   width: 170,
		   sortable: true
		},{
		   header: "Requires Email Authentication",
		   dataIndex: 'req_email_flag',
		   width: 150,
		   renderer:reqEmail,
		   sortable: true
		},{
		   header: "Site",
		   dataIndex: 'display_name',
		   width: 110,
//		   hidden: ((_global)?false:true),
		   sortable: false
		},{
		   header: "Item Type",
		   dataIndex: 'type',
		   width: 100,
		   hidden: ((_global)?false:true),
		   renderer: renderCommentType,
		   sortable: false
		},{
		   header: "Item Name",
		   dataIndex: 'item_name',
		   width: 110,
		   sortable: false
		},{
		   header: "Status",
		   dataIndex: 'status',
		   renderer: formatStatus,
		   width: 50,
		   sortable: true
		},{
		   header: "Date",
		   dataIndex: 'date',
		   width: 70,
		   sortable: true
		}]);

	// by default columns are sortable
	cm.defaultSortable = true;
	
	var menubar = [{
		text:'Add',
		id: 'commentAddButton',
		tooltip:'Add new comment',
		iconCls:'addItem',
		hidden: ((_global)?true:false),
		handler: function(){
				// add new article
				EditComment(grid,0);
		 	}
		},' ',{
			text:'Delete',
			tooltip:'Remove the selected comments',
			iconCls:'remove',
			handler: function(){
				// delete article
				Delete(grid,'/comments/delete','comment');
				_changed = true;
			}
		},'-',{
            text: 'Change Status',
            iconCls: 'publish',
            menu: [{
		        text: 'Publish selected comments',
		        iconCls: 'publish',
		        handler: function(){
		        	_changed = true;
		            ChangeStatus(grid,1,'/comments/changeStatus','comment');
		        }
		    },{
		        text: 'Unpublish selected comments',
		        iconCls: 'unpublish',
		        handler: function(){
		        	_changed = true;
		            ChangeStatus(grid,0,'/comments/changeStatus','comment');
		        }
		    }]
        }
		,'-','Search: ', ' ',
		new Ext.app.SearchField({
			store: ds,
			width:120,
			pageSize:myPageSize
		}),'-','Status: ',' ',
		new Ext.form.ComboBox({
			name:'filterCommByStatus',
			store: statusStoreWithoutTestingFilter,
			displayField:'txt',
			valueField: 'idx',
			triggerAction: 'all',
			width: 100,
			editable: false,
			mode: 'local',
			emptyText: 'Select status',
			listeners:{
				'select':{
					fn: function(combo, record) {
						var param = grid.store.lastOptions.params;
						if (combo.value!='-1') {
							param.filter_status = combo.value;
						} else {
							param.filter_status = null;
						}
						grid.store.reload({params: param});
					}
				}
			}
		})
	];
	
	// adding filters
	if (_global) {
		
		// site combo
		var sitesCommFilterStore = getSitesStore();
	    var rootNode = new sitesDataRecord({
	    	id:'0',
	    	display_name:'All Sites (Reset Filter)',
	    	parent_id:null
	    });
	    sitesCommFilterStore.on("load",function() {
	    	sitesCommFilterStore.insert(0,rootNode);
	    });
	    sitesCommFilterStore.load({params:{aco:'cms_blogs'}});
	    
	    var sitesCommDropDown = new Ext.form.ComboBox({
			name: 'siteDropDownElement',
			typeAhead: false,
			editable:false,
			store: sitesCommFilterStore,
			displayField: 'display_name',
			valueField: 'id',
			triggerAction: 'all',
			emptyText: 'Filter by site',
			width:150,
			mode: 'local',
			listeners:{
				'select':{
					fn: function(combo,record) {
						
						resetCustomFilters(combo.value,commentsTypeDropDown.getValue());
						
						var o = ds.lastOptions.params;
						o['site_id'] = combo.value;
						o['start'] = 0;
						ds.load({params:o});
					}
				}
			}
		});
	    
		// type combo
		var commentsTypeStore = new Ext.data.Store({
		    proxy: new Ext.data.HttpProxy({
				url: '/comments/getTypes'
			}),
			autoLoad: true,
		    reader: new Ext.data.JsonReader({
				    root:'results',
					totalProperty: 'total',
					id: 'id'
		    	}, [
					{name: 'type'},
					{name: 'type_disp'}
				]),
		    remoteSort: true,
		    listeners:{
		    	'loadexception':{
		    		fn: function() {
		    			alert('Error loading comment types!');
		    		}
		    	}
		    }
		});
		var rootNodeTypes = new sitesDataRecord({
	    	type:'',
	    	type_disp:'All Types (Reset Filter)'
	    });
	    commentsTypeStore.on("load",function() {
	    	commentsTypeStore.insert(0,rootNodeTypes);
	    });
		var commentsTypeDropDown = new Ext.form.ComboBox({
			name: 'commentsTypeDropDownElement',
			typeAhead: false,
			editable:false,
			store: commentsTypeStore,
			displayField: 'type_disp',
			valueField: 'type',
			triggerAction: 'all',
			emptyText: 'Select comment type',
			width:150,
			mode: 'local',
			listeners:{
		    	'select':{
		    		fn: function(combo) {
		    			
		    			resetCustomFilters(sitesCommDropDown.getValue(),combo.value);
		    			
		    			var o = ds.lastOptions.params;
		    			o['type'] = combo.value;
		    			o['item_id'] = 0;
		    			ds.load({params:o});
		    		}
		    	}
			}
		});
		
		menubar.push('-');
		menubar.push('Site:');
		menubar.push(sitesCommDropDown);
		menubar.push('-');
		menubar.push('Comment Type');
		menubar.push(commentsTypeDropDown);
	}
	
	var grid = new Ext.grid.GridPanel({
		id: 'commentsMainGrid',
		title:'',
		iconCls: 'article',
		loadMask:{msg: 'Loading...'},
		store: ds,
		cm: cm,
		trackMouseOver:true,
		selModel: new Ext.grid.RowSelectionModel({singleSelect:false}),
		enableColLock:false,
		plugins: expander,
		viewConfig: {
			emptyText:"No records found!",
			forceFit: true
		},
		tbar: menubar,
		bbar: new Ext.PagingToolbar({
			pageSize: myPageSize,
			store: ds,
			displayInfo: true,
			displayMsg: 'Displaying items {0} - {1} of {2}',
			emptyMsg: "No items to display"
		})
	});
	
	// adding additional filters
	if (_global) {
		grid.on("render",function() {
			var tb2 = new Ext.Toolbar({
				id: 'com_tb2_id',
				height: 25,
			    renderTo: grid.tbar,
			    items: [
			    	{
			    		text:'Filter comments by selecting one of the options above.'
			    	},
			    	{
			    		text:'Select Article:',
			    		hidden: true,
			    		id:'com_article_filter_text'
			    	},
			    	getArticlesDropDown(ds),
			    	{
			    		text:'Select Blog:',
			    		hidden: true,
			    		id:'com_blog_filter_text'
			    	},
			    	getBlogsDropDown(ds),
			    	{
			    		text:'Select Blog Article:',
			    		hidden: true,
			    		id:'com_blog_entries_filter_text'
			    	},
			    	getBlogEntriesDropDown(ds),
			    	{
			    		text:'Select Podcast:',
			    		hidden: true,
			    		id:'com_podcast_filter_text'
			    	},
			    	getPodcastsDropDown(ds),
			    	{
			    		text:'Select Podcast Episode:',
			    		hidden: true,
			    		id:'com_podcast_episodes_filter_text'
			    	},
			    	getPodcastEpisodesDropDown(ds),
			    	{
			    		text:'Select Repository Item:',
			    		hidden: true,
			    		id:'com_repository_filter_text'
			    	},
			    	getRepositoriesDropDown(ds)
			    ]
			});
		});
	}

	grid.on('rowdblclick', function(_grid, rowIndex, e) {
		var selectedId = _grid.store.data.items[rowIndex].id;
		EditComment(_grid, selectedId);
	});
	if(panel){
		var window=new Ext.Panel({
//			id: 'commentsWindowId',
	        title: 'Comments',
//			iconCls: 'comment',
	        layout: 'fit',
	        items: grid
		});
		
	}else{
		var window = new Ext.Window({
			id: 'commentsWindowId',
	        title: 'Manage Comments',
			iconCls: 'comment',
	        width: 750,
	        height:500,
	        minWidth: 300,
	        minHeight: 250,
	        layout: 'fit',
	        plain:true,
			modal:true,
	        buttonAlign:'center',
	        items: grid,
	        listeners: {
	        	'close':{
	        		fn: function() {
	        			if (_handler && _changed) {
	        				_handler();
	        			}
	        		}
	        	}
	        }
	    });
	}
    var EditComment = function(_grid, _selectedId) {
		
	    // pre-define fields in the form
		var idField = new Ext.form.Hidden({
	        name: 'data[Comment][id]',
			dataIndex:'id'
	    });
	    
	    var typeField = new Ext.form.Hidden({
	        name: 'data[Comment][type]',
			dataIndex:'type',
			value:_type
	    });
	    
	    var siteField = new Ext.form.Hidden({
	        name: 'data[Comment][site_id]',
			dataIndex:'site_id',
			value:_siteId
	    });
	    
		var contentField = new Ext.form.HtmlEditor({
			fieldLabel:'Content',
			id: 'commentContentId',
			name:'data[Comment][content]',
			dataIndex: 'content',
			allowBlank: false,
		    width: 500,
		    height: 200
		});
	
		var statusField = new Ext.form.ComboBox({
			id:'pubStatusComment',
			name:'pubStatusComment',
			dataIndex: 'status',
			value: '0',
			fieldLabel: 'Status',
			store: statusStoreWithoutTesting,
			displayField:'txt',
			typeAhead: false,
			selectOnFocus:true,
			valueField: 'idx',
			hiddenName: 'data[Comment][status]',
			triggerAction: 'all',
			anchor: '40%',
			editable: false,
			allowBlank: false,
			mode: 'local'
		});
	
		var formPanel = new Ext.form.FormPanel({
			id: 'formPanelComment',
			title: '',
			iconCls: 'comment-add',
			layout: 'form',
	        labelWidth: 70,
	        bodyStyle:'padding:10px;',
			autoScroll:true,
	        url:'/comments/edit/'+_itemId,
	        reader: new Ext.data.JsonReader({
						root: 'results'
					}, ['id','type','site_id','item_id','user_id','author','content','status','email','website','req_email_flag']
	        ),
	        items: [
	        	idField,
	        	siteField,
	        	typeField,
				contentField,
				statusField
			]
	    });
	    
	    formPanel.form.on('actioncomplete', function(form, action){
			if(action.type=='load'){
				// nothing to do here
			}
		});
	    
	    if (_selectedId && _selectedId>0) {
			formPanel.form.load({
		    	url:'/comments/getOne/'+_selectedId,
	     		waitMsg:'Loading ...Please wait!'
		    });
	    }
		
		var window = new Ext.Window({
			id: 'commentDetailsWindowId',
	        title: ((_selectedId && _selectedId>0)?'Edit Comment':'Add New Comment'),
			iconCls: 'site-edit',
	        width: 650,
	        height:350,
	        minWidth: 300,
	        minHeight: 250,
	        layout: 'fit',
	        plain:true,
			modal:true,
	        buttonAlign:'center',
	        items: formPanel,
	
	        buttons: [{
	            text: 'Save',
				iconCls: 'edit-save',
	            handler: function() {
	                // check form value
	                if (formPanel.form.isValid()) {
		 		        formPanel.form.submit({
				            waitMsg:'Processing',
				            failure: function(form, action) {
							    Ext.MessageBox.alert('Error Message', action.result.errorInfo);
							},
							success: function(form, action) {
							    //Ext.MessageBox.alert('Record Saved', action.result.info);
							    
							    // load store and destroy current viewport
							    _grid.store.load({params:_grid.store.lastOptions.params});
							    window.close();
							}
				        });
	                } else {
						Ext.MessageBox.alert('Error(s) Encounterd', 'Please fix the errors noted.');
					}
		        }
	        },{
	            text: 'Cancel',
				iconCls: 'edit-cancel',
	            handler: function(){
					window.close();
				}
	        }]
	    });
		
		window.show();
	};
    
	return window;
};
