
Ext.namespace("WeShotJr");

Ext.BLANK_IMAGE_URL = './js/ext-2.2/resources/images/default/s.gif';
Ext.form.Field.prototype.msgTarget = 'side';
WeShotJr.url = 'controller.php';
WeShotJr.window;
WeShotJr.venueListArray;

WeShotJr.AddShowForm = Ext.extend(Ext.form.FormPanel, {

   border:false,
   frame:true,
   labelWidth:120,
   url:'controller.php',
   initComponent: function() {
      var config = {
         defaultType:'textfield',
         defaults:{width:330},
         monitorValid:true,
         items:[{
               name:'info',
               labelSeparator:"",
               html:"<br /><br />",
               width:330,
               style:"padding-left:125px",
               xtype:'label'
            },{
               id:'date',
               name:'date',
               fieldLabel:'Show Date*',
               xtype:'datefield',
               format:'Y-m-d',
               width: 100,
               labelSeparator:"",
               allowBlank:false
            },{
               name:'start_time',
               fieldLabel:'Start Time',
               xtype:'timefield',
               width: 100,
               labelSeparator:"",
               increment: 30
            },{
               name:'cover',
               width: 50,
               labelSeparator:"",
               fieldLabel:'Cover Charge'
            },{
               name:'info',
               labelSeparator:"",
               html:"Select a venue or type a new one (same with city)...",
               width:330,
               style:"padding-left:125px",
               xtype:'label'
            },{
               name:'venue',
               fieldLabel:'Venue*',
               labelSeparator:"",
               xtype:'combo',
               triggerAction:'all',
               allowBlank:false,
               emptyText:'',
               store: WeShotJr.venueListArray,
               autoLoad:true
            },{
               name:'city',
               fieldLabel:'City',
               xtype:'combo',
               triggerAction:'all',
               mode:'local',
               emptyText:'',
               labelSeparator:"",
               store:['Dallas','Denton','Fort Worth']
            },{
               createLinkText:"Make sure it starts with http:// or it won't work!.",
               enableAlignments:false,
               enableColors:false,
               enableFont:false,
               enableFontSize:false,
               enableFormat:false,
               enableLists:false,
               name:'description',
               fieldLabel:'Show Description',
               xtype:'htmleditor',
               labelSeparator:"",
               height:135,
               value:"",
               listeners: {
                  activate: function(e) {
                     e.execCmd('delete');
                  }
               }
            },{
               name:'info',
               labelSeparator:"",
               html:"Your name and email address will <b>NOT</b> be published.",
               width:330,
               style:"padding-left:125px",
               xtype:'label'
            },{
               name:'submitted_by',
               fieldLabel:'Your Name',
               labelSeparator:""
            },{
               name:'submitted_email',
               fieldLabel:'Your E-mail',
               vtype:'email',
               labelSeparator:""
            },{
               name:'info1',
               labelSeparator:"",
               html:"<i>* Required Fields</i>",
               style:"padding-left:125px",
               xtype:'label'
            }],
         buttons:[{
               text:'Submit',
               formBind:true,
               scope:this,
               handler:this.submit
            },{
               text:'Reset',
               scope:this,
               handler:function(){
                  this.getForm().reset();
               }
            },{
               text:'Close',
               scope:this,
               handler:function(){
                  WeShotJr.window.destroy();
                  WeShotJr.window = null;
                  WeShotJr.grid.getStore().reload();
               }
         }]
      };

      // apply config
      Ext.apply(this, Ext.apply(this.initialConfig, config));
      
      // super call
      WeShotJr.AddShowForm.superclass.initComponent.apply(this, arguments);
   },
   
   onRender: function() {
      // call parent
      WeShotJr.AddShowForm.superclass.onRender.apply(this, arguments);

      // set wait message target
      this.getForm().waitMsgTarget = this.getEl();
   },
   
   submit:function() {
      this.getForm().submit({
         url:this.url,
         scope:this,
         success:this.onSuccess,
         failure:this.onFailure,
         params:{cmd:'addShow',source:'user'},
         waitMsg:'Submitting Show...'
      });
   },
   
   onSuccess:function(form, action) {
      Ext.Msg.show({
         title:'Success',
         msg:'Show submitted successfully!  It will show up in the list after we approve it.',
         modal:true,
         icon:Ext.Msg.INFO,
         buttons:Ext.Msg.OK
      });
      
      form.reset();
   },

   onFailure:function(form, action) {
      this.showError(action.result.error);
   },

   showError:function(msg, title) {
      title = title || 'Error';
      Ext.Msg.show({
         title:title,
         msg:msg,
         modal:true,
         icon:Ext.Msg.ERROR,
         buttons:Ext.Msg.OK
      });
   }
 
});
 
// register the new xtype
Ext.reg('wsjr_addShowForm', WeShotJr.AddShowForm); 

WeShotJr.launchAddForm = function(){

   // create and show window
   WeShotJr.window = new Ext.Window({
      id:'formloadsubmit-win',
      title:'Add Show',
      modal:true,
      layout:'fit',
      width:520,
      height:500,
      closable:false,
      border:false,
      items:{id:'formloadsubmit-form', xtype:'wsjr_addShowForm'}
   });

   WeShotJr.window.show();
}


Ext.onReady(function(){
   Ext.namespace("WeShotJr");
   
   // preload the venues list array
   Ext.Ajax.request({
      url: 'venues-get.php',
      success: function(response){
         WeShotJr.venueListArray = response.responseText.split(";");
      }
   });
});
