	var place;
	var map;
	var geocoder;
	var bounds;
	var ewindow;

   function loadMapWithLoc(startlat,startlong,height) {
      if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("googlemapmap"));
		map.addControl(new GLargeMapControl ());
		map.addControl(new GMapTypeControl());
		map.addControl(new GOverviewMapControl ());
		geocoder = new GClientGeocoder();
		if(height<=0)height=6;
		map.setCenter(new GLatLng(startlat, startlong),height);
		map.setMapType( G_HYBRID_MAP );
		GEvent.addListener(map, "click", function(marker, point) {
		//	X= point.lng();
		//	Y = point.lat();
			var countryselect=document.getElementById("countryselect")
			var countryid=countryselect.options[countryselect.selectedIndex].value;
			
			if(!countryid){
				alert('Please supply a valid country before you use the googlemap');
			}else{
				if(point)
				movemarkertopoint(point.lng(),point.lat(),-1,true);
			}


		});
      }
    }

   function loadFrontpageWithLoc(startlat,startlong,height) {
      if (GBrowserIsCompatible()) {
      	bounds = new GLatLngBounds();
        map = new GMap2(document.getElementById("googlemapmap"));

		map.addControl(new GLargeMapControl ());
		//map.addControl(new GMapTypeControl());
		//map.addControl(new GOverviewMapControl ());
		geocoder = new GClientGeocoder();
		if(height<=0)height=6;
		map.setCenter(new GLatLng(startlat, startlong), height);
		map.setMapType( G_SATELLITE_MAP );  
		ewindow = new EWindow(map, E_STYLE_7); 		
		map.addOverlay(ewindow);		
		
      }
    }
    
    
/*
var baseIcon = new GIcon();
baseIcon.iconSize=new GSize(32,32);
baseIcon.shadowSize=new GSize(56,32);
baseIcon.iconAnchor=new GPoint(16,32);
baseIcon.infoWindowAnchor=new GPoint(16,0);
*/

function createFrontpageToovMarker(lat,lng,title,id,useclickedmarker) {
  var point = new GLatLng(lat,lng);
  var marker;

  if(useclickedmarker){
  	marker= new GMarker(point,martini_icon);  	
  }else{
  	marker= new GMarker(point,toov_icon);
  }
  
 GEvent.addListener(marker, "mouseover", function() {
  	ewindow.openOnMarker(marker,title) ;
  });
    
  GEvent.addListener(marker, "click", function() {
  	ewindow.openOnMarker(marker,title) ;
  	clistener(marker,point,id,title);
  });
  
  bounds.extend(point);
  map.addOverlay(marker);
  if(useclickedmarker){
  	ewindow.openOnMarker(marker,title) ;
  }
}

function clistener(marker,point,id,title){
  	map.removeOverlay(marker);
    marker = new GMarker(point,martini_icon);
  	map.addOverlay(marker);
  	showfrontpagetoov(id);
	GEvent.addListener(marker, "click", function() {
		ewindow.openOnMarker(marker,title);
  		clistener(marker,point,id,title);
	  });
}

