
Ext.namespace('Ext.ux');

Ext.ux.MumbleUserLinkForm = function( config ){
    Ext.apply( this, config );

    Ext.applyIf( this, {
        defaults: { "anchor": "-20px" },
        paramsAsHash: true,
        baseParams: {},
        autoScroll: true,
        submitButtonText: "Submit"
        } );
    Ext.applyIf( this, {
        buttons: [{
                text:    this.submitButtonText,
                handler: this.submit,
                id:      'MumbleUserLinkForm_submit',
                scope:   this
            }]
        });

    var defaultconf = {items:[
    {
        "fieldLabel": "User name and Login", 
        "name": "name", 
        "xtype": "textfield"
    }, 
    {
        "inputType": "password", 
        "fieldLabel": "Password", 
        "name": "password", 
        "xtype": "textfield"
    }, 
    {
        "fieldLabel": "Link account", 
        "name": "linkacc", 
        "xtype": "checkbox"
    }, 
    {
        "text": "The account already exists and belongs to me, just link it instead of creating.", 
        "xtype": "label", 
        "cls": "form_hint_label"
    }
],fileUpload: false,};

    Ext.applyIf( this, defaultconf );
    this.initialConfig = defaultconf;

    this.api = {load:  XD_MumbleUserLinkForm.get,submit:XD_MumbleUserLinkForm.update,choices:XD_MumbleUserLinkForm.choices,};

    Ext.ux.MumbleUserLinkForm.superclass.constructor.call( this );

    this.form.api = this.api;
    this.form.paramsAsHash = true;

    if( typeof config.pk != "undefined" ){
        this.load();
    }

    this.form.addEvents({
        'submitSuccess': true,
        'submitFailure': true
    });

    if( typeof config.listeners != "undefined" ){
        if( typeof config.listeners.submitSuccess != "undefined" )
            this.form.on("submitSuccess", config.listeners.submitSuccess);
        if( typeof config.listeners.submitFailure != "undefined" )
            this.form.on("submitFailure", config.listeners.submitFailure);
    }
}

Ext.extend( Ext.ux.MumbleUserLinkForm, Ext.form.FormPanel, {
    load: function(){
        this.getForm().load({ params: Ext.applyIf( {pk: this.pk}, this.baseParams ) });
    },
    submit: function(){
        this.getForm().submit({
            params: Ext.applyIf( {pk: this.pk}, this.baseParams ),
            failure: function( form, action ){
                if( action.failureType == Ext.form.Action.SERVER_INVALID &&
                    typeof action.result.errors['__all__'] != 'undefined' ){
                    Ext.Msg.alert( "Error", action.result.errors['__all__'] );
                }
                form.fireEvent("submitFailure", form, action);
            },
            success: function( form, action ){
                form.fireEvent("submitSuccess", form, action);
            }
        });
    },
} );

Ext.reg( 'mumbleuserlinkform', Ext.ux.MumbleUserLinkForm );

