﻿
var map;
var geocoder;
var localSearch = new GlocalSearch();

function initialize() {
 map = new GMap2(document.getElementById("map"));
 map.setCenter(new GLatLng(34, 0), 1);
 geocoder = new GClientGeocoder();
 map.addControl(new GSmallZoomControl3D());
}

// addAddressToMap() is called when the geocoder returns an
// answer.  It adds a marker to the map with an open info window
// showing the nicely formatted version of the address and the country code.
function addAddressToMap(response) {
 map.clearOverlays();
 if (!response || response.Status.code != 200) {
  // alert("Sorry, we were unable to locate the address");
   document.getElementById("ctl00_ContentPlaceHolder1_TBLong").value =  '';
   document.getElementById("ctl00_ContentPlaceHolder1_TBLat").value =  '';                 
   return false;
 } else {
   place = response.Placemark[0];
   point = new GLatLng(place.Point.coordinates[1],
                       place.Point.coordinates[0]); 
   document.getElementById("ctl00_ContentPlaceHolder1_TBLong").value =  place.Point.coordinates[0];
   document.getElementById("ctl00_ContentPlaceHolder1_TBLat").value =  place.Point.coordinates[1];                 
   placeMarkerAtPoint(point);                
                                       
//   marker = new GMarker(point);
//   map.addOverlay(marker);
//      marker.openInfoWindowHtml(place.address + '<br>' +
//     '<b>Country code:</b> ' + place.AddressDetails.Country.CountryNameCode);
// setCenterToPoint(point);

//var viewport = new GLatLngBounds; 
//viewport.extend(new GLatLng(place.ExtendedData.LatLonBox.south, place.ExtendedData.LatLonBox.west)); 
//viewport.extend(new GLatLng(place.ExtendedData.LatLonBox.north, place.ExtendedData.LatLonBox.east)); 
//map.setCenter(viewport.getCenter(), map.getBoundsZoomLevel(viewport)); 

  zoomAndCenter(place);

 }
}

// showLocation() is called when you click on the Search button
// in the form.  It geocodes the address entered into the form
// and adds a marker to the map at that location.
function showLocation(address) {
 //var address = document.forms[0].q.value;
 geocoder.getLocations(address, addAddressToMap);
}

// findLocation() is used to enter the sample addresses into the form.
function findLocation(address) {
initialize()
// document.forms[0].q.value = address;
 showLocation(address);
}

function usePointFromPostcode(postcode) {
	localSearch.setSearchCompleteCallback(null, 
		function() {
			
			if (localSearch.results[0])
			{	
				map.clearOverlays();
				var resultLat = localSearch.results[0].lat;
				var resultLng = localSearch.results[0].lng;
				var point = new GLatLng(resultLat,resultLng);
				
	document.getElementById("ctl00_ContentPlaceHolder1_TBLong").value =  resultLng;
   document.getElementById("ctl00_ContentPlaceHolder1_TBLat").value =  resultLat; 
				
				
			//	callbackFunction(point);
				setCenterToPoint(point);
				//var marker = new GMarker(point);
               // map.addOverlay(marker);
				placeMarkerAtPoint(point);
			}else{
				alert("Postcode not found!");
			}
		});	
		
	localSearch.execute(postcode + ", UK");
}


function placeMarkerAtPoint(point)
{
	var marker = new GMarker(point);
	map.addOverlay(marker);
	
}

function zoomAndCenter(place)
{
var viewport = new GLatLngBounds; 
viewport.extend(new GLatLng(place.ExtendedData.LatLonBox.south, place.ExtendedData.LatLonBox.west)); 
viewport.extend(new GLatLng(place.ExtendedData.LatLonBox.north, place.ExtendedData.LatLonBox.east)); 
map.setCenter(viewport.getCenter(), map.getBoundsZoomLevel(viewport)); 
}


function setCenterToPoint(point)
{
	map.setCenter(point, 15);
}






