/*********************************************************************************/
// datova struktura -- POZOR: hodnoty pola "musia" byt v uvodzovkach
// a[id_manf] = new array(id_model, id_model, ...)
//var p = new Array();
//p[1] = new Array("1", "2", "3");
//p[2] = new Array("4", "5", "6");
//p[3] = new Array("7", "8", "9", "10");


//var options_stored = false;
var options_stored = new Array();
var s = new Array(); // savenute hodnoty vsetkych povodnych options

// action_on_parent_all moze byt bud 'disable' alebo 'all' alebo 'parents_all'
// parent_select_name moze byt cislo, vtedy bude parameter povazovany ako "id_parent_selected"
function update_child_select(data, form_name, parent_select_name, child_select_name, action_on_parent_all, child_has_all_option)
{	var parent_select;
	var id_parent_selected;
	
	if (typeof parent_select_name == "number")
	{	id_parent_selected = parent_select_name;
	}
	else
	{	parent_select = document.forms[form_name].elements[parent_select_name];
		id_parent_selected = parent_select.options[parent_select.selectedIndex].value;
		
		// return bez chybovej hlasky ak nahodou parent je empty -> nemalo by sa stat ak bude plna databaza
		if (parent_select.selectedIndex == -1) { return;}
	}
	
	var child_select = document.forms[form_name].elements[child_select_name];
	
	// ak nemame data na parent-child relaciu tak nic
	if (data.length == 0) return;
	
	if (typeof action_on_parent_all == "undefined")
	{	action_on_parent_all = "disable";
	}
	
	if (typeof child_has_all_option == "undefined")
	{	child_has_all_option = true;
	}
	
	if (typeof options_stored[child_select_name] == "undefined")
	{	options_stored[child_select_name] = false;
	}
	
	if (typeof s[child_select_name] == "undefined")
	{	s[child_select_name] = new Array();
	}
	
	// tu sme len pri onchange, takze enable...
	child_select.disabled = false;
	
	//if (!options_stored)
	if (!options_stored[child_select_name])
	{	
		var i = 0;
		for (i = 0; i < child_select.options.length; i++ )
		{	s[child_select_name][i] = new Array();
			s[child_select_name][i][0] = child_select.options[i].value
			s[child_select_name][i][1] = child_select.options[i].text;
		}
		//options_stored = true;
		options_stored[child_select_name] = true;
	}

	// vynulovat vsetko
	child_select.options.length = 0;
	
	var child_all_option_done = false; // nemusi sa v kazdom for cykle vkladat to iste do child_select.options[0]
	var i;
	for (i = 0; i < s[child_select_name].length; i++ )
	{	
		// bolo vybrane "All"
		// POZOR: predpokladam, ze "" alebo -1 maju specialny vyznam v zmysle "Show All"
		if (id_parent_selected == "" || id_parent_selected == -1)
		{	
			// nastavime "zobraz vsetky" pokec a disable-neme
			if (action_on_parent_all == "disable")
			{	if (child_has_all_option)
				{	child_select.options[0] = new Option(s[child_select_name][0][1], s[child_select_name][0][0], true, true);
				}
				child_select.disabled = true;
				break;
			}
			
			// nastavime "zobraz vsetky" pokec a vylistujeme vsetky moznosti pre selektnuteho parenta
			else if (action_on_parent_all == "all")
			{	child_select.options[child_select.options.length] = new Option(s[child_select_name][i][1], s[child_select_name][i][0]);
			}
			
			// nastavime "zobraz vsetky" pokec a vylistujeme vsetky moznosti pre vsetkych parentov
			else if (action_on_parent_all == "parents_all")
			{	//child_select.options[i] = new Option(s[i][1], s[i][0]);
				
				if (child_has_all_option && !child_all_option_done)
				{	child_select.options[0] = new Option(s[child_select_name][0][1], s[child_select_name][0][0], true, true);
					child_all_option_done = true;
				}
				
				for (var p_idx in data)
				{	if (in_array(s[child_select_name][i][0], data[p_idx]))
					{	child_select.options[child_select.options.length] = new Option(s[child_select_name][i][1], s[child_select_name][i][0]);
					}
				}
			}
		}
		else // popridavame len to co treba
		{	
			if (child_has_all_option && !child_all_option_done)
			{	child_select.options[0] = new Option(s[child_select_name][0][1], s[child_select_name][0][0], true, true);
				child_all_option_done = true;
			}
			
			if (in_array(s[child_select_name][i][0], data[id_parent_selected]))
			{	child_select.options[child_select.options.length] = new Option(s[child_select_name][i][1], s[child_select_name][i][0]);
			}
		}
	}
}