/*global Ext document */
 /*
  * Author: Sierk Hoeksma. WebBlocks.eu
  * Copyright 2007-2008, WebBlocks.  All rights reserved.
  *
  * A set of simple components use in more
  ************************************************************************************
  *   This file is distributed on an AS IS BASIS WITHOUT ANY WARRANTY;
  *   without even the implied warranty of MERCHANTABILITY or
  *   FITNESS FOR A PARTICULAR PURPOSE.
  ************************************************************************************

  License: This source is licensed under the terms of the Open Source LGPL 3.0 license.
  Commercial use is permitted to the extent that the code/component(s) do NOT become
  part of another Open Source or Commercially licensed development library or toolkit
  without explicit permission.Full text: http://www.opensource.org/licenses/lgpl-3.0.html

  * Donations are welcomed: http://donate.webblocks.eu
  */

//Register name spaces used
Ext.namespace('Ext.ux.form');

/**
 * A combo that can be filed from a velues grid. Returns the store form a string or form a datagroup id
 */
Ext.ux.form.ValuesCombo = Ext.extend(Ext.form.ComboBox, {
	
	initComponent  : function(){
		this.valueField = 'value';
		this.displayField = 'text';
		this.triggerAction = 'all';
		this.editable = false;
		this.mode = 'local';
        this.store = new Ext.data.JsonStore({
			root: 'results',
			fields: [ 'value', 'text' ]
		});
        Ext.ux.form.ValuesCombo.superclass.initComponent.call(this);
		
        this.populateStore(this.values);
    },
    
    populateStore: function(list) {
    	var data = Ext.util.JSON.decode(list);
        if (data && data.dg_id && !isNaN(data.dg_id)){
        	p = data.dg_id;
        	Ext.Ajax.request({
				url: '/datagroups/getValues',
				scope: this,
				method:"POST",
				params: {'dg_id': p},
				success: function(response, options) {
					data = Ext.util.JSON.decode(response.responseText);
					if (data && data.results)
	            		this.store.loadData(data);
				}
			});
        } else {
        	if (data && data.results)
    			this.store.loadData(data);
        }
    }

});
Ext.reg('valuescombo', Ext.ux.form.ValuesCombo);

Ext.ux.form.ValuesMultiselect = Ext.extend(Ext.ux.Multiselect, {
	
	initComponent  : function(){
		this.valueField = 'value';
		this.displayField = 'text';
//		this.triggerAction = 'all';
//		this.editable = false;
		this.mode = 'local';
		this.msgTarget = 'side';
        this.store = new Ext.data.JsonStore({
			root: 'results',
			fields: [ 'value', 'text' ]
		});
        Ext.ux.form.ValuesMultiselect.superclass.initComponent.call(this);
		
        this.populateStore(this.values);
    },
    
    populateStore: function(list) {
    	var data = Ext.util.JSON.decode(list);
        if (data && data.dg_id && !isNaN(data.dg_id)){
        	p = data.dg_id;
        	Ext.Ajax.request({
				url: '/datagroups/getValues',
				scope: this,
				method:"POST",
				params: {'dg_id': p},
				success: function(response, options) {
					data = Ext.util.JSON.decode(response.responseText);
					if (data && data.results)
	            		this.store.loadData(data);
				}
			});
        } else {
        	if (data && data.results)
    			this.store.loadData(data);
        }
    }

});
Ext.reg('valuesmultiselect', Ext.ux.form.ValuesMultiselect);