﻿
function forgotPassword() {
    if (Ext.getCmp('loginWindow')) {
        Ext.getCmp('loginWindow').close();
    }

    Ext.Msg.prompt('Forgot your password?', 'Enter your login email below', function(btn, value) {
        if (btn == 'ok') {
            Ext.Msg.wait('Sending', 'Please wait...');
            Ext.Ajax.request({
                url: 'accountDataHandler.ashx',
                params: {
                    type: 'forgotPassword',
                    'email': value
                },
                success: function(result) {
                    result = Ext.decode(result.responseText);
                    
                    if (result.success)
                        Ext.Msg.alert('Information', result.message);
                    else
                        Ext.Msg.alert('Error', result.message);
                },
                failure: function() { Ext.Msg.alert('Error', 'Could not complete the operation.'); }                
            });
        }
    });
}

function loginPopupShow(redirectUrl, cancelRedirectUrl) {
    var submit = function() {
        if (!ValidateForm(loginPopupForm)) {
            return;
        }

        Ext.Msg.wait('Logging in...', 'Please wait')
        loginPopupForm.getForm().submit({
            success: function(f, a) {
                document.location.href = redirectUrl;
            },
            failure: function(f, a) {
                var responseText = Ext.decode(a.response.responseText);

                if (responseText.notActivated) {
                    showAccountNotActivatedMessage();
                } else {

                    Ext.Msg.buttonText.ok = 'Try Again';
                    Ext.Msg.show({
                        title: 'Error',
                        msg: responseText.message,
                        buttons: Ext.Msg.OKCANCEL,
                        animEl: 'elId',
                        fn: function(btn) {
                            Ext.Msg.buttonText.ok = 'OK';
                            if (btn == 'cancel')
                                loginWindow.close();
                        }
                    });
                }
            }
        });
    };

    if (!redirectUrl) {
        redirectUrl = window.location.href;
    }

    var redirectUrlParam = '';

    if (redirectUrl) {
        redirectUrlParam = '&redirectUrl=' + escape(redirectUrl);
    }

    var loginPopupForm = new Ext.form.FormPanel({
    url: 'default.aspx?login=true' + redirectUrlParam,
        id: 'loginPopupForm',
        region: 'center',
        width: 350,
        border: false,
        monitorValid: true,
        monitorPoll: 250,
        margins: '3 0 3 3',
        cmargins: '3 3 3 3',
        labelWidth: 70,
        bodyStyle: 'background: none; padding-top: 10px; padding-left: 5px;',
        defaults: { width: 240 },
        defaultType: 'textfield',
        items: [{
            fieldLabel: 'Email',
            name: 'loginField',
            allowBlank: false,
            vtype: 'email'
        }, {
            fieldLabel: 'Password',
            name: 'password',
            allowBlank: false,
            inputType: 'password'
        }, {
            xtype: 'label',
            html: '<table width="230px;" style="margin-top : 7px;"><tr align=center><td align="center"><a style="float: right; color: #c02206;" href=\"javascript:forgotPassword()\">Forgot your password?</a></td></tr></table>',
            style: 'font-size:12px;'
}],
            buttons: [{
            text: 'Login',
                handler: submit
            }, {
                text: 'Cancel',
                handler: function() {
                    loginWindow.close();

                    if (cancelRedirectUrl) {
                        document.location.href = cancelRedirectUrl;
                    }
                }
            }
            ]
        });

    var loginWindow = new Ext.Window({
        id: 'loginWindow',
        title: 'Login',
        closable: true,
        width: 350,
        height: 160,
        //border : false,
        plain: true,
        layout: 'border',
        modal: true,
        resizable: false,
        items: [loginPopupForm],
        listeners: {
            show: function() {
                setTimeout(function() {
                    loginPopupForm.getForm().findField('loginField').focus();
                }, 100);
                new Ext.KeyMap(Ext.getCmp('loginPopupForm').body, [{
                    key: Ext.EventObject.ENTER,
                    fn: function() {
                        submit();
                    },
                    scope: this
                }]);
            }
        }
    });

    loginWindow.show();
}
