var SelectHelper = 
{
	Validar: function(v)
	{
		if (v == '' || v == null || typeof(v) == 'undefined')
		{
			return false;
		}
		else
		{
			if (typeof(v) == 'string')
			{
				var cmp = Ext.getCmp(v);
				
				if (typeof(cmp) == 'undefined')
				{
					cmp = document.getElementById(v);
				}
				
				return cmp;
			}
			else
			{
				if (typeof(v.type != 'undefined') && v.type == 'select-one')
				{
					return v;
				}
				
				if (typeof(v) == 'object' && v.id != null && v.id != '')
				{
					return Ext.getCmp(v.id);
				}
				else
				{
					return v;
				}
			}
		}
	},
	
	AgregarOption: function(selectDestino, opTexto, opValor, seleccionado)
	{
		selectDestino = this.Validar(selectDestino);
		
		if (!selectDestino || typeof(selectDestino) == 'undefined') return;
		
		if (this.ExisteOption(selectDestino, opValor))
		{
			return;
		}
		
		if (typeof(selectDestino.type != 'undefined') && selectDestino.type == 'select-one')
		{
			var nuevoOption = document.createElement('option');
		
			nuevoOption.text 		= opTexto;
			nuevoOption.value 		= opValor;
			nuevoOption.label 		= opTexto;
			nuevoOption.innerHTML	= opTexto;
			
			if (typeof(seleccionado) != 'undefined' && seleccionado)
			{
				nuevoOption.selected = true;
			}
			
			selectDestino.insertBefore(nuevoOption, selectDestino.options[0]);
			
			if (typeof(seleccionado) == 'undefined' || seleccionado == false)
			{
				var t = this;

				setTimeout(function()
				{
					t.SetOptionSeleccionada(selectDestino, '');
				}, 1);
			}
		}
		else
		{
			var selStore = selectDestino.getStore();
			
			var defaultData = {
			    value: opValor,
			    text: opTexto
			};
			
			var recId = opValor; // provide unique id for the record
			
			var r = new selStore.recordType(defaultData, recId);
			
			selStore.insert(selStore.getCount(), r);
		}
	},
	
	Vaciar: function(selectDestino)
	{
		selectDestino = this.Validar(selectDestino);
		
		if (!selectDestino || typeof(selectDestino) == 'undefined') return;
		
		if (typeof(selectDestino.type != 'undefined') && selectDestino.type == 'select-one')
		{
			while(selectDestino.options.length > 0)
			{
				selectDestino.remove(0);
			}
		}
		else
		{
			var selStore = selectDestino.getStore();
			
			selStore.removeAll();
			
			selectDestino.setValue('');
		}
	},
	
	GetValorSeleccionado: function(selectDestino)
	{
		selectDestino = this.Validar(selectDestino);
		
		if (!selectDestino || typeof(selectDestino) == 'undefined') return;
		
		if (typeof(selectDestino.type != 'undefined') && selectDestino.type == 'select-one')
		{
			for (var i=0; i < selectDestino.options.length; i++)
			{
				if (selectDestino.options[i].selected == true)
				{
					return selectDestino.options[i].value;
				}
			}
			
			return false;
		}
		else
		{
			return selectDestino.getValue();
		}
	},
	
	GetTextoSeleccionado: function(selectDestino)
	{
		selectDestino = this.Validar(selectDestino);
		
		if (!selectDestino || typeof(selectDestino) == 'undefined') return;
		
		if (typeof(selectDestino.type != 'undefined') && selectDestino.type == 'select-one')
		{
			for (var i=0; i < selectDestino.options.length; i++)
			{
				if (selectDestino.options[i].selected == true)
				{
					return selectDestino.options[i].text;
				}
			}
			
			return false;
		}
		else
		{
			var selStore = selectDestino.getStore();
			
			var selRecord = selStore.getById(selectDestino.getValue());
			
			return selRecord.get('text');
		}
	},
	
	SetOptionSeleccionada: function(selectDestino, optionValue)
	{
		selectDestino = this.Validar(selectDestino);
		
		if (!selectDestino || typeof(selectDestino) == 'undefined') return;
		
		if (typeof(selectDestino.type != 'undefined') && selectDestino.type == 'select-one')
		{
			for (var i=0; i < selectDestino.options.length; i++)
			{
				if (selectDestino.options[i].value == optionValue)
				{
//					selectDestino.options[i].selected = true;
					selectDestino.selectedIndex = i;
					return false;
				}
			}
		}
		else
		{
			selectDestino.setValue(optionValue);
		}
	},
	
	GetOptionTexto: function(selectDestino, optionValue)
	{
		selectDestino = this.Validar(selectDestino);
		
		if (!selectDestino || typeof(selectDestino) == 'undefined') return;
		
		if (typeof(selectDestino.type != 'undefined') && selectDestino.type == 'select-one')
		{
			for (var i=0; i < selectDestino.options.length; i++)
			{
				if (selectDestino.options[i].value == optionValue)
				{
					return selectDestino.options[i].text;
				}
			}
			
			return false;
		}
		else
		{
			var selStore = selectDestino.getStore();
			
			var selRecord = selStore.getById(optionValue);
			
			return selRecord.get('text');
		}
	},
	
	ExisteOption: function(selectDestino, optionValue)
	{
		selectDestino = this.Validar(selectDestino);
		
		if (!selectDestino || typeof(selectDestino) == 'undefined') return;
		
		if (typeof(selectDestino.type != 'undefined') && selectDestino.type == 'select-one')
		{
			for (var i=0; i < selectDestino.options.length; i++)
			{
				if (selectDestino.options[i].value == optionValue)
				{
					return true;
				}
			}
			
			return false;
		}
		else
		{
			var selStore = selectDestino.getStore();
			
			if (selStore.getById(optionValue))
			{
				return true;
			}
			
			return false;
		}
	},
	
	SetDisabled: function(selectDestino, valor)
	{
		selectDestino = this.Validar(selectDestino);
		
		if (!selectDestino || typeof(selectDestino) == 'undefined') return;
		
		if (typeof(selectDestino.type != 'undefined') && selectDestino.type == 'select-one')
		{
			selectDestino.disable = true;
		}
		else
		{
			selectDestino.setDisabled(valor);
		}
	}
};
