/********************************************************************************
	The filter script is a *.php file which receives $_POST['id']. 
	The return data is in format "id~name||id~name||id~name||" and so forth
	
	The script requires prototype.js to work. Uses Ajax.
********************************************************************************/
function _$$(el){
	return document.getElementById(el);
}

function joined_combo(cmb_chain, php_filter, cat_id){
	//cmb_chain - an array with the comboboxes' ids in hiearchical order
	this.filter_script = php_filter;	
	this.chain = new Array();
	this.labels = new Array();
	this.init_vals = new Array();
	this.current = 0; //current index in the chain
	this.initialized = false;
	this.cat_id = cat_id;
	
	for(var i = 0; i < cmb_chain.length; i++){
		this.chain[i] 		= _$$(cmb_chain[i][0]);
		this.labels[i] 		= cmb_chain[i][1];
		this.init_vals[i] 	= cmb_chain[i][2];
	}
	
	this.fill_slave = function(results){
		if(!results) return;
		
		var slave_opts_raw = results.split('||');
		
		this.chain[this.current + 1].options.length = 0;
		
		this.chain[this.current + 1].options[0] = new Option(this.labels[this.current + 1], 0);
		for(var i = 0; i < slave_opts_raw.length; i++){
			var temp_option = slave_opts_raw[i].split('~');
			
			this.chain[this.current + 1].options[this.chain[this.current + 1].options.length] = new Option(temp_option[1], temp_option[0]);
		}		
		
		this.chain[this.current + 1].disabled = false;
		
		//if only 1 option is available (2 with the label), autoselect it and update
		if(this.chain[this.current + 1].options.length == 2){
			this.chain[this.current + 1].selectedIndex = 1;
			this.update_slave(this.current + 1);
		}
	};
	
	this.get_ajax_results = function(){
		var parent = this;	
		this.disable_combos();
		
		new Ajax.Request(
			parent.filter_script, 
			{
				method: 'post',
				parameters: {id: parent.chain[parent.current].value, cat_id: this.cat_id},
				onSuccess: 
			 		function(transport){
				 		var response = transport.responseText;

						parent.fill_slave(response);	
						if(parent.init_vals[parent.current + 1]){
							parent.current++;
							parent.init_values();
						}
					}
			}
		);
	};
		
	this.update_slave = function(master_index){
		this.current = master_index;
		
		if(this.current < this.chain.length - 1)
			if(this.chain[this.current].value == 0){
				this.disable_combos();
			}else{
				this.get_ajax_results();
			}
	};
	
	this.disable_combos = function(){
		for(var i = this.current + 1; i < this.chain.length; i++){
			this.chain[i].disabled = true;
			this.chain[i].selectedIndex = 0;
		}
	};
	
	this.init_values = function(){
		if(this.init_vals[this.current]){
			for(var i = 0; i < this.chain[this.current].options.length; i++){
				if(this.chain[this.current].options[i].value == this.init_vals[this.current]){
					this.chain[this.current].selectedIndex = i;
					this.init_vals[this.current] = false;
					this.update_slave(this.current);
					break;
				}
			}
		}
	};
}
