Ext.ns('ES');

ES.HeatingCounterForm = Ext.extend(Ext.FormPanel,{
  frame:true,
  border:true,
  labelWidth:150,
  width: 608,
  buttons: [{text: 'Sauver'}],
  layout:'form'
  ,buttonAlign:'right'
  ,frame:false
  ,border:true
  //,bodyStyle:'padding:10px'

  ,initComponent: function() {
    var config = {
      items:[{
        title:'Compteur chauffage : '
        ,layout:'form'
        ,xtype:'fieldset'
        ,border:false
        ,defaultType:'textfield'
        ,bodyStyle:'padding:4px'
        ,items:[{
          name:'tree_node_id'
          ,fieldLabel:'Tree Node id'
          ,hidden: true
          ,hideLabel: true
        },{
          xtype:'TextField1'
          ,name:'heatingcountername'
          ,hidden: false
          ,fieldLabel:'Nom du compteur chauffage'
          ,id:'heatingcountername'
          ,allowBlank:false
        },{
          xtype: 'ESenergyCombobox'
        },{
          xtype: 'ESunitCombobox'
        },{
          xtype: 'radio',
          checked: true,
          hidden: true,
          fieldLabel: 'Remplissage',
          id:'simpleRadio',
          boxLabel: '&nbsp;&nbsp;&nbsp;&nbsp;*** Vous ne conaissez pas la consommation détaillée entre 2 remplissages  ',
          name: 'consumptionType'
        },{
          xtype: 'radio',
          hidden: true,
          fieldLabel: 'Relevé',
          id:'doubleRadio',
          boxLabel: '&nbsp;&nbsp;&nbsp;&nbsp;*** Des relevés sont effectués sur votre citerne ',
          name: 'consumptionType'
        }]
      }]
    };
     // apply config
    Ext.apply(this, Ext.apply(this.initialConfig, config));
    // call parent initComponent
    ES.HeatingCounterForm.superclass.initComponent.call(this);
    this.energyComboBox= this.items.itemAt(0).items.itemAt(2);
    this.energyComboBox.on('select',this.showRadioButton, this);
  } // end of function initComponent
  ,onRender:function() {
    ES.HeatingCounterForm.superclass.onRender.apply(this, arguments);
    var unitCombo=this.items.itemAt(0).items.itemAt(3);
    unitCombo.refresh();
  }
  ,filterUnitComboBox:function(){
    var childCombo= this.items.itemAt(0).items.itemAt(3);
    childCombo.store.filter('energyId',1);
  }
  ,isValid:function (){
    return(this.getField('heatingcountername').isValid());
  }
  ,getField:function(fieldName){
    switch(fieldName){
      case 'doubleRadio':
        return this.items.itemAt(0).items.itemAt(5);
      break;
      case 'simpleRadio':
        return this.items.itemAt(0).items.itemAt(4);
      break;
    }
    return this.getForm().findField(fieldName);
  }
  ,setField:function(fieldName,value){
    this.getField(fieldName).setValue(value);
  }
  ,showRadioButton: function(){
    var value = this.energyComboBox.getValue()
    var radio= this.getField('simpleRadio');
    var radio2= this.getField('doubleRadio');
    if(value == 2){
      radio.show();
      radio.container.up('.x-form-item').show();
      radio2.container.up('.x-form-item').show();
      radio2.show();
      this.setLabel('simpleRadio','Remplissage');
      this.setLabel('doubleRadio','Relevé');
    }
    else{
      radio.container.up('.x-form-item').hide();
      radio2.container.up('.x-form-item').hide();
      radio.hide();
      radio2.hide();

    }
  },setLabel: function(fieldName,text){
    var r = this.getForm().findField(fieldName).getEl().up('div.x-form-item');
    r.dom.firstChild.firstChild.nodeValue = String.format('{0}', text);
  }

}); 

Ext.reg('ESheatingcounterform', ES.HeatingCounterForm);

