var SearchTag =
{
	//below this list and idx are assigned;
	list:null,
	idx:null,
	//prefixes for list items for getElementById
	listPrefix:null,
	//setup on init to avoid document.getElementById
	selectLists:[],
	submit:null,
	cookieName:"st_terms",
	_doRefresh:function(name)
	{
	
		var NOT_FOUND = 0;
		var REFRESH = 1;
		var CLEAR = 2;
		var REFRESH_ALL = 3;
		var state = NOT_FOUND;
		list = this.list;
		var n = list.length;

		listIdx = this.idx;
		var userData = [];
		
		if(name == null)
			state = REFRESH;
		if(this.submit)
		{
			this.submit.disabled = "disabled";
			this.submit.className = "disabled";
			this.query = null;
		}	
		
		var _clear = function()
		{
			if(select.options.length == 1) return;
			while(select.options.length > 1)
			{
				select.remove(1);
			}
			select.className = "disabled";
			select.disabled = "disabled";				
		
		}
		var _addOpt = function(select,option,selected)
		{
			var opt = new Option(option.name,option.id);
	
			if(document.all)
				select.add(opt);
			else
				select.add(opt,null);
			
			if((selected)&&(option.id == selected))
			{
				select.selectedIndex = select.options.length-1;			
			}
			
			return opt;
		}
		var userTerms = FormUtils.getCookie(this.cookieName);
		if(userTerms != null)
		{
			userTerms = userTerms.split(",");
		}
	
		for(var i=0;i<n;i++)
		{
			var currentName = list[i].mnemonic;
			var select = this.selectLists[currentName];
			

			
			switch(state)
			{
			case NOT_FOUND:
				//locate the index list in the indexes
				selectIdx = parseInt(select.options[select.selectedIndex].value);
				if(selectIdx !== 0)
				{
					var searchId = "_"+selectIdx.toString(36);
					listIdx = listIdx[searchId];
					userData.push(selectIdx);

					if(name == currentName)
					{
						state = REFRESH;
	
					}

				}
				else
				{
					state = CLEAR;
				}
			break;
			case REFRESH:
				if(this.listPrefix+name == select.id) return;
				_clear();
				var options = list[i].children;
				var o = options.length;
				var selected = 0;
				
				if(userTerms !== null)
				{
					selected = userTerms[i];
				}
	
				for(var j=0;j<o;j++)
				{
					var option = options[j]; 
					//if(opt == null) continue;
					if(list[i].header)
					{
						var numItems = 0;
						var children = option.children;
						var p = children.length;
						var optTemp = [];
						for(var k = 0;k<p;k++)
						{
							if(listIdx["_"+children[k].id.toString(36)] !== undefined)
							{									
								optTemp.push(children[k]);
								numItems ++;
							}					
						}
						if(optTemp.length > 0)
						{
							opt = _addOpt(select,option);
							opt.className = "header";
	
							p = optTemp.length
							for(var k = 0;k<p;k++)
							{
								_addOpt(select,optTemp[k],selected);
							}
						
						}

					}
					else
					{
						if(listIdx["_"+option.id.toString(36)] !== undefined)
						{
							_addOpt(select,option,selected);
							
						}	
					}
					
								
				}

				select.className = null;
				select.disabled = null;	

				if((userTerms == null))			
					state = CLEAR;
				else
				{
					this._doRefresh(currentName);
					return;
				}
			break;
			case CLEAR:
				_clear();
			break;
			}
		}

		if(userData.length == n)
		{
			this.submit.disabled = null;
			this.submit.className = "submit";
			var searchTerms = userData.toString();
			document.cookie = this.cookieName+"="+escape(searchTerms)+"; path=/";
			document.getElementById(this.listPrefix+"query").value = searchTerms;				
		}			
							
		//if element is null, then just refresh the first element and disable all the others, if they aren't already disabled
					
	},
	init:function()
	{
		var d = document;
		var list = this.list;
		var n = list.length-1;
		var me = this;
		for(var i=n;i>-1;i--)
		{
			
			function _selectRefresh(receiver)
			{				
				
				if((receiver.options[receiver.selectedIndex].className == "header"))
				{
					receiver.selectedIndex++;
				}

				var label = receiver.id.substr(me.listPrefix.length);
				me._doRefresh(label);
		}
			
			//as usual this won't work because of the state of the indexes, better to just chunk out the ending from the item sent
			var select = d.getElementById(this.listPrefix+list[i].mnemonic);
			if (select.addEventListener)
				select.addEventListener( "change", function(){_selectRefresh(this)}, false );
			else if (select.attachEvent)
				select.attachEvent("onchange", function(){_selectRefresh(window.event.srcElement)});
			this.selectLists[list[i].mnemonic] = select;
		}
		this.submit = d.getElementById(this.listPrefix+"submit");

		this._doRefresh(null);
		//bind populate functions to items
		//loop over list and register each item 
		//make it visible
		d.getElementById(this.listPrefix).style.visibility = "visible";
		
	},
	/**
	data is stored in a data file with the structure data[[list],[idx]];
	*/
	setData:function(data)
	{
		this.list = data.list;
		this.idx = data.idx;
	}
}