function showfrontpagetoov(id){
		var pointer = this;
		// create ajax request
		var url = "includes/ajax/frontpage_map.php?id="+encodeURIComponent(id);
		var onSuccessFunc = function (req) {

			var xml = req.responseXML;
			if(xml.getElementsByTagName('results').length>0){
				var results = xml.getElementsByTagName('results')[0].childNodes;

				if(results.length>0){
					var rs = xml.getElementsByTagName('rs');
					if(rs.length==9){// we have info for this toov

						var divbody="";
					divbody+="<h3><a href='toov.php?id="+id+"'>"+rs[0].childNodes[0].nodeValue+"</a></h3>";
					if(rs[1].childNodes[0]){
						divbody+="<p>"+rs[1].childNodes[0].nodeValue+"</p>";
					}else{
						divbody+="<p></p>";
					}

					divbody+="<div class='info'>";
					divbody+="	<a href='toovlist.php?cat=showcountry&id="+rs[7].childNodes[0].nodeValue+"'>"+rs[6].childNodes[0].nodeValue+"</a> | "+rs[3].childNodes[0].nodeValue+" <a href='toovlist.php?cat=user&id="+rs[8].childNodes[0].nodeValue+"'>"+rs[2].childNodes[0].nodeValue+"</a> | "+rs[4].childNodes[0].nodeValue;
					divbody+="</div>";
					divbody+="<div class='imgbox'>";
					divbody+="	<div class='inner'>";

					if(parseInt(rs[5].childNodes[0].nodeValue)>0){
						var img="useruploads/"+rs[5].childNodes[0].nodeValue+"_tumb161.jpg";
						divbody+="		<a href='toov.php?id="+id+"'><img src='"+img+"' width='161' height='107' alt='' /></a>";

					}else{
						divbody+="		<a href='toov.php?id="+id+"'><img src='images/dummy_entry_161x107.jpg' width='161' height='107' alt='' /></a>";

					}
					divbody+="	</div>";
					divbody+="</div>";
					divbody+="<div class='buttons'>";
					divbody+="	<div class='lbuttonalt'>";
					divbody+="		<a href='aetoov.php?cat=didthis&pid="+id+"'>Did This</a>";
					divbody+="	</div>";
					divbody+="	<div class='lbuttonalt'>";
					divbody+="		<a href='aetoov.php?cat=todo&pid="+id+"'>TOOV to do</a>";
					divbody+="	</div>";
					divbody+="</div>";

					writetodiv( divbody,"mapentry")
					}
				}
			}
		};
		var onErrorFunc = function (status) { alert("AJAX error: "+status); };
		var myAjax = new _bsn.Ajax;
		myAjax.makeRequest( url, "get", onSuccessFunc, onErrorFunc );
}

function updateScreenPosAndZoom(altbounds){
  var b;
  
  if(altbounds)b=altbounds;
  else b=bounds;
  map.setZoom(map.getBoundsZoomLevel(b));

   // ===== determine the centre from the bounds ======
   var clat = (b.getNorthEast().lat() + b.getSouthWest().lat()) /2;
   var clng = (b.getNorthEast().lng() + b.getSouthWest().lng()) /2;
   map.setCenter(new GLatLng(clat,clng));
}

function updateMapLoc(){

	var deg_long=document.getElementById('degr_long').value;
	var min_long=document.getElementById('minutes_long').value;
	var sec_long=document.getElementById('seconds_long').value;
	var deg_lat=document.getElementById('degr_lat').value;
	var min_lat=document.getElementById('minutes_lat').value;
	var sec_lat=document.getElementById('seconds_lat').value;

	if (deg_long > 180 || deg_long < 0 || deg_lat > 180 || deg_lat < 0){
			alert("Enter a value less than 180 or greater than 0 for degrees.");
			return ;
	}
	else if (min_long > 60 || min_long < 0 || min_lat > 60 || min_lat < 0){
			alert("Enter a value less than 60 or greater than 0 for minutes.");
			return ;
	}
	else if (sec_long > 60 || sec_long < 0 || sec_lat > 60 || sec_lat < 0){
			alert("Enter a value less than 60 or greater than 0 for seconds.");
			return ;
	}


	if(deg_long!="" && min_long!=""&& sec_long!="" && deg_lat!="" && min_lat!=""&& sec_lat!=""){

		var dec_min_long = (min_long*1.0 + (sec_long/60.0));
		var xpos = deg_long*1.0 + (dec_min_long/60.0);

		var dec_min_lat = (min_lat*1.0 + (sec_lat/60.0));
		var ypos = deg_lat*1.0 + (dec_min_lat/60.0);


		var selectBoxlat = document.getElementById('latdir');
		var selectBoxlong = document.getElementById('longdir');

		if(selectBoxlat.options[selectBoxlat.selectedIndex].value=="S"){
			ypos=ypos*-1;
		}
		if(selectBoxlong.options[selectBoxlong.selectedIndex].value=="E"){
			xpos=xpos*-1;
		}


		movemarkertopoint(xpos,ypos,7,true);
	}
}



