function set_area(form,areas){
	state = form.state[form.state.selectedIndex].value;
	selected_val = form.area[form.area.selectedIndex].value;
	form.area.length = 0;
	if( form.area.length == 0 ){
		try{
			i = areas['state-'+state].length;
			j = 0;
			for(i=0;i<areas['state-'+state].length;i++){
				l = areas['state-'+state][i];
				form.area.options[j] = new Option(l['text'],l['value']);
				if( selected_val == l['value'] )
					form.area.selectedIndex = j;
				j++;
			}
			form.area.disabled = 0;
		} catch(e){
			form.area.options[0] = new Option('No areas in this state','invalid');
			form.area.disabled = 1;
		}
	} else {
		sel = 0;
		for(i=0;i<form.area.options.length;i++){
			if( form.area.options[i].id == state ){
				if(!sel){ sel = 1; form.area.selectedIndex = i; }
				form.area.options[i].style.display = 'block';
			} else {
				form.area.options[i].style.display = 'none';
			}
		}
	}
}

function get_areas(form){
	j = 0;
	areas = new Object();
	for(i=0;i<form.area.options.length;i++){
		o = form.area.options[i];
		s = 'state-'+o.id;
		l = Array();
		l['text'] = o.text; l['value'] = o.value;
		try{
			areas[s].push(l);
		} catch(e){
			areas[s] = Array(l);
		}
	}
	return areas;
}

function set_location(form,locations){
	state = form.state[form.state.selectedIndex].value;
	selected_val = form.location[form.location.selectedIndex].value;
	form.location.length = 0;
	if( form.location.length == 0 ){
		try{
			i = locations['state-'+state].length;
			j = 0;
			for(i=0;i<locations['state-'+state].length;i++){
				l = locations['state-'+state][i];
				form.location.options[j] = new Option(l['text'],l['value']);
				if( selected_val == l['value'] )
					form.location.selectedIndex = j;
				j++;
			}
			form.location.disabled = 0;
		} catch(e){
			form.location.options[0] = new Option('No locations in this state','invalid');
			form.location.disabled = 1;
		}
	} else {
		sel = 0;
		for(i=0;i<form.location.options.length;i++){
			if( form.location.options[i].id == state ){
				if(!sel){ sel = 1; form.location.selectedIndex = i; }
				form.location.options[i].style.display = 'block';
			} else {
				form.location.options[i].style.display = 'none';
			}
		}
	}
}

function get_locations(form){
	j = 0;
	locations = new Object();
	for(i=0;i<form.location.options.length;i++){
		o = form.location.options[i];
		s = 'state-'+o.id;
		l = Array();
		l['text'] = o.text; l['value'] = o.value;
		try{
			locations[s].push(l);
		} catch(e){
			locations[s] = Array(l);
		}
	}
	return locations;
}

var request;

function get_locations_ajax(form){
	if( typeof XMLHttpRequest == "undefined" )
		XMLHttpRequest = function() {
			try { return new ActiveXObject("Msxml2.XMLHTTP.6.0") } catch(e) {}
			try { return new ActiveXObject("Msxml2.XMLHTTP.3.0") } catch(e) {}
			try { return new ActiveXObject("Msxml2.XMLHTTP") }     catch(e) {}
			try { return new ActiveXObject("Microsoft.XMLHTTP") }  catch(e) {}
			throw new Error( "This browser does not support XMLHttpRequest or XMLHTTP." )
		};
	request = new XMLHttpRequest();
	request.open('POST', document.location.href);
	request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	request.onreadystatechange=replace_locations;
	request.send('get_locations=1&state='+form.state[form.state.selectedIndex].value);
}

function replace_locations(){
	if (request.readyState==4 || request.readyState=="complete")
	{
		// Here we work around an Internet Explorer bug:
		// http://support.microsoft.com/kb/276228
		var location_select = document.getElementById("location");
		if( location_select.outerHTML != undefined )
			location_select.outerHTML = '<select name="location" id="location">'+ request.responseText + '</select>';
		else
			location_select.innerHTML = request.responseText;
	}
}

