var map;
var gdir;

function initialize() {
  if (GBrowserIsCompatible()) {      

    map = new GMap2(document.getElementById("map_canvas"));
	map.setCenter(new GLatLng(40.104514, -74.788356), 15);
	
    var marker = new GMarker(new GLatLng(40.104514, -74.788356));

	map.openInfoWindowHtml(map.getCenter(), 
		"Nothing But The Word Deliverance Church, Inc.<br/>2071 Route 130 South<br/>Florence, NJ 08518<br/>");
	
	map.addOverlay(marker);
	
    gdir = new GDirections(map, document.getElementById("directions"));
    GEvent.addListener(gdir, "load", onGDirectionsLoad);
    GEvent.addListener(gdir, "error", handleErrors);

  }
}

function check_directions() {
	var smsg = '';

	if (document.getElementById('ctl00_cphContent_fromaddress').value == '') { smsg += '\nPlease enter your address.'; }
	if (document.getElementById('ctl00_cphContent_fromcity').value == '') { smsg += '\nPlease enter your city.'; }
	if (document.getElementById('ctl00_cphContent_fromstate').value == '') { smsg += '\nPlease enter your state.'; }
	if (document.getElementById('ctl00_cphContent_fromzip').value == '') { smsg += '\nPlease enter your zipcode.'; }			
	
	if (smsg != '') {
		smsg = 'Please review the following errors and correct:\n' + smsg
	}
	
	return smsg;
}

function setDirections() {
	var smsg = check_directions()
	
	if (smsg != '') {
		alert(smsg);
	}
	else {	
		map.clearOverlays();	
					
        var Address = document.getElementById("ctl00_cphContent_fromaddress").value;
        var City = document.getElementById("ctl00_cphContent_fromcity").value;
        var State = document.getElementById("ctl00_cphContent_fromstate").value;
        var Zip = document.getElementById("ctl00_cphContent_fromzip").value;  
        					
		var toAddress = '2071 Route 130 South Florence, NJ 08518'
		var fromAddress = Address + ' ' + City + ', ' + State + ' ' + Zip;

		gdir.load("from: " + fromAddress + " to: " + toAddress, { "locale": 'en' });      

		var request = PageMethods.SaveDirections(Address, City, State, Zip);
	}
}

function handleErrors(){
   if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
     alert("The address entered could not be found, please check the address and try again.");
	     
   else alert("An unknown error occurred.");
   
}

function onGDirectionsLoad() {
	
}

window.onunload = function() {
	GUnload();
}

window.onload = function() {
	initialize();
}
