/**
 * Registos Online v2.0
 * Copyright(c) 2008, Rui Lourenço, INPI
 * rui.lourenco@inpi.pt
 *
 * http://www.inpi.pt
 */
Inpi.Pesquisas.DesignNumero = Ext.extend(Ext.form.FormPanel, {

    frame: true,
    title: Inpi.lblNumTitle1,
    width: 894,
    bodyStyle: 'padding-top: 5px; padding-left: 10px; padding-right: 10px;',
    
    constructor: function(config){
		
		var self = this;
        		
        this.modaStore = new Ext.data.JsonStore({
            url: Inpi.Pesquisas.Config.serverController,
            baseParams: {
                cmd: 'EscolhaModalidade',
				timo: 'D',
				lang: language
            },
            method: 'POST',
            root: 'items',
            totalProperty: 'totalCount',
            fields: ['moda_codi', 'moda_desi', 'moda_desc'],
            id: 'moda_codi',
			listeners: {
				load: {
					scope: this,
					fn: function() {
						this.cboModalidade.setValue(this.modaStore.getAt(0).id);						
					}
				}
			}
        });
        this.modaStore.load();
		
               
        this.cboModalidade = new Ext.form.ComboBox({
            store: this.modaStore,
            fieldLabel: Inpi.lblModalidadeReq,
            displayField: 'moda_desi',
            valueField: 'moda_codi',
            typeAhead: true,
            mode: 'local',
            triggerAction: 'all',
            emptyText: Inpi.lblNumText1,
            selectOnFocus: true,
            forceSelection: true,
            width: 260,
            allowBlank: false,
			hiddenName: 'moda'
        });		
		
        this.fldNumero = new Inpi.Pesquisas.TextField({
            maxLength: 10,
            allowBlank: false,
            fieldLabel: Inpi.lblNumFieldLabel1,
            width: 200,
			name: 'nume'
        });
			
		
		this.pesquisar = new Ext.Button({
                text: Inpi.lblPesquisar,
                iconCls: 'registos-online-button-search-icn',
                handler: this.doPesquisar,
                scope: this
        });

		
        config = Ext.apply({
			labelWidth: 160,
            url: Inpi.Pesquisas.Config.serverController,
            //defaultType: 'textfield',
            monitorValid: true,
            items: [this.cboModalidade, this.fldNumero, this.pesquisar],
            buttonAlign: 'left',
			keys: {
 				    key: Ext.EventObject.ENTER,
    				fn: this.doPesquisar,
    				scope: this
			},
            buttons: [{
                text: Inpi.lblAnterior,
                iconCls: 'registos-online-back-icn',
                handler: this.previousForm,
                scope: this
            }]
        }, config);
        Inpi.Pesquisas.DesignNumero.superclass.constructor.call(this, config);
		
    },
	
	xpto: function() {
		Ext.Msg.alert('hello','...')		
	},
    
    doPesquisar: function(){
        if (this.validateForm()) {

					this.el.mask(Inpi.lblPesquisando, 'x-mask-loading');
                    Ext.Ajax.request({
                        method: 'POST',
                        url: Inpi.Pesquisas.Config.serverController,
                        timeout: Inpi.Pesquisas.Config.timeout,
                        params: {
                            cmd: 'ConsultaDirecta',
							lang: language,
							timo: 'D',
							moda: this.cboModalidade.getValue(),
                            nume: this.fldNumero.getValue()
                        },
                        scope: this,
                        success: function(response, options){
                            this.el.unmask();
							
							var jsonResp = Ext.util.JSON.decode(response.responseText);
                            if (!jsonResp.success) {
                                Ext.Msg.alert(Inpi.lblAtencao, jsonResp.errors.reason);
                            }
                            else {
								this.viewDetail(jsonResp.nord);
                            }
                        },
                        failure: function(response, options){
                            this.el.unmask();
                            Ext.Msg.alert(Inpi.lblErro, Inpi.lblErr1);
                        }                        
                    });


			
        }
    },
	
	viewDetail: function(nord){
		//Ext.Msg.alert('Detail', nord);
		Inpi.Pesquisas.Context.nord = nord;
		showEcran('design-detalhe', this.id);
	},
	
    showNext: function(){
		this.getForm().reset();
	},	
    
    resetForm: function(){
        this.getForm().reset();
    },
	
    previousForm: function(){
        showPreviousEcran(this.id);
    },	
    
    validateForm: function(){

        if (!this.getForm().isValid()) {
            Ext.Msg.alert(Inpi.lblAtencao, Inpi.lblAviso1);
            return false;
        }
		
        return true;
    }
    
});

Ext.reg('design-numero', Inpi.Pesquisas.DesignNumero);

