var isSaveAuth = false;
Ext.onReady(function(){
    Ext.QuickTips.init();
    
    // turn on validation errors beside the field globally
    Ext.form.Field.prototype.msgTarget = 'side';
    
    var cp = new Ext.state.CookieProvider({
        path: "/oav/",
        expires: new Date(new Date().getTime() + (1000 * 60 * 60 * 24 * 60)) //60 days
    });
    Ext.state.Manager.setProvider(cp);
    
    isSaveAuth = cp.get('saveAuth');
    
    startLoginView(cp.get('user', ''), cp.get('password', ''));
});

function startLoginView(user, password){
    // map one key by key code
    var map = new Ext.KeyMap("signon", {
        key: 13, // or Ext.EventObject.ENTER
        scope: myForm,
        fn: function(){
            login.form.submit({
                url: 'signonAction.action',
                waitMsg: 'Please wait...'
            });
        }
    });
    
    var myForm = new Ext.FormPanel({
        labelWidth: 75, // label settings here cascade unless overridden
        frame: true,
        title: 'Sign In',
        bodyStyle: 'padding:5px 5px 0',
        border: false,
        width: 350,
        defaults: {
            width: 230
        },
        defaultType: 'textfield',
        monitorValid: true,
        method: 'POST',
        
        items: [{
            id: 'mail',
            fieldLabel: 'Email',
            name: 'email',
            allowBlank: false,
            vtype: 'email',
            value: user
        }, {
            id: 'pass',
            fieldLabel: 'Password',
            name: 'passwort',
            allowBlank: false,
            inputType: 'password',
            value: password
        }, new Ext.form.Hidden({
            name: 'forwardAction',
            value: forwardAction
        }), new Ext.form.Checkbox({
            id: 'saveAuth',
            name: 'saveAuth',
            boxLabel: 'Save login data',
            hideLabel: true,
            checked: isSaveAuth
        }), new Ext.form.Label({
            html: '<div style="text-align: center;"><a href="anfrage.action">Create a new account</a> | <a href="#" onclick="showLostPassword()">I lost my password</a> | <a href="ootusermanual.pdf" target=_blank>Download user manual</a></div>'
        })],
        buttons: [{
            text: 'Login',
            type: 'submit',
            formBind: true,
            handler: function(){
                myForm.form.submit({
                    url: 'signonAction.action',
                    waitMsg: 'Please wait...'
                });
            }
        }]
    });
    
    myForm.on('actioncomplete', function(f, a){
        if (a.result.message == null) {
            window.location = a.result.redirect;
        } else {
            if (a.result.message != null || a.result.message.length != 0) {
                myForm.findById('mail').markInvalid(a.result.message);
                myForm.findById('pass').markInvalid(a.result.message);
            } else {
                window.location = a.result.redirect;
            }
        }
    });
    
    new Ext.Viewport({
        hideBorders: true,
        layout: 'border',
        items: [{
            region: 'north',
            height: 35,
            contentEl: 'siteheader',
            border: false,
            margins: '0'
        }, new Ext.Panel({
            border: false,
            region: 'center',
            layout: 'column',
            items: [{
                columnWidth: .5
            }, myForm, {
                columnWidth: .5
            }]
        })]
    });
}

function showLostPassword(){
    //password lost window
    var pwpanel = new Ext.form.FormPanel({
        labelWidth: 75,
        stateful: false,
        autoHeight: true,
        border: false,
        monitorValid: true,
        method: 'POST',
        items: [new Ext.form.Label({
            html: 'Enter your email which is registered for you. You get an email with a new password to this address when it is known.<br /><br />'
        }), new Ext.form.TextField({
            fieldLabel: 'Email',
            name: 'pwemail',
			id: 'pwemail',
            width: 200,
            allowBlank: false,
            vtype: 'email'
        })],
		buttons: [{
            text: 'Submit',
            disabled: true,
			formBind: true,
			type: 'submit',
			handler: function(){
				pwpanel.getForm().submit({
					url: 'password.action', 
					waitMsg: 'Requesting new password...',
					success: function() {
						win.close();
						Ext.MessageBox.alert('Passwort sent', 'Your passwort has been sent.');
					},
					failure: function() {
						win.close();
						Ext.MessageBox.alert('Passwort sent', 'Your passwort has been sent.');
					}
				});
			}
        }, {
            text: 'Close',
            handler: function(){
                win.close();
            }
        }]
    });
    
    var win = new Ext.Window({
        title: 'Lost Password',
        stateful: false,
        modal: true,
        closeAction: 'hide',
        layout: 'fit',
        width: 350,
        autoHeight: true,
        resizable: true,
        plain: true,
        items: [pwpanel]
    });
    
    win.show();
}
