// JavaScript Document

var RESULTS_MAP_LAYER = "Results Base Layer";
var selected_easting;
var selected_northing;
var query_url;
	
function query_map(evt)
{
	//var query_url;

	
	//Set the cursor to the hourglass 
	document.body.style.cursor = 'wait'; 
	
	//Build up the url to query sias	
	query_url = "http://" + document.batch_form.server_name.value + "/gis/cgi-bin/siscgi.exe?"
	//query_url = document.batch_form.server_name.value + "/gis/cgi-bin/siscgi.exe?"
	//query_url += "request=getfeatureinfo";
	query_url += "request=feature_info";
	query_url += "&x=" + evt.xy.x;
	query_url += "&y=" + evt.xy.y;
	query_url += "&bbox=" + map.getExtent().toBBOX(); 
	query_url += "&width=" + map.size.w;
	query_url += "&height=" + map.size.h;
	query_url += "&layers=" + get_queryable_layers();
	
	//Store easting and northing of the mouse click
	var map_click_point = map.getLonLatFromPixel(evt.xy);
	selected_easting = map_click_point.lon;
	selected_northing = map_click_point.lat;
	
	//For DEBUG purposes store the query url
	document.batch_form.query_url.value = query_url;
	
	//var results_field;
	//var results; 

	load_url(query_url, true, new_show_results, show_query_error);
	
	//Stop the query event propogating
	Event.stop(evt);		
	
}	
	
function get_queryable_layers()
{
	var queryable_layers = ""; 
	var current_layer;	
	var loop_counter;
	var first_layer = true;
	
	//Loop through all the layers to see if they are queryable
	for (loop_counter = 0; loop_counter < map.layers.length; loop_counter++)
	{		
		//Get a handle on the current layer.
		//If it isn't a base layer and it is switched on then it is queryable
		current_layer = map.layers[loop_counter];			
		if(current_layer.isBaseLayer == false && current_layer.visibility == true && current_layer.name != RESULTS_MAP_LAYER)
		{
			//Need to add this queryable layer to the list 
			
			//Append the current layer to the list
			if(current_layer.params)
			{
				//queryable_layers += current_layer.params.LAYERS;
				
				//If a layer(s) have previously been appended then append a comma seperator
				if(first_layer == false) 
				{
					queryable_layers += "," + current_layer.params.LAYERS;
				}
				else
				{				
					//Set the first queryable layer
					queryable_layers = current_layer.params.LAYERS;
					
					//This is the first queryable layer found so flag it 	
					first_layer = false;				
				}
				
			}
			
		}
		
	}
	
	//Return the list of queryable layers
	return queryable_layers;
	
}

function new_show_results(responseText)
{
	var results_field;
	var results; 
	
	try
	{

		//Get the descriptive text for the current selection
		//If there is descriptive text then display in a popup
		var popupHTML = get_query_results(responseText);
		if(popupHTML != "") 
		{
			
			//Create a popup showing the results
			OpenLayers.Popup.FramedCloud.prototype.autoSize = true; 

			var anchor_point = new OpenLayers.LonLat(selected_easting, selected_northing);
			var feature = new OpenLayers.Feature(map.baseLayer, anchor_point);
			feature.closeBox = true; 
		
			var AutoSizedFramedCloud = OpenLayers.Class(OpenLayers.Popup.FramedCloud, 
													{
														'autoSize':true
														//'autoSize':true, 
														//'minSize': new OpenLayers.Size(250, 250)
													});
		
			feature.popupClass = AutoSizedFramedCloud;
		
			feature.data.popupContentHTML = '<div style="width : 220px">' + popupHTML + '</div>';
			feature.data.icon = OpenLayers.Marker.defaultIcon();
			feature.data.autoSize = true; 
			feature.data.overflow = "auto"; 
			
			//Create a results layer 
			//var results_layer = create_lookup_results_layer(responseText); 
			var results_layer = get_popups_layer(responseText); 
			map.addLayer(results_layer); 
			
			//Set the overlay on
			results_layer.setVisibility(true);
			
			results_layer.features.push(feature);
	
			var marker = feature.createMarker(); 
					
			//Create the call-back function to be called when the user 
			//clicks on the feature's marker
			var markerClick = function (evt) {
					if (this.popup == null) {
						this.popup = this.createPopup(this.closeBox);
						map.addPopup(this.popup);
						this.popup.show();
					} else {
						this.popup.toggle();
					}
					currentPopup = this.popup;
					OpenLayers.Event.stop(evt);
				};
			
			marker.events.register('mousedown', feature, markerClick);
			
			//Add the marker to the map
			results_layer.addMarker(marker);
								
			//Store the pop with the feature
			feature.popup = feature.createPopup(true);				
			
			//map.addPopup(feature);
			map.addPopup(feature.popup);
			//results_layer.show_objects();
			
		}

		//Reset the default cursor 
		document.body.style.cursor = 'default'; 

	}
	catch(e)
	{
		alert("Problem with new_show_results() - " + e.description);
		
	}	
		
}

function show_results(response)
{
	var results_field;
	var results; 
	
	try
	{
		
		//Get a handle on the element used to display the results
		results_field = document.getElementById('show_results');
		if(results_field)
		{
				
			//Get the html descibing the selected object and display it
			results = get_query_results(response.responseText);
			results_field.innerHTML = results;
			//results_field.innerHTML = response.responseText;
				
		}
	}
	catch(e)
	{
		alert(e.description);
		
	}	
		
}

