var b_ULkeypos = -1;
var b_tmpClass = '';

function b_loadBrowser(modus, div_id, what, what_id, admin, opt, opt2, ajax)
{
	if ($('katbrowserLoadDiv_'+div_id))
	{
		$('katbrowserLoadDiv_'+div_id).show();
	}
	if ($(div_id+'_admin'))
	{
		$(div_id+'_admin').hide();
	}
	var url = 'ajax/srv/browser.php?modus='+encodeURIComponent(modus)+
						'&div_id='+encodeURIComponent(div_id)+
						'&what='+encodeURIComponent(what)+
						'&what_id='+encodeURIComponent(what_id)+
						'&opt='+encodeURIComponent(opt)+
						'&admin='+encodeURIComponent(admin)+
						'&dummy='+encodeURIComponent(new Date().getTime());
	switch (opt)
	{
		case 'suche':
			url += '&suche=true';
			break;
		case 'select':
			url += '&select=true';
			break;
	}
	switch (opt2)
	{
		case 'move':
			url += '&move=true';
			break;
	}
	switch (ajax)
	{
		case 'true':
			url += '&ajax=true';
			break;
	}
	var debugURL = false;
	if (debugURL)
	{
		var test = window.open('', 'browser_debug', 'height=200, width=1000, resizable=yes');
		test.document.write(url);
	}
	new Ajax.Request(url,
		{
			method: 'get',
			onSuccess: function (transport)
			{
				var response = transport.responseXML;
				var stylesheet = loadStyleSheet('ajax/browser.xsl');
				parseStyleSheet(stylesheet, response, $(div_id));
				if ($('katbrowserLoadDiv_'+div_id))
				{
					$('katbrowserLoadDiv_'+div_id).hide();
				}
			}
		}
	);
}

function b_resetvars()
{
	b_ULkeypos = -1;
	b_tmpClass = '';
}

function b_search(modus, div_id, search_string, e, ajax)
{
	b_createResultsSearchDiv(div_id);
	if (search_string.length > 1)
	{
		var keynum = null;
		try {keynum = e.keyCode;} catch (ex) {try {keynum = e.keyCode;} catch (ex) {}};
		switch (keynum)
		{
			case 38: // keyup
				if ($$('li.result').length > 0)
				{
					b_ULkeypos--;
					if (((b_ULkeypos + 1) < $$('li.result').length) && ((b_ULkeypos + 1) >= 0))
					{
						$$('li.result')[(b_ULkeypos + 1)].childNodes[0].className = b_tmpClass;
					}
					if (b_ULkeypos < 0)
					{
						b_ULkeypos = $$('li.result').length - 1;
						$$('li.result')[0].childNodes[0].className = b_tmpClass;
					}
					b_tmpClass = $$('li.result')[b_ULkeypos].childNodes[0].className;
					$$('li.result')[b_ULkeypos].childNodes[0].className = 'hover';
				}
				break;

			case 40: // keydown
				if ($$('li.result').length > 0)
				{
					b_ULkeypos++;
					if ((b_ULkeypos - 1) >= 0)
					{
						$$('li.result')[(b_ULkeypos - 1)].childNodes[0].className = b_tmpClass;
					}
					if (b_ULkeypos == $$('li.result').length)
					{
						b_ULkeypos = 0
						$$('li.result')[($$('li.result').length - 1)].childNodes[0].className = b_tmpClass;
					}
					b_tmpClass = $$('li.result')[b_ULkeypos].childNodes[0].className;
					$$('li.result')[b_ULkeypos].childNodes[0].className = 'result hover';
				}
				break;
				
			case 13: // enter
				if (b_ULkeypos >= 0 && b_ULkeypos < $$('li.result').length)
				{
					// location.href = $$('li.result')[b_ULkeypos].childNodes[0].href;
					$$('li.result')[b_ULkeypos].childNodes[0].onclick();
				}
				break;
				
			case 27: // esc
				$('katbrowsersearchResults_'+div_id).hide();
				b_resetvars();
				break;
				
			default:
				if ($('katbrowserLoadDiv_'+div_id))
				{
					$('katbrowserLoadDiv_'+div_id).show();
				}
				var url = 'ajax/srv/browser.php?modus='+encodeURIComponent(modus)+
									'&div_id='+encodeURIComponent(div_id)+
									'&ajax='+encodeURIComponent(ajax)+
									'&search='+encodeURIComponent(search_string);
				//document.write(url);
				new Ajax.Request(url,
					{
						method: 'get',
						onSuccess: function (transport)
						{
							var response = transport.responseXML;
							var stylesheet = loadStyleSheet('ajax/browser.xsl');
							parseStyleSheet(stylesheet, response, $('katbrowsersearchResults_'+div_id));
							$('katbrowsersearchResults_'+div_id).style.left = $('katbrowserinputsuche').cumulativeOffset()[0] + 'px';
							$('katbrowsersearchResults_'+div_id).style.top = $('katbrowserinputsuche').cumulativeOffset()[1] + $('katbrowserinputsuche').offsetHeight + 'px';
							$('katbrowsersearchResults_'+div_id).show();
							if ($('katbrowserLoadDiv_'+div_id))
							{
								$('katbrowserLoadDiv_'+div_id).hide();
							}
						}
					}
				);
				break;
		}
	}
	else
	{
		$('katbrowsersearchResults_'+div_id).hide();
	}
}

