                                
var winEMail;    
var emailRecords;    
          
var emailFelderStore =  new Ext.data.Store({
	reader: new Ext.data.JsonReader({fields: ['FeldID', 'Name']}),
  	proxy: new Ext.data.HttpProxy({url:'tabellenfelder.php?TabelleID=-1'}) 
});

function leseEmpfaenger(record) {
	Ext.Ajax.request({
		method : 'POST',
		url:'empfaenger.php?TabelleID=' + currentColumnDefinitions.data.tab_id+
		                  '&FeldID='+ record.get("FeldID")+
						  '&Filter='+currentFilter+
						  '&records='+emailRecords,
	   	success:function(response, action) {      
			var obj = Ext.decode(response.responseText);
			Ext.getCmp("Empfaenger").setValue(obj.msg);
	    },
	    failure: function(form, action) {
            Ext.MessageBox.alert(text_29, text_16);
	    }
	});
}

function emailWindow(TabelleID, records) {

	if (version == BASIC_VERSION) {
		Ext.MessageBox.alert(text_28, text_122);
		return;
	}

	if (Ext.isIE) {
		anchor = '80%';
		anchor1 = '80%';	// Fileupload
	}
	else {
		anchor = '95%';
		anchor1 = '90%';	// Fileupload
	}
	
	emailRecords = records;    
	
	if (!winEMail) {
		winEMail = new Ext.Window({
	        layout:'fit',
	        closeAction:'hide',
	        plain: true,
			modal: true,
		    width: 750,  
	        autoHeight:true,
			title:text_17,                      
			items: new Ext.FormPanel({
			    id:'emailFormPanel',
			    fileUpload: true,
			    frame:true,
		        autoHeight:true,
				items:[{
					layout:'column', 
					items:[{
						columnWidth:.5,
						items:[{
					  		xtype:'fieldset',
							title: text_18,
							autoHeight:true,
							items:[new Ext.form.ComboBox({
									id: 'AuswahlEMailFeld',
									hiddenName:'FeldID',
						            fieldLabel: text_19,
							        displayField:'Name',
							        valueField:'FeldID',
									store: emailFelderStore,                                                                 
							        typeAhead: true,
							        triggerAction: 'all',
							        emptyText:text_3,
							        selectOnFocus:true,
									editable: false,
									forceSelection: true,
									allowBlank: false,
								    labelAlign: 'left',
									labelWidth: 75,
									anchor: anchor,  
									listeners: {
								         'select': function(combo, record, index ) {						
												leseEmpfaenger(record);
										    }        
										}
							    }),	new Ext.form.ComboBox({
									id: 'AuswahlAbsender',
							        hiddenName:'Name',
						            fieldLabel: text_20,
							        displayField:'Name',
							        valueField:'Name',
									store: new Ext.data.Store({
										reader: new Ext.data.JsonReader({fields: ['AbsenderID', 'Name']}),
									  	proxy: new Ext.data.HttpProxy({url:'absender.php?TabelleID='+TabelleID})
									}),
							        typeAhead: true,
							        triggerAction: 'all',
							        emptyText:text_4,
							        selectOnFocus:true,
									editable: false,
									forceSelection: true,
									allowBlank: false,
								    labelAlign: 'left',
									labelWidth: 75,
									anchor: anchor
								}), {
									id:'Einzelversand',   
									xtype: 'checkbox',
									fieldLabel: text_21,   
									boxLabel: text_5,
									name: 'Einzelversand',
								    labelAlign: 'left',
									labelWidth: 75,
									anchor: anchor
								},{
									xtype: 'fileuploadfield',
							        msgTarget: 'side',
									id: 'anhang-file',
									emptyText: text_6,
									fieldLabel: text_22, 
									name: 'anhang-file',
									buttonText: '',
									buttonCfg: {
										iconCls: 'upload-icon'
									},        
								    labelAlign: 'left',
									labelWidth: 75,
									anchor: anchor1
								}
							]
					  	}] 
					},{ 
						columnWidth:.50,
						defaults: {
							labelWidth: 1
						},
						items: [{
							xtype:'fieldset',
							title: text_7,
							autoHeight:true,
							items :[{
								id: 'Empfaenger',
								xtype: 'textarea',
								height: 100,
								value: '',
									anchor: anchor
							}]
						}]
					}]
				},{       
					xtype: 'textfield',
			        fieldLabel: text_23,    
					id: 'Betreff',
			        name: 'Betreff',
					allowBlank: true,
					labelWidth: 75,
			        anchor: '100%'
				},{
					id: 'EMailText',
					xtype: 'htmleditor',
			        fieldLabel: text_24,    
					height: 200,
					value: '',
					anchor: '100%',
					labelWidth: 1
				}], 		
			    buttons: [{
			       	id:'Senden',
			        text:text_25,
				 	handler: function () {
				 		var form = Ext.getCmp("emailFormPanel").getForm();

                    	var msg;
						if (Ext.getCmp("anhang-file").getValue() == "")
                    		msg = text_26;
						else
                    		msg = text_27;

			           	if(form.isValid()){
			            	form.submit({
			                	url: 'emailsend.php',
                                waitMsg: msg,
						     	success: function(form, action) {                                        
			                    	Ext.MessageBox.alert(text_28, action.result.msg);
						 			winEMail.hide();						
			                     },
						 	     failure: function(form, action) { 
					 	        	switch (action.failureType) {
					 	            case Ext.form.Action.CLIENT_INVALID:
					 	                Ext.Msg.alert(text_29, text_30);
					 	                break;
					 	            case Ext.form.Action.CONNECT_FAILURE:
					 	                Ext.Msg.alert(text_29, text_31);
					 	                break;
					 	            default:
					 	               Ext.Msg.alert(text_29, action.result.msg);
						 	   		}
						 	    }
			                 });
			             }          
			        }
			    },{
			        text: text_15, 
			     	handler: function() {
			         	winEMail.hide();
			     	}
			    }]
			})
	    });   
	}     
	emailFelderStore.proxy = new Ext.data.HttpProxy({url:'emailfelder.php?TabelleID='+TabelleID})
	emailFelderStore.reload();	
}
   

emailFelderStore.on({
    load: function(store, records, options) {     
		if (store.getCount() == 0)
				Ext.MessageBox.alert(text_29, text_8);
			else {
		 		winEMail.show();   
				winEMail.center();
				if (store.getCount() == 1) {
					r = store.getAt(0);
					Ext.getCmp("AuswahlEMailFeld").setValue(r.get("Name"));
					leseEmpfaenger(r);
				}
		 	}
    }
});





