var sel = 1; // Store selected search tab

function changeSearchTab( selected ) {
	if( !selected ) selected = 1;
	
	t = new Array( 3 );
	t[1]	= document.getElementById( "option1" );
	t[2] 	= document.getElementById( "option2" );
	t[3] 	= document.getElementById( "option3" );

	t[selected].blur();		
	for( i = 1; i <= 3; i++ ) {
		t[i].className			= null;
	}
	t[selected].className		= "selected";

	saveSearchTab( selected );
	
	searchForm     = document.getElementById( "search" );
	if( selected == 1 ) searchForm.action = "http://www.lamk.fi/haku/haku.html?Submit=Etsi";
	if( selected == 2 ) searchForm.action = "http://www.lamk.fi/henkilohaku.html";
	if( selected == 3 ) searchForm.action = "http://www.lamk.fi/yleishaku.html?display=20"; 
	sel = selected;
}

function sendSearchForm() {
	if( sel == 3 ) {
		searchForm     = document.getElementById( "search" );
		submitVal = document.createElement( "input" );
		submitVal.type	= "hidden";
		submitVal.id	= "display";
		submitVal.name	= "display";
		submitVal.value	= "25";
		searchForm.appendChild( submitVal );
		document.getElementById( "hakukentta" ).value = document.getElementById( "koulutushaku" ).value;
	}
}

function saveSearchTab( value ) {
	 var today	= new Date();
	 var expire	= new Date();
	 var name	= "searchtab";
	 expire.setTime( today.getTime() + 3600000*24 );
	 document.cookie = "searchtab=" + value + ";path=/;expires = "+expire.toGMTString();
}

function getSearchTab() {
	 var store		= document.cookie;
	 var startP		= store.indexOf( "searchtab" );
	 if( startP > -1 ) {
		 var endP = store.indexOf( ';', startP );
		 if( endP == -1 ) endP = store.length;
		 return unescape( store.substring( startP + 9 + 1, endP ) );
	} else {
	  	return 0;
	}
}

function fixIESubmit() {
	searchForm     = document.getElementById( "search" );
	submitVal = document.createElement( "input" );
	submitVal.type	= "hidden";
	submitVal.id	= "Submit";
	submitVal.name	= "Submit";
	submitVal.value	= "Etsi";
	searchForm.appendChild( submitVal );
}

changeSearchTab( getSearchTab() );
fixIESubmit();

