if (!Ext.ww) Ext.ww = {};
Ext.ww.advFieldSet =  Ext.extend(Ext.form.FieldSet, {
  layout: 'table', autoHeight: true, border:false,
  layoutConfig: { columns: 1 }, initItemsHeight: false, autoInitHeight: true,
  initComponent: function() {
    if (this.advItems.length>1)
      this.layoutConfig.columns = this.advItems.length;

    for (var i=0; i<this.advItems.length; i++)
    {
      var panelWidth = ((this.width)?this.width/this.advItems.length : '100%');
      var tmpPanel = new Ext.Panel({layout:'form', border: false, width: panelWidth, style: 'padding-right: 20px;', labelWidth: this.labelWidth});

      for (var itemName in this.advItems[i])
      {
        var currItem = this.advItems[i][itemName];
        var tmpItem;

        if ( typeof(currItem) == "string" )
          tmpItem = new Ext.form.TextField({ name: itemName, fieldLabel: currItem, allowBlank: false });
        else
        {
          if (!currItem.conf) currItem.conf = {};
          currItem.conf.id = itemName;
          currItem.conf.name = itemName;
          currItem.conf.type = currItem.type || 'textfield';
          currItem.conf.value = currItem.value;
          currItem.conf.fieldLabel = currItem.label || itemName;
          currItem.conf.allowBlank = currItem.conf.allowBlank || false;
          tmpItem = ww.form._getFieldItem(currItem.conf);
        }

        if (tmpItem)
          tmpPanel.add(tmpItem);
      }
      this.add(tmpPanel);
    }
    //Ext.Window.superclass.initComponent.call(this);
  }, doLayout: function () {
    Ext.Window.superclass.doLayout.call(this);
    if (this.autoInitHeight && !this.initItemsHeight)
    {
      var item = this.items.items;
      var maxHeight = 0;
      for (var i=0; i<item.length; i++)
        if (typeof(item[i])!="function")
        {
          var itemHeight = item[i].getInnerHeight();
          if (itemHeight > maxHeight)
          {
            maxHeight = itemHeight;
            for (var j=0; j<i; j++)
              item[j].setHeight(maxHeight);
          }
          else
            item[i].setHeight(maxHeight);
        }      
      this.initItemsHeight = true;
    }
  }
});

if (!ww.form) ww.form ={};
ww.form._getFieldItem = function(conf){
  if (eval('ww.form._' + conf.type ))
    return tmpItem = eval('ww.form._' + conf.type + '(conf)');
  else
    return false;
};

ww.form._textfield = function(conf){
  return new ww.form._textfieldclass(conf);
};

ww.form._textfieldclass = Ext.extend(Ext.form.TextField, {
  maxLengthLockTyping: true,
  doLayout: function () {
    Ext.Window.superclass.doLayout.call(this);
    if (!isNaN (this.maxLength) && (this.maxLength) > 0 && this.maxLength != Number.MAX_VALUE && this.maxLengthLockTyping)
      this.el.dom.maxLength = this.maxLength;      
  }
});

ww.form._email = function(conf){
  conf.vtype = 'email';
  return new ww.form._textfieldclass(conf);
};

ww.form._phonenumber = function(conf){
  conf.vtype = "phonenumber";
  return new ww.form._textfieldclass(conf);
};

ww.form._hpnumber = function(conf){
  conf.vtype = "hpnumber";
  return new ww.form._textfieldclass(conf);
};

ww.form._label = function(conf){
  conf.html = conf.value;
  return (ww.extend({
    layout: '',
    border: false
  }, conf));
};

ww.form._radiogroup = function(conf){
  return ww.form._getRadioCBgroup(conf);
};

