function getXYpos(elem) {
   if (!elem) {
      return {"x":0,"y":0};
   }
   var xy={"x":elem.offsetLeft,"y":elem.offsetTop}
   var par=getXYpos(elem.offsetParent);
   for (var key in par) {
      xy[key]+=par[key];
   }
   return xy;
}
function HideTips(tipblock) {
	var tp = document.getElementById(tipblock);
	tp.style.display = 'none';
	tp.innerHTML='';
}
function LightTip(tipspan) {
	for (var i=1; i<6; i++) {
		var el = document.getElementById('tip'+i);
		if (el) {
			el.style.backgroundColor = 'transparent';
		}
	}
	tipspan.style.backgroundColor = '#b2b2b2';
}
function AddTip(i) {
	document.WikiSearch.word.value = document.getElementById('tipvalue'+i).value;
}
function AddTipCats(i) {
	document.EditWiki.wiki_cats.value = document.getElementById('tipvalue'+i).value;
}
function GetCaretPosition(ctrl) {
	var CaretPos = 0;	// IE Support
	if (document.selection) {
	ctrl.focus();
		var Sel = document.selection.createRange();
		Sel.moveStart('textedit', -1);
		CaretPos = Sel.text.length;
	}
	// Firefox support
	else if (ctrl.selectionStart || ctrl.selectionStart == '0')
		CaretPos = ctrl.selectionStart;
	return (CaretPos);
}

function WikiSearch_cat(wiki_lang, cats, val) {
	if (window.XMLHttpRequest) {
		Request = new XMLHttpRequest();
	}
	else if (window.ActiveXObject) {
		try {
			Request = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (CatchException) {
			Request = new ActiveXObject("Msxml2.XMLHTTP");
		}
	}
	var q=val;
	var params;
	if (GetCaretPosition(document.EditWiki.wiki_cats) >= document.EditWiki.wiki_cats.value.length) {
		params = 'wiki_lang='+wiki_lang+'&amp;wiki_action=SearchCats&amp;q='+q;

		Request.open('POST', '/WIKI/wikiMaster.pl', true);
		Request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		Request.send(params);
		Request.onreadystatechange = function()
			{
				if (Request.readyState == 4)
				{
					var r = Request.responseText;
					var x = getXYpos(document.getElementById('Cats')).x + 'px';
					var y = getXYpos(document.getElementById('Cats')).y + 20 + 'px';
					var tipblock_cat = document.getElementById('catsearchtips');
					tipblock_cat.style.left = x;
					tipblock_cat.style.top = y;
					tipblock_cat.style.display = 'block';
					tipblock_cat.innerHTML=r;
				}
			}
		}
	else {
		HideTips('catsearchtips');
	}
}
function WikiSearch_(wiki_lang, val) {
	if (window.XMLHttpRequest) {
		Request = new XMLHttpRequest();
	}
	else if (window.ActiveXObject) {
		try {
			Request = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (CatchException) {
			Request = new ActiveXObject("Msxml2.XMLHTTP");
		}
	}
	var q=val;
	var params;
	params = 'wiki_lang='+wiki_lang+'&amp;wiki_action=SearchWords&amp;q='+q;
	
	Request.open('POST', '/WIKI/wikiMaster.pl', true);
	Request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	Request.send(params);
	Request.onreadystatechange = function()
	{
		if (Request.readyState == 4)
		{
			var r = Request.responseText;
			var x = getXYpos(document.getElementById('Words')).x + 'px';
			var y = getXYpos(document.getElementById('Words')).y + 20 + 'px';
			var tipblock = document.getElementById('searchtips');
			tipblock.style.left = x;
			tipblock.style.top = y;
			tipblock.style.display = 'block';
			tipblock.innerHTML=r;
		}
	}
}
function SubmitSearch() {
	document.forms.WikiSearch.submit();
}
function ToggleHelpTable() {
	var Tbl = document.getElementById('HelpTable');
	if (!Tbl.style.display || Tbl.style.display.toLowerCase() == 'none') {
		Tbl.style.display = 'block';
	}
	else {
		Tbl.style.display = 'none';
	}
}
function ToggleLogin() {
	var Login = document.getElementById('loginDIV');
	if (!Login.style.display || Login.style.display.toLowerCase() == 'none') {
		Login.style.display = 'block';
	}
	else {
		Login.style.display = 'none';
	}
}
