var map;

function window_load(e) {
// this function is fired when the page loaded
	var anchors = document.getElementsByTagName('a');
	var formContact = document.getElementById('formContact');
	
	if (formContact) {
		setEventHandler(formContact, 'submit', form_submit);
	}
	
	// loop through and find the map link anchors
	for (var i = 0; i < anchors.length; i++) {
		if (anchors[i].className.indexOf('mapLink') != -1) {
			setEventHandler(anchors[i], 'click', mapLink_click);
		}
	}
	
	// create the Google map
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById('map'));
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
	}
	
	// display the HQ map
	displayMapMarker('HQ', '2001 Broadway, Floor 2', '', 'Oakland', 'CA', '94612', 37.809457, -122.268075);
}

function window_unload(e) {
	GUnload()
}

function displayMapMarker(locationName, address, address2, city, state, zip, latitude, longitude) {
	// load the google map
	if (map) {
		// create icon
		var icon = new GIcon();
		icon.image = '/images/map_marker.gif';
		icon.shadow = 'http://www.google.com/mapfiles/shadow50.png';
		icon.iconSize = new GSize(20, 34);
		icon.shadowSize = new GSize(37, 34);
		icon.iconAnchor = new GPoint(9, 34);
		icon.infoWindowAnchor = new GPoint(9, 2);
		icon.infoShadowAnchor = new GPoint(18, 25);
		
		// get the lat/long of the location
		var latlng = new GLatLng(latitude, longitude);

		// set the map center
		map.setCenter(latlng, 15);
		
		// create and display the marker
		var marker = new GMarker(latlng, { icon:icon });
		GEvent.addListener(marker, 'click', function() {
			marker.openInfoWindowHtml('<p><strong>' + locationName + '</strong><br />' + address + (address2 ? '<br />' + address2 : '') + '<br />' + city + ', ' + state + ' ' + zip + '</p>');
		});
		map.clearOverlays
		map.addOverlay(marker, icon);		
	}
}

function mapLink_click(e) {
	var event = e ? e : window.event;
	var element = event.target ? event.target : event.srcElement;
	
	// set the map based on which link was clicked	
	if (element.innerHTML.indexOf('Reno') != -1) {
		displayMapMarker('Reno Office', '10425 Double R Blvd.', '', 'Reno', 'NV', '89521', 39.436316, -119.757369);		
	}
	else if (element.innerHTML.indexOf('Phoenix') != -1) {
		displayMapMarker('Phoenix Office', '2415 East Camelback Road, Suite 700', '', 'Phoenix', 'AZ', '85016', 33.508076, -112.028724);
	}
	else if (element.innerHTML.indexOf('Fresno') != -1) {
		displayMapMarker('Fresno Office', '5070 N. Sixth Street, Suite 154', '', 'Fresno', 'CA', '93710', 36.809751, -119.763165);
	}
	else if (element.innerHTML.indexOf('Pasadena') != -1) {
		displayMapMarker('Pasadena Office', '201 S. Lake Ave,  Suite 509', '', 'Pasadena ', 'CA', '91101', 34.142229, -118.132883);
	}
	else {
		displayMapMarker('HQ', '2001 Broadway, Floor 2', '', 'Oakland', 'CA', '94612', 37.809457, -122.268075);
	}
}

function form_submit(e) {
	var event = e ? e : window.event;
	var frm = event.target ? event.target : event.srcElement;
	var isValid = true;
	
	// validate the form fields
	if (frm.firstName.value == '') {
		alert('You must enter your first name.');
		frm.firstName.focus();
		isValid = false;
	}
	else if (frm.lastName.value == '') {
		alert('You must enter your last name.');
		frm.lastName.focus();
		isValid = false;
	}
	else if (frm.organization.value == '') {
		alert('You must enter your organization.');
		frm.organization.focus();
		isValid = false;
	}
	else if (frm.email.value == '' || !validEmail(frm.email.value)) {
		alert('You must enter a valid email address.');
		frm.email.focus();
		isValid = false;
	}	
	else if (frm.comments.value == '') {
		alert('You must enter your comments.');
		frm.comments.focus();
		isValid = false;
	}
	
	// stop the form submission if necessary
	if (!isValid) {
		if (event.preventDefault) {				
			event.preventDefault();			
		}
		
		return isValid;
	}
}

// set page events
setEventHandler(window, 'load', window_load);
setEventHandler(window, 'unload', window_unload);
