Ext.ns('Ext.ecpack');


/**
 * Ext ECPack
 *
 * @author NOMURA Akiyuki <aki-nomura@sus4.co.jp
 */

Ext.ecpack.UserRegister = Ext.extend(Ext.ecpack.Base, {
    labelAlign: 'left',


    headerCfg: {
        html: 'ユーザー登録に必要な情報をご入力の上、送信ボタンを押してください。',
        style: 'padding: 10px;'
    },



    initComponent: function(){
        
        this.defaultItems = [
            {
                name: 'nickname',
                fieldLabel: 'ユーザー名',
                allowBlank: false,
                vtype: 'alphanum',
                anchor: '95%'
            },
            {
                name: 'email',
                fieldLabel: 'Eメール',
                allowBlank: false,
                vtype: 'email',
                anchor: '95%'
            },
            {
                xtype: 'fieldset',
                title: 'パスワード',
                defaultType: 'textfield',
                layout: 'column',
                anchor: '95%',
                defaults: {
                    allowBlank: false,
                    inputType: 'password',
                    plugins: [Ext.ux.FieldLabeler],
                    columnWidth: 0.5,
                    vtype: 'alphanum'
                },
                items: [
                    {
                        name: 'password',
                        fieldLabel: 'パスワード'
                    },
                    {
                        name: 'password_confirm',
                        fieldLabel: '&nbsp;パスワード(確認)'
                    }
                ]
            }
        ];
        this.defaultItemsAfter = [
            {
                xtype: 'checkbox',
                name: 'agree',
                inputValue: 1,
                boxLabel: '規約に同意して登録します'
            }
        ];

        Ext.apply(this, {
            url: String.format('{0}/index.php/user/submit', Ext.ecpack.config.baseUrl),
            buttons: [
            {
                text: 'キャンセル',
                handler: this.onCancel.createDelegate(this)
            },
            {
                text: '登録',
                handler: this.onSend.createDelegate(this)
            }
            ]
        });

        Ext.ecpack.UserRegister.superclass.initComponent.call(this); 
    },



    onSend: function(config)
    {
        config = config || {};

        var f = this.getForm();
        var arr = this.getForm().getValues();

        if(arr['password'] !== arr['password_confirm'])
        {
            this.getForm().markInvalid([{
                id: 'password',
                msg: 'パスワード(確認)と一致しません。もう一度入力し直してください'
            }]);
            this.getForm().findField('password').focus();
            return false;
        }

        if(arr['agree'] != 1)
        {
            Ext.Msg.show({
                title: this.title,
                msg: '登録するには、利用規約に同意する必要があります。<br />規約に同意して、登録を進めてよろしいですか。',
                buttons: Ext.Msg.YESNO,
                animEl: this.dom,
                icon: Ext.Msg.QUESTION,
                fn: function(detail)
                {
                    if(detail === 'yes')
                    {
                        this.getForm().findField('agree').setValue(true);
                        this.onSend();
                    }
                },
                scope: this
            });

            return false;
        }

        Ext.ecpack.UserRegister.superclass.onSend.call(this);
    }


});
Ext.reg('ecpackUserRegister', Ext.ecpack.UserRegister); 

