var prevSelection = -1;
var offset = 0;
var parentsInFields = new Array;
//var categoryPath = new Array;

//tabela przechowuje wszystkie wybrane kategorie
parentsInFields[0] = 0;

function showAdress(){
	if (parentsInFields[1] > 0 ){
		for(i = 0; i < categories_services.length; i++){
			if (categories_services[i] == parentsInFields[1]){
				var adress_div = new getObj('div_company_adress');
				adress_div.style.display = 'block';
				document.frmReportAdd.services_show.value = 1;
				return true;
			}
		}
	}
	var adress_div = new getObj('div_company_adress');
	adress_div.style.display = 'none';
	document.frmReportAdd.services_show.value = 0;
	return false;
}


function arrayContent(){
	var path = '';
	for(i = 0; i < parentsInFields.length; i++){
		path += i + ' => ' + parentsInFields[i] + '\n';
	}
	alert(path);
}

/*******************************************************/

function makePath(category_id, i){
	parentsInFields[i] = category_id
	for (j=0; j<category.length; j++){
			if (category[j][0] == category_id){
				makePath(category[j][1], ++i);
			}
	}
}

/*******************************************************/

function createPath(){
	//konstrujemy sciezke kategorii
	var path = '';
	
	if (getChildsQty(parentsInFields[parentsInFields.length-1]) == 0 ){
		for(i = 0; i < parentsInFields.length; i++){
			category_name = getCategoryName( parentsInFields[i] );
			if (category_name != false) {
				path += category_name;
				if (i < parentsInFields.length-1) path += '&nbsp;&raquo;&nbsp;';
			}
		}		
	}

	//w div'a na stronie wstawiamy sciazke kategorii
	var x = new getObj('divCategorySelection');
	if (x.obj) x.obj.innerHTML = path;
}

/*******************************************************/

/**
* Funkcja czysci zawartosc pol select, ktorych ID jest wieksze badz rowne od podanego
* oraz usuwa ze stosu odpowiednia ilosc ID wybranych elementow
*/
function clearRest(select_id){
	for(i = select_id; i < selects_array.length; i++){
		if (selects_array[i]){
			selects_array[i].options.length = 0;
		}
	}
}
/*******************************************************/

/**
* Funkcja czysci zawartosc tabel selectedItems i parentsInFields z pozycji powyzej podanej
*/
function clearArrays(index){
	if (index != -1){
		parentsInFields.splice(index, 100);
	}
}
/*******************************************************/

/**
* Funkcja wypelnia pole select o indeksie select_id pozycjami z tabeli category o parent_id podanym w parametrze
*/
function fillElements(select_id, parent_id, selected_item){
	if(selects_array[select_id]){
		index = 0;
		selects_array[select_id].options.length = 0;
		for (j=0; j<category.length; j++){
			if (category[j][1] == parent_id){
				selects_array[select_id].options[index] = new Option(category[j][2], category[j][0]);

				if ( (selected_item) && (selected_item > 0) && ( category[j][0] == selected_item ) ) {
					selects_array[select_id].options[index].selected = true;	
				}

				index++;
			}
		}		
	}
}
/*******************************************************/

/**
* Funkcja zwraca ilosc "potomkow" danej kategorii
*/
function getChildsQty(category_id){
	for (j=0; j<category.length; j++){
		if (category[j][0] == category_id){
			return category[j][3];
			index++;
		}
	}
	return 0;
}
/*******************************************************/

/**
* Funkcja zwraca nazwe kategorii po jej ID
*/
function getCategoryName(category_id){
	for (j=0; j<category.length; j++){
		if (category[j][0] == category_id){
			return category[j][2];
			index++;
		}
	}
	return false;
}
/*******************************************************/

/**
* Funkcja inicjuje wypelnianie pol select
*/ 
function initSelection(){

	if (parentsInFields.length > 4){
		a_length = 4;
		diff = parentsInFields.length - 4;
		if ( getChildsQty(parentsInFields[parentsInFields.length-1]) == 0 ){
			diff = diff - 1;
			prevSelection = prevSelection - 1;
			offset = offset - 1;
		}
		
	} else {
		a_length = parentsInFields.length;
		diff = 0;
	}
		
	for(i = 0; i < a_length; i++){
		//wypelniamy pola select odpowiednimi danymi
		fillElements(i, parentsInFields[diff+i], parentsInFields[diff+i+1]);
	}
}


/**
* GLOWNA FUNKCJA WYWOLYWANA W MOMENCIE KLIKNIECIA NA POLE SELECT
*/
function selectionChange(select_id){
	var p_index;
	var index;
	var cat_id;
	
	index = selects_array[select_id].selectedIndex;
	
	if (index < 0) return;
	
	cat_id = selects_array[select_id].options[index].value;
	
	if (cat_id <= 0) return;
	
	if (getChildsQty(cat_id) == 0) {
		input_category.value = cat_id;		
	} else {
		input_category.value = '';		
	}
	
	currSelection = prevSelection;
	
	if (prevSelection != (select_id + offset)) {
	
		prevSelection = select_id + offset;
		offset = (prevSelection - 3) + 1;
	
	}

	if (offset < 0) offset = 0;

	p_index = prevSelection + 1;

	//ID kategorii dodajemy do tabeli
	parentsInFields[ p_index ] 	= cat_id;
	
	//usuwamy nastepne elementy z powyzszej tablicy (na wypadek powrotu)
	clearArrays( p_index + 1 );

	//czyscimy pola select 
	clearRest(select_id+1);
	
	//ostatecznie wywolujemy funkcje inicjujaca wypelnienie wszystkich pol
	initSelection();
	
	//konstrujemy sciezke kategorii
	createPath();
	
	//pokazujemy dodatkowe pola jezeli pierwsza kategoria ze sciezki jest kategoria uslugowa
	showAdress();
	
}