/*
	D I S T A N C E   C A L C U L A T O R
	-------------------------------------

	The distances array is set up as a matrix, where the 
	CoOrdinates are the IDs of the places within the aPlaces 
	array.

		
	For Example:
		
	Assume the contents of the aPlaces array are as follows:
		
	aPlaces[0] = "Adelaide"
	aPlaces[1] = "Canberra"
	aPlaces[2] = "Sydney"
	aPlaces[3] = "Melbourne"

	The aDistances array (which is two dimensional) would be 
	set up in a grid as follows:


		     0     1     2     3
		0    *     **    **    **
		1    1212  *     **    **
		2    1422  304   *     **
		3    755   651   893   *

		*  There is no need to define this value since it would be 
		   0 (adelaide - adelaide) - and the calculator knows 
		   this

		** There is no need to define this value as it is a duplicate 
		   of a value already in the matrix (the distance from 
		   canberra to sydney is the same as the distance from sydney 
		   to canberra).


	You can use the javascript shorthand for defining arrays 
	x = [value1,value2,etc,etc], so in code, the eample above 
	would be set up as:

		aDistances[1] = [1212]
		aDistances[2] = [1422,304]
		aDistances[3] = [755,651,893]

	Easy huh?
		
	Adding an extra addPlace("placeName") into the startup above 
	will automatically adjust the dimensions of the arrays - all 
	that is needed is an extra line on the bottom of the code 
	you create to populate the distances array.
		
	So, to add Perth to the above example, after including a line 
	of code 'addPlace("Perth")' after all the other items, add the 
	following code to the code where you populate the aDistances 
	array:

		aDistances[4] = [2713,3925,4136,3468]
*/




var startPlace = 0
var endPlace = 0
var currentParam = "start"
var aPlaces = new Array(0)
var aDistances = new Array(0)

// Establish a list of places (the index of a 
// place in the array is defined by the order 
// in which it was added)
	addPlace("Adelaide")
	addPlace("Alice Springs")
	addPlace("Brisbane")
	addPlace("Cairns")	
	addPlace("Canberra")
	addPlace("Darwin")
	addPlace("Hobart")
	addPlace("Melbourne")
	addPlace("Mount Isa")
	addPlace("Perth")
	addPlace("Surfers Paradise")
	addPlace("Sydney")

// Set up the distances array
	initialiseDistancesArray()

	aDistances[1] = [1693]
	aDistances[2] = [2127,3064]
	aDistances[3] = [2845,2435,1826]
	aDistances[4] = [1212,2905,1331,3157]
	aDistances[5] = [3225,1532,3502,2953,4233]
	aDistances[6] = [1002,2700,1927,3753,903,4232]
	aDistances[7] = [755,2488,1675,3501,651,3980,252]
	aDistances[8] = [2850,1157,1907,1278,2724,1675,3070,2818]
	aDistances[9] = [2713,3722,4427,4727,3925,4283,3720,3468,4973]
	aDistances[10] = [2207,3144,80,1908,1251,3662,2092,1840,1987,4507]
	aDistances[11] = [1422,2960,1027,2853,290,4095,1145,893,2420,4136,947]


