MessageWriteWindow = function (config) {
	this.width = 520;
	this.height = 420;
	this.border = false;
	this.title = "Nachricht schreiben";
	Ext.apply(this,config);
	MessageWriteWindow.superclass.constructor.call(this);
}
Ext.extend(MessageWriteWindow,Ext.Window,{
	onActionComplete : function (f, a){
					if(a && a.result && a.result.success){
						var path = window.location.pathname,
							path = path.substring(0, path.lastIndexOf('/') + 1);
						    Ext.MessageBox.alert('Nachricht gesendet', a.result.msg);
							//window.location = path;  // Change this path to redirect to your path
						window.location.reload( false );
					 }
	},
	onActionFailed : function(f,a){
			this.onCapthaChange();
			var form = this.messagewritePanel.getForm();
			Ext.MessageBox.alert('Fehler', a.result.errors.msg);
			if (a.result.errors.id){
				var f = form.findField(a.result.errors.id);
				if(f){
					f.focus();
				}
			}
			//Do Action when Login Failed
	},
	onCapthaChange : function(){
				var captchaURL = this.captchaURL;
				var curr = Ext.get('activateCodeImg');
				curr.slideOut('b', {callback: function(){
							Ext.get( 'activateCodeImg' ).dom.src= captchaURL+new Date().getTime();
							curr.slideIn('t');		
				}},this);
	},
	initComponent : function () {
				this.submitUrl = "ajax.php";
				this.captchaURL = "captcha/CaptchaSecurityImages.php?width=160&height=80&characters=4&t=";
				var boxCaptcha = 	new Ext.BoxComponent({
									columnWidth:.35,	
									autoEl: {
										tag:'img'
										,id: 'activateCodeImg'
										,title : 'Anklicken, um ein neues zu laden'
										,src:this.captchaURL+new Date().getTime()
									}
									,listeners : {
											'click' : function () {
											Ext.MessageBox.alert('test');
											}
										}	
				});
				boxCaptcha.on('render',function (){
					var curr = Ext.get('activateCodeImg');
					curr.on('click',this.onCapthaChange,this);
				},this);
				this.messagewritePanel = new Ext.form.FormPanel({
						frame:true,
						height: 100,
						region : 'center',
						id: 'messagewritepanel',
						baseParams : {
							task : 'messagewrite'
						},
						bodyStyle:'padding:10px',
						buttons : [
							{
								text : 'Senden',
								handler: onSubmit
							}
						],
						items: [
								{
									layout : 'form',
									items : [
										{
											layout : 'form',
											columnWidth:.65,
											layoutConfig: {
												labelSeparator: ''
											},
											items : [
														{
															fieldLabel: 'Ihre E-Mailadresse',
															name: 'email',
															xtype : 'textfield',
															anchor:'90%',
															vtype: 'email' 
														},
														{
															fieldLabel: 'Name / Vorname',
															xtype : 'textfield',
															name: 'name',
															anchor:'90%'
														},
														{
															fieldLabel: 'Nachricht',
															xtype : 'textarea',
															name: 'message',
															height: '150',
															anchor:'90%'
														},
														{
															fieldLabel: 'Sicherheitscode ',
															name: 'code',
															xtype : 'textfield',
															anchor:'90%'											
														}
											]
									},
										boxCaptcha
									]
								}
						]
						,listeners: {
							'actioncomplete': {
								fn: this.onActionComplete,
								scope: this
							},
							'actionfailed': {
								fn: this.onActionFailed,
								scope: this
							}
						}
						,url: this.submitUrl
					});		
			var form = this.messagewritePanel.getForm();
			function onSubmit() {
				form.submit({
					reset : true
				});
			};
		
			this.layout = "border";
			this.items = [
				/*
				{
					name: 'message',
					xtype:'textarea',
					//html : 'Nachricht',
					region : 'north',
					height: 140
				},
				*/
				this.messagewritePanel
			];
			MessageWriteWindow.superclass.initComponent.call(this);
	}
});
//Ext.onReady(function () {
function show_message_write() {
	var messagewritewindow = new MessageWriteWindow();
	messagewritewindow.show();
}
//});
