defaultlocation = new GLatLng (-28.3, 134);
point = Array ();
point['a'] = Array ();
point['b'] = Array ();
point['c'] = Array ();
point['d'] = Array ();
line = Array ();


function submittrip () {
	geocoder.getLocations (
		document.getElementById ('idfrom').value + ' Australia', 
		function (resulta) {
			geocoder.getLocations (
				document.getElementById ('idto').value + ' Australia', 
				function (resultb) {
					if (resulta.Status.code == 200 && resulta.Placemark[0].AddressDetails.Accuracy == 4) {
						document.getElementById ('idfrom').value = resulta.Placemark[0].AddressDetails.Country.AdministrativeArea.Locality.LocalityName + ' ' + resulta.Placemark[0].AddressDetails.Country.AdministrativeArea.AdministrativeAreaName;
						document.getElementById ('idfromlat').value = resulta.Placemark[0].Point.coordinates[1];
						document.getElementById ('idfromlng').value = resulta.Placemark[0].Point.coordinates[0];
					} else {
						document.getElementById ('idfromlat').value = '';
						document.getElementById ('idfromlng').value = '';
					}
					if (resultb.Status.code == 200 && resultb.Placemark[0].AddressDetails.Accuracy == 4) {
						document.getElementById ('idto').value = resultb.Placemark[0].AddressDetails.Country.AdministrativeArea.Locality.LocalityName + ' ' + resultb.Placemark[0].AddressDetails.Country.AdministrativeArea.AdministrativeAreaName;
						document.getElementById ('idtolat').value = resultb.Placemark[0].Point.coordinates[1];
						document.getElementById ('idtolng').value = resultb.Placemark[0].Point.coordinates[0];
					} else {
						document.getElementById ('idtolat').value = '';
						document.getElementById ('idtolng').value = '';
					}
					document.getElementById ('idtripform').submit ();
				}
			);
		}
	);
}

function showaddress (address, letter) {
	geocoder.getLocations (
		address + ' Australia', 
		function (result) {
			if (result.Status.code == 200 && result.Placemark[0].AddressDetails.Accuracy == 4) {
				if (point[letter]['marker'])
					map.removeOverlay (point[letter]['marker']);
				latlng = new GLatLng (result.Placemark[0].Point.coordinates[1], result.Placemark[0].Point.coordinates[0]);
				point[letter]['latlng'] = latlng;
				point[letter]['marker'] = new GMarker (latlng, icon[letter]);
				map.addOverlay (point[letter]['marker']);
				joinpoints ();
				showallpoints ();
			} else {
				if (point[letter]) {
					map.removeOverlay (point[letter]['marker']);
					point[letter] = Array ();
				}
				if (line['x'])
					map.removeOverlay (line['x']);
			}
		}
	);
}

function showpoint (lat, lng, letter) {
	if (point[letter]['marker'])
		map.removeOverlay (point[letter]['marker']);
	latlng = new GLatLng (lat, lng);
	point[letter]['latlng'] = latlng;
	point[letter]['marker'] = new GMarker (latlng, icon[letter]);
	map.addOverlay (point[letter]['marker']);
	joinpoints ();
	showallpoints ();
}

function hidepoint (letter) {
	if (point[letter]) {
		map.removeOverlay (point[letter]['marker']);
		point[letter] = Array ();
	}
	if ((letter == 'a' || letter == 'b') && line['x'])
		map.removeOverlay (line['x']);
	if ((letter == 'c' || letter == 'd') && line['y'])
		map.removeOverlay (line['y']);
	//showallpoints ();
}

function joinpoints () {
	if (point['a']['latlng'] && point['b']['latlng']) {
		if (line['x'])
			map.removeOverlay (line['x']);
		line['x'] = new GPolyline ([point['a']['latlng'], point['b']['latlng']], "#00a4e9", 5, 0.75);
		map.addOverlay (line['x']);
	}
	if (point['c']['latlng'] && point['d']['latlng']) {
		if (line['y'])
			map.removeOverlay (line['y']);
		line['y'] = new GPolyline ([point['c']['latlng'], point['d']['latlng']], "#83b400", 5, 0.75);
		map.addOverlay (line['y']);
	}
}