function checkPosition(event)
{
	if ($$('div.katbrowsersearchResults')[0])
	{
		var divSearchFrame = $$('div.katbrowsersearchResults')[0];
		if (divSearchFrame.style.display != 'none')
		{
			if (!((Event.pointerX(event) > divSearchFrame.offsetLeft) &&
					(Event.pointerX(event) < (divSearchFrame.offsetLeft + divSearchFrame.offsetWidth)) &&
					(Event.pointerY(event) > divSearchFrame.offsetTop) && 
					(Event.pointerY(event) < (divSearchFrame.offsetTop + divSearchFrame.offsetHeight))))
			{
				divSearchFrame.hide();
				b_resetvars();
			}
		}
	}
}

Event.observe(document, "mousedown", checkPosition, false);

function b_delete(modus, div_id, kat_id, admin, ajax)
{
	if (confirm('Soll diese Katagorie wirklich gelöscht werden ?'))
	{
		var url = 'ajax/srv/browser_admin.php?action=delete&modus='+encodeURIComponent(modus)+
							'&div_id='+encodeURIComponent(div_id)+
							'&kat_id='+encodeURIComponent(kat_id);
		if (typeof ajax != 'undefined')
		{
			url += '&ajax='+encodeURIComponent(ajax);
		}
		new Ajax.Request(url,
			{
				method: 'get',
				onSuccess: function (transport)
				{
					var response = transport.responseText;
					$(div_id+'_admin').hide();
					b_loadBrowser(modus, div_id, '', '', admin, '', '', ajax);
				}
			}
		);
	}
}

function b_delete_prod(modus, div_id, kat_id, admin)
{
	if (confirm(getLang("kBase", "delProd")))
	{
		var url = 'ajax/srv/browser_admin.php?action=delete_prod&modus='+encodeURIComponent(modus)+
							'&div_id='+encodeURIComponent(div_id)+
							'&kat_id='+encodeURIComponent(kat_id);
		new Ajax.Request(url,
			{
				method: 'get',
				onSuccess: function (transport)
				{
					var response = transport.responseText;
					$(div_id+'_admin').hide();
					b_loadBrowser(modus, div_id, '', '', admin, '', '');
				}
			}
		);
	}
}

function b_hidewindow(windowname)
{
	if ($(windowname))
	{
		$(windowname).hide();
	}
}

