function show_props2(obj, obj_name) {
	var result = '';
	for (var i in obj) {
		if (i)
			result += obj_name + '.' + i + ' = ' + obj[i] + '<br />\n';
	}
	return result
}
function getcat(theform, lvl) {
	var maxcatlevel = 4;
	for (var i = lvl+1; i <= maxcatlevel; i++) {
		$('#cat'+i+'div').html('');
	}
	var str = $("#"+theform.id).serialize();
	$.ajax({
		data	: 'act=getcat&'+str,
		success	: function(thedata) {
			if (thedata.err)	feedback('err',thedata.err);
			else {
				$('#cat'+thedata.lvl+'div').html(thedata.dat);
				$('#resultsbox').html(thedata.count);
			}
		}
	});
}
function usq(theform) {
	var str = $("#"+theform.id).serialize();
	$.ajax({
		data	: 'act=usq&'+str,
		success	: function(thedata) {
			if (thedata.err)	feedback('err',thedata.err);
			else {
				$('#resultsbox').html(thedata.dat);
			}
		}
	});
}
function showcompanies(theform) {
	var str = $("#"+theform.id).serialize();
	$.ajax({
		data	: 'act=showcompanies&'+str,
		success	: function(thedata) {
			if (thedata.err)	feedback('err',thedata.err);
			else {
				$('#companyboxinner').html(thedata.dat);
				// sO.onresizewindow();
			}
		}
	});
}
function showcompaniesmap(theform) {
	var str = $("#"+theform.id).serialize();
	$.ajax({
		data	: 'act=showcompaniesmap&'+str,
		success	: function(thedata) {
			if (thedata.err)	feedback('err',thedata.err);
			else {
				positions = thedata.dat;
				setupMarkers();
			}
		}
	});
}
function showcompaniesonmapsq(sqid) {
/* new version! */
	$.ajax({
		data	: 'act=showcompaniesonmapsq&sqid=' + sqid,
		success	: function(thedata) {
			if (thedata.err)	feedback('err',thedata.err);
			else {
				var positions = thedata.dat;
				setupMarkers(positions);
			}
		}
	});
}
function shownewsforcat(catid, thelink) {
	$('.catselected').removeClass('catselected');
	thelink.className += " catselected";
	$.ajax({
		data	: 'act=shownewsforcat&catid=' + catid,
		success	: function(thedata) {
			if (thedata.err)	feedback('err',thedata.err);
			else {
				if (thedata.cnt == 0) {
					alert("Geen relevante gegevens gevonden.");
				}
				$('#newsitemslistboxinner').html(thedata.dat);
				$('#newsitemslistboxinner').jScrollPane({showArrows:true});
				companyboxgraybar();
			}
		}
	});
}
function showcompaniesonmapcat(catid, thelink) {
	$.ajax({
		data	: 'act=showcompaniesonmapcat&catid=' + catid,
		success	: function(thedata) {
			if (thedata.err)	feedback('err',thedata.err);
			else {
				if (thedata.cnt == 0) {
					alert("Geen relevante gegevens gevonden.");
				}
				var positions = thedata.dat;
				setupMarkers(positions);
			}
		}
	});
}
function externaltoblank() {
	var alllinks = document.getElementsByTagName("a");
	for (var i = 0; i < alllinks.length; i++) {
		if (alllinks[i].rel && alllinks[i].rel=='external') {
			alllinks[i].target = "_blank";
		}
	}
}
function usq(theform) {
	var str = $("#"+theform.id).serialize();
	$.ajax({
		data	: 'act=usq&'+str,
		success	: function(thedata) {
			if (thedata.err)	feedback('err',thedata.err);
			else {
				$('#resultsbox').html(thedata.dat);
			}
		}
	});
}
jQuery.exists = function(selector) {return ($(selector).length > 0);}
function companyboxgraybar() {
	if ($.exists('#companybox div.jScrollPaneTrack')) {
		$('#companyboxgraybar').css('display','none');
	}
	else {
		$('#companyboxgraybar').css('display','block');
	}
	if ($.exists('#companieslistbox div.jScrollPaneTrack')) {
		$('#companieslistboxgraybar').css('display','none');
	}
	else {
		$('#companieslistboxgraybar').css('display','block');
	}
	if ($.exists('#newsitemslistbox div.jScrollPaneTrack')) {
		$('#newsitemslistboxgraybar').css('display','none');
	}
	else {
		$('#newsitemslistboxgraybar').css('display','block');
	}
	if ($.exists('#pageforletter div.jScrollPaneTrack')) {
		$('#pageforlettergraybar').css('display','none');
	}
	else {
		$('#pageforlettergraybar').css('display','block');
	}
	if ($.exists('#listforletter div.jScrollPaneTrack')) {
		$('#listforlettergraybar').css('display','none');
	}
	else {
		$('#listforlettergraybar').css('display','block');
	}
}
function showcompaniesforcat(catid,thespan) {
	$('#infowindow').css('display','none');
	$('.catselected').removeClass('catselected');
	thespan.className += " catselected";
	var str = $("#queryform").serialize();
	$('#urllist').remove('');
	$.ajax({
		data	: 'act=showcompaniesforcat&'+str,
		success	: function(thedata) {
			if (thedata.err)	alert(thedata.err);
			else {
				$('#companieslistboxinner').html(thedata.dat);
				$('#companieslistboxinner').css('display','block');
				$('#sqid').val(thedata.sq);
				//externaltoblank();
				$('#companieslistboxinner').jScrollPane({showArrows:true});
				if ($.exists('#companieslistbox div.jScrollPaneTrack')) {
					$('#companieslistboxgraybar').css('display','none');
				}
				else {
					$('#companieslistboxgraybar').css('display','block');
				}
			}
		}
	});
}
function keepulblocked(thename,thevalue) {
	$.ajax({
		data	: 'act=setsessionvalue&name=ulblock_'+thename+'&value='+thevalue
	});
}
function showurl(sl, sq, sr) {
	sl.href = "showurl.php?sq="+sq+"&sr="+sr;
	return false;
}
function showul(theul,thespan, expandtext, collapsetext, nameclicked) {
	if (document.getElementById("ul_"+theul).style.display == 'block' && !nameclicked) {
		document.getElementById("ul_"+theul).style.display = 'none';
		keepulblocked(theul,0);
		thespan.innerHTML = '+';
		thespan.title = expandtext;
	}
	else {
		document.getElementById("ul_"+theul).style.display = 'block';
		keepulblocked(theul,1);
		thespan.innerHTML = '-';
		thespan.title = collapsetext;
	}
	$('#companyboxinner').jScrollPane({showArrows:true});
	companyboxgraybar();
}
function showiw(id, elem) {
	$.ajax({
		data	: 'act=getinfowindow&id='+id,
		success	: function(thedata) {
			if (thedata.err)	alert(thedata.err);
			else {
				$('#infowindow').html(thedata.dat);
				$('#infowindow').css('display','block');
				$('#infowindow').css('top', elem.offsetTop);
			}
		}
	});
}
function hideiw() {
	$('#infowindow').css('display','none');
}
// new functions above
function getWindowHeight() {
	var windowHeight = 0;
	if (typeof(window.innerHeight)=='number') {
		windowHeight = window.innerHeight;
	}
	else {
		if (document.documentElement && document.documentElement.clientHeight) {
			windowHeight = document.documentElement.clientHeight;
		}
		else {
			if (document.body && document.body.clientHeight) {
				windowHeight = document.body.clientHeight;
			}
		}
	}
	return windowHeight;
}
function getCCSSValue(daElement,daProperty,daProperty2) {
	if (document.defaultView && document.defaultView.getComputedStyle) {
		return document.defaultView.getComputedStyle(daElement,'').getPropertyValue(daProperty);
	}
	else if (document.all) {
		return daElement.currentStyle[daProperty2];
	}
}

