
function getXmlHttpRequest()
{
    var xmlhttp=null;
    if (window.XMLHttpRequest)
    {
        xmlhttp=new XMLHttpRequest();
    }
    else if (window.ActiveXObject)
    {// code for IE5 and IE6
        try
        {
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (e)
        {
        }
    }
    return xmlhttp;
}



var xmlHttp = null;

function updateLatLong(type,id,lat,lon)
{
    xmlHttp = getXmlHttpRequest();
    if(!xmlHttp)
        return;
  //  var url = "http://localhost/devon/updatelatlong.php?type=" + type + "&id=" + id + "&lat=" + lat + "&lon=" + lon;
    var url = "http://www.campsite-review.co.uk/updatelatlong.php?type=" + type + "&id=" + id + "&lat=" + lat + "&lon=" + lon;
    xmlHttp.open("GET",url,true);
    xmlHttp.onreadystatechange = updateLatLongDone;
    xmlHttp.send(null);
}
function updateLatLongDone()
{
    //alert(xmlHttp.readyState + "," + xmlHttp.status);
   if(xmlHttp.readyState==4 && xmlHttp.status==200)
   {
   //    alert(xmlHttp.getAllResponseHeaders());
   //    alert(xmlHttp.responseText);
   }
}

var map = null;
var geocoder = null;
var shadow = null;
var clickIcon = null;
var clickMarker = null;
var markers = null;
var selected = null;
var infowindow = null;
var boundsOverlay = null;
var viewportOverlay = null;
var initialized = false;
var hashFragment = "";
var regPostcode = /^([a-zA-Z]){1}([0-9][0-9]|[0-9]|[a-zA-Z][0-9][a-zA-Z]|[a-zA-Z][0-9][0-9]|[a-zA-Z][0-9]){1}([ ])([0-9][a-zA-z][a-zA-z]){1}$/;

function showResults(results, status) {
 
    if (! results)
    {
        alert("Geocoder did not return a valid response");
    }
    else
    {
        if(status == google.maps.GeocoderStatus.OK)
        {
            if(results.length==1)
            {
                i=0;
                latlng=document.getElementById('latlng');
                county=document.getElementById('county');
                town=document.getElementById('town');
                latlng.value=results[i].geometry.location.lat()+','+results[i].geometry.location.lng();
                
                for (i=0;i<results[0].address_components.length;i++){
                    for (j=0;j<results[0].address_components[i].types.length;j++){
                        switch(results[0].address_components[i].types[j])
                        {
                        case "administrative_area_level_2":
                            county.value = results[0].address_components[i].long_name;
                            break;
                        case "postal_town":
                            town.value = results[0].address_components[i].long_name;
                            break;
                        }
                    }
                }
                showMap(results[0].geometry.location.lat(), results[0].geometry.location.lng());
            }
            else
                 alert("no results");
        }
        else
            alert(status);
    }
}
function showMap(lat,lng)
{
    var myLatlng = new google.maps.LatLng(lat, lng);
    var myOptions = {
      zoom: 12,
      center: myLatlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    if(map)
        map.setCenter(myLatlng);
    else
        map = new google.maps.Map(document.getElementById("map"), myOptions);
    var marker = new google.maps.Marker({
          position: myLatlng
      });

     // To add the marker to the map, call setMap();
     marker.setMap(map);
}

function geocode_v3(postcode)
{
    if(regPostcode.test(postcode) == false)
    {
        alert('Invalid Postcode');
        return;
    }
    geocoder = new google.maps.Geocoder();
    var request = { 'address': postcode };
    var hash = 'q=' + request.address;
 //   hash += '&country=' + country;
 //   request.country = country;
//hashFragment = '#' + escape(hash);
// window.location.hash = escape(hash);
    geocoder.geocode(request, showResults);
}

function usePointFromPostcode(postcode, callbackFunction)
{
	geocoder = new google.maps.Geocoder();
    var request = { 'address': postcode };
    var hash = 'q=' + request.address;
    geocoder.geocode(request, callbackFunction);
}

 //<![CDATA[ 
    var markerHtml;
    var map;
    var mapWidth = 330;
    var mapHeight = 400;
    var localSearch;
    var gSelectedResults = [];
    var gCurrentResults = [];
    
    // Create our "tiny" marker icon
    var gSmallIcon;
    
    var localSearch; // = new GlocalSearch();

    function mapOnLoad(width,height)
    {
        if(width)
        {
            mapWidth = width;
            mapHeight = height;
        }
        var latlng = new google.maps.LatLng(glat,glong);
        var opt =
        { 
        center:latlng,
        zoom:gzoom,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        disableAutoPan:false,
        navigationControl:true,
        navigationControlOptions: {style:google.maps.NavigationControlStyle.SMALL },
        mapTypeControl:true,
        mapTypeControlOptions: {style:google.maps.MapTypeControlStyle.DROPDOWN_MENU}
        };
        map = new google.maps.Map(document.getElementById("map"),opt);
        
        var marker = new google.maps.Marker({
          position: latlng, 
          map: map, 
          title:gmarker_title
        });   
     //   google.maps.event.trigger(map, 'resize');
        return; 
               
        if((glat==0)&&(gpostcode!=""))
            ;
        else
        {
        //    var center = new GLatLng(glat, glong);
            //map.setCenter(center, gzoom, glat?G_HYBRID_MAP:G_NORMAL_MAP);
       //     map.setCenter(center, gzoom, G_NORMAL_MAP);
            
            if(glat!=0)
            {
            //    var marker = new GMarker(center);
            //    map.addOverlay(marker);
                localSearch = new google.search.LocalSearch();
             //   localSearch = new GlocalSearch();
                localSearch.setCenterPoint(map);
                localSearch.setSearchCompleteCallback(null, OnLocalSearch);
                localSearch.setResultSetSize(GSearch.LARGE_RESULTSET);
            }
        }
    }
    function doSearch() {
      var cat = document.getElementById("catInput").value;
      localSearch.execute(cat);
    }
    
// Called when Local Search results are returned, we clear the old
    // results and load the new ones.
    function OnLocalSearch() {
      if (!localSearch.results) return;
      var searchWell = document.getElementById("searchwell");

      // Clear the map and the old search well
      searchWell.innerHTML = "";
      for (var i = 0; i < gCurrentResults.length; i++) {
        if (!gCurrentResults[i].selected()) {
          map.removeOverlay(gCurrentResults[i].marker());
        }
      }

      gCurrentResults = [];
      for (var i = 0; i < localSearch.results.length; i++) {
        gCurrentResults.push(new LocalResult(localSearch.results[i]));
      }

      var attribution = localSearch.getAttribution();
      if (attribution) {
        document.getElementById("searchwell").appendChild(attribution);
      }

      // move the map to the first result
      var first = localSearch.results[0];
    //  map.recenterOrPanToLatLng(new GPoint(parseFloat(first.lng), parseFloat(first.lat)));

    }

    // Cancel the form submission, executing an AJAX Search API search.
    function CaptureForm(searchForm) {
      localSearch.execute(searchForm.input.value);
      return false;
    }



    // A class representing a single Local Search result returned by the
    // Google AJAX Search API.
    function LocalResult(result) {
      this.result_ = result;
      this.resultNode_ = this.unselectedHtml();
  //    document.getElementById("searchwell").appendChild(this.resultNode_);
      map.addOverlay(this.marker(gSmallIcon));
    }
    
    
    // Returns the GMap marker for this result, creating it with the given
    // icon if it has not already been created.
    LocalResult.prototype.marker = function(opt_icon) {
      if (this.marker_) return this.marker_;
      var marker = new GMarker(new GLatLng(parseFloat(this.result_.lat),
                                         parseFloat(this.result_.lng)),
                               opt_icon);
      GEvent.bind(marker, "click", this, function() {
        marker.openInfoWindow(this.selected() ? this.selectedHtml() :
                                                this.unselectedHtml());
      });
      this.marker_ = marker;
      return marker;
    }

    // "Saves" this result if it has not already been saved
    LocalResult.prototype.select = function() {
      if (!this.selected()) {
        this.selected_ = true;

        // Remove the old marker and add the new marker
        gMap.removeOverlay(this.marker());
        this.marker_ = null;
        gMap.addOverlay(this.marker(G_DEFAULT_ICON));

        // Add our result to the saved set
        document.getElementById("selected").appendChild(this.selectedHtml());

        // Remove the old search result from the search well
        this.resultNode_.parentNode.removeChild(this.resultNode_);
      }
    }

    // Returns the HTML we display for a result before it has been "saved"
    LocalResult.prototype.unselectedHtml = function() {
      var container = document.createElement("div");
      container.className = "unselected";
      container.appendChild(this.result_.html.cloneNode(true));
   
      return container;
    }

    // Returns the HTML we display for a result after it has been "saved"
    LocalResult.prototype.selectedHtml = function() {
      return this.result_.html.cloneNode(true);
    }

    // Returns true if this result is currently "saved"
    LocalResult.prototype.selected = function() {
      return this.selected_;
    }

 //   GSearch.setOnLoadCallback(onLoad);
    
//]]>


