function rescueLookUp() {

	if(!document.getElementById('rescue_locator')) return false;
	
	var location_form = document.getElementById('rescue_locator'); //Set the form variable
	
	location_form.onsubmit = function() {
	
		var postcode = document.getElementById('postcode')
	
		if(postcode.value == '') { //Is there an postcode added
		
			alert('Please enter your postcode');
			return false;
		
		} else { //Postcode entered
		
			var geocoder = new GClientGeocoder();	// geocoder class 
		
			geocoder.getLatLng(postcode.value, function(point) {
				if (!point) {
				//alert(document.getElementById("postcode").value + " not found");
				} else {
					if(document.getElementById("googlelatlong")) {
					
						document.getElementById("googlelatlong").value = point;
					
					} else {
						
						document.getElementById("co_ords").value = point;
						
					}
				//alert(point);
				}
			});
			
			setTimeout(function(){
				if((document.getElementById("googlelatlong") && document.getElementById("googlelatlong").value != "") || (document.getElementById("co_ords") && document.getElementById("co_ords").value != "")){
				//alert(document.getElementById("googlelatlong").value);
				document.getElementById('rescue_locator').submit();
				}
				else{
				alert(postcode.value + " not found");
				}
			}, 1500);
			
			return false;
		
		}
		
		return false;
	
	}
	

}

//Find the co-ordinates for the postcode search
function getPostcodePoint(postcode, callBack) {
  
	var locationSearch = new GlocalSearch(); //Initialise the class

	locationSearch.setSearchCompleteCallback(null,
															  
		function() {
	
			//Are there results to show
			if (locationSearch.results.length > 0) { 
			
				var latitude = locationSearch.results[0].lat; //Get tha latitude
				var longitude = locationSearch.results[0].lng; //Get the longitude
				var point = new GLatLng(latitude,longitude); //Find the point the postcode
				
				eval(callBack)(point); //Return the location back to the call back
		
			} else { //No results found for this postcode
				
				alert("Postcode not found!");
			
			}
		
	}); 
	
	//Do the search for the postcode
	locationSearch.execute(postcode + ", UK");
	
}
	

//Add the controls to the map
function addControls(map) {
	
	map.getDefaultUI();
	map.addControl(new GLargeMapControl3D()); // zoom controls
	map.addControl(new GScaleControl()); // zoom controls

}

//Gets the location of sites for the pet advisor
function centreLocation() {
	
	if($('#googlelatlong').length == 0) return false;
	
	//Location of centre
	var lat = $('#lat').val();
	var long = $('#long').val();
	var canvas = document.getElementById('map');
	
	var geocoder = new GClientGeocoder(); // geocoder class
	this.map = new GMap2(canvas); // map
	
	var point = new GLatLng(lat,long);
	var marker = new GMarker(point, {draggable: true});
	
	this.map.setCenter(point, 13);
	this.map.addOverlay(marker);
	
	addControls(this.map); //Add controls to the map

	dragListener(marker, this.map); //Event listener
	
}

//Manage the dragable markers
function dragListener(marker, map) {

	GEvent.addListener(marker, "dragend", function() { //Listen for a drag
												   
		var location = marker.getLatLng(); //Get the new location of the marker
		
		map.panTo(location); //Move the map to the new point
		
		//Update the centre information
		$.ajax({
			type: 'GET',
			url: 'libs/includes/ajax/new_map_location.php?latlng='+location
		});
		
	});

}

//If the centre has moved we need to look for there new location
function postcodeSearch(el) {
	
	$(el).click(function () {
		
		var postcode = $('#postcode').val(); //Get the postcode
		var geocoder = new GClientGeocoder(); // geocoder class
		
		getPostcodePoint(postcode, 'plotPostcode'); //Find the co-ordinates for the postcode
		
		return false;
						  
    });
	
}

function startSearch() {

	if(!document.getElementById('advisor_search_form')) return false;
	
	var location_form = document.getElementById('advisor_search_form'); //Set the form variable
	
	location_form.onsubmit = function() {
	
		var postcode = document.getElementById('postcode');
	
		if(postcode.value == '') { //Is there an postcode added
		
			alert('Please enter your postcode');
			return false;
		
		} else { //Postcode entered
		
			var locationSearch = new GlocalSearch(); //Initialise the class

			locationSearch.setSearchCompleteCallback(null,
																	  
			function() {
		
				//Are there results to show
				if (locationSearch.results.length > 0) { 
				
					var latitude = locationSearch.results[0].lat; //Get tha latitude
					var longitude = locationSearch.results[0].lng; //Get the longitude
					var point = new GLatLng(latitude,longitude); //Find the point the postcode
					
					$('#co_ords').val(point);
					
					setTimeout(function(){		
								
						if($('#co_ords').val() != '') {
							
							document.getElementById('advisor_search_form').submit();
						
						} else {
						
							alert(postcode.value + " not found")
							
						}
						
					}, 1500);
			
				} else { //No results found for this postcode
					
					alert("Postcode not found!");
				
				}
				
			}); 
	
			//Do the search for the postcode
			locationSearch.execute(postcode.value + ", UK");
	
		}
		
		return false;
		
	}

}

function plotCentre(lat, long) {

	var canvas = document.getElementById('resultsMap'); //Set the map canvas
	var map = new GMap2(canvas); //Create the maps
	var point = new GLatLng(lat,long);
	map.addOverlay(marker); //Put the marker down

}

//Put the location of the searchee on the map
function fromLocation(point) {

	var canvas = document.getElementById('map'); //Set the map canvas
	var map = new GMap2(canvas); //Create the maps
		
	map.setCenter(point, 13); //Set the new centre of the map
	map.addOverlay(marker); //Put the marker down
	
	addControls(map); //Add controls to the map

}

//Place the marker on to the map
function plotPostcode(point) {
	
	var canvas = document.getElementById('map'); //Set the map canvas
	var map = new GMap2(canvas); //Create the maps
		
	map.setCenter(point, 13); //Set the new centre of the map
	
	var marker = new GMarker(point, {draggable: true}); //Make the marker draggable
	map.addOverlay(marker); //Put the marker down
	
	addControls(map); //Add controls to the map
	
	//Update the centre information
	$.ajax({
		type: 'GET',
		url: 'libs/includes/ajax/new_map_location.php?latlng='+point
	});
	
	dragListener(marker, map); //Listen for a drag

}

window.onunload = function() {

	 GUnload();

}