function set_region(form,regions){
	state = form.state[form.state.selectedIndex].value;
	selected_val = form.region[form.region.selectedIndex].value
	form.region.length = 0;
	if( form.region.length == 0 ){
		try{
			i = regions['state-'+state].length;
			j = 0;
			for(i=0;i<regions['state-'+state].length;i++){
				l = regions['state-'+state][i];
				form.region.options[j] = new Option(l['text'],l['value']);
				if( selected_val == l['value'] )
					form.region.selectedIndex = j;
				j++;
			}
			form.region.disabled = 0;
		} catch(e){
			form.region.options[0] = new Option('No regions in this state','invalid');
			form.region.disabled = 1;
		}
	} else {
		sel = 0;
		for(i=0;i<form.region.options.length;i++){
			if( form.region.options[i].id == state ){
				if(!sel){ sel = 1; form.region.selectedIndex = i; }
				form.region.options[i].style.display = 'block';
			} else {
				form.region.options[i].style.display = 'none';
			}
		}
	}
}

function get_regions(form){
	j = 0;
	regions = new Object();
	for(i=0;i<form.region.options.length;i++){
		o = form.region.options[i];
		s = 'state-'+o.id;
		l = Array();
		l['text'] = o.text; l['value'] = o.value;
		try{
			regions[s].push(l);
		} catch(e){
			regions[s] = Array(l);
		}
	}
	return regions;
}


var map = null;
var marker = null;
var geocoder = null;
var name = null;
var blocation = null;
var bstate = null;
var mapedit = 0;
var au = 'Australia';
var state_au = null;
var loc_state_au = null;
var street_loc = null;
var street_loc_state = null;
var street_loc_state_au = null;
var large = 1;
var hidee = 0;

// Hide the google copyright and tos because it is too long and leaks out into the page with the small map
function hide_google_copyright_tos(){
	$('a.gmnoprint').prev('span').hide();
	$("a:contains('Terms of Use')").prev('span').hide();
	$('a.gmnoprint').hide();
	$("a:contains('Terms of Use')").hide();
}

function map_setup(){
	if( map == null ){
		map = new GMap2(document.getElementById("map"),{draggableCursor: 'default'});
		// Set the default map center to Australia
		map.setCenter(new GLatLng(-27.839076,134.296875), large?4:2);
		if( large ){
			map.enableScrollWheelZoom();
			map.enableContinuousZoom();
			map.addControl(new GLargeMapControl());
			map.addControl(new GMapTypeControl());
			map.addControl(new GOverviewMapControl(new GSize(100,100)));
		} else {
			map.disableDragging();
			GEvent.addListener(map, "click", function() { document.location.href += "map/"; });

			// We hide lots because once doesn't work when loading the same page for the second time
			$(document).ready(function(){ hide_google_copyright_tos(); setTimeout(hide_google_copyright_tos, 100); });
			GEvent.addListener(map, 'load', function(){ hide_google_copyright_tos(); setTimeout(hide_google_copyright_tos, 100); });
			GEvent.addListener(map, 'tilesloaded', function(){ hide_google_copyright_tos(); setTimeout(hide_google_copyright_tos, 100); });
			hide_google_copyright_tos(); 
			setTimeout(hide_google_copyright_tos, 100);
			setTimeout(hide_google_copyright_tos, 200);
			setTimeout(hide_google_copyright_tos, 300);
			setTimeout(hide_google_copyright_tos, 400);
			setTimeout(hide_google_copyright_tos, 500);
			setTimeout(hide_google_copyright_tos, 600);
			setTimeout(hide_google_copyright_tos, 700);
			setTimeout(hide_google_copyright_tos, 800);
			setTimeout(hide_google_copyright_tos, 900);
			setTimeout(hide_google_copyright_tos, 1000);
			setTimeout(hide_google_copyright_tos, 1500);
			setTimeout(hide_google_copyright_tos, 2000);
		}
		if( mapedit ){
			GEvent.addListener(map,"click", function(overlay,latlng) {
				if( overlay )
					overlay.openInfoWindowHtml(name + '<br>' + blocation + ', ' + bstate);
				else {
					marker_setup(latlng);
					latlng_setup(latlng);
				}
			});
		}
	}
}

function geocoder_setup(){
	if( geocoder == null ){
		geocoder = new GClientGeocoder();
		geocoder.setBaseCountryCode('au');
	}
}