ww.form._getRadioCBgroup = function(conf, type){
  var oInt = ww.clone(conf);
  var inputName = conf.name;
  var options = conf.options;
  Ext.apply(oInt, {type: null, options : null, name: 'grp_' + inputName, id: 'grp_' + inputName, columns: (conf.columns||1)});

  var items = []; var i = 0;
  for (var opt in options) {
    if (typeof(options[opt]) != 'function') {
      items[i] = ww.el.getObject_viaSingle(options[opt], ['inputValue', 'boxLabel'], (typeof(options[opt])=='string')?i:0);
      if (conf.value){
        conf.value = conf.value.replace (" ", "");
        var arrValue = conf.value.split(",");
        
        for (var j=0; j<arrValue.length; j++)
          if (items[i].inputValue == arrValue[j]) {
            items[i].checked = true;
            break;
          }
      }      
      Ext.apply(items[i], {name: inputName, parentid: 'grp_' + inputName});
      i++;
    }
  }

  oInt.items = items;
  return ((type=='cbg')?new Ext.form.CheckboxGroup(oInt):new Ext.form.RadioGroup(oInt));
};

ww.form._getComboArrayItems = function(value, label){
  var tmpArray= new Array(2);
  tmpArray[0] = value;
  tmpArray[1] = label;
  return tmpArray;
};

ww.form._combobox = function(conf){
  var options = conf.options;
  var optionFields = conf.optionFields || ['field1', 'field2'];
  conf.options = '';
  conf.optionFields = '';

  if (options.length>0){
    var dataArray = new Array(options.length);
    for (var i=0; i<options.length; i++) {
      var opt = options[i];
      if (typeof(opt) == "string")
        dataArray[i] = ww.form._getComboArrayItems(opt, opt);
      else if (typeof(opt) == "array")
        dataArray[i] = opt;
      else {
        for (var subItem in opt)
          dataArray[i] = ww.form._getComboArrayItems(subItem, opt[subItem]);
      }
    }
  }
  
  var store = new Ext.data.SimpleStore({
    data: dataArray,
    fields: optionFields
  });

  var finalConf = ww.extend({
    store: store, valueField: optionFields[0], displayField: optionFields[1] || optionFields[0], mode: 'local', triggerAction: 'all',
    emptyText: 'Please select ...', editable:false
  }, conf);

  return new Ext.form.ComboBox(finalConf);
};

ww.form._checkboxgroup = function(conf){
  return ww.form._getRadioCBgroup(conf, "cbg");
};

ww.form._textarea = function(conf){
  conf.maxLengthLockTyping = conf.maxLengthLockTyping || true;
  conf.enableKeyEvents = conf.enableKeyEvents || true;
  if (conf.maxLengthLockTyping){
    if (!conf.listeners) conf.listeners = [];
    conf.listeners['keydown'] = function(obj, e){
      if (this.el.dom.value.length >= this.maxLength)
        this.el.dom.value = this.el.dom.value.substring(0, this.maxLength-1);
    };
    conf.listeners['change'] = function(obj, oldV, newV){
      if (this.el.dom.value.length > this.maxLength)
        this.el.dom.value = this.el.dom.value.substring(0, this.maxLength);
    };
  }
  return new Ext.form.TextArea(conf);
};

ww.form._numberfield = function(conf){  
  return new ww.form._numberfieldclass(conf);
};

ww.form._numberfieldclass = Ext.extend(Ext.form.NumberField, {
  maxLengthLockTyping: true,
  doLayout: function () {
    Ext.Window.superclass.doLayout.call(this);
    if (!isNaN (this.maxLength) && (this.maxLength) > 0 && this.maxLength != Number.MAX_VALUE && this.maxLengthLockTyping)
      this.el.dom.maxLength = this.maxLength;
  }
});

ww.form._postcode = function(conf){
  conf.minLength = conf.numLength || conf.minLength || 6;
  conf.maxLength = conf.numLength || conf.maxLength || 6;
  return ww.form._numberfield(conf);
};

/*ww.form._phonenumber = function(conf){
  conf.minLength = conf.minLength || 8;
  conf.maxLength = conf.maxLength || 8;
  return ww.form._numberfield(conf);
};*/
