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 = '';

function refresh_page()
{
	document.location.replace(document.location.href);
}

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_element_position(element)
{
	var top = 0;
	var left = 0;

	if(element.offsetParent)
	{
		left = element.offsetLeft;
		top = element.offsetTop;

		while(element = element.offsetParent)
		{
			left += element.offsetLeft;
			top += element.offsetTop;
		}
	}

	return [left, top];
}

function get_cursor_positions(event)
{
	event = event || window.event;

	var cursor = {x:0, y:0};

	if (event.pageX || event.pageY)
	{
		cursor.x = event.pageX;
		cursor.y = event.pageY;
	}
	else
	{
		cursor.x = event.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft) - document.documentElement.clientLeft;
		cursor.y = event.clientY + (document.documentElement.scrollTop || document.body.scrollTop) - document.documentElement.clientTop;
	}

	return [cursor.x, cursor.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;
			}
		}
	}
}

function openpopup(popurl){
var winpops=window.open(popurl,"","width=800px,height=600px,toolbar,location,status,scrollbars,menubar,resizable")
}

var hide_timer;
var scroll_timer;
var current_popup = false;

function start_scroll(element_id, direction)
{
	scroll_timer = setInterval("scroll_element('" + element_id + "', '"+direction+"')", 10);
}

function stop_scroll()
{
	clearInterval(scroll_timer);
}

function scroll_element(element_id, direction)
{
	var element = document.getElementById(element_id);

	if (direction == "up") 
	{
		var new_top = element.scrollTop - 2;
		if (new_top < 0) 
		{
			new_top = 0;
			clearInterval(scroll_timer);
		}
	}
	else 
	{
		var new_top = element.scrollTop + 2;
		if (new_top > element.offsetHeight) 
		{
			new_top = element.offsetHeight;
			clearInterval(scroll_timer);
		}
	}

	element.scrollTop = new_top;
}

function show_faq(event, faq_answer_id, type)
{
	if(current_popup !== false)
	{
		hide_faq_now();
	}

	current_popup = "faq_popup_" + type;

	var faq_popup = document.getElementById("faq_popup_" + type);
	var faq_popup_content = document.getElementById("faq_popup_" + type + "_content");
	var faq_answer = document.getElementById("faq_" + faq_answer_id);

	faq_popup_content.innerHTML = faq_answer.innerHTML;
	faq_popup_content.scrollTop = 0;

	var mouse_position = get_cursor_positions(event);

	if(type == "left")
	{
		var new_left = (mouse_position[0] - (faq_popup.offsetWidth / 2) + 200);
	}
	else
	{
		var new_left = (mouse_position[0] - (3 * (faq_popup.offsetWidth / 2)) + 225);
	}

	var new_top = (mouse_position[1] - faq_popup.offsetHeight - (faq_popup.offsetHeight / 2) + 175);

	faq_popup.style.left = new_left + "px";
	faq_popup.style.top =  new_top + "px";

	faq_popup.style.visibility = "visible";
}

function clear_faq_timeout()
{
	clearTimeout(hide_timer);
}

function hide_faq_now()
{
	var faq_popup = document.getElementById(current_popup);

	faq_popup.style.visibility = "hidden";

	current_popup = false;
}

function hide_faq(delayed)
{
	clear_faq_timeout();

	if(delayed === true)
	{
		hide_timer = setTimeout("hide_faq_now()", 1000);
	}
	else
	{
		hide_faq_now();
	}
}

function cancel_bubble(event)
{
	if(navigator.userAgent.indexOf("MSIE") >= 0)
	{
		window.event.cancelBubble = true;
		window.event.returnValue = false;
	}
	else
	{
		event.stopPropagation();
		event.preventDefault();
	}
}