function marker_setup(latlng){
	if( !latlng || !map ){
		return;
	}
	if( marker == null ){
		marker_options = {title: name};
		if( mapedit ) marker_options.draggable = true;
		marker = new GMarker(latlng, marker_options);
		map.addOverlay(marker);
		if( mapedit ){
			GEvent.addListener(marker, "dragstart", function() {});
			GEvent.addListener(marker, "dragend", function() {
				latlng_setup(marker.getLatLng());
			});
		}
	} else {
		marker.setLatLng(latlng);
	}
}

function latlng_setup(latlng){
	document.save.latitude.value = latlng.lat();
	document.save.longitude.value = latlng.lng();
}

function load_location(latitude, longitude, name_, mapedit_){
	name = name_;
	mapedit = mapedit_;
	if (GBrowserIsCompatible()) {
		map_setup();
		latlng = new GLatLng(latitude, longitude);
		if( large ) map.setCenter(latlng, 16);
		marker_setup(latlng);
	}
}

function load_street(street, blocation_, bstate_, name_, mapedit_){
	name = name_;
	mapedit = mapedit_;
	blocation = blocation_;
	bstate = bstate_;
	street = street.replace(/^\s+|\s+$/g, '');
	if( !street.length ) street = null;
	if (GBrowserIsCompatible()) {
		map_setup();
		geocoder_setup();

		state_au = bstate+' '+au;
		loc_state_au = blocation+' '+bstate+' '+au;
		street_loc = street+' '+blocation;
		street_loc_state = street+' '+blocation+' '+bstate;
		street_loc_state_au = street+' '+blocation+' '+bstate+' '+au;

		// This is a chain of AJAX queries
		geocoder.getLatLng(state_au,function(latlng){
		if(latlng && large)
			map.setCenter(latlng, 5);

		geocoder.getLatLng(loc_state_au,function(latlng){
		if(latlng && large)
			map.setCenter(latlng, 12);

		// Only continue if the street has non-whitespace
		if(street) geocoder.getLatLng(street,function(latlng){
		if(latlng) {
			if( large ) map.setCenter(latlng, 13);
			marker_setup(latlng, 13);
		}

		geocoder.getLatLng(street_loc,function(latlng){
		if(latlng) {
			if( large ) map.setCenter(latlng, 13);
			marker_setup(latlng, 13);
		}

		geocoder.getLatLng(street_loc_state,function(latlng){
		if(latlng) {
			if( large ) map.setCenter(latlng, 13);
			marker_setup(latlng, 13);
		}

		geocoder.getLatLng(street_loc_state_au,function(latlng){
		if(latlng) {
			if( large ) map.setCenter(latlng, 13);
			marker_setup(latlng, 13);
		}

		});
		});
		});
		});
		});
		});
	}
}

function location_changed(){
	latlng = new GLatLng(document.save.latitude.value, document.save.longitude.value);
	if( large ) map.setCenter(latlng, 13);
	marker_setup(latlng, 13);
}


function create_tracking_button(field, data){
	document.write('<a href="#" id="show_'+field+'" onclick="trigger_tracking_button(\''+field+'\'); return false;">Click here to view</a><span id="'+field+'" style="display: none;">'+data+'</span>');
}

function create_custom_tracking_button(field, data, pretext, text, posttext){
	document.write('<span id="show_'+field+'">'+pretext+'<a href="#" onclick="trigger_tracking_button(\''+field+'\'); return false;">'+text+'</a>'+posttext+'</span><span id="'+field+'" style="display: none;">'+data+'</span>');
}

function trigger_tracking_button(field){
	var data = document.getElementById(field);
	var show_data = document.getElementById('show_'+field);
	if(data.style.display == 'none') {
		// Yay faked AJAX :)
		show_data.style.display = 'none';
		data.style.display = 'inline';
		// Now some real AJAX to log the click
		if( typeof XMLHttpRequest == "undefined" )
			XMLHttpRequest = function() {
				try { return new ActiveXObject("Msxml2.XMLHTTP.6.0") } catch(e) {}
				try { return new ActiveXObject("Msxml2.XMLHTTP.3.0") } catch(e) {}
				try { return new ActiveXObject("Msxml2.XMLHTTP") }     catch(e) {}
				try { return new ActiveXObject("Microsoft.XMLHTTP") }  catch(e) {}
				throw new Error( "This browser does not support XMLHttpRequest or XMLHTTP." )
			};
		request = new XMLHttpRequest();
		request.open('POST', document.location.href);
		request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		request.send('request_field=1&field='+field);
		request.onreadystatechange = function(){};
	}
}
