var map = null;
var GPSlat = "51.399206"; //default long/ lat
var GPSlng = "-0.571289";
var zoom = 13;
google.load("maps", "2.x");
var localSearch = new GlocalSearch();
google.setOnLoadCallback(init);

function init() {
    var longLat = document.getElementById('AddressEdit_GeolocationTextBox').value.split(",");
    if (longLat[0] != null) { GPSlat = longLat[0] }
    if (longLat[1] != null) { GPSlng = longLat[1] }
    
    
	 if (GBrowserIsCompatible()) {

	 			/* initialize the map */
				map = new GMap2(document.getElementById("map"));
				map.enableContinuousZoom();
				map.addMapType(G_SATELLITE_MAP);
				map.addControl(new GMapTypeControl());
				map.addControl(new GLargeMapControl3D());

				/* center the map */
				var point = new GLatLng(GPSlat, GPSlng);
				var marker = new GMarker(point, { draggable: true });
				map.addOverlay(marker);
				
				map.setCenter(point, zoom);
				localSearch.setCenterPoint(map);
				GEvent.addListener(map, "dragend", function() { localSearch.setCenterPoint(map); });

				GEvent.addListener(marker, "dragend", function() {
				    var point = this.getLatLng();
				    if (document.getElementById('lat') != null) { document.getElementById('lat').value = point.y; }
				    if (document.getElementById('lng') != null) { document.getElementById('lng').value = point.x; }
				    document.getElementById('AddressEdit_GeolocationTextBox').value = point.y + "," + point.x;
				});	    
		}
	else {
				document.getElementById("map").innerHTML="<div>init failed.</div>";
	}
}

function LS(term, callbackFunction) {
  localSearch.setSearchCompleteCallback(null, function() {
      if (localSearch.results[0]) {    
        var resultLat = localSearch.results[0].lat;
        var resultLng = localSearch.results[0].lng;
        var point = new GLatLng(resultLat,resultLng);
        var marker = new GMarker(point, { draggable: true })
				
		    map.setCenter(point, zoom);
		    map.clearOverlays();
		    map.addOverlay(marker);
				GEvent.addListener(marker, "dragend", function() {
		      var point = this.getLatLng();
			    if(document.getElementById('lat')!=null){document.getElementById('lat').value = point.y;}
			    if (document.getElementById('lng') != null) { document.getElementById('lng').value = point.x; }
			    document.getElementById('AddressEdit_GeolocationTextBox').value = point.y +"," + point.x;
			  });
				callbackFunction();		    
      }
      else {
        alert("sorry, no results.");
      }
    });  
  localSearch.execute(term);
}

function getByPostCode() {

    postCode = document.getElementById('AddressEdit_PostCodeTextBox').value;
    countryDropDown = document.getElementById('AddressEdit_addressCountryDropDownList');
    country = countryDropDown[countryDropDown.selectedIndex].text;
    
    search = postCode + " " + country;
    //alert(search);
    LS(search, fetch);
}

function getBySearch() {

    search = document.getElementById('searchBox').value;
    countryDropDown = document.getElementById('AddressEdit_addressCountryDropDownList');
    country = countryDropDown[countryDropDown.selectedIndex].text;

    search = search + " " + country;
    //alert(search);
    LS(search, fetch);
}

function getJustGeoFromSearch() {
    search = document.getElementById('searchBox').value;
    countryDropDown = document.getElementById('AddressEdit_addressCountryDropDownList');
    country = countryDropDown[countryDropDown.selectedIndex].text;

    search = search + " " + country;

    LS(search, fetchJustGeo);
}


function fetch(){
		/* fetching the result array into form fields */
        var street=''; var city ='';var region ='';var country ='';var address0 = '';var address1 = '';  var postCode = '';
        document.getElementById('lat').value = localSearch.results[0].lat;
        document.getElementById('lng').value = localSearch.results[0].lng;
        document.getElementById('AddressEdit_GeolocationTextBox').value = localSearch.results[0].lat + "," + localSearch.results[0].lng;
        //document.getElementById('AddressEdit_buildingNameTextBox').value = localSearch.results[0].titleNoFormatting;

		if(localSearch.results[0].streetAddress.length > 0){street = localSearch.results[0].streetAddress;}
		if(localSearch.results[0].city.length > 0){city = localSearch.results[0].city;}
		if(localSearch.results[0].region.length > 0){region = localSearch.results[0].region;}
		if(localSearch.results[0].country.length > 0){country = localSearch.results[0].country;}
		if(localSearch.results[0].addressLines.length > 0){address0 = localSearch.results[0].addressLines[0];}
		if(localSearch.results[0].addressLines.length > 1){address1 = localSearch.results[0].addressLines[1];}
		if(localSearch.resulta[0].postCode.length > 0){postCode = localSearch.resulta[0].postCode;}

		if (document.getElementById('AddressEdit_addressLine1TextBox') != null) { document.getElementById('AddressEdit_addressLine1TextBox').value = street; }
		if (document.getElementById('AddressEdit_AddressTownTextBox') != null) { document.getElementById('AddressEdit_AddressTownTextBox').value = city; }
		if (document.getElementById('AddressEdit_AddressRegionTextBox') != null) { document.getElementById('AddressEdit_AddressRegionTextBox').value = region; }
		if (document.getElementById('AddressEdit_AddressPostCode') != null) { document.getElementById('AddressEdit_AddressPostCode').value = postCode; }
		
		
  	//if(document.getElementById('country')!=null){document.getElementById('country').value=country;}
		//if (document.getElementById('AddressEdit_addressLine2TextBox') != null) { document.getElementById('AddressEdit_addressLine2TextBox').value = address0; }
		//if (document.getElementById('AddressEdit_addressLine3TextBox') != null) { document.getElementById('AddressEdit_addressLine3TextBox').value = address1; }
}

function fetchJustGeo() {
    /* fetching the result array into form fields */
    var street = ''; var city = ''; var region = ''; var country = ''; var address0 = ''; var address1 = '';
    document.getElementById('lat').value = localSearch.results[0].lat;
    document.getElementById('lng').value = localSearch.results[0].lng;
    document.getElementById('AddressEdit_GeolocationTextBox').value = localSearch.results[0].lat + "," + localSearch.results[0].lng;


}

function entered(e){var keyCode;if(window.event)keycode=window.event.keyCode;else if(e)keycode=e.which;else return false;return(keycode==13);}
