// target dieses Fensters zuweisen
window.name = 'MidosaSearch';

function markAllCBs(a_event) {
	var div = (window.event) ? event.srcElement.parentNode : a_event.target.parentNode;
	var inputArr = div.getElementsByTagName('INPUT');
	var value = null;
	for ( var i = 0; i < inputArr.length; i++) {
		var o = inputArr[i];
		if (o.getAttribute('type') == 'checkbox' && !o.disabled) {
			if (value == null)
				value = o.checked;
			o.checked = !value;
		}
	}
}

function getFilterField() {
	var inp = document.getElementById("FBfilter");
	if (inp == null)
		alert("Field with name='FBfilter' not found");
	return inp;
}

function replaceVidInVids(vids, vid) {
	if (vids == null || vids == '')
		return vids;

	var arr = vids.split(/\s*,\s*/);
	var res = null;
	for (i = 0; i < arr.length; i++) {
		if (arr[i] == vid)
			continue;

		if (res == null)
			res = arr[i];
		else
			res = res + ',' + arr[i];
	}
	return res;
}

function removeSelectedVid(checkbox) {
	// step 1: drop vid from FBfilter
	var inp = getFilterField();
	if (inp.value != '') {
		var result = replaceVidInVids(inp.value, checkbox.value);
		inp.value = result;
	}

	// step 2: disable checkbox and label
	var p = checkbox.parentNode;
	if (p != null) {
		if (checkbox.nextSibling != null) {
			var s = checkbox.nextSibling;
			if (s.getAttribute('for') != null && s.getAttribute('for') == checkbox.getAttribute('id'))
				s.setAttribute('style', 'color: grey;');
		}

		checkbox.checked = false;
		checkbox.disabled = true;
		p.normalize();
	}
}

function removeSelectedVids(a_event) {
	var inp = getFilterField();
	if (inp == null)
		return;

	var div = (window.event) ? event.srcElement.parentNode : a_event.target.parentNode;
	var inputArr = div.getElementsByTagName('INPUT');
	for ( var i = inputArr.length - 1; i >= 0; i--) {
		var o = inputArr[i];
		if (o.getAttribute('type') == 'checkbox' && o.getAttribute('name') == 'v' && !o.checked) {
			removeSelectedVid(o);
		}
	}

	var re = new RegExp('\\bFBfilter=[\\w-_,]*\\b');
	for ( var i = 0; i < document.links.length; i++) {
		var link = document.links[i];
		if (link != null && link.href != null && link.href.match(re)) {
			link.href = link.href.replace(re, 'FBfilter=' + inp.value);
		}
	}
}

function loaded() {
	init();
	setTimeout('document.getElementById( "query").focus();', 500);
}

function showHideByID(id) {
	var target = document.getElementById(id);
	if (target != null) {
		showHide(target);
	}
}

function showHide(_this) {
	if (_this.getAttribute('expanded') == 'true') {
		_this.src = 'images/collapse.gif';
		var showNode = _this.parentNode.nextSibling;
		while (showNode.nodeType != 1)
			showNode = showNode.nextSibling;
		showNode.style.display = 'none';

		_this.setAttribute('expanded', 'false');
	} else {
		_this.src = 'images/expand.gif';
		var showNode = _this.parentNode.nextSibling;
		while (showNode.nodeType != 1)
			showNode = showNode.nextSibling;
		showNode.style.display = '';

		_this.setAttribute('expanded', 'true');
	}
}

function getRequestParam(name) {
	name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
	var regexS = "[\\?&]" + name + "=([^&#]*)";
	var regex = new RegExp(regexS);
	var results = regex.exec(window.location.href);
	if (results == null)
		return "";
	return results[1];
}

function hasRequestParam(name) {
	var param = getRequestParam(name);
	return param != null && param.length > 0;
}
