var changedSelect = false;

/*
* fill language-specific names into select-box
*/
function fillNames(language) {
    var groupBox = document.forms['formProductSearch'].elements['select_product_group'];
    var typeBox  = document.forms['formProductSearch'].elements['select_product_type'];
    var prodBox  = document.forms['formProductSearch'].elements['select_producer'];
    
    groupBox[0]  = new Option(dropnames["product_group"][language], "", false, false);
    typeBox[0]   = new Option(dropnames["product_type"][language], "", false, false);
    prodBox[0]   = new Option(dropnames["producer"][language], "", false, false);
    
    groupBox.selectedIndex = 0;
    typeBox.selectedIndex = 0;
    prodBox.selectedIndex = 0;
}


/*
* sorts the product_groups and producers-arrays in ascending order
*/
function arraySort(arr) {
    var mem;
	for (var i=0; i<arr.length; i++) {
		for (var j=i+1; j<arr.length; j++) {
			if (arr[j][1]<arr[i][1]) {
				mem = arr[j];
				arr[j] = arr[i];
				arr[i] = mem;
			}
		}
	}
}


/*
* fills select-box with product-groups or producers
* 
* vars:
*   box := JS Object for the specific select-box in the form
*   elems := array of options loaded into the select-box, to read
*            out of file "/<language>/productsearch_config.js"
*   selected := value (as string) of the option that should be
*               preselected after loading
*/
function loadSelect(box, elems, selected) {
	var selectEntry = false;
	var pos = 0;
	
	// sort the elems-array
	arraySort(elems);
	
	for(var i=0; i<elems.length; i++) {
		if(selected == elems[i][0]) {
			selectEntry = true;
			/*
			* changedSelect is used for the select-box
			* of product_types which loads its options
			* automatically onload if product_groups -
			* select was changed here onload.
			*/
			changedSelect = true;
			// ie-bug, see below
			pos = i;
		}
		else
			selectEntry = false;
		newEntry = new Option(elems[i][1], elems[i][0], false, selectEntry);
		box[box.length] = newEntry;
	}
	if(changedSelect)
		updateProductSearch();
	
	if(searchActive && selected != "") {
		/*
		* because of a bug in internet explorer 6.0.2800 it's
		* necessary to set selectedIndex again, otherwise the
		* option selected always goes one option up in the list
		*/
		box.options.selectedIndex = pos+2;
		box.options.selectedIndex = pos+2;
	}
}


/*
* changes between active and inactive search-button and switches searching active
* if on of the following options is chosen:
*  - product_group
*  - product_group + product_type
*  - product_group + product_type + producer
*  - product_group + producer
*  - producer 
*/
function checkSearchForm(lang) {
	var product = document.forms['formProductSearch'].elements['select_product_group'].value;
	var producer = document.forms['formProductSearch'].elements['select_producer'].value;

	if(product == "" && producer == "") {
		if(lang == "de")
			alert("Bitte wählen Sie zunächst eine Produktgruppe oder einen Hersteller aus.");
		else
    	alert("Please choose a product or a manufacturer first.");
		return false;
	}
	return true;
}

/*
* changes product-types in dependency of chosen product-group
*/
function updateProductSearch() {
	var group_box = document.forms['formProductSearch'].elements['select_product_group'];
	var type_box = document.forms['formProductSearch'].elements['select_product_type'];
	var type_box_length = type_box.options.length;
	
	// delete product-types (old group) from select
	for(var i=type_box_length; i>1; i--) {
		type_box.options[i] = null;
	}

	// if product-group is chosen 
	if(group_box.value != "") {
		var pos;
		
		// sort the product_types[group_box.value]-array
		arraySort(product_types[group_box.value]);
		
		// fill select with product-types that belong to the chosen product-group
		for(var i=0; i<product_types[group_box.value].length; i++) {
			if(product_type_selected != "" && product_type_selected == product_types[group_box.value][i][0]) {
				selectEntry = true;
				pos = i;
			}
			else
				selectEntry = false;
			newEntry = new Option(product_types[group_box.value][i][1], product_types[group_box.value][i][0], false, selectEntry);
			type_box.options[type_box.options.length] = newEntry;
		}
		
		if(searchActive && pos >= 0) {
			// because of a bug in internet explorer 6.0.2800 it's necessary to set selectedIndex again, otherwise
			// the option selected always goes one option up in the list
			type_box.options.selectedIndex = pos+2;
		}
	}
}
