/*
this is used for filtered linked selects
use the Init method to initiate a child select and declare its parent

the FilteredSelects must have options with id attributes. 
value for the option is declared in the value attribute of the option element.
parent of the option is declared in the id attribute of the option and must be hold exactly the same value
corresponding value attribute of one of the options of the parent selects - not necessarily the first parent.
*/

function FilteredSelect_Item_CreateOption(){
	return new Option(this.text, this.value);
}

function FilteredSelect_Item(value, filter, text){
	this.value = value;
	this.filter = filter;
	this.text = text;
	this.checked = false;
	this.createOption = FilteredSelect_Item_CreateOption;
}


function FilteredSelect_Init(filteredSelectId, parentSelectId, val){
	s = document.getElementById(filteredSelectId);
	if(s){
		if(!s.initialized){
			//fix width
			//s.style.width=s.offsetWidth;
			//prepare the items array from the options
			var items = new Array;
			for(i=0; i<s.options.length; i++){
				var item = new FilteredSelect_Item(s.options[i].value, s.options[i].id, s.options[i].innerHTML);
				items[i] = item;
			}
			//attach methods
			document.getElementById(filteredSelectId).Sort = FilteredSelect_Sort;
			document.getElementById(filteredSelectId).Clear = FilteredSelect_Clear;
			document.getElementById(filteredSelectId).Fill = FilteredSelect_Fill;
			document.getElementById(filteredSelectId).AddFilter = FilteredSelect_AddFilter;
			//set FilteredSelect fields
			s.items = items;
			s.initialized = true;
			//if we have a parent select hook to its onchange event handler and run it once
			if(parentSelectId){
				s.parentSelectId = parentSelectId;
				s.parentSelect = document.getElementById(parentSelectId);
				document.getElementById(parentSelectId).childFilteredSelect = s;
				document.getElementById(parentSelectId).onchange = FilteredSelect_ParentSelect_OnChange;
				document.getElementById(parentSelectId).change = FilteredSelect_ParentSelect_OnChange;
				document.getElementById(parentSelectId).FilterChild = FilteredSelect_ParentSelect_FilterChild;
				//save selectedIndex (IE bug, IE saves selectedIndex on history.back() rather than the selectedValue)
				//selectedIndex = s.selectedIndex;
				if(val){
					s.value = val
				}
				//run the parents change() to run inital filter
				document.getElementById(parentSelectId).change();
				//restore saved selectedIndex
				//s.selectedIndex = selectedIndex;
				
			}
		}
	}
	else
	{
		alert("FilteredSelect javascript error: "+selectId+" does not exist!");
	}
}

function FilteredSelect_Clear(){
	//preserve the selected value
	this.selectedValue = this.value;
	//uncheck all items
	for(i=0; i<this.items.length; i++){	
		this.items[i].checked = this.items[i].value=="any";
	}
	//clear the select options
	this.innerHTML = '';
}

function FilteredSelect_Fill(){
	for(i=0; i<this.items.length; i++){
		if(this.items[i].checked) {
			//add the option
			this.options[this.options.length] = this.items[i].createOption();
			//preserve the selected value
			if(this.options[this.options.length-1].value==this.selectedValue) {
				this.selectedIndex = this.options.length-1;
			}
		}
	}
}

function FilteredSelect_Sort(){
	for(i=0; i<this.options.length-1; i++){
		for(j=i; j<this.options.length; j++){
			if((this.options[j].innerHTML<this.options[i].innerHTML) && ((this.options[i].value!="any")) || (this.options[j].value=="any")){
				optioni = new Option(this.options[i].innerHTML, this.options[i].value)
				optionj = new Option(this.options[j].innerHTML, this.options[j].value)
				this.options[i] = optionj;
				this.options[j] = optioni;
			}
		}
	}
}

function FilteredSelect_ParentSelect_FilterChild(targetChild){
		if(this.value=="any"){
			//if any is selected then we must loop through the entire options collection
			//and add child options to the child select for each existing option in the parent select options collection
			for(var i=0; i<this.options.length; i++){
				if(this.options[i].value!="any"){
					targetChild.AddFilter(this.options[i].value);	
				}
			}
			//go to the next parent up...		
			if(this.parentSelect){	
				this.parentSelect.FilterChild(targetChild);
			}
		}
		else
		{
			targetChild.AddFilter(this.value);	
		}	
}

function FilteredSelect_ParentSelect_OnChange(){
	if(this.childFilteredSelect && this.childFilteredSelect.initialized){	
		//keep the selectedValue if possible
		selectedValue = this.childFilteredSelect.value;
		//clear the child FilteredSelect
		this.childFilteredSelect.Clear();
		//apply new filters to child
		this.FilterChild(this.childFilteredSelect);
		//fill the child filtered select
		this.childFilteredSelect.Fill();
		//disable the child select if only 1 option is available
		this.childFilteredSelect.disabled = this.childFilteredSelect.options.length<=1
		//call the childs onchange since its options changed its selectedvalue may have also changed
		if(this.childFilteredSelect.childFilteredSelect) this.childFilteredSelect.change();
	}
	else
	{
		//the child FilteredSelect does not exist yet or isn't initialized yet so we'll recall this function in 10ms
		window.setTimeout("document.getElementById('"+this.id+"').change()", 10);
	}
}

function FilteredSelect_AddFilter(filter){
	for(i=0; i<this.items.length; i++){
		if(this.items[i].filter==filter) {
			this.items[i].checked=true;
		}
	}
}