var selectedLocation = function (val){
	// create ajax request
	var countryselect=document.getElementById("countryselect")
	if(!countryselect)
		return ;

	var countryid=countryselect.options[countryselect.selectedIndex].value;

	if(!countryid)
		return ;
		
	if(!val) return ;
	if(!countryid)return ;
	var url = "includes/ajax/coordinates.php?idsearch=true&country="+countryid+"&input="+encodeURIComponent(val);
	var meth = "get";
	var onSuccessFunc = function (req) {
		var xml = req.responseXML;
		var els = xml.getElementsByTagName("rs");		
		if(els.length==4){// dan hebben we alle params
			var direction_lat=els[0].firstChild.nodeValue;
			var direction_long=els[1].firstChild.nodeValue;
			if(els[2].firstChild){
				var dec_lat=els[2].firstChild.nodeValue;
				var dec_long=els[3].firstChild.nodeValue;
				

				if(direction_lat=="S"){
					dec_lat=dec_lat*-1;
				}
				if(direction_long=="E"){
					dec_long=dec_long*-1;
				}
				var selectBoxlat = document.getElementById('latdir');
				var selectBoxlong = document.getElementById('longdir');
				if(selectBoxlat && selectBoxlong){
					if(direction_lat=="S"){
						selectBoxlat.selectedIndex=1;
					}else{
						selectBoxlat.selectedIndex=0;
					}

					if(direction_long=="E"){
						selectBoxlong.selectedIndex=1;
					}else{
						selectBoxlong.selectedIndex=0;
					}
				}
				
				var lta=document.getElementById('LT_address');
				if(lta){ //check of we op aetoov page zitten
 					movemarkertopoint(dec_long,dec_lat,11,true);
				}else{					
					map.setCenter(new GLatLng(dec_lat,dec_long),13); 
						
				}
				//document.addnewtoovform.LT_address.focus();
				
				if(lta && !lta.disabled){
					lta.focus();
				}
			}
		}

	};
	var onErrorFunc = function (status) { alert("AJAX error: "+status); };

	var myAjax = new _bsn.Ajax;
	myAjax.makeRequest( url, meth, onSuccessFunc, onErrorFunc );
};

function movemarkertopoint(dec_long,dec_lat,depth,iwanttoupdateformdata){
	
	var countryselect=document.getElementById("countryselect")
	if(!countryselect)
		return ;

	var countryid=countryselect.options[countryselect.selectedIndex].value;	
	
	if(!countryid){
		alert('please select the country before pinpointing the location on the map');
		return false;
	}


	if(place)
	{
		map.removeOverlay(place);
	}

	if(document.getElementById('xcoordinate')){
		document.getElementById('xcoordinate').value = dec_long;
	}

	if(document.getElementById('ycoordinate')){
		document.getElementById('ycoordinate').value = dec_lat;
	}

	if(document.getElementById('viewdepth')){
		document.getElementById('viewdepth').value = depth;
	}

	place = new GMarker(new GLatLng(dec_lat,dec_long),{ icon:toov_icon,draggable: true} );	
	map.addOverlay(place);

	if(depth>0){
		map.setCenter(new GLatLng(dec_lat, dec_long));
		map.setCenter(null,depth);
	}else{
		map.setCenter(new GLatLng(dec_lat, dec_long));
	}

	GEvent.addListener(place, "dragend", function() {
		
		  movemarkertopoint(place.getPoint().lng(),place.getPoint().lat(),map.getZoom(),iwanttoupdateformdata);
		  ///updateformdata(place.getPoint().lng(),place.getPoint().lat(),depth);
		  });

    if(iwanttoupdateformdata){
		updateformdata(dec_long,dec_lat,depth);
    }

}

