//*****************************************************************
//*****************************************************************
// COPYRIGHT 2009, Vertex Software
//*****************************************************************
//*****************************************************************


//**************************************************************
// ValidateAddress
//**************************************************************
function ValidateAddress() {
	try {
		var geocoder;
		var address = $("#Address").val();
		var address2 = $("#Address2").val();
		var city = $("#City").val();
		var state = $("#State").val();
		var zip = $("#Zip").val();
		if( address && city && state && zip ) {
			if( typeof GClientGeocoder == "function" ) {
				geocoder = new GClientGeocoder();
				geocoder.getLatLng( address + ", " + city + ", " + state + " " + zip, SetLatLngFromAddress );
				}
			else {
				alert( "You must wait for Google Maps to initialize" );
				}
			}
		else {
			alert("You must enter Address, City, State, and Zip to look up Longitude and Latitude" );
			}
		}
	catch (error) {
		alert( "ValidateAddress: " + error.description );
		}
	}


//--------------------------------------------------------------
// SetLatLngFromAddress
//--------------------------------------------------------------
function SetLatLngFromAddress( point ) {
	try {
		if( point ) {
			var marker = new GMarker( point );
			var latLng = String(marker.getLatLng()).split(",");
			var lat = parseFloat( latLng[0].replace("(","") );
			var lng = parseFloat( latLng[1] );
			$('#Latitude').val( lat );
			$('#Longitude').val( lng );
			}
		else {
			alert("Sorry, could not find the specified address. Use the google map below to find the location you are looking for. Click the map to capture the coordinates of that location.");
			}
		}
	catch (error) {
		alert( "SetLatLong: " + error.description );
		}
	}


//**************************************************************
// SetLatLngFromMap
//**************************************************************
function SetLatLngFromMap( point ) {
	try {
		if( String(point).match(/\,/) ) {
			point = String(point).split(",");
			$('#Latitude').val( parseFloat(point[0].replace("(","")) );
			$('#Longitude').val( parseFloat(point[1]) );
			}
		}
	catch (error) {
		alert( "SetLongitudeLatitudeFields: " + error.description );
		}
	}


//--------------------------------------------------------------
// AddEventListener
//--------------------------------------------------------------
function AddEventListener() {
	try {
		if( typeof GEvent == "object" ) {
			GEvent.addListener(gCurrentMap, 'click', new Function( "overlay", "point", "SetLatLngFromMap(point)" ) );
			}
		}
	catch (error) {
		alert( "AddEventListener: " + error.description );
		}
	}

window.setTimeout( AddEventListener, 2000 );
