Ext.ns('am.eu.ControllFormPanel');
am.eu.FormPanel = Ext.extend( Ext.form.FormPanel,{
    postParams:{}
    ,initComponent: function(){
        Ext.apply( this,{
            border: false
            ,bodyStyle: "background:none"
        });
        am.eu.FormPanel.superclass.initComponent.apply( this, arguments );

        this.afterMethod( 'onRender', this.doRender, this);
        this.afterMethod( 'show', this.doShow, this);
    }
    
    ,doRender: function(){
        var el = this.getEl();
        this.getForm().waitMsgTarget = this.getEl();
        if( this.initialConfig.messageText ){
            //this.initialConfig.messageText && Ext.fly( 'EUInfoText' ).update( this.initialConfig.messageText );
            el.insertFirst({
                tag: 'div',
                cls: 'message',
                html: this.initialConfig.messageText
            });
        }
    }

    ,failure: function( form, action ){
        Ext.Msg.alert( 'Warning', ( action.result && action.result.errorMessage ) || 'Failure' );
    }

    ,doShow: function(){
        this.getForm().reset();
    }

});

Ext.ns('am.eu.ControllFormPanel');
am.eu.ControllFormPanel = Ext.extend( am.eu.FormPanel, {
    
    initComponent: function(){
        Ext.apply( this,{
            items: [{
                xtype: 'textfield'
                ,fieldLabel: 'Registreringsnr'
                ,name: 'RegistrationNumber'
                ,width: 90
				,listeners:{
                    specialkey:{
                        fn: function(f,o){
                            if(o.getKey()==13){
                                this.submitOK();
                            }
                        }
                        ,scope: this
                     }
                }
                //,allowBlank: false
            }]
            ,buttons: [{
                text: 'Søk'
                ,name: 'send'
                ,formBind: true
                ,scope: this
                ,handler: this.submitOK
            }]
        });
        am.eu.ControllFormPanel.superclass.initComponent.apply( this, arguments );
    }

    ,submitOK: function(){
        this.getForm().submit({
             url: '/bridge/mnet/EuControll/search'
            ,scope: this
            ,success: function( form, action ){
                Ext.fly('EUContent').update( action.result.html);
                this.hide();
                Ext.getCmp( 'EU_RegistrationFormPanel').show();
            }
            ,failure: this.failure
            ,timeout: 5000
            ,params: {
                action: 'save'
            }
            ,waitMsg: 'Laster...'
        });
    }
    
});

Ext.ns('am.eu.RegistrationFormPanel');
am.eu.RegistrationFormPanel = Ext.extend( am.eu.FormPanel, {

    initComponent: function(){
        Ext.apply( this,{
            labelWidth: 50
            ,defaults:{
                width: 140
            }
            ,items: [{
                xtype: 'textfield'
                ,fieldLabel: 'Navn'
                ,name: 'Name'
            },{
                xtype: 'textfield'
                ,fieldLabel: 'E-post'
                ,name: 'Email'
            },{
                xtype: 'textfield'
                ,fieldLabel: 'Mobil'
                ,name: 'Mobile'
            },{
                xtype: 'textfield'
                ,fieldLabel: 'Postnr.'
                ,name: 'ZipCode'
            }]
            ,buttons: [{
                text: 'Avbryt'
                ,name: 'cancel'
                ,formBind: true
                ,scope: this
                ,handler: this.submitCancel
            },{
                text: 'Gi meg varsel'
                ,name: 'send'
                ,formBind: true
                ,scope: this
                ,handler: this.submitSend
            }]
        });
        am.eu.RegistrationFormPanel.superclass.initComponent.apply( this, arguments );
    }

    ,submitSend: function(){
        this.getForm().submit({
             url: '/bridge/mnet/EuControll/registration'
            ,scope: this
            ,success: function( form, action ){
                Ext.fly('EUContent').update( action.result.html);
                this.hide();
                Ext.getCmp( 'EU_ControllFormPanel').show();
            }
            ,failure: this.failure
            ,timeout: 5000
            ,params: {
                action: 'save'
            }
            ,waitMsg: 'Lagring...'
        });
    }

    ,submitCancel: function(){
        this.hide();
        Ext.getCmp( 'EU_ControllFormPanel').show();
    }
    //Lagring
});

Ext.onReady(function() {
    Ext.QuickTips.init();
    
    var controllPanel = new am.eu.ControllFormPanel({
        renderTo: 'EUForms'
        ,id:'EU_ControllFormPanel'
        ,messageText: 'Sjekk når bilen din skal på EU kontroll'
    });
    var registrationPanel = new am.eu.RegistrationFormPanel({
        hidden: true
        ,renderTo: 'EUForms'
        ,id:'EU_RegistrationFormPanel'
        ,messageText: 'Jeg vil at Automester gir meg varsel på neste EU kontroll på e-post og SMS'
    });
    
});