function updateformdata(dec_long,dec_lat,depth){

	//latitude convert do degrees
	var gpsdeg_lat = parseInt(dec_lat);
	var remainder_lat = dec_lat - (gpsdeg_lat * 1.0);
	var gpsmin_lat = remainder_lat * 60.0;
	document.getElementById('degr_lat').value = gpsdeg_lat;
	document.getElementById('minutes_lat').value = parseInt(gpsmin_lat);
	var remainder_lat2 = gpsmin_lat - (parseInt(gpsmin_lat)*1.0);
	document.getElementById('seconds_lat').value = parseInt(remainder_lat2*60.0);

	var selectBoxlat = document.getElementById('latdir');
	if(gpsdeg_lat<0){

		selectBoxlat.selectedIndex=1;
		document.getElementById('degr_lat').value*=-1;
		document.getElementById('minutes_lat').value*=-1;
		document.getElementById('seconds_lat').value*=-1;
	}else{
		selectBoxlat.selectedIndex=0;
	}

	//longitude convert do degrees
	var gpsdeg_long = parseInt(dec_long);
	var remainder_long = dec_long - (gpsdeg_long * 1.0);
	var gpsmin_long = remainder_long * 60.0;
	document.getElementById('degr_long').value = gpsdeg_long;
	document.getElementById('minutes_long').value = parseInt(gpsmin_long);
	var remainder_long2 = gpsmin_long - (parseInt(gpsmin_long)*1.0);
	document.getElementById('seconds_long').value = parseInt(remainder_long2*60.0);

	var selectBoxlong = document.getElementById('longdir');
	if(gpsdeg_long<0){
		selectBoxlong.selectedIndex=1;
		document.getElementById('degr_long').value*=-1;
		document.getElementById('minutes_long').value*=-1;
		document.getElementById('seconds_long').value*=-1;
	}else{
		selectBoxlong.selectedIndex=0;
	}

}

var selectedCountry = function (val){

	//check if a valid country is set

	var pointer = this;
	// create ajax request
	var url = "includes/ajax/countries.php?precise=1&input="+encodeURIComponent(val);
	var onSuccessFunc = function (req) {

			var xml = req.responseXML;
			if(xml.getElementsByTagName('results').length>0){
				var results = xml.getElementsByTagName('results')[0].childNodes;

				if(results.length>0){
					var rs = xml.getElementsByTagName('rs');
					if(rs.length==6){// we have coords for this country

						var swlat=parseFloat(rs[4].childNodes[0].nodeValue);//south
						var swlong=parseFloat(rs[1].childNodes[0].nodeValue);//west		
						
						var nelat=parseFloat(rs[2].childNodes[0].nodeValue);//north
						var nelong=parseFloat(rs[3].childNodes[0].nodeValue);//east
						var latlang=new GLatLng((swlat+nelat)/2,(swlong+nelong)/2);
						map.setCenter(latlang); 
						map.setZoom(map.getBoundsZoomLevel(new GLatLngBounds(new GLatLng(swlat,swlong),new GLatLng(nelat,nelong)))); 
 				        
						var hasgooglegeolocations = parseInt(rs[5].childNodes[0].nodeValue);
						
						if(document.getElementById("LT_location")){
							document.getElementById("LT_location").disabled=false;
						}
						if(document.getElementById("LT_address")){

							if(hasgooglegeolocations==1){
								document.getElementById("LT_address").disabled=false;								
								toggleLayer('go_address','block');
							}else{
								document.getElementById("LT_address").disabled=true;
								toggleLayer('go_address','none');
																		
								document.getElementById("LT_address").value="";
							}
						}
					}
					if(document.getElementById("LT_location")){						
						new AutoSuggestControl(document.getElementById("LT_location"), new BasicSuggestions("includes/ajax/locations.php?country="+encodeURIComponent(val)+"&input="),selectedLocation,3);
					}
					document.getElementById("LT_location").value="";
					////document.addnewtoovform.LT_location.focus();
					var ltl= document.getElementById('LT_location');
					ltl.focus();

				}
			}
	};
	var onErrorFunc = function (status) { alert("AJAX error: "+status); };
	var myAjax = new _bsn.Ajax;
	myAjax.makeRequest( url, "get", onSuccessFunc, onErrorFunc );
};



