ie = navigator.userAgent.indexOf("MSIE") != -1 && !window.opera;
iever = "0";

if (ie) {
 iever = navigator.userAgent.indexOf("MSIE");
 iever = navigator.userAgent.substring(iever+5, iever+6);
}

linktotal = 0;
highlighted = -1;
openedbox = "";
hiddenbox = "";
func = "";
values = new Array();
valuesid = new Array();
var to;
lastchar = "";
editwidth = 0;
cto = 0;
var donothing = false;

function htmltranslate(x) {
    for (t = 0; t < transl.length; t++) {
        p = transl[t].split("|");
        while (x.indexOf(p[1]) != -1) x = x.replace(p[1], p[0]);
    }
    return x;
}

function trim(t) {
	while (t.substring(0,1) == " ") t = t.substring(1, t.length);
	while (t.substring(t.length - 1, t.length) == " ") t = t.substring(0, t.length - 1);
	return t;
}

// used to fill textfields
function use(h, t) {
	var re =  /<\S[^><]*>/g
	var re2 =  /#/g

	//alert(h + t);
	t = htmltranslate(t);

	if (t.length > 0) {
		e = t.split("!!??!!");
		links = "";
		display_more = false;
		if (e[e.length-1] == "display_more_results") {
			display_more = true;
			e.length = e.length - 1;
		}
		values.length = e.length;
		for (i = 0; i < e.length; i++) {
			s = unescape(e[i]).split("||&&||");
			link = "<tr><td id=\"l" + i + "\" class=\"searchresults_off\" onMouseOver=\"highlight(" + i + ")\" onClick=\"selectcontent(" + i + ")\" onSelectStart=\"return false\">" + s[1].replace(re2, "") + "</td></tr>\n";
			values[i] = s[1].replace(re, "");
			valuesid[i] = s[0];
			links += link;
		}
		if (display_more) {
			links += '<tr>';
			links += '<td bgcolor="#d5d5d5"><img src="/library/190/images/spacer.gif" width="1" height="1" border="0"></td>';
			links += '</tr>';
			links += '<tr height="22" bgcolor="#f2f2f2">';
			links += '<td align="center" class="allsearchresults" onMouseDown="donothing=true;">Meer dan 20 resultaten gevonden. Verfijn uw zoek.</td>';
			links += '</tr>';
		}
		html = '<table cellpadding="0" cellspacing="0" border="0" style="border: 1px solid #d5d5d5">';
		html += links;
		html += '</table>';
		
		minwidth = $("mydropdown").offsetWidth;
		$("mydropdown").innerHTML = html;
		for (i = 0; i < e.length; i++) {
			$("l"+i).style.width = minwidth + "px";
		}

		$("mydropdownIframe").style.zIndex = 10000;
		$("mydropdown").style.zIndex = 10001;
		$("mydropdownIframe").style.height = $("mydropdown").offsetHeight + "px";
		$("mydropdownIframe").style.visibility = "visible";
		$("mydropdown").style.visibility = "visible";
		linktotal = e.length;
	} else {
		hiddenbox.value='';
		links = "";
		links += '<tr height="22" bgcolor="#f2f2f2">';
		links += '<td id="l0" align="center" class="allsearchresults" onMouseDown="donothing=true;">Geen resultaten gevonden. Verfijn uw zoek.</td>';
		links += '</tr>';
	
		html = '<table cellpadding=0 cellspacing="0" border="0" style="border: 1px solid #d5d5d5">';
		html += links;
		html += '</table>';
		
		minwidth = $("mydropdown").offsetWidth;
		$("mydropdown").innerHTML = html;
		$("l0").style.width = minwidth + "px";

		$("mydropdownIframe").style.height = $("mydropdown").offsetHeight + "px";
		$("mydropdownIframe").style.visibility = "visible";
		$("mydropdown").style.visibility = "visible";
		linktotal = 0;
	}
	if ($("search_wait")) $("search_wait").src = "/library/190/images/spacer.gif";
	if ($("search_wait2")) $("search_wait2").src = "/library/190/images/spacer.gif";

}

