/*
* Registration.js
*
*/

var eudlr_RegistrationWindow;
var eudlr_RegistrationPanel;
function eudlr_RegistrationResultHandler(res) {
    if(eudlr_WaitMsgBox) {
        eudlr_WaitMsgBox.hide();
        eudlr_WaitMsgBox = null;
    }
    if((!res) || (!res.state)) {
        Ext.Msg.minWidth = 360;
        Ext.Msg.show({
            title: 'Systemfehler',
            msg: 'Fehler in der Server-Kommunikation.<br/>Bitte probieren Sie es später noch einmal.',
            modal: true,
            icon: Ext.Msg.ERROR,
            buttons: Ext.Msg.OK
        });
        return;
    }
    if('OK' == res.state) {
        Ext.Msg.minWidth = 300;
        if(0 == res.data) {
            Ext.Msg.show({
                title: 'Registrierung erfolgreich',
                msg: 'Ihre Registrierung war erfolgreich. Sie erhalten eine E-Mail mit weiteren Informationen, wo Sie Ihre Anmeldung auch nochmal bestätigen müssen.',
                modal: true,
                icon: Ext.Msg.INFO,
                buttons: Ext.Msg.OK
            });

            eudlr_RegistrationWindow.close();    
        }
        else if(1 == res.data) {
            Ext.Msg.show({
                title: 'Registrierung fehlgeschlagen',
                msg: 'Ihre Registrierung konnte NICHT durchgeführt werden. Die E-Mail-Adresse wird schon verwendet. Bitte wählen Sie eine andere.',
                modal: true,
                icon: Ext.Msg.ERROR,
                buttons: Ext.Msg.OK
            });
        }
        else {
            Ext.Msg.show({
                title: 'Registrierungsfehler',
                msg: 'Allgemeiner Fehler, probieren Sie es später noch einmal.',
                modal: true,
                icon: Ext.Msg.ERROR,
                buttons: Ext.Msg.OK
            });
        }
    }
}

function eudlr_OpenRegistration() {
    eudlr_RegistrationPanel = new Ext.Panel({
        preventBodyReset: true,
        width: 400,
        height : 300,
        bodyStyle : 'align : center; font-size : 11pt; padding: 4px;',
        items : [
            {
                xtype : 'label',
                text : eudlr_msg_PleaseRegister,
                style : 'font-weight : bold'
            },
            {
                xtype : 'form',
                id : 'registrationForm',
                border:false,
                style : "padding-right : 10px",
                items : [
                    {
                     xtype : 'textfield',
                     id : 'email',
                     name : 'email',
                     fieldLabel : 'E-Mail',
                     allowBlank : false,
                     width : 260
                    },
                    {
                     xtype : 'textfield',
                     id : 'password1',
                     name : 'password1',
                     inputType : 'password',
                     fieldLabel : 'Kennwort',
                     allowBlank : false,
                     width : 260
                    }
                    ,
                    {
                     xtype : 'textfield',
                     id : 'password2',
                     name : 'password2',
                     inputType : 'password',
                     fieldLabel : 'Kennwort-Best.',
                     allowBlank : false,
                     width : 260
                    },
                    {
                     xtype : 'textfield',
                     id : 'firstName',
                     name : 'firstName',
                     fieldLabel : 'Vorname',
                     width : 260
                    },
                    {
                     xtype : 'textfield',
                     id : 'lastName',
                     name : 'lastName',
                     fieldLabel : 'Nachname',
                     width : 260
                    }
                ],
                buttons : [
                    {
                        text : 'OK',
                        handler : function() {
                            var email = Ext.get('email').getValue().trim();
                            
                            if(email.length == 0) {
                                Ext.Msg.minWidth = 240;
                                Ext.Msg.show({
                                    title: 'Eingabefehler',
                                    msg: 'E-Mail darf nicht leer sein!',
                                    modal: true,
                                    icon: Ext.Msg.ERROR,
                                    buttons: Ext.Msg.OK
                                });
                            }
                            else {
                                var pwd1 = Ext.get('password1').getValue();
                                var pwd2 = Ext.get('password2').getValue();
                                if(pwd1 != pwd2) {
                                    Ext.Msg.minWidth = 400;
                                    Ext.Msg.show({
                                        title: 'Eingabefehler',
                                        msg: 'Kenntwort und Kennwort-Bestätigung sind nicht identisch!',
                                        modal: true,
                                        icon: Ext.Msg.ERROR,
                                        buttons: Ext.Msg.OK
                                    });
                                }
                                else if(pwd1.length < 6) {
                                    Ext.Msg.minWidth = 200;
                                    Ext.Msg.show({
                                        title: 'Eingabefehler',
                                        msg: 'Kennwort muss mindestens 6 Zeichen haben!',
                                        modal: true,
                                        icon: Ext.Msg.ERROR,
                                        buttons: Ext.Msg.OK
                                    });
                                }
                                else {
                                    var firstName = Ext.get('firstName').getValue();
                                    if((!firstName) || (firstName.trim().length == 0)) firstName='nn';
                                    var lastName = Ext.get('lastName').getValue();
                                    if((!lastName) || (lastName.trim().length == 0)) lastName='nn';
                                    eudlr_registerUser(
                                        email, pwd1, firstName, lastName, eudlr_RegistrationResultHandler
                                    );
                                    eudlr_WaitMsgBox = Ext.MessageBox.wait('Anfrage läuft...', "Server-Anfrage");
                                }
                            }

                        }
                    },
                    {
                        text : 'Abbrechen',
                        handler : function() {
                            eudlr_RegistrationWindow.close();

                        }
                    }
                ]
            }
        ]
    });
    
    eudlr_RegistrationWindow = new Ext.Window({
                        title:'Benutzerregistrierung EU-DLR-Antragsassistent',
                        autosize : true,
                        x : 100,
                        y : 180,
                        width: 400,
                        height:260,
                        closable: true,
                        items: eudlr_RegistrationPanel,
                        close : function(wnd) {
                            if(eudlr_LoginWindow) eudlr_LoginWindow.enable();
                            eudlr_RegistrationWindow.hide(null);
                            eudlr_RegistrationWindow.destroy();
                            eudlr_RegistrationWindow = null;
                        }
                    });
    eudlr_RegistrationWindow.show();
}