function showlexiconentrieswithletter(span,lang,item) {
	$('.lexletter').removeClass('lexletterselected');
	span.className += ' lexletterselected';
	$.ajax({
		data	: 'act=showlexiconentrieswithletter&l=' + span.innerHTML + '&t=' + lang + '&i=' + item,
		success	: function(thedata) {
			if (thedata) {
				if (thedata.err)	feedback('err',thedata.err);
				else {
					$('#listforletter').html(thedata.list);
					$('#listforletter').css('display','block');
					$('#pageforletter').html(thedata.page);
					//$('#pageforletter').localScroll();
				}
			}
		}
	});
}
function setlanguage(lang) {
	$.ajax({
		data	: 'act=setlanguage&value=' + lang.toLowerCase(),
		success	: function(thedata) {
			if (thedata.err)	feedback('err',thedata.err);
			else {
				location.href = location.href;
			}
		}
	});
}
function ss(thename,thevalue,pagereload) {
	$.ajax({
		data	: 'act=setsessionvalue&name=' + thename + '&value=' + thevalue,
		success	: function(thedata) {
			if (thedata.err)	feedback('err',thedata.err);
			else {
				if (pagereload) location.href = location.href;
			}
		}
	});
}
function showLoginWindow() {
	$('#loginwindow').css('display','block');
}
function autocomplete(e) {
	// if (navigator.vendor == 'KDE') { // konqueror does not scroll down automatically?
	// see key 40
	var key= e.keyCode;
	var theinput = e.target;
	var theselect = document.getElementById('jcms_dynsel');
	var val = theinput.value;
	if (key == 27) {	// escape
		theselect.style.display = 'none';
		return;
	}
	else if (key == 38) {	// arrow up
		if (theselect.options[theselect.selectedIndex - 1])
			theselect.selectedIndex = theselect.selectedIndex - 1;
		return false;
	}
	else if (key == 40) {	// arrow down
		if (navigator.vendor == 'KDE') {
			theselect.focus();
		}
		else {
			if (theselect.options[theselect.selectedIndex + 1])
				theselect.selectedIndex = theselect.selectedIndex + 1;
			if (theselect.style.display != 'none')	// we allow it on an empty box too!
				return false;
		}
	}
	else if (key == 13) {	// enter
		theinput.value = theselect.options[theselect.selectedIndex].text;
		document.getElementById(e.data.storehere).value = theselect.value
		theselect.style.display = 'none';
		return false;
	}
	else {
		// reset the selectedIndex if key is not 38 or 40 or 13
		theselect.selectedIndex = 0;
	}
	if (theinput.value == '' && (!(e.type == 'keyup' && (key == 38 || key == 40)))) {
		// if the inputvalue is empty and we're not arrowup/arrowdown-ing
		theselect.style.display = 'none';
		return;		// at least one letter, we're not going to show them all by default
	}
	// opera seems to need it here, but prevent flickering in other browsers
	if (window.opera)	theselect.style.display = 'inline';
	$.ajax({
		data	: 'act=' + e.data.act + '&word=' + theinput.value,
		success	: function(thedata) {
			if (thedata.err)	alert(thedata.err);
			else {
				// remove earlier displayed options from the selectbox
				while (theselect.childNodes.length > 0) {
					theselect.removeChild(theselect.childNodes[0]);
				}
				// (re)fill the selectbox with actual data
				for (var i = 0; i < thedata.dat.length; i++) {
					var opt = document.createElement("option");
					// IE 5.5 does not understand opt.text
					opt.value = thedata.dat[i]['val'];
					opt.innerHTML = thedata.dat[i].key;
					if (i == 0) opt.selected = 'selected';
					theselect.appendChild(opt);
				}
				// position the selectbox near the input
				var pos = getpos(theinput);
				var xpos = pos[0] + 8;				// move a little right
				var ypos = pos[1] + theinput.offsetHeight - 4;	// move a little up
				theselect.style.left  = xpos + 'px';
				theselect.style.top   = ypos + 'px';
				theselect.style.width = theinput.offsetWidth + 'px';
				// add some event logic (select option by mouse click)
				theselect.onclick = function() {
					theinput.value = theselect.options[theselect.selectedIndex].text;
					document.getElementById(e.data.storehere).value = theselect.value
					theselect.style.display = 'none';
				}
				theselect.onkeyup = function(e) {
					// we need this for konqueror...
					if (e.keyCode == 13) {
						theinput.value = theselect.value;
						theselect.style.display = 'none';
					}
				}
				theselect.style.display = 'inline';
				return false;
			}
		}
	});
}
function getpos(el) {
	var posX = el.offsetLeft;
	var posY  = el.offsetTop;
	while(el = el.offsetParent) {
		posX += el.offsetLeft;
		posY += el.offsetTop;
	}
	return [posX,posY];
}
function getQuerystring(key, str, default_) {
	if (default_==null) default_="";
	key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
	var qs = regex.exec(str);
	if (qs == null)	return default_;
	else		return qs[1];
}

