
Ext.namespace('Ext.ux');

Ext.ux.MumbleForm = 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:      'MumbleForm_submit',
                scope:   this
            }]
        });

    var defaultconf = {items:[
    {
        "fieldLabel": "Server Name", 
        "name": "name", 
        "xtype": "textfield"
    }, 
    {
        "fieldLabel": "Website URL", 
        "name": "url", 
        "xtype": "textfield"
    }, 
    {
        "grow": true, 
        "fieldLabel": "Welcome Message", 
        "name": "motd", 
        "xtype": "textarea"
    }, 
    {
        "fieldLabel": "Server Password", 
        "name": "passwd", 
        "xtype": "textfield"
    }, 
    {
        "text": "Password required to join. Leave empty for public servers.", 
        "xtype": "label", 
        "cls": "form_hint_label"
    }, 
    {
        "inputType": "password", 
        "fieldLabel": "Superuser Password", 
        "name": "supw", 
        "xtype": "textfield"
    }, 
    {
        "fieldLabel": "Player name regex", 
        "name": "player", 
        "xtype": "textfield"
    }, 
    {
        "fieldLabel": "Channel name regex", 
        "name": "channel", 
        "xtype": "textfield"
    }, 
    {
        "displayField": "v", 
        "valueField": "k", 
        "fieldLabel": "Default channel", 
        "name": "defchan", 
        "xtype": "choicescombo"
    }, 
    {
        "fieldLabel": "Timeout", 
        "name": "timeout", 
        "xtype": "numberfield"
    }, 
    {
        "fieldLabel": "Require Certificate", 
        "name": "certreq", 
        "xtype": "checkbox"
    }, 
    {
        "fieldLabel": "Maximum length of text messages", 
        "name": "textlen", 
        "xtype": "numberfield"
    }, 
    {
        "fieldLabel": "Allow HTML to be used in messages", 
        "name": "html", 
        "xtype": "checkbox"
    }, 
    {
        "fieldLabel": "Remember last channel", 
        "name": "rememchn", 
        "xtype": "checkbox"
    }, 
    {
        "text": "Remember the channel users were in when they quit, and automatically move them to that channel when they join.", 
        "xtype": "label", 
        "cls": "form_hint_label"
    }, 
    {
        "fieldLabel": "Version to recommend", 
        "name": "sgversion", 
        "xtype": "textfield"
    }, 
    {
        "fieldLabel": "Suggest to use positional audio", 
        "name": "sgpositional", 
        "xtype": "checkbox"
    }, 
    {
        "fieldLabel": "Suggest to use Push-To-Talk", 
        "name": "sgptt", 
        "xtype": "checkbox"
    }
],fileUpload: false,};

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

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

    Ext.ux.MumbleForm.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.MumbleForm, 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( 'mumbleform', Ext.ux.MumbleForm );

