NTTGA.Component.MembershipForm = function()
{
  var leftColItems;
  
  if ( ! NTTGA.Session.MemberData )
  {
    leftColItems = 
    [
      userCombo = new Ext.form.ComboBox({
        name: 'username',
        fieldLabel: 'Registered Username',
        width: 220,
        hiddenName: 'userid',
        displayField: 'username',
        valueField: 'userid',
        forceSelection: true,
        typeAhead: true,
        triggerAction: 'all',
        store: new Ext.data.JsonStore({
          url: '/request/Request.php?m=members&f=getAllUsers',
          root: 'data',
          fields: [ 'userid', 'username' ],
          autoLoad: true,
          listeners:
          {
            load: function (store,records,index)
            {
              if ( NTTGA.Session.UserId != 0 )
              {
                userCombo.setValue(NTTGA.Session.UserId);
              }
            }
          }
        })
      }),
      {xtype: 'textfield', name: 'firstname', fieldLabel: 'First Name*', width: 220, allowBlank: false},
      {xtype: 'textfield', name: 'lastname', fieldLabel: 'Last Name*', width: 220, allowBlank: false},
      {xtype: 'textfield', name: 'address1', fieldLabel: 'Address 1*', width: 220, allowBlank: false},
      {xtype: 'textfield', name: 'address2', fieldLabel: 'Address 2', width: 220},
      {xtype: 'textfield', name: 'city', fieldLabel: 'City*', width: 220, allowBlank: false},
      {xtype: 'textfield', name: 'state', fieldLabel: 'State*', width: 220, allowBlank: false, maxLength: 2},
      {xtype: 'textfield', name: 'zip', fieldLabel: 'Zipcode*', width: 220, allowBlank: false},
      {xtype: 'textfield', name: 'email', fieldLabel: 'Email*', width: 220, allowBlank: false},
      {xtype: 'textfield', name: 'phone1', fieldLabel: 'Phone 1*', width: 220, allowBlank: false},
      {xtype: 'textfield', name: 'phone2', fieldLabel: 'Phone 2', width: 220},
      {xtype: 'hidden', name:'paid_date', value: new Date()},
      saleItem = new Ext.form.ComboBox({
        allowBlank: false,
        width: 220,
        name: 'saleitems',
        fieldLabel: 'Select Membership and T-Shirt Size',
        hiddenName: 'sale_item_id',
        displayField: 'short_description',
        valueField: 'item_id',
        forceSelection: true,
        typeAhead: true,
        triggerAction: 'all',
        store: new Ext.data.JsonStore({
          url: '/request/Request.php?m=sale_items&f=getMemberships',
          root: 'data',
          fields: [ 'item_id', 'short_description' ],
          autoLoad: true
        })
      })      
    ];
  } else {
    leftColItems =
    [
      {xtype: 'textfield', name: 'username', fieldLabel: 'Username', width: 220, readOnly: true, value: NTTGA.Session.MemberData.username },
      {xtype: 'textfield', name: 'firstname', fieldLabel: 'First Name*', width: 220, allowBlank: false, value: NTTGA.Session.MemberData.firstname },
      {xtype: 'textfield', name: 'lastname', fieldLabel: 'Last Name*', width: 220, allowBlank: false, value: NTTGA.Session.MemberData.lastname },
      {xtype: 'textfield', name: 'address1', fieldLabel: 'Address 1*', width: 220, allowBlank: false, value: NTTGA.Session.MemberData.address1 },
      {xtype: 'textfield', name: 'address2', fieldLabel: 'Address 2', width: 220, value: NTTGA.Session.MemberData.address2 },
      {xtype: 'textfield', name: 'city', fieldLabel: 'City*', width: 220, allowBlank: false, value: NTTGA.Session.MemberData.city },
      {xtype: 'textfield', name: 'state', fieldLabel: 'State*', width: 220, allowBlank: false, maxLength: 2, value: NTTGA.Session.MemberData.state },
      {xtype: 'textfield', name: 'zip', fieldLabel: 'Zipcode*', width: 220, allowBlank: false, value: NTTGA.Session.MemberData.zip },
      {xtype: 'textfield', name: 'email', fieldLabel: 'Email*', width: 220, allowBlank: false, value: NTTGA.Session.MemberData.email },
      {xtype: 'textfield', name: 'phone1', fieldLabel: 'Phone 1*', width: 220, allowBlank: false, value: NTTGA.Session.MemberData.phone1 },
      {xtype: 'textfield', name: 'phone2', fieldLabel: 'Phone 2', width: 220, value: NTTGA.Session.MemberData.phone2 },
      {xtype: 'hidden', name:'userid', value: NTTGA.Session.UserId },
      {xtype: 'hidden', name:'paid_date', value: new Date()},
      saleItem = new Ext.form.ComboBox({
        allowBlank: false,
        width: 220,
        name: 'saleitems',
        fieldLabel: 'Select Membership and T-Shirt Size',
        hiddenName: 'sale_item_id',
        displayField: 'short_description',
        valueField: 'item_id',
        forceSelection: true,
        typeAhead: true,
        triggerAction: 'all',
        store: new Ext.data.JsonStore({
          url: '/request/Request.php?m=sale_items&f=getMemberships',
          root: 'data',
          fields: [ 'item_id', 'short_description' ],
          autoLoad: true
        })
      })      
    ];
  }
  
  var config =
  {
    xtype: 'form',
    id: 'membershipForm',
    title: 'Membership Form',
    frame: true,
    autoHeight: true,
    method: 'POST',
    url: '/payment/process.php',
    standardSubmit: true,
    onSubmit: Ext.emptyFn,
    items: leftColItems
  };
  
  config.buttons = 
  [
    {
      xtype: 'button',
      text: 'Save',
      handler: function()
      {
        memberForm = Ext.getCmp( 'membershipForm' );
        
        memberForm.getForm().getEl().dom.action = '/payment/process.php';
        
        if ( memberForm.form.isValid() )
        {
          memberForm.form.submit();
        } else {
          Ext.MessageBox.alert('Errors', 'Please fix the errors noted.');
        }
      }
    },{
      text: 'Cancel',
      handler: function(){editWindow.destroy();}
    }
  ];

  NTTGA.Component.MembershipForm.superclass.constructor.call(this, config);
};

Ext.extend( NTTGA.Component.MembershipForm, Ext.FormPanel );