function showallpoints () {
	var swLat, swLng, neLat, neLng;
	if (point['a']['latlng']) swLat = point['a']['latlng'].lat ();
	if (point['b']['latlng'] && (! swLat || point['b']['latlng'].lat () > swLat)) swLat = point['b']['latlng'].lat ();
	if (point['c']['latlng'] && (! swLat || point['c']['latlng'].lat () > swLat)) swLat = point['c']['latlng'].lat ();
	if (point['d']['latlng'] && (! swLat || point['d']['latlng'].lat () > swLat)) swLat = point['d']['latlng'].lat ();
	if (point['a']['latlng']) swLng = point['a']['latlng'].lng ();
	if (point['b']['latlng'] && (! swLng || point['b']['latlng'].lng () < swLng)) swLng = point['b']['latlng'].lng ();
	if (point['c']['latlng'] && (! swLng || point['c']['latlng'].lng () < swLng)) swLng = point['c']['latlng'].lng ();
	if (point['d']['latlng'] && (! swLng || point['d']['latlng'].lng () < swLng)) swLng = point['d']['latlng'].lng ();
	sw = new GLatLng (swLat, swLng);
	if (point['a']['latlng']) neLat = point['a']['latlng'].lat ();
	if (point['b']['latlng'] && (! neLat || point['b']['latlng'].lat () < neLat)) neLat = point['b']['latlng'].lat ();
	if (point['c']['latlng'] && (! neLat || point['c']['latlng'].lat () < neLat)) neLat = point['c']['latlng'].lat ();
	if (point['d']['latlng'] && (! neLat || point['d']['latlng'].lat () < neLat)) neLat = point['d']['latlng'].lat ();
	if (point['a']['latlng']) neLng = point['a']['latlng'].lng ();
	if (point['b']['latlng'] && (! neLng || point['b']['latlng'].lng () > neLng)) neLng = point['b']['latlng'].lng ();
	if (point['c']['latlng'] && (! neLng || point['c']['latlng'].lng () > neLng)) neLng = point['c']['latlng'].lng ();
	if (point['d']['latlng'] && (! neLng || point['d']['latlng'].lng () > neLng)) neLng = point['d']['latlng'].lng ();
	ne = new GLatLng (neLat, neLng);
	if (swLat) {
		bounds = new GLatLngBounds(sw, ne);
		zoom = map.getBoundsZoomLevel (bounds);
		if (zoom  > 12) zoom = 12;
		map.setCenter (bounds.getCenter (), zoom);	
	} else
		map.setCenter (defaultlocation, 4);
};

function initialize () {
	if (GBrowserIsCompatible ()) {
		map = new GMap2 (document.getElementById ("gmap"));
		geocoder = new GClientGeocoder ();
		geocoder.setBaseCountryCode ('AU')
		map.addControl (new GSmallMapControl ());
		map.addControl(new GMapTypeControl());
		map.setCenter (defaultlocation, 4);
		if (document.getElementById ('idfromlat') && document.getElementById ('idfromlat').value)
			showpoint (document.getElementById ('idfromlat').value, document.getElementById ('idfromlng').value, 'a');
		if (document.getElementById ('idtolat') && document.getElementById ('idtolat').value)
			showpoint (document.getElementById ('idtolat').value, document.getElementById ('idtolng').value, 'b');
		if (document.getElementById ('idrfromlat') && document.getElementById ('idrfromlat').value)
			showpoint (document.getElementById ('idrfromlat').value, document.getElementById ('idrfromlng').value, 'c');
		if (document.getElementById ('idrtolat') && document.getElementById ('idrtolat').value)
			showpoint (document.getElementById ('idrtolat').value, document.getElementById ('idrtolng').value, 'd');
	}
}

formsent = 0;