var processing = false;

function init() {
	Ext.QuickTips.init();
	Ext.form.Field.prototype.msgTarget = 'under';
	
	var feedbackForm = new Ext.FormPanel({
		renderTo: Ext.DomQuery.selectNode('#feedback_form'),
		labelAlign: 'top',
		frame: false,
		border: false,
		monitorValid: true,
		defaults: {
			width: 390
		},
		defaultType: 'textfield',
		items: [{
				fieldLabel: 'Category',
				name: 'category',
				xtype: 'combo',
				mode: 'local',
				triggerAction: 'all',
				editable: false,
				allowBlank: false,
				emptyText: 'Please select...',
				store: new Ext.data.ArrayStore({
					fields: ['short', 'long'],
					data: [
					       ['product', 'Product Feedback'],
					       ['problem', 'Report a Problem'],
					       ['business', 'Business Inquiry'],
					       ['other', 'Other']
					]
				}),
				valueField: 'short',
				displayField: 'long'
			},{
				fieldLabel: 'Subject',
				name: 'subject',
				allowBlank: false
			},{
				fieldLabel: 'Message',
				name: 'message',
				xtype: 'textarea',
				allowBlank: false
			},{
				fieldLabel: 'Domains',
				name: 'domains',
				allowBlank: true
			},{
				fieldLabel: 'Email Address',
				name: 'email',
				vtype: 'email',
				allowBlank: false
			}
		],
		buttonAlign: 'left',
		buttons: [{
			text: 'Submit Message',
			formBind: true,
			handler: function() {
				if (processing == false) {
					processing = true;
					feedbackForm.form.submit({
						url: '/ws/feedback',
						method: 'post',
						success: function(form, action) {
							processing = false;
							Ext.DomHelper.overwrite(Ext.get('feedback_form'), [
									{tag: 'p', 'class': 'globalBodyText', html: 'Thank you for your feedback'},
									{tag: 'p', 'class': 'globalBodyText', html: 'If you submitted a support issue, we will respond via email as soon as possible. Can\'t wait? Check out our <a href="http://forums.sharethis.com">support forums</a> and our <a href="http://blog.sharethis.com/faq">support home page</a>.'}
								]
							);
							Ext.get('feedback_form').removeClass('form');
						},
						failure: function(form, action) {
							processing = false;
							var message = !Ext.isEmpty(action.result.statusMessage) ? action.result.statusMessage : 'An unknown error was encountered.';
							Ext.MessageBox.alert('Error', message);
						}
					});
				}
			}
		}],
		keys: {
			key: Ext.EventObject.ENTER,
			fn: function() { Ext.DomQuery.selectNode('#feedback_form button:first').click(); }
		}
	});
	Ext.DomQuery.selectNode('#feedback_form input:first').focus();
}