function show_errors(response)
{
	var results_field;
	var results; 
	
	try
	{
		//Get a handle on the element used to display the results
		results_field = document.getElementById('show_results');
		if(results_field)
		{
				
			//Get the html descibing the selected object and display it
			results_field.innerHTML = response.responseText;
				
		}
	}
	catch(e)
	{
		alert("Problem with show_results() - " + e.description);
		
	}		
}


function get_query_results(query_xml)
{

	//Initalise local variables
	var root_node;
	var feature_nodes;
	var current_node;
	var query_results ="";
	var selected_feature = "";
	var node_count = 0; 
	
	try
	{
		
		//Load the results into an XML dom
		root_node = OpenLayers.parseXMLString(query_xml);
	
		//Ensure we have a valid handle on the results xml
		if(root_node) 
		{
			//Get a handle on all the found features
			feature_nodes = root_node.getElementsByTagName("Feature");		
			if(feature_nodes)
			{
				
				//Loop through all the returned nodes and extract details if required
				for (node_count = 0; node_count < feature_nodes.length; node_count++) 
				{
	
					//Get a handle on the current node
					current_node = feature_nodes.item(node_count);
	
					//Determine the type of feature found
					selected_feature = current_node.getAttribute("typeName");
					switch(selected_feature)
					{
						case 'electoral_ward' : 
							//Return information on the selected electoral ward
							query_results += get_electoral_ward_details(current_node);
							break;
							
						case 'aberdeenshire_area' : 
							//Return information on the selected aberdeenshire area
							query_results += get_abshire_area_details(current_node);
							break;
	
						case 'confirm_lighting_unit' : 
						
							//Return information on the selected lighting unit
							query_results += get_confirm_light_details(current_node);
							
							break;
	
						case 'housing_audit_2007' :
							//Dealing with an (2007) housing land audit settlement
							//query_results += get_old_land_audit_details(current_node, "housing", "Housing Land Audit (2007)");
							query_results += get_old_land_audit_details(current_node, "Housing Land Audit (2007)");
							break;
							
						case 'housing_audit_2008' :
							//Dealing with an housing land audit settlement
							//query_results += get_default_object_details(get_parent_object_type(current_node.parentNode), current_node);
							query_results += get_land_audit_details(current_node, "housing", "Housing Land Audit");
							break;

						case 'housing_audit_live' :
							//Dealing with an (2007) housing land audit settlement
							//query_results += get_old_land_audit_details(current_node, "housing", "Housing Land Audit (2007)");
							//query_results += get_old_land_audit_details(current_node, "Housing Land Audit (2007)");
							query_results += get_land_audit_details(current_node, "housing_live", "Housing Land Audit");
							break;
							
						case 'housing_audit_draft' :
							//Dealing with an housing land audit settlement
							//query_results += get_default_object_details(get_parent_object_type(current_node.parentNode), current_node);
							query_results += get_land_audit_details(current_node, "housing_draft", "Housing Land Audit");
							break;

						case 'emp_audit_2008' :
							//Dealing with an employment land audit settlement
							query_results += get_land_audit_details(current_node, "employment", "Employment Land Audit");
							break;
						case 'housing_market_areas' :
							//Dealing with an housing land audit settlement
							query_results += get_housing_market_details(current_node);
							break;
						case 'plan_app_geom' :
							//Dealing with a planning application
							query_results += get_plan_app_geom_details(current_node);
							break;
						
						case 'alp_settlement_geom' :
							//Dealing with an alp settlement
							query_results += get_alp_settlement_details(current_node)
							break;
						case 'alp_settlement' :
							//Ignore plain alp_settlement
							break;
						case 'plan_app':
							//Ignoring plain plan app
							break;					
						case 'road_unit' :
							query_results += get_road_unit_details(current_node, selected_easting, selected_northing);		
							break;
						case 'spr_route' :
							//query_results += get_road_unit_details(current_node, selected_easting, selected_northing);		
							query_results += get_cycle_path_section_details(current_node, selected_easting, selected_northing)
							break;
							
						case 'display_context' :
							//Ignore the display_context entries
							break;
	
						case 'reinstatement_category' :
							//Ignore the reinstatement_category entries
							break;
	
						case 'base_gazetteer_data' :
							//Ignore the reinstatement_category entries
							break;
	
						case 'recycling_location' :
							query_results += get_rec_loc_details(get_parent_object_type(current_node.parentNode), current_node);
							break;
							
						default:
							
							//There is no specific function to extract data for the current 
							//node so use the default function
							//query_results += get_default_object_details(get_parent_object_type(current_node.parentNode), current_node);
							//query_results += "<h2>" + selected_feature + "</h2>";
							query_results + "";
							break;
							
					}
					
				}
				
			}
			
		}
	}
	catch(e)
	{
		alert("Problem with query_results() - " + e.description);
		
	}		
	//Return the extract data
	return query_results;

}

