	var map;
  var gdir;
  var geocoder = null;
   var addressMarker;
  google.load("maps", "2.93");
	  function initialize() {
	       // Initialize default values
				
        map = new GMap2(document.getElementById("map_canvas"));
        gdir = new GDirections(map, document.getElementById("directions"));
        GEvent.addListener(gdir, "load", onGDirectionsLoad);
        GEvent.addListener(gdir, "error", handleErrors);

				var mapControl = new GMapTypeControl();
				map.addControl(mapControl);
				map.addControl(new GLargeMapControl());



	       // If ClientLocation was filled in by the loader, use that info instead
	       if (google.loader.ClientLocation) {
						latlng = new google.maps.LatLng(google.loader.ClientLocation.latitude, google.loader.ClientLocation.longitude);
	        	location = "<div class='ip_location'><span class='ip_location_label'>Showing your location based on IP: </span><span class='ip_location_display'>" + getFormattedLocation() + "</span></div><span class='location_note'><b>Please note:</b>  Locating by IP address is not 100% accurate.<br />If your current starting location is incorrect, please enter it below.</span>";
	       		setDirections(getFormattedLocation(), "35 Broad St Red Bank, NJ");
						setFromForm();
	
	
	          
					}
					else {
						var latlng = new google.maps.LatLng(40.493076,-74.451814);
			      var location = "Showing default location for map: New York, NY";
						setDirections("New York, NY", "35 Broad St Red Bank, NJ");
						setFromForm();
											  
					}

					document.getElementById("location").innerHTML = location;
    }
    
		function setDirections(fromAddress, toAddress) {
      gdir.load("from: " + fromAddress + " to: " + toAddress);
			
		}

    function handleErrors(){
	   if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
	     alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code);
	   else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
	     alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);
	   
	   else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
	     alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);

	//   else if (gdir.getStatus().code == G_UNAVAILABLE_ADDRESS)  <--- Doc bug... this is either not defined, or Doc is wrong
	//     alert("The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.\n Error code: " + gdir.getStatus().code);
	     
	   else if (gdir.getStatus().code == G_GEO_BAD_KEY)
	     alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);

	   else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
	     alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
	    
	   else alert("An unknown error occurred.");
	   
	}

	function onGDirectionsLoad(){ 
      // Use this function to access information about the latest load()
      // results.

      // e.g.
      // document.getElementById("getStatus").innerHTML = gdir.getStatus().code;
	  // and yada yada yada...
	}
 function getFormattedLocation() {
    if (google.loader.ClientLocation.address.country_code == "US" &&
      google.loader.ClientLocation.address.region) {
      return google.loader.ClientLocation.address.city + ", " 
          + google.loader.ClientLocation.address.region.toUpperCase();
    } else {
      return  google.loader.ClientLocation.address.city + ", "
          + google.loader.ClientLocation.address.country_code;
    }
  }

  function setFromForm(){
		document.getElementById("fromAddress").value=getFormattedLocation();
	}


// Removes Value of input box onFocus
	function tbTest_focus(e,o){
	if(o.firstTime){return}
	o.firstTime=true;
	o.value="";
}


// Switches the values of toAddress and fromAddress
function switchDirections() {
		a=document.getElementById("toAddress").value;
		b=document.getElementById("fromAddress").value;
		setDirections(a,b);
		writeTo(a,b);
		
		}

// Returns toAddress and fromAddress to their original values
function writeTo(fromvar,tovar) {
// flip the directions
		firstLoc=fromvar;
		nextLoc=tovar;
		document.getElementById("fromAddress").value=(firstLoc);
		document.getElementById("toAddress").value=("35 Broad St Red Bank, NJ");
		//	alert(theVar);
			}
			

			
		

	
