function InitLatLon() {
	         
        var form = document.getElementById("aspnetForm");
        hdnLat = document.getElementById("hdnLat");
        if( hdnLat != null ) hdnLat.value = ( londonlat == lat ) ? '0' : lat;
        
	hdnLng = document.getElementById("hdnLng");
        if( hdnLng != null ) hdnLng.value = ( londonlng == lng ) ? '0' : lng;
        //newcoordLat = document.getElementById("newcoordLat");
        //newcoordLng = document.getElementById("newcoordLng");	    
	//newcoordLat.innerHTML = '0';
	//newcoordLng.innerHTML = '0';

	    return false;
	    
}

     
function load() {

  if( document.getElementById("map") != null ) {

    if (GBrowserIsCompatible()) {
        
	    map = new GMap2(document.getElementById("map"));	         
	    InitLatLon();

        GEvent.addListener(map, "moveend", function() {
                var center = map.getCenter();
	            var zoom = map.getZoom();
		    var hdnZoom = document.getElementById("hdnZoom");
		    if( hdnZoom != null ) hdnZoom.value = zoom;
            }
        );   

	    map.addControl(new GSmallMapControl());
	    var MapTypes = map.getMapTypes();
        map.addControl(new GMapTypeControl());
	    map.enableContinuousZoom();
        map.enableDoubleClickZoom();

        map.setCenter(new GLatLng(lat, lng), zoom);

    	geocoder = new GClientGeocoder();

        // initial display
        ZoomToPoint( null );
	    
        var text='<div style="background-color: #fff; width: 200px;></div>';
        //marker.openInfoWindowHtml(text);

	
   	    GEvent.addListener(map, "click", function(overlay, point) {
	            if (overlay) {
                    map.removeOverlay(overlay);
	                if( hdnLat != null ) hdnLat.value = '0';
	                if( hdnLng != null ) hdnLng.value = '0';
	                //newcoordLat.innerHTML = '0';
	                //newcoordLng.innerHTML = '0';	
	            } else {
                    map.clearOverlays();
                    map.addOverlay(new GMarker(point));
	                if( hdnLat != null ) hdnLat.value = point.y;
	                if( hdnLng != null ) hdnLng.value = point.x;

	                //newcoordLat.innerHTML = point.y;
	                //newcoordLng.innerHTML = point.x;
                }
		    }
		);

	    // create our postcode search object
	    localSearch = new GlocalSearch();
	    localSearch.setAddressLookupMode( GlocalSearch.ADDRESS_LOOKUP_ENABLED );

    }
		
  }

}

function Point( lat, lng ) {
        this.lat = parseFloat(hdnLat.value);
        this.lng = parseFloat(hdnLng.value);
    }

function SetFromCoordinates() {    
    var point = new Point();
    //alert( point.lat );
    ZoomToPoint( new Array(point), 10 );
    return false;
}

function ZoomToPoint( inObject, inZoom ) {
	//alert( inZoom );
    if( inObject != null ) {
	lat = inObject[0].lat;
	lng = inObject[0].lng;
    }else{
	lat = ( londonlat == initlat ) ? londonlat : initlat;
	lng = ( londonlng == initlng ) ? londonlng : initlng;
    }

//alert(lat);
	var ttt = /^[0-9.\-]+$/;
	// check if the coordinates are numeric
	if( !ttt.test(lat) || !ttt.test(lng) ){
	    alert("The coordinates you have entered are invalid");
	// check if the coordinates are withi England location
	//}else if( (lat > 55.838 || lat < 49.923 || lng > 1.845 || lng < -5.77) && (lat != 0 || lng != 0) ) {
	//    alert("The coordinates you have entered are invalid");
	// if the coordinates are ok move to point and put the marker on the map
	}else{
	    var point;
	    if( lat == 0 && lng == 0 ) {
	        point = new GLatLng(parseFloat(londonlat),parseFloat(londonlng));
	    }else{
	        point = new GLatLng(parseFloat(lat),parseFloat(lng));
	    }
	    //map.panTo( point );
	    //map.setZoom( inZoom );
	    if( inZoom != undefined ) {
	    map.setCenter( point , parseInt(inZoom));
	    window.setTimeout(function() {
		map.panTo(point);
	    }, 2000);
	    }else{
	    map.panTo( point );
	    }
/*map.setCenter(new GLatLng(37.4419, -122.1419), 13);
window.setTimeout(function() {
  map.panTo(new GLatLng(37.4569, -122.1569));
}, 2000);*/
	    var marker = new GMarker(point);
	    map.clearOverlays();
	    map.addOverlay(marker);
    	
	    //newcoordLat.innerHTML = ( londonlat == lat ) ? '0' : lat;
	    //newcoordLng.innerHTML = ( londonlng == lng ) ? '0' : lng;
	    if( hdnLat != null ) hdnLat.value = ( londonlat == lat ) ? '0' : lat;
	    if( hdnLng != null ) hdnLng.value = ( londonlng == lng ) ? '0' : lng;
	}
	
	return false;
}
	
	
//#######################################################################################
// Postcode Search handler function
//#######################################################################################
function doPostCodeSearch() {
	         
    txtPostcode = document.getElementById("txtPostcode");        
    if( txtPostcode != null ) {        
	    if (txtPostcode.value.replace(/^\s+|\s+$/g, '') != "") { // we have a search query
		    usePointFromPostcode(txtPostcode.value.replace(/^\s+|\s+$/g, ''), ZoomToPoint);
	    }else{
	        alert("The postcode you entered is invalid. Please check to ensure the postcode is correct");
	    }
	}else{
	    alert("The postcode doesn't exist");
	}
	
	return false;
}
function doAddressSearch() {
//alert(zoom);
	         
    address = document.getElementById("address"); 
    if( address != null ) {        
	    if (address.value.replace(/^\s+|\s+$/g, '') != "") { // we have a search query
		    usePointFromAddress(address.value.replace(/^\s+|\s+$/g, ''), ZoomToPoint);
	    }else{
	        alert("The postcode you entered is invalid. Please check to ensure the postcode is correct");
	    }
	}else{
	    alert("The address doesn't exist");
	}
	
	return false;
}