function get_electoral_ward_details(ward_node)
{
	var ward_details = "";
	var current_node; 
	var type_name;
	var council_name;
	var ward_name = ""; 
	var ward_number = 0;
	var node_count = 0; 
	
	//Ensure a valid ward node has been received
	if(ward_node)
	{
	
		try
		{
	
			//Extract the ward details
			for (node_count = 0; node_count < ward_node.childNodes.length; node_count++) 
			{

				//Get the current node 
				current_node = ward_node.childNodes[node_count];			
			
				//Ensure we are dealing with a 'property' node
				if(current_node.nodeName == 'property')
				{
					
					//Extract the information from the current child node
					type_name = current_node.getAttribute('typeName');
					switch(type_name)
					{
						case 'council':
							council = current_node.firstChild.nodeValue;
							break;
						
						case 'ew_name':
							ward_name = current_node.firstChild.nodeValue;
							break;
					
						case 'ew_number':
							ward_number= current_node.firstChild.nodeValue;
							break;
					
						default:
							break;
					}

				}
			
			}
		
			//Ensure an 'Aberdeenshire' electoral ward has been selected 
			if(council == 'Aberdeenshire')
			{
				//Build up the ward details text to display
				ward_details += "<h2>Electoral Ward</h2>";
				ward_details += "<p><a href='http://www.aberdeenshire.gov.uk/councillors/contact/ward_" + ward_number + ".asp'>" + ward_name + "</a></p>";
			}
			
		}
		catch(err)
		{
			alert('An error occured : ' + err.description);
		}
		
	}
	
	//Return ward details to display
	return ward_details;
	
}

function get_abshire_area_details(area_node)
{
	var area_details = "";
	var current_node; 
	var type_name;
	var area_name = ""; 
	var node_count = 0; 
	
	//Ensure a valid area node has been received
	if(area_node)
	{
	
		try
		{
	
			//Extract the areadetails
			for (node_count = 0; node_count < area_node.childNodes.length; node_count++) 
			{

				//Get the current node 
				current_node = area_node.childNodes[node_count];			
			
				//Ensure we are dealing with a 'property' node
				if(current_node.nodeName == 'property')
				{
					
					//Extract the information from the current child node
					type_name = current_node.getAttribute('typeName');
					switch(type_name)
					{
						case 'name':
							area_name = current_node.firstChild.nodeValue;
							break;
						
						default:
							break;
					}

				}
			
			}
		
			//Build up the area details text to display
			area_details += "<h2>Aberdeenshire Area</h2>";
			area_details += "<p>" + area_name + "</p>";
			
		}
		catch(err)
		{
			alert('An error occured : ' + err.description);
		}
		
	}
	
	//Return the area details to display
	return area_details;
	
}

function get_plan_app_geom_details(plan_app_geom_node)
{
	var plan_app_details = "";
	var current_node; 
	var type_name;
	var node_count = 0; 
	var map_id = "";
	var rec_nr = ""; 
	var seq_nr = ""; 
	var plan_app_url; 
	

	//Ensure a valid area node has been received
	if(plan_app_geom_node)
	{
	
		try
		{
	
			//Extract the planm app geom details
			for (node_count = 0; node_count < plan_app_geom_node.childNodes.length; node_count++) 
			{

				//Get the current node 
				current_node = plan_app_geom_node.childNodes[node_count];			
			
				//Ensure we are dealing with a 'property' node
				if(current_node.nodeName == 'property')
				{
					
					//Extract the information from the current child node
					type_name = current_node.getAttribute('typeName');
					switch(type_name)
					{
						case 'map_id':
							map_id = current_node.firstChild.nodeValue;
							break;
						case 'rec_nr':
							rec_nr = current_node.firstChild.nodeValue;
							break;
						case 'seq_nr':
							seq_nr = current_node.firstChild.nodeValue;
							break;
						
						default:
							break;
					}

				}
			
			}
			
			//Build up the URL to get the full planning application details
			plan_app_url = "http://" + document.batch_form.server_name.value  + "/gis/cgi-bin/siscgi.exe";
			plan_app_url += "?request=info_lookup&type=plan_app_geom&id=" + map_id + "/" + rec_nr + "/" + seq_nr;

			//Get the full plan app details 
			OpenLayers.loadURL(plan_app_url, '', this, show_planning_results, show_errors); 
			
			//Build up the area details text to display
			//plan_app_details += "<h2>Planning Application</h2>";
			
		}
		catch(err)
		{
			alert('An error occured : ' + err.description);
		}
		
	}
	
	//Return the planning app details to display
	return plan_app_details;
	
}


function show_planning_results(response)
{
	var results_field;
	var results; 
		
	//Get a handle on the element used to display the planning results	
	results_field = document.getElementById('show_results');
	if(results_field)
	{
			
		//Get the html descibing the selected object and display it
		results = get_plan_app_results(response.responseText);
		
		results_field.innerHTML += results;
			
	}
		
}