/* maps */
function getMapData(f,map) {
	// form has fields: mycountry, mypcc and myradius
	if (f.mycountry.value == 'NL') {
		if (! f.mypcc.value.match(/^\d{4}( ?[a-z]{2})?$/i)) {
//			alert("Incorrecte Nederlandse postcode");
//			return false;
		}
	}
	$.ajax({
		data	: 'act=getMapData&country=' + f.mycountry.value + '&pc=' + f.mypcc.value,
		success	: function(thedata) {
			if (thedata.err) {
				//alert("err: " + thedata.err);
				// what to do now depends on the error...
				var fulladdr = f.mycountry.value;
				if (f.mypcc.value != '') {
					fulladdr = f.mypcc.value + ',' + fulladdr;
				}
				var zoomlevel = getAppropriateZoomLevel(f.radius.value);
				showAddress(map, fulladdr, f.mycountry.value, zoomlevel, 1);
			}
			else {
				var point = new google.maps.LatLng(thedata.dat['lat'],thedata.dat['lng']);
				var zoomlevel = getAppropriateZoomLevel(f.radius.value);
				map.setCenter(point);
				map.setZoom(zoomlevel);
				return thedata.dat
			}
		}
	});
}
function getAppropriateZoomLevel(val) {
	if (val <= 1.25) return 14;
	if (val <= 2.5)  return 13;
	if (val <=   5)	 return 12;
	if (val <=  10)	 return 11;
	if (val <=  20)	 return 10;
	if (val <=  40)	 return  9;
	if (val <=  80)	 return  8;
	return 7;
}
function setgm(f, which) {
	if (which) {
		if (which == 'country')
			ss('mycountry', f.mycountry.value);
		else if (which == 'radius')
			ss('mydistance', f.radius.value);
		else if (which == 'pc')
			ss('mypcc', f.mypcc.value);
	}
//	if (f.mypcc.value == '' || f.radius.value == '') return;
	$('#map').css('visibility','visible');
	getMapData(f,map);
}
function resetmypcc(f) {
	f.mypcc.value = '';
}
function showCompaniesOnMap(map) {
	// determine the boundaries
	// construct the sql query
	// for every query result: display point on map

	// still to do: restrict using where clause
	// map should be input to function
	$.ajax({
		data	: 'act=showCompaniesOnMap',
		success	: function(thedata) {
			if (thedata.err) {
				alert("Er is een fout opgetreden bij het ophalen van de bedrijfsgegevens via showCompaniesOnMap");
			}
			else {
				// results are in json-format
				for (i = 0; i < thedata.dat.length; i++) {
					var point = new GLatLng(thedata.dat[i]['lat'],thedata.dat[i]['lng']);
					var redIcon5 = new GIcon();
					redIcon5.image = "images/ym.png";
					redIcon5.iconAnchor = new GPoint(6, 20);
					redIcon5.iconSize = new GSize(32,32);
					redIcon5.infoWindowAnchor = new GPoint(38,10);
					var marker = new GMarker(point, {icon: redIcon5, title: thedata.dat[i]['lat']+ ','+thedata.dat[i]['lng']});
					GEvent.addListener(marker, 'click', function(){
						this.openExtInfoWindow(
							map,
							"extInfoWindow_coolBlues",
							"<div><div class=\"header\">Jeroen Vader</div>Conradstraat 170/3<br/>1018 NN  Amsterdam</div>",
							{beakOffset: 1}
						)
					});
					map.addOverlay(marker);
				}
			}
		}
	});
}
function showAddress(map, addr1, addr2, precision, attempt) {
	var geocoder = new GClientGeocoder();
	geocoder.getLatLng(
		addr1,
		function(point) {
			if (!point) {
				if (attempt == 1 && (addr1 != addr2))
					showAddress(map, addr2, addr2, precision, 2);
				else
					showAddress(map, addr2, addr2, 10, 2);
			}
			else {
				map.setCenter(point);
				map.setZoom(precision);
			}
		}
	)
}
function gload() {
	var latlng = new google.maps.LatLng(52.35,4.927343379257179);
	var myOptions = {
		zoom: 7,
		center: latlng,
		navigationControl: true,
		mapTypeId: google.maps.MapTypeId.ROADMAP,
		streetViewControl: false,
		navigationControlOptions: {
			style: google.maps.NavigationControlStyle.ZOOM_PAN
		}
	};
	map = new google.maps.Map(document.getElementById("map"), myOptions);
	google.maps.event.addListener(map, 'zoom_changed', function() {
		var currentzoom = map.getZoom();
		if (currentzoom >= 14) {
			map.setOptions({streetViewControl: true});
		}
		else {
			map.setOptions({streetViewControl: false});
		}
	});
	mgr = new MarkerManager(map);
	setgm(document.searchgm);
}
function getmapinfowindow(theid) {
	$.ajax({
		data	: 'act=getmapinfowindow&id='+theid,
		success	: function(thedata) {
			if (thedata.err)	feedback('err',thedata.err);
			else {
				$('#infowindow').html(thedata.dat);
				$('#infowindow').css('display','block');
			}
		}
	});
}
function setupMarkers(positions) {
	mgr.clearMarkers();
	var markers = [];
	var tmpIcon = new google.maps.MarkerImage('blue-dot.png');
	for (var i = 0; i < positions.length; ++i) {
		var marker = new google.maps.Marker({
			position: new google.maps.LatLng(positions[i][0],positions[i][1]),
			icon: tmpIcon
		});
		google.maps.event.addListener(marker, 'mouseover', function(y) {
			return function() {
				getmapinfowindow(y);
			}
		} (positions[i][2]));
		markers.push(marker);
	}
	mgr.addMarkers(markers,3);
	mgr.refresh();
}
function defsearch(theform) {
	var querystringargs = getquerystringargs();
	for (arg in querystringargs) {
		alert(arg);
	}
	if (theform.s1) {
		var s1 = getCheckedRadioValue(theform.s1);
		if (s1 != '') {
			querystringargs['s1'] = s1;
		}
	}
	var qs = '';
	for (arg in querystringargs) {
		if (qs == '')	qs += '?';
		else		qs += '&';
		qs += arg + '=' + encodeURIComponent(querystringargs[arg]);
	}
//	alert(qs);
	location.href = location.pathname + qs;
//	var langval = $.urlParam('lang');
}
function getCheckedRadioValue(radio) {
	for (var i=0; i < radio.length; i++) {
		if (radio[i].checked) return radio[i].value
	}
	return false;
}
$.urlParam = function(name){
	var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
	if (!results) { return 0; }
	return results[1] || 0;
}
function getquerystringargs() {
	var args = new Object();
	var query = location.search.substring(1);
	var pairs = query.split('&');
	for (var i = 0; i < pairs.length; i++) {
		var pos = pairs[i].indexOf('=');
		if (pos == -1) continue;
		var argname = pairs[i].substring(0,pos);
		var argvalue = pairs[i].substring(pos+1);
		args[argname] = decodeURIComponent(argvalue);
		// or: args[argname] = unescape(argvalue);
	}
	return args;
}
	
