var popwin=null;
var searchready=false;

function picpop(id,x,y) {
 if (popwin && !popwin.closed) {popwin.close();};
 return !(popwin=window.open('picpop.php?id='+id+'&x='+x+'&y='+y,'','width='+x+',height='+y+',scrollbars=no,resizable=no,menubar=no,status=no,directories=no,location=no,toolbar=no'));
 };

function trim(s) {return s.replace(/^\s+|\s+$/g,'');};

function tarkistakala(f) {
 if ((trim(f.pnimi.value)=='') && (trim(f.kunta.value)=='') && (trim(f.remonttityyppi.value)=='')) {alert('Syötä vähintään yksi hakuehto.'); return false;};
 };

function tarkistayrit(f) {
 if ((trim(f.pnimi.value)=='') && (trim(f.kunta.value)=='') && (trim(f.ynimi.value)=='') && (t.toimiala.selectedIndex==0)) {alert('Syötä vähintään yksi hakuehto.'); return false;};
 };

 function do_alist(val,e) {
	 if (document.all)
	        Key = window.event.keyCode;
	    else
	    	Key = e.which;

	 if (Key == 38 || Key == 40) return false;

	 if (Key == 13) {
		document.getElementById('asiasanat').value=document.getElementById('asana_' + focusindex).innerHTML;
		document.getElementById('asana').style.display = 'none';
		document.getElementById('haepaikka').focus();
		return false;
	}

	 if (val.length == 0){
	  document.getElementById("asana").innerHTML="";
	  document.getElementById('asana').style.display = 'none';
	  return false;
	 }
	 	focusindex = 0;
	 	hit = false;
		index = 0;
	 	i = 0;
	 	j = 0;
		txt = "";
		eka = "";
		txtHtml = eka;
		vals = val.split(' ');
		if (vals.length > 1) val = vals[(vals.length - 1)];
		val = val.toLowerCase();
		while (asanat[i]) {
			txt = "<a id=\"tag_" + j + "\" name=\"tag_" + j + "\" class=\"tag\" href=\"javascript:void(0);\" onenter=\"change_text(document.getElementById('asana_" + j + "').innerHTML," + j + ");\" ";			
			txt += "onmouseover=\"document.getElementById('asana_" + j + "').style.background = '#CCFFFF';this.focus();\" ";
			txt += "onmouseout=\"document.getElementById('asana_" + j + "').style.background = '#FFFFFF';\">";
			txt += "<div name=\"asana_" + j + "\" id=\"asana_" + j + "\" onclick=\"change_text(this.innerHTML," + j + ");\">";			
			txt += asanat[i] + "</div></a>";
			tag = asanat[i];
			if (tag == val) eka = txt;
			else if (tag.substring(0,val.length) == val) txtHtml += txt;			
			else txt = "";

			if (txt != "") {j++;}
			i++;
		}


		document.getElementById('asana').innerHTML = eka + txtHtml;

		/*ps=findPosAndSize(document.getElementById('asiasanat'));
		document.getElementById('asana').style.left=ps.left+'px';
		document.getElementById('asana').style.top=(ps.top+ps.height)+'px';*/
		//alert(document.getElementById('asana').innerHTML);
		if (!(eka == "" && txtHtml == "")) {
			document.getElementById('tag_' + 0).style.background = '#CCFFFF';
			document.getElementById('asana').style.display = 'block';
		}
		else
			document.getElementById('asana').style.display = 'none';

		return true;
}

 function change_text(val,j) {
	 i = 0;
	 focusindex = j;
	 while (document.getElementById('tag_' + i))
 		document.getElementById('tag_' + i++).style.background = '#FFFFFF';

 	document.getElementById('tag_' + focusindex).style.background = '#CCFFFF';
	 document.getElementById('asiasanat').value = val;

	document.getElementById('asana').style.display = 'none';
	 document.getElementById('haepaikka').focus();
 }

 function findPosAndSize(o) {
	 var curLeft=curTop=width=height=0;
	 if (o.offsetWidth) {width=o.offsetWidth};
	 if (o.offsetHeight) {height=o.offsetHeight};
	 if (o.offsetParent) {
	  curLeft=o.offsetLeft;
	  curTop=o.offsetTop;
	  while (o=o.offsetParent) {
	   curLeft+=o.offsetLeft;
	   curTop+=o.offsetTop;
	   };
	  };
	 return {'left':curLeft, 'top':curTop, 'width':width, 'height':height};
	 };


	 document.onkeydown = keyhandler;
	 var focusindex = 0;

	 function keyhandler(e) {
		    if (document.all)
		        Key = window.event.keyCode;
		    else
		    	Key = e.which;


		    i = 0;
		    if (Key == 38) {
		    	focusindex--;
		    	if (focusindex < 0) focusindex = document.getElementsByName('tag').length - 1;

		    	while (document.getElementById('asana_' + i))
		    		document.getElementById('asana_' + i++).style.background = '#FFFFFF';

		    	document.getElementById('asana_' + focusindex).style.background = '#CCFFFF';
			go_anchor('asana_' + focusindex);
		    }
		    if (Key == 40) {
		    	focusindex++;
		    	if (focusindex == document.getElementsByName('tag').length) focusindex = 0;

		    	while (document.getElementById('asana_' + i))
		    		document.getElementById('asana_' + i++).style.background = '#FFFFFF';

		    	document.getElementById('asana_' + focusindex).style.background = '#CCFFFF';
			go_anchor('asana_' + focusindex);
		    }

	}

	function go_anchor(n){				 
		document.getElementById("asana").scrollTop = document.getElementById(n).offsetTop;
		 
	}


