function getElem(p2)
{
	if(document.getElementById) return document.getElementById(p2);
	else if(document.all) return document.all[p2];
}
function clearFields()
{
	var field_name=getElem('username_textfield');
	var field_pw=getElem('password_textfield');
	if(field_name.value == 'username')
	{
		field_name.value="";
		field_pw.value="";
	}
}
function overtakeQuery()
{
	var pattern='search=';
	var search=getElem('search');
	var query=window.location.search;
	var pos1=query.indexOf(pattern);
	if(pos1>=0)
	{
		var pos2=query.indexOf('&',pos1+pattern.length);
		if(pos2<0) pos2=query.length;
		q=query.substring(pos1+pattern.length,pos2);
		q=q.replace(/\+/g,' ');
		search.value=unescape(q);
	}
}
function highlightWord(node, word) 
{
	/* recursion */
	if (node.hasChildNodes) 
	{
		/* local var, do not use global! */
		var i=0;
		for (; i < node.childNodes.length; i++) 
			highlightWord(node.childNodes[i], word);
	}
	/* 3 is aa text node */
	if (node.nodeType == 3) 
	{ 
		/* we're looking for this word,but don't match "ich" in "Zurich", or "er" in erklaeren */
		var word_low = word.toLowerCase();
		
		var pos=0;
		while ((pos=node.nodeValue.toLowerCase().indexOf(word_low,pos)) >=0)
		{
			var skip=false;
			if(pos>0)
				if(node.nodeValue.charAt(pos-1).match(/[A-Za-z]/))
					skip=true;
			if(node.nodeValue.charAt(pos+word.length).match(/[A-Za-z]/))
				skip=true;
			if(!skip)
			{
				var parent = node.parentNode;
				/* stop endelss recursion, because we will insert child nodes */
				if(parent.className=="search_word") break;
				/* text before the word we're looking for */
				var head=document.createTextNode(node.nodeValue.substr(0, pos));
				/* make the highlight */
				var highlight_text = document.createTextNode(node.nodeValue.substr(pos,word.length));
				var highlight = document.createElement("span");
				highlight.className = "search_word";
				highlight.appendChild(highlight_text);
				/* insert head, highlight */
				parent.insertBefore(head,node);
				parent.insertBefore(highlight,node);
				/* insert tail */
				node.nodeValue=node.nodeValue.substr(pos + word.length);
				pos=0;
			}
			else pos++;
			

		}
	}
}
function searchHighlight(patterns) 
{
	/* check first intern url, then extern referrer */
	candidates=new Array(document.URL,document.referrer);
	for(i=0;i<candidates.length;i++)
	{
		/* check all patterns, take the first that matches */
		for(j=0;j<patterns.length;j++)
		{
			/* convert from URL encoded String to normal string */
			candidates[i]=unescape(candidates[i]);
			if (candidates[i].indexOf(patterns[j]) >=0)
			{
				pos1=candidates[i].indexOf(patterns[j]);
				pos2=candidates[i].indexOf('&',pos1);
				if(pos1<pos2 && pos1>=0 && pos2>0 && pos1!=(pos2-patterns[j].length))
					st=candidates[i].substring(pos1,pos2);
				else if(pos1>=0 && pos2<0 && pos1!=(candidates[i].length-patterns[j].length))
					st=candidates[i].substring(pos1,candidates[i].length);
				else return;

				sta = st.split('=');
				if(sta.length==2)
				{
					q1=sta[1].replace(/\+/g,' ');
					w=q1.split(/\s+/);
					for (k=0;k<w.length;k++)
					{
						/* search intern */
						if(i==0) highlightWord(getElem('searchable'),(w[k]));
						/* search extern */
						else highlightWord(getElem('body'),(w[k]));
					}
				}
				return;
			}
		}
	}
}