Ext.ns('Pubbli');

Pubbli.TermsGrid = Ext.extend(Ext.Panel, {
	initComponent : function() {
	
		var config = {
			title : 'Terms',
			frame : true,
			height : 200,
			layout : 'fit',
			items : [{
				xtype : 'textarea',
				id : 'terms_area',				
			listeners : {
				scope : this,
				afterrender : function(){
					Ext.Ajax.request({
						url : 'http://www.pubblicommerce.com/action_tnc.php?reading=1&listing_id=' + this.oListingId,
						method : 'GET',		
						success : function(o) {
							var json = Ext.decode(o.responseText);
							
							if(json.success){

								Ext.getCmp('terms_area').setValue(json.data);
							}
						},
						failure : function() {
							//console.log('error');
						},
						scope : this
					});
				}
			}
			}],
			bodyStyle : {
				background : '#fff'
			},
			buttons : [{
				text : 'Save terms',
				scope : this,
				handler : function(){
					Ext.Ajax.request({
						url : 'http://www.pubblicommerce.com/action_tnc.php?storing=1&listing_id=' + this.oListingId,
						method : 'POST',		
						params : {
							terms : Ext.getCmp('terms_area').getValue()
						},
						success : function(o) {
							var json = Ext.decode(o.responseText);
							
							if(json.success){
									Ext.Msg.alert('Done', 'Saved successfuly');
							}else{
									Ext.Msg.alert('Error Occured : ', json.error);
							}
							
						},
						failure : function(o) {
							Ext.Msg.alert('Error Occured : Connection Failure!');
						},
						scope : this
					});
				}
			}]
		};

		Ext.apply(this, Ext.apply(this.initialConfig,
						config));
						
		Pubbli.TermsGrid.superclass.initComponent.apply(
				this, arguments);
	}
});

