function ClearSelectsFrom(select_id) {
	var arr_name = selects_arraya;
	for (i = select_id; i < 3; i++) {
		arr_name[i].length = 0;
  		arr_name[i].options[0] = new Option(line);
  	}
}

function InsertValue(select_id, index, cat_name, cat_index) {       
	selects_arraya[select_id].options[index] = new Option(cat_name);
	selects_arraya[select_id].options[index].value=cat_index;
}

function LastOrPenultimate(cat_index) {            	
	if(!category_children[cat_index])
		return 1;		
	else {   
		if (!allow_to_select_penultimate)
			return 0;
		else {   						
			if (allow_to_select_penultimate==2) 				return 1;
				
			for (i = 0; i < category_children[0].length; i++) {   		
						
				if(category_children[0][i]==cat_index)
					return 0;
			}							
			return 1;				
		}
	}
}

function RefreshByParent(select_id) {                       
	parent_tmp=parents[select_id+offset];       

		if((category_children.length!=0)&&(category_children[parent_tmp])) {
  		for (i = 0; i < category_children[parent_tmp].length; i++) {                                                  
	  		cat_index=category_children[parent_tmp][i];

			InsertValue(select_id,i,category_name[cat_index],cat_index);  	

						if (cat_index==parents[select_id+offset+1])
				selects_arraya[select_id].selectedIndex=i; 

						if (i == (category_children[parent_tmp].length-1))
				InsertValue(select_id,i+1,line,0);                	
  		}
	}   	
}

function AfterChange(select_id) {                                  
	if (select_id>1) {                       
				ClearSelectsFrom(0);
				RefreshByParent(0);
		RefreshByParent(1);
		RefreshByParent(2);
	} else {
		ClearSelectsFrom(select_id+1);  
		RefreshByParent(select_id+1);
	}	
}

function ChangeSelection(select_id, t) {

	index=t.selectedIndex; 
	document.sell_form.category.value = '';	
	
	if (index<0)
		return;        
	
	cat_index=t.options[index].value;

	document.sell_form.subcategory.value = cat_index;
	
	parents[select_id+offset+1]=cat_index;
	parents.length=select_id+offset+2;

	
//	if (LastOrPenultimate(cat_index))	
		document.sell_form.category.value = cat_index;

	if ((select_id<2)&&(offset)) {                
		minus = 2 - select_id;

		if (offset<minus)
			offset=0
		else
			offset-=minus;
	
				ClearSelectsFrom(0);
				
		RefreshByParent(0);
		RefreshByParent(1);
		RefreshByParent(2);	
		
		return;	
	}

		if (t.options[index].text==line) {
		ClearSelectsFrom(select_id+1);		
	} else {        	
         	if (!category_children[cat_index]) {
         	  ClearSelectsFrom(select_id+1);		
         	}
         	else {    		
			     if  (select_id==2) {
                        	offset++;
                        }       
			AfterChange(select_id);		                        
        	}
        }
}

function FindParent(child,level) {	
	if (category_children.length>0)                
		for (p=0;p<category_children.length;p++)
			if (category_children[p])
				for (c=0;c<category_children[p].length;c++)
					if (category_children[p][c]==child) {
						parents_rev[level]=p;
						parents_index_rev[level]=c;
					
						if (level>2)
							offset=level-2;
								
						if (p!=0)
							FindParent(p,level+1);
						return;
					}										
}

function ChangeByIndex(index,subindex) {                        
	parents_index = new Array;
	
	ClearSelectsFrom(0);
	
	parents.length=1;
	parents_rev.length=0;
	parents_index.length=0;
	offset=0;

	if (index=='' || index==0)	
		index=subindex;
	             
	if (!category_name[index]) {
		AfterChange(-1);
		document.sell_form.category.value='';		
		return;
	}                 		

	FindParent(index,0);
	for (i=0;i<parents_rev.length;i++) {                     
		parents[i]=parents_rev[parents_rev.length-1-i];
		parents_index[i]=parents_index_rev[parents_rev.length-1-i];
	}              
	
	for (j=0;j<3;j++) {
		if(parents.length>j) {
			RefreshByParent(j);				
			selects_arraya[j].selectedIndex=parents_index[j+offset]; 
	  	}		     
	  	
	}      
	
   	if (selects_arraya[parents.length-1])
		ChangeSelection(parents.length-1);

	
				
	/*
	if (!LastOrPenultimate(index))
		document.sell_form.category.value='';
	*/
}