var map;
var minZoom = 7;

function init()
{
	// create the map
	map = new GMap2(document.getElementById("map_canvas"));
	map.setMapType(G_NORMAL_MAP);
	map.addControl( new GSmallZoomControl3D());
	G_NORMAL_MAP.getMinimumResolution = function() { return minZoom };
	map.setCenter(new GLatLng( 42.3581, -71.0647), 9);
	document.addressForm.address.focus()
}

function addressButtonClick()
{
	var address = document.addressForm.address.value;
	
	if ( address.length == 5 && !isNaN(address) )
	{
		//var geocoder = new GClientGeocoder();

		//geocoder.getLatLng( address, function(point)
		//{
		//	if (!point)
		//	{
		//		alert(address + " not found");
		//	}
		//	else
		//	{
				 


				/*processLocations( point );*/
				setLocation( address );//, point );
		//	}
		//});
	}
	else
	{
		alert("please enter a zip code");
	}
}

function setLocation ( zip )//, point )
{
	GDownloadUrl("locations.xml", function(doc)
	{
		var xmlDoc = GXml.parse(doc);
		var areas = xmlDoc.getElementsByTagName("serviceArea");
		var found = false;
		var zips;
		//var location = document.getElementById("location");

		for ( i=0; i<areas.length; i++ )
		{
			zips = areas[i].getElementsByTagName("zip");

			for ( j=0; j<zips.length; j++ )
			{
				if ( zips[j].getAttribute("code") == zip )
				{

				document.location = areas[i].getAttribute("url");
				found = true;
				}
				
			}
			
			
		}
		if ( !found )
		document.location = "no-service.html";
		
		var geocoder = new GClientGeocoder();

		geocoder.getLatLng( zip, function(point)
		{
			var blueIcon = new GIcon(G_DEFAULT_ICON);
			blueIcon.image = "pin.png";
			blueIcon.iconSize = new GSize(20, 29);
			var markerOptions = { icon:blueIcon }; 
			var marker = new GMarker( point, markerOptions );
			map.setCenter(point, 8);
		

		
		map.addOverlay(marker);
		marker.openInfoWindowHtml(

											  "    <div id=\"contact\" style=\"width:300px;font-weight:normal;color:#666;padding-right:0;\">\n" +
											  "       <h3 style=\"font-weight:normal;color:#666;line-height:20px;text-transform:none;\">Sorry. Unfortunately, we are unable to find the ZIP code you entered in our database of ZIP codes available through this site. Please visit <a href=\"http://assistinghands.com\" target=\"_blank\">www.assistinghands.com</a> for more information..</h3>\n" +
											  "    </div>");
		
		GEvent.addListener(marker, "click", function()
					{
							marker.openInfoWindowHtml(
			
											  "    <div id=\"contact\" style=\"width:300px;font-weight:normal;color:#666;padding-right:0;\">\n" +
											  "       <h3 style=\"font-weight:normal;color:#666;line-height:20px;text-transform:none;\">Sorry. Unfortunately, we are unable to find the ZIP code you entered in our database of ZIP codes available through this site. Please visit <a href=\"http://assistinghands.com\" target=\"_blank\">www.assistinghands.com</a> for more information..</h3>\n" +
											  "    </div>");
		
		});
	  });
	});
}
