// JScript File

// Change this variable to customize the ending address
var endAddress="1486 Pottstown Pike, Pottstown, Pa 19465";
 
var map=null;
var geocoder;
var directions;
var directionsPanel;
var headerPanel;
var mapPanel;
var reasons=[];
 
// Array for decoding the failure codes
reasons[G_GEO_SUCCESS]            = "Success";
reasons[G_GEO_MISSING_ADDRESS]    = "Missing Address: The address was either missing or had no value.";
reasons[G_GEO_UNKNOWN_ADDRESS]    = "Unknown Address:  No corresponding geographic location could be found for the specified address.";
reasons[G_GEO_UNAVAILABLE_ADDRESS]= "Unavailable Address:  The geocode for the given address cannot be returned due to legal or contractual reasons.";
reasons[G_GEO_BAD_KEY]            = "Bad Key: The API key is either invalid or does not match the domain for which it was given";
reasons[G_GEO_TOO_MANY_QUERIES]   = "Too Many Queries: The daily geocoding quota for this site has been exceeded.";
reasons[G_GEO_SERVER_ERROR]       = "Server error: The geocoding request could not be successfully processed.";

// List of words to be standardized
var standards = [   ["road","rd"],   
                    ["street","st"], 
                    ["avenue","ave"], 
                    ["av","ave"], 
                    ["drive","dr"],
                    ["saint","st"], 
                    ["north","n"],   
                    ["south","s"],    
                    ["east","e"], 
                    ["west","w"],
                    ["expressway","expy"],
                    ["parkway","pkwy"],
                    ["terrace","ter"],
                    ["turnpike","tpke"],
                    ["highway","hwy"],
                    ["lane","ln"]
                 ];

// Convert words to standard versions 
function standardize(a) {
  for (var i=0; i<standards.length; i++) {
    if (a == standards[i][0])  {a = standards[i][1];}
    }
      return a;
}

// Check if two addresses are sufficiently different
function different(a,b) {

  // only interested in the bit before the first comma in the reply
  var c = b.split(",");
  b = c[0];

  // convert to lower case
  a = a.toLowerCase();
  b = b.toLowerCase();

  // remove apostrophies
  a = a.replace(/'/g ,"");
  b = b.replace(/'/g ,"");

  // replace all other punctuation with spaces
  a = a.replace(/\W/g," ");
  b = b.replace(/\W/g," ");

  // replace all multiple spaces with a single space
  a = a.replace(/\s+/g," ");
  b = b.replace(/\s+/g," ");

  // split into words
  awords = a.split(" ");
  bwords = b.split(" ");

  // perform the comparison
  var reply = false;
  for (var i=0; i<bwords.length; i++) 
  {
    if (standardize(awords[i]) != standardize(bwords[i])) {reply = true;}
  }
  return reply;
}

function SetCenter(address) {
    
    geocoder.getLatLng(address,
        function(point) {
            if (!point) {
                alert(address + " not found.");
            }
            else {
                map.setCenter(point,13);
                var marker=new GMarker(point);
                map.addOverlay(marker);
            }
        });
}
function initialize() {

     if (GBrowserIsCompatible()) {
        directionsPanel=document.getElementById("pnlDirections");
        headerPanel=document.getElementById("Header");
        headerPanel.innerHTML="<b>Directions to " + endAddress + ":</b>";
        mapPanel=document.getElementById("JSMap");
        
        // Create a geocoder to process addresses
        geocoder=new GClientGeocoder();

        // Generate the map and center it at the end address
        map = new GMap2(mapPanel);
        SetCenter(endAddress);
        map.setUIToDefault();
     }
}

function GetMapAndDirections(address) 
{   
     // Clear the overlays on the map
     map.clearOverlays();
      
     // Get the directions in a GDirections object; 
     // by default, this also includes the directions, 
     // which will be placed in another panel
     directionsPanel.innerHTML="";
     directions=new GDirections(map,directionsPanel);
     var addresses="from: " + address + " to: " + endAddress;
     directions.load(addresses);
         
     // Call the server-side method to save the address to the 
     // local XML file (requires AJAX)
     PageMethods.SaveAddressInformation(address,onSuccess,onFailure);
 }

function onSuccess(result,userContext,methodName) {

	// Uncomment the following line to debug the data storage method.
	// Also, you may need to add enable custom error pages in web.config.
	// alert("Method call a success: " + methodName);
}

function onFailure(error,userContext,methodName) {

	// Uncomment the following line to debug the data storage method.
	// Also, you may need to add enable custom error pages in web.config.
	// alert("Error calling method \"" + methodName + "\": " + error.get_message());
}