// Preload Images
	obj_1_unselected=new Image(10,10)
	obj_1_unselected.src="/rulesregulations/images/obj_1_unselected.gif"
	obj_1_selected=new Image(10,10)
	obj_1_selected.src="/rulesregulations/images/obj_1_selected.gif"
	obj_1_tail=new Image(10,10)
	obj_1_tail.src="/rulesregulations/images/obj_1_tail.gif"
	obj_1_tail_unselected=new Image(10,10)
	obj_1_tail_unselected.src="/rulesregulations/images/obj_1_tail_unselected.gif"
	obj_1_tail_label=new Image(10,10)
	obj_1_tail_label.src="/rulesregulations/images/obj_1_tail_label.gif"

	obj_2_unselected=new Image(10,10)
	obj_2_unselected.src="/rulesregulations/images/obj_2_unselected.gif"
	obj_2_selected=new Image(10,10)
	obj_2_selected.src="/rulesregulations/images/obj_2_selected.gif"
	obj_2_tail_label=new Image(10,10)
	obj_2_tail_label.src="/rulesregulations/images/obj_2_tail_label.gif"




	function setCurrentParam(paramName)
		/*
		This function will select which parameter (start, or finish) the user 
		is choosing from the map. It will swap images so that the parameter 
		they are choosing will be highlighted.
		*/
		{

		// If the calling procedure has explicitly told us which is to be the 
		// current parameter, then use it. Otherwise, we will toggle the 
		// current parameter.
		if (paramName != "")
			{
			currentParam = paramName
			}
		else
			{
			if (currentParam == "start")
				{currentParam = "finish"}
			else
				{currentParam = "start"}					
			}

		// Do the image swapping
		if (currentParam == "start")
			{
			document.images["obj_1"].src = obj_1_selected.src
			document.images["obj_1_tail"].src = obj_1_tail.src
			document.images["obj_2"].src = obj_2_unselected.src
			document.images["tail"].src= obj_1_tail_label.src
			}
		else
			{
			document.images["obj_1"].src = obj_1_unselected.src
			document.images["obj_1_tail"].src = obj_1_tail_unselected.src
			document.images["obj_2"].src = obj_2_selected.src
			document.images["tail"].src= obj_2_tail_label.src
		}
	}



	function addPlace(placeName)
		{
		// This function adds a place to the highest index in the 
		// places array and then increases the size of the array.

		aPlaces.length = aPlaces.length + 1
		aPlaces[aPlaces.length-1] = placeName
		}



	function renderOptions(defaultPlaceID)
		{
		// This function will build the HTML that will draw a 
		// listbox with each of the places in our array as an 
		// option.

		for(x = 0;x < aPlaces.length; x++)
			{
			if (x == defaultPlaceID)
				{
				document.write(unescape("<option SELECTED>" + aPlaces[x] + "</option>%0D"))				
				}
			else
				{
				document.write(unescape("<option>" + aPlaces[x] + "</option>%0D"))
				}
			}
		}


		
	function setPlace(placeID)
		{
		if (currentParam == "start")
			{
			document.distance.start.selectedIndex = placeID
			setCurrentParam("finish")
			}
		else
			{
			document.distance.finish.selectedIndex = placeID
			setCurrentParam("start")
			}

		calculateResults()
		}



	function calculateResults()
		{
		// Begin by sorting out which placeID is greater, and making sure 
		// that the start placeID is greater than the finish PlaceID.
		if (document.distance.start.selectedIndex < document.distance.finish.selectedIndex)
			{
			placeID_start = document.distance.finish.selectedIndex
			placeID_finish = document.distance.start.selectedIndex
			}
		else
			{
			placeID_start = document.distance.start.selectedIndex
			placeID_finish = document.distance.finish.selectedIndex
			}				

		//Do the lookup
		if (placeID_start != placeID_finish)
			{
			// !!!!!! TESTING !!!!!!!
			// alert("start:" + placeID_start + ", Finish:" + placeID_finish)
			// !!!!!! !!!!!!! !!!!!!!
			result_km = aDistances[placeID_start][placeID_finish]
			result_mi = Math.round(result_km / 1.6)

			result_time_hours = Math.round(10*result_km * .012)/10

			document.distance.result_distance.value = result_km + " km / " + result_mi + " mi"
			document.distance.result_time.value = result_time_hours + " hours"
			}
		else
			{
			document.distance.result_distance.value = "0 km"
			document.distance.result_time.value = "No car required"
			}
		}



		function initialiseDistancesArray()
			{
			aDistances.length = aPlaces.length
			for(x = 1; x < aDistances.length; x++)
				{
				aDistances[x] = new Array()
				}
			}