function get_plan_app_results(query_xml)
{

	//Initalise local variables
	var root_node;
	var lookup_nodes;	
	var current_node;
	var child_nodes;
	var current_child;
	var query_results = "";
	var node_count = 0; 
	var node_count2 = 0; 
	var node_name;
	var node_value;
	var proposal = "";
	var decision = "";
	var plan_app_url = "";
	
	//Load the results into an XML dom

	try
	{
	root_node = OpenLayers.parseXMLString(query_xml);

	//Ensure we have a valid handle on the results xml
	if(root_node) 
	{
		//Get a handle on all the lookup results
		lookup_nodes = root_node.getElementsByTagName("planning_app");	
		if(lookup_nodes)
		{

			//Loop through all the returned nodes and extract details if required
			for (node_count = 0; node_count < lookup_nodes.length; node_count++) 
			{

				//Get a handle on the current node
				current_node = lookup_nodes.item(node_count);
				query_results += "<h2>Planning Application</h2>";

				//Loop through all the next nodes and extract details if required
				for (node_count2 = 0; node_count2 < current_node.childNodes.length; node_count2++) 
				{
					
					//Get a handle on the current child node
					current_child = current_node.childNodes.item(node_count2);
					
					if(current_child.nodeType == 1)
					{
						if(current_child.hasChildNodes())
						{

							node_name = current_child.nodeName;
							node_value = current_child.firstChild.nodeValue;
							
							//Display the results as appropriate
							switch(node_name)
							{
								case 'refno':
									plan_app_url = "<p><a href='http://www.aberdeenshire.gov.uk/planning/apps/detail.asp?ref_no=" + node_value + "'>" + node_value + "</a></p>";
									break;
								case 'proposal':
									proposal = "<p>" + node_value + "</p>";
									break;
								case 'decision':
									decision = "<p>" + node_value + "</p>";
									break;
								default:
									//query_results += "<p>" + node_name + " : " + node_value + "</p>";							
							}
							

						}
					}
					
				}

				query_results += plan_app_url;
				query_results += proposal;
				query_results += decision;
				
				//query_results += current_node.firstChild.nodeValue;
				
			}
			
		}
		
	}
	}
	catch(e)
	{
		alert(e.description)
	}
	//Return the extract data
	return query_results;

}

function get_alp_settlement_details(alp_settlement_geom_node)
{
	var alp_settlement_details = "";
	var current_node; 
	var type_name;
	var node_count = 0; 
	var map_id = "";
	var rec_nr = ""; 
	var seq_nr = ""; 
	var alp_settlement_url; 
	

	//Ensure a valid area node has been received
	if(alp_settlement_geom_node)
	{
		try
		{
	
			//Extract the planm app geom details
			for (node_count = 0; node_count < alp_settlement_geom_node.childNodes.length; node_count++) 
			{

				//Get the current node 
				current_node = alp_settlement_geom_node.childNodes[node_count];			
			
				//Ensure we are dealing with a 'property' node
				if(current_node.nodeName == 'property')
				{
					
					//Extract the information from the current child node
					type_name = current_node.getAttribute('typeName');
					switch(type_name)
					{
						case 'map_id':
							map_id = current_node.firstChild.nodeValue;
							break;
						case 'rec_nr':
							rec_nr = current_node.firstChild.nodeValue;
							break;
						case 'seq_nr':
							seq_nr = current_node.firstChild.nodeValue;
							break;
						
						default:
							break;
					}

				}
			
			}
			
			//Build up the URL to get the full alp settlement details
			alp_settlement_url = "http://" + document.batch_form.server_name.value  + "/gis/cgi-bin/siscgi.exe";
			alp_settlement_url += "?request=info_lookup&type=alp_settlement_geom&id=" + map_id + "/" + rec_nr + "/" + seq_nr;

			//Query SIAS for the full ALP details and display
			OpenLayers.loadURL(alp_settlement_url, '', this, show_alp_results, show_errors); 
			
		}
		catch(err)
		{
			alert('An error occured : ' + err.description);
		}
		
	}
	
	//Return the alp settlement details to display
	return alp_settlement_details;
	
}

function show_alp_results(response)
{
	var results_field;
	var results; 
		
	//Get a handle on the element used to display the alp results	
	results_field = document.getElementById('show_results');
	if(results_field)
	{
			
		//Get the html descibing the selected object and display it
		results = get_alp_results(response.responseText);

		//Display the results
		results_field.innerHTML += results;
			
	}
		
}
function get_alp_results(query_xml)
{

	//Initalise local variables
	var root_node;
	var lookup_nodes;	
	var current_node;
	var child_nodes;
	var current_child;
	var query_results = "";
	var node_count = 0; 
	var node_count2 = 0; 
	var node_name;
	var node_value;
	
	//Load the results into an XML dom

	try
	{
		root_node = OpenLayers.parseXMLString(query_xml);

		//Ensure we have a valid handle on the results xml
		if(root_node) 
		{
			//Get a handle on all the lookup results
			lookup_nodes = root_node.getElementsByTagName("alp_settlement");	
			if(lookup_nodes)
			{

				//Loop through all the returned nodes and extract details if required
				for (node_count = 0; node_count < lookup_nodes.length; node_count++) 
				{

					//Get a handle on the current node
					current_node = lookup_nodes.item(node_count);
					query_results += "<h2>ALP Settlement</h2>";

					//Loop through all the next nodes and extract details if required
					for (node_count2 = 0; node_count2 < current_node.childNodes.length; node_count2++) 
					{
					
						//Get a handle on the current child node
						current_child = current_node.childNodes.item(node_count2);
					
						if(current_child.nodeType == 1)
						{
							if(current_child.hasChildNodes())
							{

								node_name = current_child.nodeName;
								node_value = current_child.firstChild.nodeValue;
							
								//Display the results as appropriate
								switch(node_name)
								{
									case 'location':
										query_results += "<p>" + node_value + "</p>";
										break;
									case 'notes':
										query_results += "<p>" + node_value + "</p>";
										break;
									default:
										;
										//query_results += "<p>" + node_name + " : " + node_value + "</p>";							
								}
							

							}
						}
					
					}

				
					//query_results += current_node.firstChild.nodeValue;
				
				}
			
			}
		
		}
	}
	catch(e)
	{
		alert(e.description)
	}
	
	//Return the extract data
	return query_results;

}

