var PpsMailWindow = function() {	

	this.ppsMailForm = new PpsMailForm();
	
	this.url = "index.php?eID=profile_pages_system_form_mailer";
	
	PpsMailWindow.superclass.constructor.call(this, new Ext.Window({
        title    : 'E-Mail schreiben ...',
        shadow   : true,
        modal	 : true,
        closable : true,
        width    : 500,
        height   : 500,
        border 	 : false,
        plain    : false,
        footer	 : true,
        maximizable: false,
        resizable: false,
        layout   : 'border',
        items : [
                 this.ppsMailForm
        ]
    }));
	
	this.ppsMailForm.collapsePanel.on("collapse", function(panel) {
	
		this.setHeight(500);
		
		},
		this
	);
	
	this.ppsMailForm.collapsePanel.on("expand", function(p) {
	
		this.setHeight(590);
	
		},
		this	
	);
	
	this.ppsMailForm.closeButton.on("click", function() {		
			this.close();
		}, this);
	
	this.ppsMailForm.sendButton.on("click", this.sendForm, this);
	
	Ext.Ajax.on("beforerequest", this.showWaitMessage, this);
	Ext.Ajax.on("requestcomplete", this.closeWaitMessage, this);
	
}

//Extend the mail window with additional functions
Ext.extend(PpsMailWindow, Ext.Window, {
	
	sendForm: function() {
	
		var mailForm = this.ppsMailForm.getForm();
		
		var fields = mailForm.getValues();
		
		if (mailForm.isValid()) {
			
			for(var index in fields) {
			
				if (fields[index] != "") {
					
					// quote special chars
					fields[index] = fields[index].replace(/\\/g,'\\');
					fields[index] = fields[index].replace(/'/g,"\'");
					fields[index] = fields[index].replace(/"/g,'\"');
				}
				
			}
			
			Ext.Ajax.request({
					url: this.url,
					params: {
				            mail: Ext.util.JSON.encode(fields)
							},
					method: 'POST'
			});
			
		} else {
			
			Ext.MessageBox.alert("Formular nicht vollständig!", "Einige benötigte Felder sind leider noch nicht vollständig ausgefüllt! Bitte überprüfen Sie Ihre Eingabe");
		
		}
	},
	
	showWaitMessage: function() {
		
		this.waitBox = Ext.MessageBox.wait("Ihre Nachricht wird versandt!", "E-Mail versenden");
	},
	
	closeWaitMessage: function() {
	
		this.waitBox.hide();
		this.close();
	}
});