function get_browser()
{
	if(document.all)
	{
		return "IE";
	}
	else if(document.getElementById)
	{
		return "NN";
	}
}

// get the browser type
var browser = get_browser();

function getXmlHttpRequestObject()
{
	if (window.XMLHttpRequest)
	{
		return new XMLHttpRequest();
	}
	else if(window.ActiveXObject)
	{
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
	else
	{
		return false;
	}
}

var xml_request = getXmlHttpRequestObject();

function load_xml_from_string(string)
{
	// code for IE
	if (window.ActiveXObject)
	{
		var doc = new ActiveXObject("Microsoft.XMLDOM");
		doc.async = "false";
		doc.loadXML(string);
	}
	// code for Mozilla, Firefox, Opera, etc.
	else
	{
		var parser = new DOMParser();
		var doc = parser.parseFromString(string, "text/xml");
	}
	
	return doc;
}

var ModalDialogWindow;
var ModalDialogInterval;
var ModalDialog = new Object;

ModalDialog.value = '';
ModalDialog.eventhandler = '';

var refreshing = false;

function refresh_page(){
	if (refreshing == false) {
		var url = document.location.href;
		refreshing = true;
		
		if (url.substr(url.length - 1) == "#") {
			url = url.substr(0, url.length - 1);
		}
		
		document.location.replace(url);
	}
}

function ModalDialogMaintainFocus()
{
	try
	{
		if (ModalDialogWindow.closed)
		{
			window.clearInterval(ModalDialogInterval);
			eval(ModalDialog.eventhandler);
			return;
		}

//			ModalDialogWindow.focus();
	}
	catch (everything)
	{

	}
}

function ModalDialogRemoveWatch()
{
	ModalDialog.value = '';
	ModalDialog.eventhandler = '';
}

function ModalDialogShow(page_to_show, do_refresh)
{
	var do_refresh = (do_refresh == null) ? true : do_refresh;

	ModalDialogRemoveWatch();

	if(do_refresh === true)
	{
		ModalDialog.eventhandler = "refresh_page()";
	}

	var args='width=800,height=610,left=325,top=300,toolbar=0,';
	args += 'location=0,status=0,menubar=0,scrollbars=1,resizable=0'; 

	ModalDialogWindow=window.open("","",args);
	ModalDialogWindow.document.open('');
	ModalDialogWindow.document.location = page_to_show;
	ModalDialogWindow.document.close();
	ModalDialogWindow.focus();
	ModalDialogInterval = window.setInterval("ModalDialogMaintainFocus()", 5);
}

function get_cursor_positions(event)
{
	var x, y;

	if(browser == "IE")
	{
		x = window.event.clientX + document.documentElement.scrollLeft + document.body.scrollLeft;
		y = window.event.clientY + document.documentElement.scrollTop + document.body.scrollTop;
	}
	else if(browser == "NN")
	{
		x = event.clientX + window.scrollX;
		y = event.clientY + window.scrollY;
	}
	
	return new Array(x, y)
}

function hide_window(item_id)
{
	if(document.getElementById(item_id))
	{
		document.getElementById(item_id).style.visibility = "hidden";
	}
}

function show_window(event, item_id)
{
	var cursor_position = get_cursor_positions(event);

	if(document.getElementById(item_id))
	{
		var the_window = document.getElementById(item_id);

		the_window.style.visibility = "visible";
		the_window.style.left = cursor_position[0] + "px";
		the_window.style.top = cursor_position[1] + 20 + "px";
	}
}

function hide_help(destination_id)
{
	if(document.getElementById(destination_id))
	{
		document.getElementById(destination_id).innerHTML = "";
	}
}

function show_help(item_id, destination_id)
{
	if(document.getElementById(item_id) && document.getElementById(destination_id))
	{
		document.getElementById(destination_id).innerHTML = document.getElementById(item_id).innerHTML;
	}
}

function find_address()
{
	// get the postcode
	var postcode = document.getElementById('search_postcode').value;

	get_postcode_addresses(postcode);
}

function get_postcode_addresses(postcode)
{
	if (xml_request.readyState == 4 || xml_request.readyState == 0)
	{
		xml_request.open("GET", "find_address.php?postcode=" + escape(postcode), false);
		xml_request.send(null);

		// get the address list
		var address_list = document.getElementById('address_list');

		// get the xml document from the returned xml string
		var doc = xml_request.responseXML;

		var root_node = doc.documentElement;
		var child_nodes = root_node.childNodes;

		// empty the current list
		address_list.options.length = 0;
		
		// add a "please select address" option as default selected
		address_list.options[0] = new Option("Please select an address...", "");
		
		// loop through all the addresses
		var options_count = 1;
		
		for(var i = 0; i < child_nodes.length; i++)
		{
			var node = child_nodes[i];
			
			if(node.nodeType == 1)
			{
				var id = node.getElementsByTagName('id');
				var description = node.getElementsByTagName('description');

				// add an entry to the address list
				address_list.options[options_count] = new Option(description[0].firstChild.nodeValue, id[0].firstChild.nodeValue);
				options_count++;
			}
		}
	}
}

function select_address()
{
	// get the currently selected address
	var current_address = document.getElementById('address_list').selectedIndex;
	var address_id = document.getElementById('address_list').options[current_address].value;
	
	if(address_id != "")
	{	
		get_address_from_id(address_id);
	}
}

function clear_address_fields()
{
	var fields_to_clear = new Array("building_name_or_number", "sub_building_name", 
									"thoroughfare_name_descriptor", "dependent_locality", 
									"post_town", "county", "postcode");

	for(var i = 0; i < fields_to_clear.length; i++)
	{
		var current_field = fields_to_clear[i];
		if(document.getElementById(current_field))
		{
			document.getElementById(current_field).value = "";
		}
	}
}

function get_address_from_id(address_id)
{
	if (xml_request.readyState == 4 || xml_request.readyState == 0)
	{
		xml_request.open("GET", "get_address.php?address_id=" + escape(address_id), false);
		xml_request.send(null);

		var doc = xml_request.responseXML;

		var root_node = doc.documentElement;
		var child_nodes = root_node.childNodes;

		// clear the current address fields
		clear_address_fields();

		// loop through all the child nodes and replace the content into the fields on the page
		for(var i = 0; i < child_nodes.length; i++)
		{
			var node = child_nodes[i];

			if(node.nodeType == 1)
			{
				if(node.nodeName == "id")
				{
					document.getElementById("paf_reference").value = node.firstChild.nodeValue;
				}
				else if(node.nodeName == "thoroughfare_name")
				{
					document.getElementById("thoroughfare_name_descriptor").value = node.firstChild.nodeValue;
				}
				else if(node.nodeName == "thoroughfare_descriptor")
				{
					document.getElementById("thoroughfare_name_descriptor").value += " " + node.firstChild.nodeValue;
				}
				else if(node.nodeName == "organisation_name")
				{
					var organisation_name = node.firstChild.nodeValue;
				}
				else
				{
					if(document.getElementById(node.nodeName))
					{
						document.getElementById(node.nodeName).value = node.firstChild.nodeValue;
					}
				}
			}
		}
		
		// check if the paon has been filled in...if not then fill with organisation name
		if(document.getElementById('building_name'))
		{
			if(document.getElementById('building_name').value == "")
			{
				document.getElementById('building_name').value = organisation_name;
			}
		}
	}
}