function get_land_audit_details(land_audit_node, audit_type, title)
{
	var his_details = "";
	var object_details = "";
	var current_node; 
	var type_name;
	var field_name; 
	var field_value;
	var site_refno = ""; 
	var old_site_refno = ""; 
	var base_url = "";
	var site_link = "";
	var requested_layers = "";
	var location = ""; 
	var owner = ""; 	
	var developer = ""; 	
	var site_type = ""; 	
	var node_count = 0; 
	
	//Ensure a valid his node has been received
	if(land_audit_node)
	{
	
		//Determine the type of land audit 
		switch(audit_type)
		{
			case 'housing_live':
				//base_url = "http://www3.aberdeenshire.gov.uk/statistics/hla";
				base_url = "/statistics/hla";
				requested_layers = "Housing Land Audit Live";
				break;
			case 'housing_draft':
				//base_url = "http://www3.aberdeenshire.gov.uk/statistics/hla";
				base_url = "/statistics/hla";
				requested_layers = "Housing Land Audit Draft";
				break;
			case 'employment':
				//base_url = "http://www3.aberdeenshire.gov.uk/statistics/ela";
				base_url = "/statistics/ela";
				requested_layers = "Employement Audit 2008";
				break;
			default:
				//base_url = "http://www3.aberdeenshire.gov.uk/statistics/hla";
				base_url = "/statistics/hla";
				break;
		}
		
		try
		{
	
			//Extract the land audit details
			for (node_count = 0; node_count < land_audit_node.childNodes.length; node_count++) 
			{

				//Get the current node 
				current_node = land_audit_node.childNodes[node_count];			
			
				//Ensure we are dealing with a 'property' node
				if(current_node.nodeName == 'property')
				{
					
					//Get the field name 
					field_name = current_node.getAttribute("typeName"); 	
					//Get the field value 
					if(current_node.firstChild)
					{
						field_value= current_node.firstChild.nodeValue;
					}
					else
					{
						field_value= current_node.nodeValue;
					}					
					
					//Extract the information from the current child node
					type_name = current_node.getAttribute('typeName');
					switch(type_name)
					{
						case 'siteref':
							site_refno = current_node.firstChild.nodeValue;
							//site_link = base_url + "/detail.asp?ref_no=" + field_value;
							object_details += "<p>Site Ref : <a href='" + site_link + "&layers=" + requested_layers + "&stage=draft" + "'>" + field_value + "</a></p>";
							//alert("site_refno = " + site_refno);
							break;
						case 'oldsiteref':
							
							if(current_node.firstChild != null)
							{
								old_site_refno = current_node.firstChild.nodeValue;
								//site_link = base_url + "/detail.asp?ref_no=" + field_value+ "&stage=draft" ;
								object_details += "<p>Old Site Ref : <a href='" + site_link  + "&layers=" + requested_layers + "&stage=draft" + "'>" + field_value + "</a></p>";
								
							}
							else
							{
								old_site_refno = "";
							}
							//alert("old_site_refno = " + old_site_refno);
							break;
						case 'location':
							location = current_node.firstChild.nodeValue;
							object_details += "<p>" + field_name + " : " + field_value + "</p>";
							break;
						case 'owner':
							owner = current_node.firstChild.nodeValue;
							object_details += "<p>" + field_name + " : " + field_value + "</p>";
							break;
						case 'developer':
							developer = current_node.firstChild.nodeValue;
							object_details += "<p>" + field_name + " : " + field_value + "</p>";
							break;
						case 'sitetype':
							site_type = current_node.firstChild.nodeValue;
							object_details += "<p>" + field_name + " : " + field_value + "</p>";
							break;
						
						default:
							//object_details += "<p>" + field_name + " : " + field_value + "</p>";
							break;
					}

				}
			
			}
		
			//Build up the his details text to display
			if(site_refno != "")
			{
				if(audit_type == "housing_live" || audit_type == "housing_draft")
				{
					//Do we need to use the old site ref
					var land_audit_url;	
					if(site_refno != "")
					{
						//Build up the URL to get the full land audit details								
						land_audit_url = "/map_utils/hla_details.aspx?ref=" + site_refno;
						
						//Need to specify if it is a live or draft land audit 
						if(audit_type == "housing_live")
						{
							//This is a live land audit site
							land_audit_url = land_audit_url + "&status=live";
						}
						else
						{
							//Assume this is a draft land audit site
							land_audit_url = land_audit_url + "&status=draft";							
						}
						
					}
					else 
					{
						//land_audit_url = "/map_utils/hla_details.aspx?ref=" + site_refno;
						
					}
					
					//his_details += "<h2>" + title + "</h2>";					   
					//his_details += "<a href='" + land_audit_url + "'>" + land_audit_url + "</a>";
					//Query SIAS for the full land audit details and display
					OpenLayers.loadURL(land_audit_url, '', this, show_housing_land_audit_results, show_errors); 
					
					   
				}
				else
				{
		
					//Employment Land Audit 
					//Do we need to use the old site ref
					var land_audit_url;	
					if(site_refno != "")
					{
						//Build up the URL to get the full alp settlement details								
						land_audit_url = "/map_utils/ela_details.aspx?ref=" + site_refno;
					}
					
					//Query SIAS for the full land audit details and display
					OpenLayers.loadURL(land_audit_url, '', this, show_employment_land_audit_results, show_errors); 
					
					//his_details += "<h2>" + title + "</h2>";
					//his_details += object_details;
					//his_details += "<p>" + site_refno + "</p>";
					//his_details += "<p>" + location + "</p>";
					//his_details += "<p>" + owner + "</p>";			
					//his_details += "<p>" + developer + "</p>";			
					//his_details += "<p>" + site_type + "</p>";
				}
			}
			
		}
		catch(err)
		{
			alert('An error occured : ' + err.description);
		}
		
	}
	
	//Return the his details to display
	return his_details;
	
}