function b_browseradmin(modus, div_id, kat_id, ajax)
{
	$(div_id+'_admin').innerHTML = '<img src="media/hp/ajaxrequest.gif"/>';
	$(div_id+'_admin').style.left = $(div_id+'_img_'+kat_id).cumulativeOffset()[0] + 10 - (Prototype.Browser.IE ? $('v4_overallTable').cumulativeOffset()[0] : 0) + 'px';
	$(div_id+'_admin').style.top = $(div_id+'_img_'+kat_id).cumulativeOffset()[1] + 10 + 'px';
	$(div_id+'_admin').show();
	var url = 'ajax/srv/browser_admin.php?action=edit&modus='+encodeURIComponent(modus)+
						'&div_id='+encodeURIComponent(div_id)+
						'&kat_id='+encodeURIComponent(kat_id);
	if (typeof ajax != 'undefined')
	{
		url += '&ajax='+encodeURIComponent(ajax);
	}
	new Ajax.Request(url,
		{
			method: 'get',
			onSuccess: function (transport)
			{
				var response = transport.responseXML;
				var stylesheet = loadStyleSheet('ajax/browser.xsl');
				parseStyleSheet(stylesheet, response, $(div_id+'_admin'));
			}
		}
	);
}

function b_browseradmin_prod(modus, div_id, kat_id, ajax)
{
	$(div_id+'_admin').innerHTML = '<img src="media/hp/ajaxrequest.gif"/>';
	$(div_id+'_admin').style.left = $(div_id+'_img_prod_'+kat_id).cumulativeOffset()[0] + 10 - (Prototype.Browser.IE ? $('v4_overallTable').cumulativeOffset()[0] : 0) + 'px';
	$(div_id+'_admin').style.top = $(div_id+'_img_prod_'+kat_id).cumulativeOffset()[1] + 10 + 'px';
	$(div_id+'_admin').show();
	var url = 'ajax/srv/browser_admin.php?action=edit_prod&modus='+encodeURIComponent(modus)+
						'&div_id='+encodeURIComponent(div_id)+
						'&kat_id='+encodeURIComponent(kat_id);
	if (typeof ajax != 'undefined')
	{
		url += '&ajax='+encodeURIComponent(ajax);
	}
	new Ajax.Request(url,
		{
			method: 'get',
			onSuccess: function (transport)
			{
				var response = transport.responseXML;
				var stylesheet = loadStyleSheet('ajax/browser.xsl');
				parseStyleSheet(stylesheet, response, $(div_id+'_admin'));
			}
		}
	);
}

function b_browseradmin_new(modus, div_id, kat_id, ajax)
{
	$(div_id+'_admin').innerHTML = '<img src="media/hp/ajaxrequest.gif"/>';
	$(div_id+'_admin').style.left = $(div_id+'_img_new_'+kat_id).cumulativeOffset()[0] + 10 - (Prototype.Browser.IE ? $('v4_overallTable').cumulativeOffset()[0] : 0) + 'px';
	$(div_id+'_admin').style.top = $(div_id+'_img_new_'+kat_id).cumulativeOffset()[1] + 10 + 'px';
	$(div_id+'_admin').show();
	var url = 'ajax/srv/browser_admin.php?action=new&modus='+encodeURIComponent(modus)+
						'&div_id='+encodeURIComponent(div_id)+
						'&kat_id='+encodeURIComponent(kat_id);
	if (typeof ajax != 'undefined')
	{
		url += '&ajax='+encodeURIComponent(ajax);
	}
	new Ajax.Request(url,
		{
			method: 'get',
			onSuccess: function (transport)
			{
				var response = transport.responseXML;
				var stylesheet = loadStyleSheet('ajax/browser.xsl');
				parseStyleSheet(stylesheet, response, $(div_id+'_admin'));
			}
		}
	);
}

function b_updateadminvars(modus, div_id, kat_id, admin, ajax)
{
	if ($('adminform_'+div_id))
	{
		if ($('adminform_'+div_id).name.value != '')
		{
			var url = 'ajax/srv/browser_admin.php?action=update&modus='+encodeURIComponent(modus)+
								'&div_id='+encodeURIComponent(div_id)+
								'&kat_id='+encodeURIComponent(kat_id);
			if (typeof ajax != 'undefined')
			{
				url += '&ajax='+encodeURIComponent(ajax);
			}
			new Ajax.Request(url,
				{
					method: 'post',
					postBody: $('adminform_'+div_id).serialize(),
					onSuccess: function (transport)
					{
						var response = transport.responseText;
						if (response == 'error_name')
						{
							alert(getLang("kBase", "nameExists"));
							$('adminform_'+div_id).name.focus();
						}
						else
						{
							$(div_id+'_admin').hide();
							b_loadBrowser(modus, div_id, '', '', admin, '', '', ajax);
						}
					}
				}
			);
		}
		else
		{
			alert(getLang("kBase", "pleaseInsertName"));
			$('adminform_'+div_id).name.focus();
		}
	}
}