/*function showAddress(address) {
 var country = document.getElementById("Country").value;
 address = address+( country == "" ? "" : ","+country );
 if (geocoder) {
 geocoder.getLatLng(
 address,
 function(point) {
 if (!point) {
 alert(address + " not found");
 } else {
 
 posset = 1;

 map.setMapType(G_NORMAL_MAP); //G_HYBRID_MAP
 map.setCenter(point,16);
 zm = 1;
 marker.setPoint(point);
 GEvent.trigger(marker, "click");
 }
 }
 );
 }
}*/

//#######################################################################################
// Google AJAX Search API - takes an address, calculates name, lat and lng, returns results in array
// filter matches outside the screen, then zoom screen to position.. 
// there are filters to ensure results are uk based only.... 
//#######################################################################################
function usePointFromPostcode(postcode, callbackFunction) {
	// Set up the callback function that will be executed once the GLocalSearch object finishes
	localSearch.setSearchCompleteCallback( null, function() {
		// Remove anything with lat/longs that are not in the UK
		// (mostly bad results will be in San Francisco - the default centre for the
		// GLocalSearch object)
		for( i = 0; i < localSearch.results.length; i++ ) {
			
			if( localSearch.results[i].lat > 55.838 || localSearch.results[i].lat < 49.923 ||
				localSearch.results[i].lng > 1.845 || localSearch.results[i].lng < -5.77 ) {
				
				// Remove an object from the array, and decrement the counter by one.
				// It will be re-incremented by the for loop, and the element in the same position
				// will be tested again. Otherwise we would jump over the element
				localSearch.results.splice( i, 1 );
				i--;
			}
		}
		
		// If there is at least one result remaining, then we will invoke the callback function,
		// passing it the array of results. Otherwise we just display an error.
		if ( localSearch.results[0] ) {
			callbackFunction(localSearch.results);
		} else {
			alert("We could not determine the location from the postcode you entered. Please check to ensure the postcode is correct.");
		}
	});
	
	// Invoke the search and wait for it to pass the result to our callback function
	localSearch.execute(postcode + ", UK");
	return false;
}
function usePointFromAddress(address, callbackFunction) {
	// Set up the callback function that will be executed once the GLocalSearch object finishes
	localSearch.setSearchCompleteCallback( null, function() {
		// Remove anything with lat/longs that are not in the UK
		// (mostly bad results will be in San Francisco - the default centre for the
		// GLocalSearch object)
		for( i = 0; i < localSearch.results.length; i++ ) {
			
			if( localSearch.results[i].lat > 150 || localSearch.results[i].lat < -150 || localSearch.results[i].lng > 150 || localSearch.results[i].lng < -150 ) {
				
				// Remove an object from the array, and decrement the counter by one.
				// It will be re-incremented by the for loop, and the element in the same position
				// will be tested again. Otherwise we would jump over the element
				localSearch.results.splice( i, 1 );
				i--;
			}
		}
		
		// If there is at least one result remaining, then we will invoke the callback function,
		// passing it the array of results. Otherwise we just display an error.
		if ( localSearch.results[0] ) {
			zoom = 15;
			callbackFunction(localSearch.results,zoom);
		} else {
			alert("We could not determine the location from the postcode you entered. Please check to ensure the postcode is correct.");
		}
	});
	
	// Invoke the search and wait for it to pass the result to our callback function
	
	
	
	
	
	country = document.getElementById("Country").value; // nazwa kraju zamiast kodu!!!!!
	
	localSearch.execute( address + (country!=""?", "+country:"") );
	return false;
}

