﻿
var feedbackForm = new Ext.form.FormPanel({
  defaultType: 'textfield',
  labelAlign: 'top',
  padding: 3,
  id: 'feedbackForm',
  items: [
    {
      fieldLabel: 'Name',
      name: 'name',
      width: 100,
      allowBlank: false
    },
    {
      fieldLabel: 'Email (optional)',
      name: 'email',
      widht: 100
    },
    {
      xtype: 'textarea',
      fieldLabel: 'Comment',
      name: 'feedback',
      width: 280,
      height: 100,
      allowBlank: false
    }
  ],
  buttons: [{
    text: 'Submit',
    handler: function(){
      feedbackFormHandler();
    }
  }]
});

var feedbackWindow = new Ext.Window({
  id: 'feedbackWindow',
  items: [feedbackForm],
  header: true,
  title: 'Feedback',
  closable: true,
  closeAction: 'hide',
  collapsible: true,
  width: 310,
  height: 305,
  plain: false,
  frame: true,
  initHidden: true,
  initCollapsed: true,
  shadow: false,
  resizable: false	
});

function feedbackFormHandler(){
    feedbackForm.getForm().submit({
      clientValidation: true,
      url: 'Feedback.asmx/SendFeedback',
      waitMsg: 'Sending feedback...',
      success: function(form, action){ 
        Ext.Msg.alert('Success', action.result.message);
        feedbackForm.findByType('textarea')[0].setValue('');
        feedbackWindow.hide();
      },
      failure: function(form, action) {
        switch (action.failureType) {
          case Ext.form.Action.CLIENT_INVALID:
              Ext.Msg.alert('Failure', 'Form fields may not be submitted with invalid values.');
            break;
          case Ext.form.Action.CONNECT_FAILURE:
            Ext.Msg.alert('Failure', 'Server communication failed, please check your Internet connection is still active.');
            break;
          case Ext.form.Action.SERVER_INVALID:
            Ext.Msg.alert('Failure', action.result.message);
            break;
        }
      }
    });
  }
