var results;
     
   //this function is called when user presses a link
   function multipleAddresses(e){
		var temp;
		var sourceId;
		var lon = document.getElementById("givenLon");
		var lat = document.getElementById("givenLat");
		var country = document.getElementById("givenCountry");
		var range = document.getElementById("givenRange");
		
		if(e.srcElement) //IE
		{
			temp = window.event.srcElement.id;     //TEST!!!!!!!!!
		}
		else //Mozilla
		{
			//parse which address was selected
			temp = e.target.id;	
		}
			//parse which is the number of the pressed link
			temp.split("k");
			sourceId = temp[4];
			//alert(sourceId);
			//alert(results.Placemark[sourceId].address);
			
				//save longitude and latitude
				lon.value = results.Placemark[sourceId].Point.coordinates[1];
				alert(lon.value);
				lat.value = results.Placemark[sourceId].Point.coordinates[0];
				//alert(lat.value);
			
					//save country(always after the last comma in the response)
					var split = results.Placemark[sourceId].address.split(",");
					var argAmount = split.length;
					country.value = split[argAmount - 1];
					//alert(country.value);
					
					document.postForm.submit();		
	}
	  
    if (GBrowserIsCompatible()) { 
      
      // ====== Create a Client Geocoder ======
      var geo = new GClientGeocoder(); 

      // ====== Array for decoding the failure codes ======
      var reasons=[];
      reasons[G_GEO_SUCCESS]            = "Success";
      reasons[G_GEO_MISSING_ADDRESS]    = "Missing Address: The address was either missing or had no value.";
      reasons[G_GEO_UNKNOWN_ADDRESS]    = "Unknown Address:  No corresponding geographic location could be found for the specified address.";
      reasons[G_GEO_UNAVAILABLE_ADDRESS]= "Unavailable Address:  The geocode for the given address cannot be returned due to legal or contractual reasons.";
      reasons[G_GEO_BAD_KEY]            = "Bad Key: The API key is either invalid or does not match the domain for which it was given";
      reasons[G_GEO_TOO_MANY_QUERIES]   = "Too Many Queries: The daily geocoding quota for this site has been exceeded.";
      reasons[G_GEO_SERVER_ERROR]       = "Server error: The geocoding request could not be successfully processed.";
      
      // ====== Geocoding ======
      function showAddress() {

        var search = document.getElementById("address").value;
		var lon = document.getElementById("givenLon");
		lon.value = "";
		var lat = document.getElementById("givenLat");
		lat.value = "";
		var country = document.getElementById("givenCountry");
		country.value = "";
		var range = document.getElementById("givenRange");		

		// ====== Perform the Geocoding ======        
        geo.getLocations(search, function (result){
            // If that was successful
            if (result.Status.code == G_GEO_SUCCESS) {
              results = result;

			  //How many resuts were found
              //document.getElementById("message").innerHTML = "Found " +result.Placemark.length +" results";
              
			  //save user given range
			  range.value = document.getElementById("sliderDisplay").value;
			  //alert(range.value);
			
			  //if several addresses found, provide links from which to choose to user
			  if(result.Placemark.length > 1){
				document.getElementById("message").innerHTML += "<br>Given address is not unique. Please, choose from the provided addresses.";
			  
				//Loop through the results, placing markers
				for (var i=0; i<result.Placemark.length; i++) {
					var p = result.Placemark[i].Point.coordinates;
					var marker = new GMarker(new GLatLng(p[1],p[0]));
					document.getElementById("message").innerHTML += "<br>" +'<A id="link'+i+'" HREF="" onclick="javascript:multipleAddresses(event); return false">' + result.Placemark[i].address + marker.getPoint() +"</A>";
				}
																
				  // centre the map on the first result
				  var p = result.Placemark[0].Point.coordinates;
				  //map.setCenter(new GLatLng(p[1],p[0]),14);
			  }
				
				//just one address found
				else{
			
					//document.getElementById("message").innerHTML = "Found " +result.Placemark.length +" result";
					var p = result.Placemark[0].Point.coordinates;
					var marker = new GMarker(new GLatLng(p[1],p[0]));
					//document.getElementById("message").innerHTML += "<br>1: "+ result.Placemark[0].address + marker.getPoint();
				
					//save longitude, latitude and range
					lon.value = p[1];
					lat.value = p[0];
					range.value = document.getElementById("sliderDisplay").value;
					
						//save country(always after the last comma in the response)
						var split = result.Placemark[0].address.split(",");
						var argAmount = split.length;
						country.value = split[argAmount - 1];
						//alert(country.value);
						
						document.postForm.submit();				
						
				}				
			}
				// ====== Decode the error status ======
				else {
				  var reason="Code "+result.Status.code;
				  if (reasons[result.Status.code]) {
					reason = reasons[result.Status.code]
				  } 
				  alert('Could not find "'+search+ '" ' + reason);	
				 
				}
          }
        );
	  }
	}
    // display a warning if the browser was not compatible
    else {
      alert("Sorry, the Google Maps API is not compatible with this browser");
    }
