/* list box GUI with filter for better search engine indexing
 * v1.0, 18.07.2006

 * developed by Simon Speich
 * for www.lfi.ch, copyright (c) 2006
 * Eidg. Forschungsanstalt WSL, Birmensdorf
 * You can use/alter this freely as long as this entire 
 * copyright notice is included.
 
*/

String.prototype.TrimLast = function() {
	// Strip whitespace (or any other character) from end of a string
	if (arguments[0] && arguments[0] != '') { 
		var RegExpr = new RegExp(arguments[0]+"+$");
		return this.replace(RegExpr, '');
	}
	else { return this.replace(/\s+$/,''); }
}

function FilterList() {
	var arrMatch = new Array();									// store matches
	var Val = this.FldSource.value;							// text used as filter
	if (Val.lastIndexOf(" ") == Val.length-1) {	// remove trailing spaces, IE doesn' split last separator
	 	// copy back original array when using space key (display unfiltered list)
		arrMatch = this.arrFilterAll;
	}
	else {	// use last word as filter input
		Val = Val.split(/\s/);
		Val = Val.pop();
		Val = new RegExp(Val, "i");

		// create new array with matches
		var i = 0;	
		var Len = this.arrFilterAll.length;
		for (; i < Len; i++) {
			if (Val.test(this.arrFilterAll[i][1])) {
				arrMatch[arrMatch.length] = new Array(this.arrFilterAll[i][0], this.arrFilterAll[i][1]);
			}
		}
	}
	// write array with matches back to form select array or to div-span
	i = 0;
	ListLen = arrMatch.length;
	while (this.FldTarget.firstChild) {
		this.FldTarget.removeChild(this.FldTarget.firstChild);
	}
	for (; i < ListLen; i++) {
		this.FldTarget.appendChild(arrMatch[i][0]);
	}
}

function SetTarget(e) {
	e = Bw.Ie ? event : e;
  var Key = e.keyCode;
	var Ref = (Bw.Ie ? e.srcElement : e.target);
	this.FilterList();
}

Filter.prototype.SetTarget = SetTarget;

function Filter(FrmName, FldTarget, FldSource) {
	var Self = this;
	// html objects are not avaible before page is loaded
	this.FldSource = d.getElementById(FldSource);	// input element used as source. typed text is used to filter select list
	this.FldTarget = d.getElementById(FldTarget);	// div-with-spans element to be filtered
	this.FilterList = FilterList;									// filtering method
	
	// copy select to array for filtering
	var i = 0;
	var El = this.FldTarget;
	var Children = El.getElementsByTagName('div');
	this.arrFilterAll = new Array();					// stores all select/span elements before filtering
	for (; i < Children.length; i++) {
		var Node = Children[i].cloneNode(true);	// store copy of node with all attributes and children
		var El = Node;
		while (El && El.nodeType != 3) {							// find first text node. <span> element can contain other elements such as <a>
			El = El.firstChild;
		}
		var Text = El.nodeValue;
		this.arrFilterAll[i] = new Array(Node, Text);
	}
	if (Bw.Ie) {
		this.FldSource.attachEvent('onkeyup', function(e) { Self.SetTarget(e) });
	}
	else if (Bw.Dom) {
		this.FldSource.addEventListener('keyup', function(e) { Self.SetTarget(e) }, true);
	}
}
