﻿    // configure postcode web service URLS
    var WebServiceURL_JSON = "http://www.heritage.norfolk.gov.uk/NHE_PostcodeSearch/NorfolkPostcodes.asmx/GetSpatialFromPostcode_asJSON";
    var WebServiceURL_XML = "http://www.heritage.norfolk.gov.uk/NHE_PostcodeSearch/NorfolkPostcodes.asmx/GetSpatialFromPostcode_asXML";
  
  // Define global variables
    var Postcode_marker;
    var Postcode_bounds = new GLatLngBounds();
    
    // Load the data from XML Web Service and load it on the map. 

    function getSpatialFromPostcode_asJSON(sPostcode,iMaxRows,sIPAddress,sURL){
        setCursorHourglass();
        GDownloadUrl(WebServiceURL_JSON + "?sPostcode="+sPostcode+"&iMaxRows="+iMaxRows+"&sIPAddress="+sIPAddress+"&sURL="+sURL, function(data, responseCode) {
          // To ensure against HTTP errors that result in null or bad data,
          // always check status code is equal to 200 before processing the data
          if(responseCode == 200) {
            var xml = GXml.parse(data);
            var JSONobject = eval("(" + GXml.value(xml) + ")"); 
            for (var i = 0; i < JSONobject.items.length; i++) {
              var point = new GLatLng(JSONobject.items[i].Latitude,
                                      JSONobject.items[i].Longitude);
        //      var marker = new GMarker(point);
        //      addPopupContent(marker, "Postcode: " + JSONobject.items[i].Postcode)
              
      	        Postcode_marker = new PdMarker(point);
	            Postcode_marker.setTooltip(JSONobject.items[i].Postcode);
             
              map.addOverlay(Postcode_marker);
              Postcode_bounds.extend(Postcode_marker.getPoint());
            }
              map.setZoom(map.getBoundsZoomLevel(Postcode_bounds));
              map.setCenter(Postcode_bounds.getCenter());
          } else if(responseCode == -1) {
            alert("Data request timed out. Please try later.");
          } else { 
            alert("Request resulted in error. Check XML file is retrievable.");
          }
        });
        setCursorNormal();
    }

    // Load the data from JSON Web Service and load it on the map. 

    function getSpatialFromPostcode_asXML(sPostcode,iMaxRows,sIPAddress,sURL){
        setCursorHourglass();
        GDownloadUrl(WebServiceURL_XML + "?sPostcode="+sPostcode+"&iMaxRows="+iMaxRows+"&sIPAddress="+sIPAddress+"&sURL="+sURL, function(data, responseCode) {
          // To ensure against HTTP errors that result in null or bad data,
          // always check status code is equal to 200 before processing the data
          if(responseCode == 200) {
            var xml = GXml.parse(data);
            var locations = xml.documentElement.getElementsByTagName("Location");
            for (var i = 0; i < locations.length; i++) {
              var point = new GLatLng(GXml.value(locations[i].getElementsByTagName("Latitude").item(0)),
                                      GXml.value(locations[i].getElementsByTagName("Longitude").item(0)));
              
                Postcode_marker = new PdMarker(point);
	            Postcode_marker.setTooltip(GXml.value(locations[i].getElementsByTagName("Postcode").item(0)));

              map.addOverlay(Postcode_marker);
              Postcode_bounds.extend(Postcode_marker.getPoint());
            }
              map.setZoom(map.getBoundsZoomLevel(Postcode_bounds));
              map.setCenter(Postcode_bounds.getCenter());
          } else if(responseCode == -1) {
            alert("Data request timed out. Please try later.");
          } else { 
            alert("Request resulted in error. Check Web Service is online.");
          }
        });
        setCursorNormal();
    }
    
    // Remove postcode markers if required
    function removeMarkers(){
        var Postcode_marker = map.getFirstMarker();
        while (Postcode_marker != null)
            {
	            Postcode_marker.remove();	
	            Postcode_marker = map.getFirstMarker();
            }
        Postcode_bounds=null;
        Postcode_bounds=new GLatLngBounds();
    }
    
    function setCursorHourglass(){
        document.body.style.cursor = 'wait';
    }
    
    function setCursorNormal(){
        document.body.style.cursor = 'default';
    }