function getContinent()
{
var oDDLContinent = document.all("ctl00_ContentPlaceHolder1_DDLContinent");
var curTextDDLContinent = oDDLContinent.options[oDDLContinent.selectedIndex].text;
    if (curTextDDLContinent != "Please select")
    {
    document.getElementById("ctl00_ContentPlaceHolder1_Continent").value = curTextDDLContinent;
   
    buildAddress()
    }
}


function getNation()
{
var oDDLNation = document.all("ctl00_ContentPlaceHolder1_DDLNation");
var curTextDDLNation = oDDLNation.options[oDDLNation.selectedIndex].text;
    if (curTextDDLNation != "Please select")
    {
    document.getElementById("ctl00_ContentPlaceHolder1_Nation").value = curTextDDLNation;
    buildAddress()
    }
}


function getCity()
{
var oDDLCity = document.all("ctl00_ContentPlaceHolder1_DDLCity");
var curTextDDLCity = oDDLCity.options[oDDLCity.selectedIndex].text;
    if (curTextDDLCity != "Please select" && curTextDDLCity != "Other")
    {
    document.getElementById("ctl00_ContentPlaceHolder1_City").value = curTextDDLCity;
    }
    else
    {
    document.getElementById("ctl00_ContentPlaceHolder1_City").value = ""
    }
    buildAddress()
}


function getCityOther()
{
var curTextTBCityOther = document.getElementById("ctl00_ContentPlaceHolder1_TBCityOther").value;
    if (curTextTBCityOther != "")
    {
    document.getElementById("ctl00_ContentPlaceHolder1_City").value = curTextTBCityOther;
    buildAddress()
    }
}

function getAddress()
{
var curTextTBAddress = document.getElementById("ctl00_ContentPlaceHolder1_TBAddress").value;
    //if (curTextTBAddress != "")
    //{
    document.getElementById("ctl00_ContentPlaceHolder1_Address").value = curTextTBAddress;
    buildAddress()
    //}
}

function getPostcode()
{
var curTextTBPostcode = document.getElementById("ctl00_ContentPlaceHolder1_TBPostcode").value;
    //if (curTextTBPostcode != "")
    //{
    document.getElementById("ctl00_ContentPlaceHolder1_Postcode").value = curTextTBPostcode;
    buildAddress()
   // }
}



function buildAddress()
{
var addressStr;
addressStr=""
    if(document.getElementById("ctl00_ContentPlaceHolder1_DDLContinent").value != "")
    {
    addressStr = document.getElementById("ctl00_ContentPlaceHolder1_Continent").value;
    }

    
    if(document.getElementById("ctl00_ContentPlaceHolder1_Nation").value != "")
    {
    addressStr = addressStr + ' ' + document.getElementById("ctl00_ContentPlaceHolder1_Nation").value;
    }
    
    if(document.getElementById("ctl00_ContentPlaceHolder1_City").value != "")
    {
    addressStr = addressStr + ' ' + document.getElementById("ctl00_ContentPlaceHolder1_City").value;
    }
    
    if(document.getElementById("ctl00_ContentPlaceHolder1_Address").value != "")
    {
    addressStr = addressStr + ' ' + document.getElementById("ctl00_ContentPlaceHolder1_Address").value;
    }
    if(document.getElementById("ctl00_ContentPlaceHolder1_Postcode").value != "")
    {
    addressStr = addressStr + ' ' + document.getElementById("ctl00_ContentPlaceHolder1_Postcode").value;
    }
    
    if(document.getElementById("ctl00_ContentPlaceHolder1_Nation").value == "United Kingdom" && document.getElementById("ctl00_ContentPlaceHolder1_Postcode").value != "" || document.getElementById("ctl00_ContentPlaceHolder1_Nation").value == "Inghilterra" && document.getElementById("ctl00_ContentPlaceHolder1_Postcode").value != "" ) 
    {
    usePointFromPostcode(document.getElementById("ctl00_ContentPlaceHolder1_Postcode").value);
    }
   else
    {
    findLocation(addressStr);
    }

}