function show_housing_land_audit_results(response)
{
	var results_field;
	var results; 
		
	//Get a handle on the element used to display the alp results	
	results_field = document.getElementById('show_results');
	if(results_field)
	{
			
		//Get the html descibing the selected object and display it
		//results = get_housing_land_audit_results(response.responseText);
		results = get_land_audit_results(response.responseText);

		//Display the results
		results_field.innerHTML = results;
			
	}
		
}

function get_land_audit_results(query_xml)
{

	//Initalise local variables
	var root_node;
	var lookup_nodes;	
	var current_node;
	var child_nodes;
	var current_child;
	var query_results = "";
	var site_refno;
	var site_refno_text;
	var site_link;
	var site_type = "";
	var site_type_header = "";
	var location = "";
	var status;
	var node_count = 0; 
	var node_count2 = 0; 
	var node_name;
	var node_value;
	
	//Load the results into an XML dom

	try
	{
		root_node = OpenLayers.parseXMLString(query_xml);

		//Ensure we have a valid handle on the results xml
		if(root_node) 
		{
			//Get a handle on all the lookup results
			lookup_nodes = root_node.getElementsByTagName("housing_audit");	
			if(lookup_nodes)
			{

				//Loop through all the returned nodes and extract details if required
				for (node_count = 0; node_count < lookup_nodes.length; node_count++) 
				{

					//Get a handle on the current node
					current_node = lookup_nodes.item(node_count);
					//query_results += "<h2>Housing Land Audit (2008)</h2>";

					//Loop through all the next nodes and extract details if required
					for (node_count2 = 0; node_count2 < current_node.childNodes.length; node_count2++) 
					{
					
						//Get a handle on the current child node
						current_child = current_node.childNodes.item(node_count2);
					
						if(current_child.nodeType == 1)
						{
							if(current_child.hasChildNodes())
							{

								node_name = current_child.nodeName;
								node_value = current_child.firstChild.nodeValue;
							
								//Display the results as appropriate
								switch(node_name)
								{
									case 'type':
									
										site_type = node_value;
										if(site_type == "live")
										{
											site_type_header = "<h2>Housing Land Audit (2010)</h2>";
										}
										else
										{
											site_type_header = "<h2>Draft Housing Land Audit (2011)</h2>";
										}
										break;

									case 'siteref':
										site_refno = node_value;
										//site_link = "/statistics/hla/detail.asp?ref_no=" + node_value + "&layers=Housing Land Audit Live&stage=live";
										//site_refno_text = "<a href='" + site_link + "'>" + node_value + "</a>";									
										//query_results += "<p>" + site_refno_text + "</p>";
										break;
									
									case 'location':
										location = node_value;
										break;
									case 'status':
									
										//Ensure we show a user friendly status
										switch(node_value)
										{
											case 'Full PP':
												status = "Full Planning Permission";
												break;
											case 'Outline PP':
												status = "Outline Planning Permission";
												break;
											default:
												status = node_value;
										}										
										//query_results += "<p>" + status + "</p>";
										break;
									default:
										;
										//query_results += "<p>" + node_name + " : " + node_value + "</p>";							
								}
							

							}
						}
					
					}

				
					//query_results += current_node.firstChild.nodeValue;
				
				}
			
				//Build up the text to display 
				query_results = site_type_header;
				if(site_type == "live")
				{
					site_link = "/statistics/hla/detail.asp?ref_no=" + site_refno + "&layers=Housing Land Audit Live&stage=live";
					site_refno_text = "<a href='" + site_link + "'>" + site_refno + "</a>";									
					
				}
				else
				{
					site_link = "/statistics/hla/detail.asp?ref_no=" + site_refno + "&layers=Draft Housing Land Audit&stage=draft";
					site_refno_text = "<a href='" + site_link + "'>" + site_refno + "</a>";									
					
				}
				query_results += "<p>" + site_refno_text + "</p>";
				query_results += "<p>" + location + "</p>";
				query_results += "<p>" + status + "</p>";				


			}
		
		}
	}
	catch(e)
	{
		alert(e.description)
	}
	
	//Return the extract data
	return query_results;

}