function selectcountrybyid(){

	//check if a valid country is set

	var countryselect=document.getElementById("countryselect")
	if(!countryselect)
		return ;

	var countryid=countryselect.options[countryselect.selectedIndex].value;

	if(!countryid)
		return ;
		
	var pointer = this;
	// create ajax request
	var url = "includes/ajax/countries.php?idsearch=true&precise=1&input="+countryid;
	var onSuccessFunc = function (req) {

			var xml = req.responseXML;
			if(xml.getElementsByTagName('results').length>0){
				var results = xml.getElementsByTagName('results')[0].childNodes;

				if(results.length>0){
					var rs = xml.getElementsByTagName('rs');
					if(rs.length==6){// we have coords for this country
						document.getElementById("countryhidden").value=rs[0].childNodes[0].nodeValue;
						var swlat=parseFloat(rs[4].childNodes[0].nodeValue);//south
						var swlong=parseFloat(rs[1].childNodes[0].nodeValue);//west		
						
						var nelat=parseFloat(rs[2].childNodes[0].nodeValue);//north
						var nelong=parseFloat(rs[3].childNodes[0].nodeValue);//east
						var latlang=new GLatLng((swlat+nelat)/2,(swlong+nelong)/2);
						map.setCenter(latlang); 
						map.setZoom(map.getBoundsZoomLevel(new GLatLngBounds(new GLatLng(swlat,swlong),new GLatLng(nelat,nelong)))); 
 				        
						var hasgooglegeolocations = parseInt(rs[5].childNodes[0].nodeValue);
						
						if(document.getElementById("LT_location")){
							document.getElementById("LT_location").disabled=false;
						}
						if(document.getElementById("LT_address")){

							if(hasgooglegeolocations==1){
								document.getElementById("LT_address").disabled=false;								
								toggleLayer('go_address','block');
							}else{
								document.getElementById("LT_address").disabled=true;
								toggleLayer('go_address','none');
																		
								document.getElementById("LT_address").value="";
							}
						}
					}
					if(document.getElementById("LT_location")){						
						new AutoSuggestControl(document.getElementById("LT_location"), new BasicSuggestions("includes/ajax/locations.php?idsearch=true&country="+countryid+"&input="),selectedLocation,3);
					}
					document.getElementById("LT_location").value="";
					////document.addnewtoovform.LT_location.focus();
					var ltl= document.getElementById('LT_location');
					ltl.focus();

				}
			}
	};
	var onErrorFunc = function (status) { alert("AJAX error: "+status); };
	var myAjax = new _bsn.Ajax;
	myAjax.makeRequest( url, "get", onSuccessFunc, onErrorFunc );
};


function showAddress(address,locatie,land) {
  if(!address){
  	alert("Please provide an address");
  	return false;
  }
  if (geocoder) {

  	if(locatie){
  		address=address+" "+locatie;
  	}
  	if(land){
  		address=address+" "+land;
  	}

    geocoder.getLatLng(
      address,
      function(point) {
        if (!point) {
          alert(address + " not found");
          return false;
        } else {

		  movemarkertopoint(point.lng(),point.lat(),15,true);
        }
      }
    );
  }
}

function onbluraddress(){
	var lta= document.getElementById('LT_address');

	if(lta.value){
		var ltl= document.getElementById('LT_location');
		var ltc= document.getElementById('countryhidden');
		
		return showAddress(lta.value,ltl.value,ltc.value);
	}
	return true;
}