function b_newadminvars(modus, div_id, kat_id, admin, ajax)
{
	if ($('adminform_'+div_id))
	{
		if ($('adminform_'+div_id).name.value != '')
		{
			var url = 'ajax/srv/browser_admin.php?action=insert&modus='+encodeURIComponent(modus)+
								'&div_id='+encodeURIComponent(div_id)+
								'&kat_id='+encodeURIComponent(kat_id);
			if (typeof ajax != 'undefined')
			{
				url += '&ajax='+encodeURIComponent(ajax);
			}
			new Ajax.Request(url,
				{
					method: 'post',
					postBody: $('adminform_'+div_id).serialize(),
					onSuccess: function (transport)
					{
						var response = transport.responseText;
						if (response == 'error_name')
						{
							alert(getLang("kBase", "nameExists"));
							$('adminform_'+div_id).name.focus();
						}
						else
						{
							$(div_id+'_admin').hide();
							b_loadBrowser(modus, div_id, '', '', admin, '', '', ajax);
						}
					}
				}
			);
		}
		else
		{
			alert(getLang("kBase", "pleaseInsertName"));
			$('adminform_'+div_id).name.focus();
		}
	}
}

function b_moveDokuTo(linkTypID, linkID)
{
	if ($('moveDokuContainer_dokuID'))
	{
		var dokuID = $('moveDokuContainer_dokuID').innerHTML;
		var linkTypName = '';
		switch (linkTypID)
		{
			case '0':
				linkTypName = getLang("all", "showAllDocuments");
				break;
			default:
				linkTypName = getLang("all", "assignmentToShort_" + linkTypID);
				break;
		}
		var moveToString = linkTypName;
		if ((linkID > 0) && (linkTypID != '0') && (linkTypID != '2'))
		{
			new Ajax.Request('ajax/srv/dokument_move.php?getname=true&linkTypID='+encodeURIComponent(linkTypID)+'&linkID='+encodeURIComponent(linkID),
				{
					method: 'get',
					asynchronous: false,
					onSuccess: function (transport)
					{
						moveToString += ' > '+transport.responseText;
					}
				}
			);
		}
		if (confirm(getLang("compInfo", "content_moveText").replace("#1#", moveToString)))
		{
			var dokuID = $('moveDokuContainer_dokuID').innerHTML;
			var url = 'ajax/srv/dokument_move.php?dokuID='+encodeURIComponent(dokuID)+
								'&linkTypID='+encodeURIComponent(linkTypID)+
								'&linkID='+encodeURIComponent(linkID);
			new Ajax.Request(url,
				{
					method: 'get',
					onSuccess: function (transport)
					{
						var response = transport.responseText;
						if (response == 'ok')
						{
							toggleDokuContainer('');
							window.location.href = 'index.php?section=content&linkTypID='+linkTypID+'&linkID='+linkID+'&select=true';
						}
						else
						{
							alert(getLang("compInfo", "content_moveErr"));
							toggleDokuContainer('');
						}
					}
				}
			);
		}
	}
	else
	{
		alert(getLang("compInfo", "content_moveErr"));
		toggleDokuContainer('');
	}
}

function b_createResultsSearchDiv(divID)
{
	if (!$('katbrowsersearchResults_'+divID))
	{
		var searchDiv = document.createElement('div');
		searchDiv.className = 'katbrowsersearchResults';
		searchDiv.id = 'katbrowsersearchResults_'+divID;
		searchDiv.style.display = 'none';
		document.body.appendChild(searchDiv);
	}
}

function b_licTyp_autoZuw(id)
{
  window.open('index.php?section=info&sub=autoZuw&ltID=6&lID=-' + id, '_blank', 'width=800,height=400,resizable=yes,scrollbars=yes,top=50,left=50');
}