function show_employment_land_audit_results(response)
{
	var results_field;
	var results; 
		
	//Get a handle on the element used to display the alp results	
	results_field = document.getElementById('show_results');
	if(results_field)
	{
			
		//Get the html descibing the selected object and display it
		results = get_employment_land_audit_results(response.responseText);

		//Display the results
		results_field.innerHTML += results;
			
	}
		
}
function get_employment_land_audit_results(query_xml)
{

	//Initalise local variables
	var root_node;
	var lookup_nodes;	
	var current_node;
	var child_nodes;
	var current_child;
	var query_results = "";
	var site_refno;
	var site_refno_text;
	var site_link;
	var status;
	var node_count = 0; 
	var node_count2 = 0; 
	var node_name;
	var node_value;
	
	//Load the results into an XML dom

	try
	{
		root_node = OpenLayers.parseXMLString(query_xml);

		//Ensure we have a valid handle on the results xml
		if(root_node) 
		{
			//Get a handle on all the lookup results
			lookup_nodes = root_node.getElementsByTagName("employment_audit_2008");	
			if(lookup_nodes)
			{

				//Loop through all the returned nodes and extract details if required
				for (node_count = 0; node_count < lookup_nodes.length; node_count++) 
				{

					//Get a handle on the current node
					current_node = lookup_nodes.item(node_count);
					query_results += "<h2>Employment Land Audit (2008)</h2>";

					//Loop through all the next nodes and extract details if required
					for (node_count2 = 0; node_count2 < current_node.childNodes.length; node_count2++) 
					{
					
						//Get a handle on the current child node
						current_child = current_node.childNodes.item(node_count2);
					
						if(current_child.nodeType == 1)
						{
							if(current_child.hasChildNodes())
							{

								node_name = current_child.nodeName;
								node_value = current_child.firstChild.nodeValue;
							
								//Display the results as appropriate
								switch(node_name)
								{
									case 'siteref':
										site_refno = node_value;
										site_link = "/statistics/ela/detail.asp?ref_no=" + node_value;
										site_refno_text = "<a href='" + site_link + "'>" + node_value + "</a>";									
										query_results += "<p>" + site_refno_text + "</p>";
										break;
									
									case 'location':
										query_results += "<p>" + node_value + "</p>";
										break;
									case 'status':
									
										//Ensure we show a user friendly status
										switch(node_value)
										{
											default:
												status = node_value;
										}										
										query_results += "<p>" + status + "</p>";
										break;
									default:
										;
										//query_results += "<p>" + node_name + " : " + node_value + "</p>";							
								}
							

							}
						}
					
					}

				}
			
			}
		
		}
	}
	catch(e)
	{
		alert(e.description)
	}
	
	//Return the extract data
	return query_results;

}

function get_old_land_audit_details(land_audit_node, title)
{
	var land_audit_details = "";
	var object_details = "";
	var current_node; 
	var field_name; 
	var field_value;
	var site_refno = ""; 
	var site_link = "";
	var site_refno_text = ""; 
	var location = ""; 
	var status = ""; 	
	var node_count = 0; 
	
	//Ensure a valid his node has been received
	if(land_audit_node)
	{
	
		try
		{
	
			//Extract the land audit details
			for (node_count = 0; node_count < land_audit_node.childNodes.length; node_count++) 
			{

				//Get the current node 
				current_node = land_audit_node.childNodes[node_count];			
			
				//Ensure we are dealing with a 'property' node
				if(current_node.nodeName == 'property')
				{
					
					//Get the field name 
					field_name = current_node.getAttribute("typeName"); 	

					//Get the field value 
					if(current_node.firstChild)
					{
						field_value= current_node.firstChild.nodeValue;
					}
					else
					{
						field_value= current_node.nodeValue;
					}					
					
					//Extract the information from the current child node
					type_name = current_node.getAttribute('typeName');
					switch(type_name)
					{
						case 'siteref':
							site_refno = current_node.firstChild.nodeValue;
							//site_link = "http://www3.aberdeenshire.gov.uk/statistics/hla/detail.asp?ref_no=" + field_value + "&layers=Housing Audit 2007&stage=live";
							site_link = "/statistics/hla/detail.asp?ref_no=" + field_value + "&layers=Housing Land Audit 2007&stage=live";
							site_refno_text = "<a href='" + site_link + "'>" + field_value + "</a>";
							break;
						case 'location':
							location = current_node.firstChild.nodeValue;
							break;
						case 'status':
							status = current_node.firstChild.nodeValue;
							break;						
						default:
							//object_details += "<p>" + field_name + " : " + field_value + "</p>";
							break;
					}

				}
			
			}
		
			//Build up the his details text to display
			if(site_refno != "")
			{
				land_audit_details += "<h2>" + title + "</h2>";
				land_audit_details += "<p>" + site_refno_text + "</p>";
				land_audit_details += "<p>" + location + "</p>";
				land_audit_details += "<p>" + status + "</p>";
			}
			
		}
		catch(err)
		{
			alert('An error occured : ' + err.description);
		}
		
	}
	
	//Return the his details to display
	return land_audit_details;
	
}

