var prev_taso = -1;
var hlRow=new Object;
var hlRowno = -1;
var hlRowClassName;
var hlRowStyle;
var hlRowStylebg;
var tekstisyotto = false;
var	cntSelected = 0;
var cntPicked = 0;
var alustus = true;
var edlen = 0;
var inputpos = 0;

var maxi = 0;
var i = 0;
var apos = 0;
var opos = 0;
var ruopos = 0;
var insertpos = 0;

var municipalities = new Array();
var firstChar = "A";
var charMunicipalities;

function getselected2()
{
	if (document.searchForm.namelist.selectedIndex == -1) { return 0; }
	var hakuna = document.searchForm.namelist.options[document.searchForm.namelist.selectedIndex].text;
	if (document.searchForm.municipalities.value.substr(0, inputpos).indexOf(hakuna + " ")  == -1) {
		document.searchForm.municipalities.value = document.searchForm.municipalities.value.substr(0, inputpos) + hakuna;
		edlen = document.searchForm.municipalities.value.length;
			document.getElementById('layermain').style.zIndex = '8';
			document.getElementById('namesboxl').style.zIndex = '2';
			document.getElementById('namesboxl').style.visibility="hidden";
	}
}

function populate_namelist()
{
	document.searchForm.namelist.length = 0;
	insertpos = 0;
		
	inputpos = document.searchForm.municipalities.value.lastIndexOf(" ") + 1;
	var haettava = document.searchForm.municipalities.value.substr(inputpos);
	haettava = haettava.toUpperCase();
	var len = haettava.length;
	var found = 0;
	var re;
	
	if (len == 0) { 
		hideNamesbox();
		return 0;
	}
	var eka = haettava.substr(0,1);
	if (municipalities[eka] != null) {
		var munis = municipalities[eka];
		for (i = 0; i < munis.length; i++) {
			if (len == 1 || munis[i].substr(4, len).toUpperCase() == haettava) {
				if (document.searchForm.municipalities.value.substr(0, inputpos).toUpperCase().indexOf(munis[i].substr(4).toUpperCase() + " ")  == -1) {
					document.searchForm.namelist.options[insertpos] = new Option(munis[i].substr(4), munis[i].substr(0, 3));
					insertpos++;
					found++;
				}
			}
		}
		if (found == 1) {
			document.searchForm.municipalities.value = document.searchForm.municipalities.value.substr(0, inputpos) + document.searchForm.namelist.options[0].text;
			if (document.searchForm.municipalities.createTextRange) {
				  var range = document.searchForm.municipalities.createTextRange();
				  range.moveStart('character', inputpos + len);
				  range.select();
			}
			edlen = document.searchForm.municipalities.value.length;
		} else {
			edlen = len;
		}
		if (found > 1) { 
			document.getElementById('namesboxl').style.heigth=found * 20 + "px";
			document.getElementById('layermain').style.zIndex = '8';
			document.getElementById('namesboxl').style.zIndex = '9';
			document.getElementById('namesboxl').style.visibility="visible";
			document.searchForm.namelist.size = found;
		} else {
			hideNamesbox();
		}
	}
}

function hideNamesbox()
{
	if (document.getElementById('namesboxl').style.visibility=="visible") {
		document.getElementById('layermain').style.zIndex = '8';
		document.getElementById('namesboxl').style.zIndex = '2';
		document.getElementById('namesboxl').style.visibility="hidden";
	}
}

function showKuntasel()
{
	document.getElementById('layermain').style.zIndex = '8';
	document.getElementById('kuntaselL').style.zIndex = '9';
	document.getElementById('pophdrL').style.zIndex = '10';
	document.getElementById('kuntaselL').style.visibility="visible";
	document.getElementById('pophdrL').style.visibility="visible";
}

function selectKunta(kuntatxt)
{
	if (kuntatxt != "") {
		if (document.searchForm.municipalities.value == "") {
			document.searchForm.municipalities.value = kuntatxt;
		} else {
			if (ifExists(kuntatxt,  document.searchForm.municipalities.value) == false) {
				document.searchForm.municipalities.value = document.searchForm.municipalities.value + ", " + kuntatxt;
			}
		}
	}
}

function ifExists(myString, inString) 
{
	var pattern = /\s*,\s*/g;
	myString = myString.toUpperCase();
	inString = inString.toUpperCase();
	list = inString.split(pattern);
	for (i=0; i < list.length; i++) {
		if (myString == list[i]) {
			return true;
		} 
	}
   	return false;
}

function hideKunta()
{
	document.getElementById('kuntaselL').style.zIndex = '1';
	document.getElementById('pophdrL').style.zIndex = '2';
	document.getElementById('kuntaselL').style.visibility="hidden";
	document.getElementById('pophdrL').style.visibility="hidden";
	document.getElementById('layermain').style.zIndex = '8';
	hideNamesbox();
}

function popHelp(W,H,url) {
    helpwin = window.open('','Ohje',
        'width='+W+',height='+H+',toolbars=0,scrollbars=1,location=0,statusbars=0,menubar=0,resizable=1');
    helpwin.focus();
    helpwin.location = url;
}

function submitForm(searchForm) {
	if ((searchForm.municipalities.value == "" || searchForm.municipalities.value == null )&& 
		(searchForm.profession.value == "" || searchForm.profession.value == null) && 
		(searchForm.province.value == "" || searchForm.province.value == null) && 
		(searchForm.searchPhrase.value == "" || searchForm.searchPhrase.value == null)) {
		document.getElementById('emptySearchError').style.visibility="visible";
		document.getElementById('emptySearchError').style.display="block";
		return false;
	} else {
		document.getElementById('emptySearchError').style.visibility="hidden";
		document.getElementById('emptySearchError').style.display="none";
		return searchForm.submit();
	}
}
