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); }
 }
}



// Etsii väliosaston valintalistalta
function advs_select_2_1(mcid, tcid)
{
 var	l1,tmp1,
	item1 = document.getElementById('advs_sel_1'),
	item3 = document.getElementById('advs_sel_3');

 // Väliosasto?
 for(l1=0; l1<item3.length; l1++)
 if(item3.options[l1].value==tcid) 
	return(l1);

 // Pääosasto?
 tmp1=-1;
 for(l1=0; l1<item3.length; l1++)
 if(item3.options[l1].value==mcid) 
 { 
	tmp1=l1;
	break; 
 }

 // Lisätään puuttuva pääosasto
 if(tmp1==-1)
 {
	tmp1=document.createElement('option');
	tmp1.value=mcid;
	tmp1.text=item1.options[mcid.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;
 }

 // Lisätään puuttuva väliosasto
 tmp5		= mcid.substr(1);

 tmp3		= document.createElement('option');
 tmp3.value	= tcid;
 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] == tcid.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); }
 }

 return(tmp1+1);	
}


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

 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;

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

 // Haetaan osastorakenne sel_3:sta
 tmp4 = advs_select_2_1('M'+tmp2[0],'T'+tmp2[1]);

 // Lisätään uusi elementti
 tmp3		= document.createElement('option');
 tmp3.value	= tmp2[2];
 tmp3.text	= String.fromCharCode(160,160)+item2.options[item2.selectedIndex].text;

 item3.selectedIndex=-1;
 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;
}


function advs_select_4(osastot)
{
  var	l1,l2,tmp3,tmp4,teksti,mcid,
	item1 = document.getElementById('advs_sel_1'),
	item3 = document.getElementById('advs_sel_3');

  item3.selectedIndex=-1;
  for(l1=0; l1<osastot.length; l1+=3)
  {
	mcid = osastot[l1];

	// Estetään tuplat
	for(tmp3=-1,l2=0; l2<item3.length; l2++)
	if(item3.options[l2].value==osastot[l1+2]) { tmp3=l2; break; }
	if(tmp3!=-1) continue;

	// Aliosaston haku
	tmp4=advs_select_2_1('M'+mcid, 'T'+osastot[l1+1]);

	// Noudetaan kuvausteksti
	teksti="";
	for(l2=0; l2<advs_array[mcid].length; l2+=3)
	if( advs_array[mcid][l2+1]==osastot[l1+2] )
	{
		teksti=advs_array[mcid][l2+2];
		break;
	}

	// Uusi elementti
	tmp3		= document.createElement('option');
	tmp3.value	= osastot[l1+2];
	tmp3.text	= String.fromCharCode(160,160,160,160,160,160)+teksti;

	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); }
	}
  }
}