function get_housing_market_details(housing_market_node)
{
	var housing_market_details = "";
	var object_details = "";
	var current_node; 
	var type_name;
	var field_name; 
	var field_value;
	var name = ""; 
	var node_count = 0; 
	
	//Ensure a valid his node has been received
	if(housing_market_node)
	{
	
		try
		{
	
			//Extract the his etails
			for (node_count = 0; node_count < housing_market_node.childNodes.length; node_count++) 
			{

				//Get the current node 
				current_node = housing_market_node.childNodes[node_count];			
			
				//Ensure we are dealing with a 'property' node
				if(current_node.nodeName == 'property')
				{
					
					//Get the field name 
					field_name = current_node.getAttribute("typeName"); 	
					//Get the field value 
					if(current_node.firstChild)
					{
						field_value= current_node.firstChild.nodeValue;
					}
					else
					{
						field_value= current_node.nodeValue;
					}					
					
					//Extract the information from the current child node
					type_name = current_node.getAttribute('typeName');
					switch(type_name)
					{
						case 'layername':
							name = current_node.firstChild.nodeValue;
							break;
						default:
							//object_details += "<p>" + field_name + " : " + field_value + "</p>";
							break;
					}

				}
			
			}
		
			//Build up the his details text to display
			housing_market_details += "<h2>Housing Market Area</h2>";
			housing_market_details += "<p>Name : " + name + "</p>";

			
		}
		catch(err)
		{
			alert('An error occured : ' + err.description);
		}
		
	}
	
	//Return the housing market area details to display
	return housing_market_details;
	
}
function get_rec_loc_details(spurious_parameter, rec_loc_node)
{
	var rec_loc_details = "";
	var object_details = "";
	var current_node; 
	var type_name;
	var field_name; 
	var field_value;
	var type = "";
	var location = ""; 
	var node_count = 0; 
	
	//Ensure a valid his node has been received
	if(rec_loc_node)
	{
	
		try
		{
	
			//Extract the his etails
			for (node_count = 0; node_count < rec_loc_node.childNodes.length; node_count++) 
			{

				//Get the current node 
				current_node = rec_loc_node.childNodes[node_count];			
			
				//Ensure we are dealing with a 'property' node
				if(current_node.nodeName == 'property')
				{
					
					//Get the field name 
					field_name = current_node.getAttribute("typeName"); 	
					//Get the field value 
					if(current_node.firstChild)
					{
						field_value= current_node.firstChild.nodeValue;
					}
					else
					{
						field_value= current_node.nodeValue;
					}					
					
					//Extract the information from the current child node
					type_name = current_node.getAttribute('typeName');
					switch(type_name)
					{
						case 'type':
							type = current_node.firstChild.nodeValue;
							break;
						case 'location_description':
							location = current_node.firstChild.nodeValue;
							break;
						default:
							//object_details += "<p>" + field_name + " : " + field_value + "</p>";
							break;
					}

				}
			
			}
		
			//Build up the rec_loc details text to display
			rec_loc_details += '<div style="width : 220px"><h2>' + type + '</h2>';
			rec_loc_details += "<p>" + location + "</p>";
			rec_loc_details += "<p>&nbsp;</p>";
			rec_loc_details += '</div">';
			
		}
		catch(err)
		{
			alert('An error occured : ' + err.description);
		}
		
	}
	
	//Return the recycling location details to display
	return rec_loc_details;
	
}
function get_default_object_details(object_name, default_node)
{
	var object_details = "";
	var current_node; 
	var type_name;
	var field_name; 
	var field_value;
	var node_count = 0; 
	
	//Ensure a valid ward node has been received
	if(default_node)
	{
	
		//Get the type of object we are deailing with (from the parent node)
		object_details += "<h2>" + object_name + "</h2>";
	
		//Extract the ward details
		for (node_count = 0; node_count < default_node.childNodes.length; node_count++) 
		{

			//Get the current node 
			current_node = default_node.childNodes[node_count];

			//Ensure we are dealing with a 'property' node
			if(current_node.nodeName == 'property')
			{
				//Get the field name 
				field_name = current_node.getAttribute("typeName"); 	
				//Get the field value 
				if(current_node.firstChild)
				{
					field_value= current_node.firstChild.nodeValue;
				}
				else
				{
					field_value= current_node.nodeValue;
				}

				//Build up the html description of the object
				object_details += "<p>" + field_name + " : " + field_value + "</p>";
			}
			
		}
		
	}
	
	//Return ward details to display
	return object_details;
	
}




function get_parent_object_type(child_node)
{
	var parent_node; 
	var object_type = "";
	
	//Ensure a valid child node has been supplied.
	if(child_node)
	{
		//Get a handle on the parent node	
		parent_node = child_node.parentNode;
		if(parent_node)
		{
		
			//Get the parent node's object type
			object_type = parent_node.getAttribute('typeName');
			
		}
	}
	
	//Return the object type 
	return object_type; 
	
}

function load_url(request_url, asynchronous, result_handler, error_handler)
{
	var oXMLHttpRequest;
	var return_text = ""; 
	
	oXMLHttpRequest	= new XMLHttpRequest;
	
	//Get any road fault details (in a synchronous manner)
	oXMLHttpRequest.open("GET", OpenLayers.ProxyHost + request_url, asynchronous);
	oXMLHttpRequest.onreadystatechange	= function()
	{
		//Has the request finished?
		if(oXMLHttpRequest.readyState == 4)
		{
			//Was the request successful?
			if(oXMLHttpRequest.status == 200)
			{
				//new_show_results(oXMLHttpRequest.responseText);
				return_text = oXMLHttpRequest.responseText; 				
				result_handler(oXMLHttpRequest.responseText);
			}
			else
			{
				//Show error results if required
				if(error_handler)
				{
					error_handler(oXMLHttpRequest.status, oXMLHttpRequest.statusText);
				}
			}
		}
	};
	oXMLHttpRequest.send();	

	return return_text;
}

function show_query_error(error_code, error_text)
{
	alert(error_code + " : " + error_text);
}

function get_popups_layer(responseText)
{
	//Declare local variables
	var popups_layer; 
	
	try
	{

		//Check to see if the results layer is already loaded
		popups_layer = map.getLayersByName("Results")[0];	
		if(popups_layer) 
		{
					
			//Clear out any existing popups 
			popups_layer.clearFeatures();

			//Clear out any existing markers
			popups_layer.clearMarkers();

			//alert("Redrawing Layer...");				
			popups_layer.redraw(); 
		
			//Layer already exists so remove it
			map.removeLayer(popups_layer);
			
		}
		
		//Re-create the layer afresh
		popups_layer = create_lookup_results_layer(responseText); 
			

		
	}
	catch(e)
	{
		alert("Problem with get_popups_layer() - " + e.description);
		
	}		
	
	//Return the layer to show the popups
	return popups_layer;
}
