Ext.ns('App.ContactForm');

ContactForm = Ext.extend(Ext.form.FormPanel, {
	initComponent: function(){
		this.invalidElement = Ext.get(this.invalidEl);
		Ext.apply(this, {
			header: false,
			labelWidth: 50,
			defaults: {width: 230},
			buttonAlign: 'center',
			defaultType: 'textfield',
			border: false,
			bodyBorder: false,
			bodyStyle: {
				'background-color': 'transparent'
			},
			items: [{
				fieldLabel: 'Name',
				name: 'name',
				allowBlank: false,
				anchor: '95%'
			},{
				fieldLabel: 'Subject',
				name: 'subject',
				anchor: '95%'
			},{
				fieldLabel: 'Email',
				name: 'email',
				allowBlank: false,
				vtype: 'email',
				anchor: '95%'
			},{
				xtype: 'htmleditor',
				fieldLabel: 'Message',
				name: 'msg',
				allowBlank: false,
				anchor: '95%',
				enableFont: false,
				enableSourceEdit: false,
				enableFontSize: false,
				enableColors: false,
				enableAlignments: false
			}],
			buttons: [{
				id: 'send-btn',
				text: 'Send',
				icon: 'resources/images/icons/email_go.png',
				cls: 'x-btn-text-icon',
				handler: this.sendEmail,
				scope: this
			},{
				text: 'Cancel/Reset',
				icon: 'resources/images/icons/email_delete.png',
				cls: 'x-btn-text-icon',
				handler: function(btn){
					this.getForm().reset();
					Ext.getCmp('send-btn').enable();
					this.invalidElement.setDisplayed(false);
				},
				scope: this
			}]
		});
		
		ContactForm.superclass.initComponent.apply(this, arguments);
		
	},
	sendEmail: function(btn){
		
		if(this.getForm().isValid()){
			try{
				this.invalidElement.setDisplayed(false);
			}catch(e){};
			
			btn.disable();
			
			this.getForm().submit({
				url: 'emailer/send/',
				method: 'POST',
				success: function(form, action){
					this.btn.enable();
					
					Ext.Msg.alert("Email Sent!", "Thanks for the email. We will be contacting your shortly.");
					this.form.getForm().reset();
				},
				failure: function(form, action){
					this.btn.enable();
					Ext.Msg.alert("Email Error!", action.result.msg);
				},
				scope: {
					form: this,
					btn: btn
				}
			});
		}else{
			this.invalidElement.update('<span>There are some missing fields in your form request.</span>');
		}
	}
});