function selectcontent(t) {
	donothing = true;
	try {
		window.clearTimeout(cto);
	} catch(e) {}
	openedbox.value = values[t];
	openedbox.focus();
	hiddenbox.value = valuesid[t];
	//location.href="/" + valuesid[t];
	nolinks();
  //	if (func != "") eval(func);
	try { eval(func) } catch(err) {}
}

function nolinks() {
	$("mydropdownIframe").style.visibility = "hidden";
	$("mydropdown").style.visibility = "hidden";
	$("mydropdown").innerHTML = "";
	linktotal = 0;
	highlighted = -1;
}

function nolinks2() {
	window.setTimeout("nolinks()", 250);
}

var keypressed;

function keycatch(e) {
	if (ie) c = event.keyCode;
	else c = e.which;
	keypressed = c;
	up = false;
	down = false;
	if (c == 38) { if (highlighted > 0) highlight(highlighted-1); }
	else if (c == 40) { if (highlighted + 1 < linktotal) highlight(highlighted + 1); }
	else if (c == 13) { 	if (highlighted >= 0) selectcontent(highlighted);	}
	else if (c == 27) { nolinks(); }
	else { highlighted = -1; }
}

function highlight(n) {
	if (highlighted > -1) $("l" + highlighted).className = "searchresults_off";
	$("l" + n).className = "searchresults_on";
	highlighted = n;
}

// find openspaces
function dosearch(obj, obj2, obj3) {
	c = keypressed;
	if (c != 13 && c != 38 && c != 40) {
		lastchar = c;

		var argv = dosearch.arguments;
		var argc = dosearch.arguments.length;
		var extra_par = "";
		if (argc > 3) {
			for (i = 3; i < argc; i++) {
				extra_par += ", '" + argv[i] + "'";
			}
		}

		if (to) window.clearTimeout(to);
		to = window.setTimeout("dosearch2('" + obj.id + "', '" + obj2 + "', '" + obj3 + "'" + extra_par + ")", 300);
	}
}

function dosearch2(obj, obj2, obj3) {
	obj = $(obj);
	v = trim(obj.value);
	if (dosearch2.arguments.length == 3 && v != "" && $("search_wait") != undefined) $("search_wait").src = "images/icon_loading.gif";
	if (dosearch2.arguments.length > 3 && v != "" && $("search_wait2") != undefined) $("search_wait2").src = "images/icon_loading.gif";
	
	position_layer(obj, obj3);
	
	openedbox = obj;
	hiddenbox = $(obj2);
	
	extra = "";
	if (dosearch2.arguments.length > 3) {
		extra = dosearch2.arguments[3];
	}
	
	if (v != "") {
		ajax("/library/190/ajax_postcodes.php?lookup=" + escape(v) + extra, "GET", false, false, "use");
	} else nolinks();
}

function position_layer(obj, obj2) {
	obj = $(obj);

	el = $(obj2);
	a = getpos(obj2);

	$("mydropdownIframe").style.width = el.offsetWidth + "px";
	$("mydropdownIframe").style.top = (a[1] + el.offsetHeight - 4) + "px";
	//$("mydropdownIframe").style.top = (a[1] + el.offsetHeight + 1) + "px";
	$("mydropdownIframe").style.left = (a[0]) + "px";

	$("mydropdown").style.width = el.offsetWidth + "px";
	$("mydropdown").style.top = (a[1] + el.offsetHeight - 4) + "px";
	//$("mydropdown").style.top = (a[1] + el.offsetHeight + 1) + "px";
	$("mydropdown").style.left = (a[0]) + "px";

	addevent(window, "resize", function() { position_layer(obj, obj2); });
}

function myMouseup() {
	if (donothing) {
		donothing = false;
		return;
	} else {
		try { nolinks2() }
		catch(err) 	{ }
	}
}

document.onmouseup = myMouseup;
document.onkeydown = keycatch;
