var markers = [];

function debug() {
	for (i = 0; i < markers.length; i++) {
		alert(markers[i]);
	}
}
function checkMarkers(id, chk) {
	if (markers[id] != 0) {
		if (chk) {
			markers[id].show();
		} else {
			markers[id].hide();
		}
	} else {
		alert('Address not mapped since it could not be found.');
	}
}

function buildMaps(lat, lon, arr) {
	if (GBrowserIsCompatible()) {

		var map = new GMap2(document.getElementById("map_canvas"));
		map.setCenter(new GLatLng(lat, lon), 9);
		map.addControl(new GSmallMapControl());
		map.addControl(new GOverviewMapControl());
		map.addControl(new GMapTypeControl());

		//var mm = new GMarkerManager(map, {borderPadding:1});
		var c_address = '';
		var display = '';

		// Create a base icon for all of our markers that specifies the
		// shadow, icon dimensions, etc.
		var baseIcon = new GIcon(G_DEFAULT_ICON);
		baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
		baseIcon.iconSize = new GSize(20, 34);
		baseIcon.shadowSize = new GSize(37, 34);
		baseIcon.iconAnchor = new GPoint(9, 34);
		baseIcon.infoWindowAnchor = new GPoint(9, 2);

		// Creates a marker whose info window displays the letter corresponding
		// to the given index.
		function createMarker(point, index, display) {
			// Create a lettered icon for this point using our icon class
			var letter = String.fromCharCode("A".charCodeAt(0) + index);
			var letteredIcon = new GIcon(baseIcon);

			letteredIcon.image = "http://www.google.com/mapfiles/marker" + letter + ".png";

			// Set up our GMarkerOptions object
			markerOptions = { icon:letteredIcon };
			var marker = new GMarker(point, markerOptions);

			GEvent.addListener(marker, "click", function() {
				marker.openInfoWindowHtml(display);
			});

			return marker;
		}

		markers = new Array();

		var geocoder = new GClientGeocoder();
		for (var i = 0; i < arr.length; i++) {
			//c_address = '';
			display = '<div style="color:#000000; text-align:left">';

			if (arr[i][0].length > 0) {
				display += '<b>' + arr[i][0] + '</b><br />';
			}
			if (arr[i][1].length > 0) {
			//	c_address += arr[i][1];
				display += arr[i][1] + '<br />';
			}
			if (arr[i][2].length > 0) {
			//	c_address += ', ' + arr[i][2];
				display += arr[i][2];
			}
			if (arr[i][3].length > 0) {
			//	c_address += ', ' + arr[i][3];
				display += ', ' + arr[i][3];
			}
			if (arr[i][4].length > 0) {
			//	c_address += ' ' + arr[i][4];
				display += ' ' + arr[i][4];
			}
			display += '</div>';

			if (arr[i][5] != null && arr[i][6] != null) {
				var latlng = new GLatLng(arr[i][5], arr[i][6]);
				marker = createMarker(latlng, i, display);
				map.addOverlay(marker);
			} else {
				marker = 0;
			}
			markers.push(marker);
		}

	}
}

function buildMap(lat, lon, name, address, city, state, zip) {
	if (GBrowserIsCompatible()) {
		var map = new GMap2(document.getElementById("map_canvas"));
		map.setCenter(new GLatLng(lat, lon), 11);
		map.addControl(new GSmallMapControl());
		map.addControl(new GOverviewMapControl());

		var comp_address = '';
		var display = '<div style="color:#000000; text-align:left">';
		if (name.length > 0) {
			display += '<b>' + name + '</b><br />';
		}
		if (address.length > 0) {
			comp_address += address;
			display += address + '<br />';
		}
		if (city.length > 0) {
			comp_address += ', ' + city;
			display += city;
		}
		if (state.length > 0) {
			comp_address += ', ' + state;
			display += ', ' + state;
		}
		if (zip.length > 0) {
			comp_address += ' ' + zip;
			display += ' ' + zip;
		}
		display += '</div>';

		var geocoder = new GClientGeocoder();
		try {
			geocoder.getLatLng(comp_address, function(glatlng) {
				if (glatlng != null) {
					//alert ("recieved lat/lng for address: " + glatlng.lat() + " " + glatlng.lng());
					var lat = glatlng.lat();
					var lng = glatlng.lng();
					//updateLatLng(27105, lat, lng);

					var marker2 = new GMarker(glatlng);
					map.addOverlay(marker2);



					GEvent.addListener(marker2, "click", function() {
						marker.openInfoWindowHtml(display);
					});

					/*map.openInfoWindow(marker0.getCenter(),
						document.createTextNode("Hello, world"));*/
					/*showMarkerFlag = true;
					GEvent.addListener(marker0, "click", function() {
						showMarkerFlag = false;
						showMarker(0);
					});
					GEvent.addListener(marker0, "mouseover", function() {
						if (showMarkerFlag) {
							showMarkerFlag = false;
							showMarker(0);
						}
					});
					GEvent.addListener(marker0, "infowindowclose", function() {
						showMarkerFlag = true;
					});	*/
				}
			});
		} catch (e) {
			alert("error:"+e);
		}
	}
}

function validateZip(f) {
	if (f.postal.value == '') {
		alert('Please enter a zip code.');
		f.postal.focus();
	} else if (f.postal.value.length < 5) {
		alert('Please enter a valid 5 digit zip code.');
		f.postal.focus();
	} else {
		var s = f.elements["type[]"];
		var list = new Array();
		var check = false;

		if (s != undefined) {
			// Check if the items are selected.
			if (s.length > 0) {
				for (var i = 0; i < s.length; i++) {
					if (s[i].checked == true) {
						list.push(s[i].value);
						check = true;
					}
				}
			} else if (s.checked) {
				check = true;
				list.push(s.value);
			}
		}
		if (!check) {
			alert('Please check off at least one dealer type.');
		} else {
			cursor_wait();
			document.getElementById('action').value = 'Searching...';
			var url = 'functions/load_dealers.php';
			var params = 'postal=' + f.postal.value + '&type=' + list.join(',') + '&showMap=' + f.showMap.checked;
			var ajax = new Ajax.Updater({success: 'resultsDiv'}, url, {method: 'get', evalScripts: true, parameters: params, onFailure: reportError});
		}
	}
	return(false);
}
