﻿
function editProfile() {
    var nav = new Ext.form.FormPanel({
        url: 'editProfile.ashx?saveToProfile=true',
        region: 'center',
        width: 450,
        border: false,
        monitorValid: true,
        monitorPoll: 350,
        margins: '3 0 3 3',
        cmargins: '3 3 3 3',
        labelWidth: 130,
        bodyStyle: 'background: transparent; padding-top: 10px; padding-left: 10px;',
        defaults: { width: 250 },
        defaultType: 'textfield',
        items: [{
            fieldLabel: 'Login(email)',
            name: 'email',
            allowBlank: false,
            vtype: 'email'
        }, {
            fieldLabel: 'New password',
            name: 'newpass',
            allowBlank: false,
            inputType: 'password',
            value: '**********'
        }, {
            fieldLabel: 'Your Name',
            name: 'first',
            allowBlank: false
        }, {
            fieldLabel: 'My site is',
            name: 'site',
            allowBlank: false,
            regex: domainValidator,
            regexText: domainInvalidValidatorText
        }, {
            name: 'passwordHash',
            inputType: 'hidden'
        }
        ],
        listeners: {
            beforerender: function() {
                Ext.Ajax.request({
                    url: 'editProfile.ashx',
                    success: function(result) {
                        var user = Ext.decode(result.responseText);
                        var str = user.userCompetitors.toString();

                        nav.getForm().findField('email').setValue(user.email);
                        nav.getForm().findField('site').setValue(user.homeSite);
                        //nav.getForm().findField('last').setValue(user.lastName);
                        nav.getForm().findField('first').setValue(user.firstName);
                        nav.getForm().findField('passwordHash').setValue(user.passwordHasch);
                    },
                    failure: function() { Ext.Msg.alert('Error', 'Cannot load profile settings.'); },
                    params: { updateView: true }
                });
            }
        },

        buttons: [{
            text: 'Save',
            //formBind: true,
            handler: function() {

                if (!ValidateForm(nav)) {
                    return;
                }

                Ext.Msg.wait('Saving...', 'Please wait...');
                Ext.Ajax.request({
                    url: 'editProfile.ashx',
                    success: function(result) {

                        result = Ext.decode(result.responseText);

                        if (result.success) {
                            if (nav.getForm().findField('newpass').getValue() != '**********' && nav.getForm().findField('newpass').getValue() != '') {
                                var hashNewPass = MD5(nav.getForm().findField('newpass').getValue());
                                var oldHash = nav.getForm().findField('passwordHash').getValue();
                                if (hashNewPass != oldHash) {

                                    var passwordPrompt = new Ext.Window({
                                        title: 'Changing password',
                                        closable: true,
                                        width: 300,
                                        height: 120,
                                        plain: true,
                                        layout: 'form',
                                        modal: true,
                                        resizable: false,
                                        border: false,
                                        labelWidth: 250,
                                        labelAlign: 'top',
                                        buttonAlign: 'center',
                                        bodyStyle: 'padding-left: 20px; padding-top: 10px;',
                                        items: [
                                            new Ext.form.TextField({
                                                fieldLabel: 'Please confirm new password',
                                                name: 'password',
                                                width: 250,
                                                allowBlank: false,
                                                inputType: 'password'
                                            })
                                        ],
                                        buttons: [{
                                            text: 'OK',
                                            handler: function(btn) {

                                                var txt = passwordPrompt.items.items[0].getValue();

                                                if (MD5(txt) == hashNewPass) {
                                                    nav.getForm().findField('email').setValue(trimBoth(nav.getForm().findField('email').getValue()));
                                                    nav.getForm().findField('first').setValue(trimBoth(nav.getForm().findField('first').getValue()));
                                                    //nav.getForm().findField('last').setValue(trimBoth(nav.getForm().findField('last').getValue()));
                                                    nav.getForm().findField('site').setValue(trimBoth(nav.getForm().findField('site').getValue()));
                                                    nav.getForm().findField('passwordHash').setValue(hashNewPass);
                                                    nav.getForm().submit({
                                                        success: function(f, a) {
                                                            passwordPrompt.close();
                                                            Ext.Msg.alert('Information', 'Profile settings were successfully saved.');
                                                            profileEdit.close();

                                                            var userInformationPanel = Ext.getCmp('userInformationPanel');
                                                            userInformationPanel.load({ url: userInformationPanel.initialConfig.autoLoad });
                                                        },
                                                        failure: function(f, a) {
                                                            Ext.Msg.alert('Error', 'Cannot save profile settings.');
                                                            passwordPrompt.close();
                                                        }
                                                    });
                                                }
                                                else {
                                                    Ext.Msg.alert('Error', 'Password and confirm password do not match.');
                                                    return false;
                                                }
                                            }
                                        }
                                        ],
                                        listeners: {
                                            hide: function() {
                                                Ext.Msg.hide();
                                            }
                                        }
                                    });

                                    passwordPrompt.show();
                                }
                            } else {

                                nav.getForm().findField('email').setValue(trimBoth(nav.getForm().findField('email').getValue()));
                                nav.getForm().findField('first').setValue(trimBoth(nav.getForm().findField('first').getValue()));
                                //nav.getForm().findField('last').setValue(trimBoth(nav.getForm().findField('last').getValue()));
                                nav.getForm().findField('site').setValue(trimBoth(nav.getForm().findField('site').getValue()));
                                nav.getForm().submit({
                                    success: function(f, a) {
                                        Ext.Msg.alert('Information', 'Profile settings were successfully saved.');
                                        profileEdit.close();

                                        var userInformationPanel = Ext.getCmp('userInformationPanel');
                                        userInformationPanel.load({ url: userInformationPanel.initialConfig.autoLoad });
                                    },
                                    failure: function(f, a) {
                                        Ext.Msg.alert('Error', 'Cannot save profile settings.');
                                    }
                                });
                            }
                        }
                        else {
                            Ext.Msg.alert('Error', result.message);
                        }
                    },
                    failure: function() {
                        Ext.Msg.alert('Error', 'Cannot save profile settings.');
                    },
                    params: { checkEmail: true,
                        email: nav.getForm().findField('email').getValue(),
                        site: nav.getForm().findField('site').getValue()
                    }
                });
            }
        }, {
            text: 'Cancel',
            handler: function() {
                profileEdit.hide();
            }
        }
            ]
    });


    var profileEdit = new Ext.Window({
        title: 'Profile Settings',
        closable: true,
        width: 450,
        height: 190,
        plain: true,
        layout: 'border',
        modal: true,
        resizable: false,
        items: [nav]
    });

    profileEdit.show();
}
