southSoundLatitude = 47.10939;
southSoundLongitude = -122.39593;
southSoundZoom = 8;

var map; // GMap

var homeLatitude = southSoundLatitude;
var homeLongitude = southSoundLongitude;
var homeZoom = southSoundZoom;

var userMark; // GMarker
var arrowIcon = makeIcon("arrow"); // GIcon

/* Make an icon based on the folder the icon information is in. */
function makeIcon(color) {
	var xMarker = new GIcon();
	xMarker = new GIcon();
	xMarker.image = "markers/"+color+"/marker.png";
	xMarker.iconSize = new GSize(20, 34);
	xMarker.shadow = "markers/"+color+"/shadow.png";
	xMarker.printImage = "markers/"+color+"/markerie.gif";
	xMarker.printShadow = "markers/"+color+"/shadowie.gif";
	xMarker.mozPrintImage = "markers/"+color+"/markerff.gif";
	xMarker.mozPrintShadow = "markers/"+color+"/shadowff.gif";
	xMarker.shadowSize = new GSize(37, 34);
	xMarker.iconAnchor = new GPoint(9, 34);
	return xMarker;
}

function load() {
	
	var form = document.habitat_form;

	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		map.addControl(new GSmallMapControl());
		map.setCenter(new GLatLng(homeLatitude, homeLongitude), homeZoom);
		
		GEvent.addListener(map, "click", function(marker, point) {
			  
			// If the arrow is not on the map yet...
			if (!userMark) {
				
				userMark = new GMarker(point, {icon: arrowIcon, draggable: true});
				map.addOverlay(userMark);
				form.latitude.value = point.y;
				form.longitude.value = point.x;
				
				
				// When the user is finished dragging the marker, update the coordinates
				GEvent.addListener(userMark, "dragend", function() {
					
					if (userMark.getPoint().y > 85) {
						var p = userMark.getPoint();
						p.y = 85;
						userMark.setPoint(p);
					}
					if (userMark.getPoint().y < -85) {
						var p = userMark.getPoint();
						p.y = -85;
						userMark.setPoint(p);
					}
			
					form.latitude.value = userMark.getPoint().y;
					form.longitude.value = userMark.getPoint().x;
				});
			}
		});
		
		if (form.latitude.value != 0 && form.longitude.value != 0) {
			GEvent.trigger(map, "click", null, new GLatLng(form.latitude.value, form.longitude.value));
		}
		
	}
}





function validate(form) {
	
	if (trim(form.group_name.value) == "") {
		alert("Please tell us the name of the group.");
		return false;
	}
	
	if (trim(form.location.value) == "") {
		alert("Please tell us where the event will take place.");
		return false;
	}
	
	if (form.latitude.value == "0" || form.longitude.value == "0") {
		alert ("Please use the map to mark on the map where the event will be held.")
		return false;
	}
	
	if (trim(form.project.value) == "") {
		alert("Please tell us what you will be doing.");
		return false;
	}
	
	if (trim(form.date_and_time.value) == "") {
		alert("Please tell us what time and days the event will take place.");
		return false;
	}
	
	if (form.expiration_date.value == "" ||
		form.expiration_date.value.match(/^\d{1,2}[\/-]\d{1,2}[\/-]\d{4}$/)) {
			// It's okay
	} else {
		alert("Please make sure the expiration date fits the pattern, or is left blank. Invalid dates that fit the pattern, such as 12/34/5678, will be ignored.");
		return false;
	}
	
	if (trim(form.phone.value) == "" && trim(form.email.value) == "") {
		alert("Please offer either a phone or an e-mail address for contact information.");
		return false;
	}
	
	form.submit.disabled = true;
	return true;
}




/* Trim whitespace before and after text. */
/*        Version December 3, 2006        */
function trim(sString) {
   
   if (sString == null)
      return "";
   
   return sString.replace(/^\s*|\s*$/g,"");
   
}