function advs_select_1()
{
 var 	item1 = document.getElementById('advs_sel_1'),
 	item2 = document.getElementById('advs_sel_2'),
 	item3 = document.getElementById('advs_sel_3'),
	tmp2,l1,l2,tmp1,tmp3,vo;

 if(item2.length>0)
 for(l1=item2.length; l1>=0; l1--)
 	item2.remove(l1);

 if(item1.selectedIndex==-1) return;

 tmp1=parseInt(item1.options[item1.selectedIndex].value);
 if(tmp1<1) return;

 vo=-1;

 for(l1=0; l1<(advs_array[tmp1].length/3); l1++)
 {
 	tmp2 = document.createElement('option');
	tmp2.text = "";

	if(advs_array[tmp1][l1*3]==2)	
	{
		tmp2.text=String.fromCharCode(160,160,160,160);
	} else {
		tmp2.text = "";
		vo=advs_array[tmp1][l1*3+1];
	}

	tmp2.value=tmp1+","+vo+","+advs_array[tmp1][l1*3+1];
	tmp2.text+=advs_array[tmp1][l1*3+2];

	for(l2=0; l2<item3.length; l2++)
	if(item3.options[l2].value==advs_array[tmp1][l1*3+1])
	{
		tmp2.style.color='#CCCCCC';
		break;
	}

	if(advs_array[tmp1][l1*3]==1)
	{
		tmp2.style.fontWeight='bold';
		if(item2.length>0)
		{
			tmp3=document.createElement('option');
			tmp3.value="";
			tmp3.text="";
			try { item2.add(tmp3,null); } catch(e) { item2.add(tmp3); }
		}
	}

	try { item2.add(tmp2,null); } catch(e) { item2.add(tmp2); }
 }
}

function advs_select_2()
{
 var	item1 = document.getElementById('advs_sel_1'),
	item2 = document.getElementById('advs_sel_2'),
	item3 = document.getElementById('advs_sel_3'),
 	tmp1,tmp2,tmp3,l1,tmp4,tmp5;

 if(item2.selectedIndex==-1) return;
 if(item2.options[item2.selectedIndex].value=="") return;
 tmp2=item2.options[item2.selectedIndex].value.split(",");
 if(tmp2[0]<1) return;
 if(tmp2[1]==tmp2[2] || tmp2[1]==-1) return;

 tmp2[0]='M'+tmp2[0];
 tmp2[1]='T'+tmp2[1];

 item3.selectedIndex=-1;

 // Etsitään väliosasto
 tmp4=-1;
 for(l1=0; l1<item3.length; l1++)
 if(item3.options[l1].value==tmp2[1])  
 {
	tmp4=l1; 
	break;
 }

 // Lisätään puuttuva väliosasto
 if(tmp4==-1)
 {
	// Etsitään pääosasto
	tmp1=-1;
	for(l1=0; l1<item3.length; l1++)
	if(item3.options[l1].value==tmp2[0]) 
	{ 
		tmp1=l1;
		break; 
	}

 	// Lisätään puuttuva pääosasto
	if(tmp1==-1)
	{
		tmp1=document.createElement('option');
		tmp1.value=tmp2[0];
		tmp1.text=item1.options[tmp2[0].substr(1)-1].text;
		tmp1.style.fontWeight='bold';
		try {
			if(item3.length==0)	item3.add(tmp1,null);
			else			item3.add(tmp1,item3.options[0]);
		} catch(e) {
			item3.add(tmp1,0);
		}
		tmp1=0;
	}
	
	tmp5		= tmp2[0].substr(1);
 	tmp3		= document.createElement('option');
 	tmp3.value	= tmp2[1];
 	tmp3.text	= String.fromCharCode(160,160,160);
	tmp3.style.fontWeight='bold';


	for(l1=0; l1<(advs_array[tmp5].length/3); l1++)
	if( advs_array[tmp5][l1*3+1] == tmp2[1].substr(1) )
	{
		tmp3.text+=advs_array[tmp5][l1*3+2];
		break;
	}	

 	if( tmp1 == (item3.length-1) )
	{
 		try { item3.add(tmp3,null); } catch(e) { item3.add(tmp3); }
	} else {
 		try { item3.add(tmp3,item3.options[tmp1+1]); } catch(e) { item3.add(tmp3,tmp1+1); }
	}
	
	tmp4=tmp1+1;
 }

 // Lisätään aliosasto
 for(l1=0; l1<item3.length; l1++)
 if(item3.options[l1].value==tmp2[2]) return;

 tmp3		= document.createElement('option');
 tmp3.value	= tmp2[2];
 tmp3.text	= String.fromCharCode(160,160)+item2.options[item2.selectedIndex].text;

 if( tmp4 == (item3.length-1) )
 {
 	try { item3.add(tmp3,null); } catch(e) { item3.add(tmp3); }
 } else {
 	try { item3.add(tmp3,item3.options[tmp4+1]); } catch(e) { item3.add(tmp3,tmp4+1); }
 }
 
 item2.options[item2.selectedIndex].style.color='#CCCCCC';

}

function advs_select_3()
{
 var	item2 = document.getElementById('advs_sel_2'),
	item3 = document.getElementById('advs_sel_3'),
 	t1,l1,t2,tmp1,tmp2;

 if( (t1 = item3.selectedIndex)<0 ) return;
 if( item3.options[t1].value.substr(0,1) == 'M') return;
 if( item3.options[t1].value.substr(0,1) == 'T') return;

 for(l1=0; l1<item2.length; l1++)
 {
 	t2=item2.options[l1].value.split(",");
	if(t2[2] == item3.options[t1].value)
	{
		item2.options[l1].style.color='';
		break;
	}
 }

 item3.remove(t1);

 tmp1 = item3.options[t1-1].value.substr(0,1);
 if( tmp1 == 'T' && ( t1>=item3.length || item3.options[t1].value.substr(0,1) == 'T' || item3.options[t1].value.substr(0,1) == 'M' ) )
	item3.remove(t1-1);

 tmp1 = item3.options[t1-2].value.substr(0,1);
 if( tmp1 == 'M' && ( t1>=item3.length || item3.options[t1-1].value.substr(0,1) == 'M' ) )
	item3.remove(t1-2);

 item3.selectedIndex=-1;
}

