// Global variables
var fldOrganisation;
var fldProperty;
var fldStreet;
var fldLocality;
var fldTown;
var fldCounty;
var fldPostCode;

// Display the popup address lookup window
function PopupAddress()
{
	//debugger;
	
	// Check for the right number of arguments
	// Oragnisation, property, street, town, county, post code
	if (arguments.length < 7)
	{
		alert("popupAddress() : Wrong number of arguments.");
		return void(0);
	}
	
	try
	{
		// Store the field names here
		fldOrganisation = PopupAddress.arguments[0];
		fldProperty = PopupAddress.arguments[1];
		fldStreet = PopupAddress.arguments[2];
		fldLocality = PopupAddress.arguments[3];
		fldTown = PopupAddress.arguments[4];
		fldCounty = PopupAddress.arguments[5];
		fldPostCode = PopupAddress.arguments[6];
		
		// Get the actual field elements
		if (fldOrganisation != "") fldOrganisation = document.getElementById(fldOrganisation);
		if (fldProperty != "") fldProperty = document.getElementById(fldProperty);
		if (fldStreet != "") fldStreet = document.getElementById(fldStreet);
		if (fldLocality != "") fldLocality = document.getElementById(fldLocality);
		if (fldTown != "") fldTown = document.getElementById(fldTown);
		if (fldCounty != "") fldCounty = document.getElementById(fldCounty);
		if (fldPostCode != "") fldPostCode = document.getElementById(fldPostCode);

		// Build the seeded address
		var seedAddress = "";
		seedAddress += (("object" == typeof(fldOrganisation)) && ("" != fldOrganisation.value))?(("" != seedAddress)?", " + fldOrganisation.value:fldOrganisation.value):"";
		seedAddress += (("object" == typeof(fldProperty)) && ("" != fldProperty.value))?(("" != seedAddress)?", " + fldProperty.value:fldProperty.value):"";
		seedAddress += (("object" == typeof(fldStreet)) && ("" != fldStreet.value))?(("" != seedAddress)?", " + fldStreet.value:fldStreet.value):"";
		seedAddress += (("object" == typeof(fldLocality)) && ("" != fldLocality.value))?(("" != seedAddress)?", " + fldLocality.value:fldLocality.value):"";
		seedAddress += (("object" == typeof(fldTown)) && ("" != fldTown.value))?(("" != seedAddress)?", " + fldTown.value:fldTown.value):"";
		seedAddress += (("object" == typeof(fldCounty)) && ("" != fldCounty.value))?(("" != seedAddress)?", " + fldCounty.value:fldCounty.value):"";
		seedAddress += (("object" == typeof(fldPostCode)) && ("" != fldPostCode.value))?(("" != seedAddress)?", " + fldPostCode.value:fldPostCode.value):"";
		
		// Open the address lookup window
		// Body pos is 1,1 so width and height accomodate this
		OpenAddressWindow(510, 480, seedAddress);
	}
	catch(e)
	{
		alert("PopupAddress() : Unable to launch the address search dialog.\n" + e);
	}
}

function OpenAddressWindow(iWidth, iHeight, seedAddress)
{
	try
	{
		// See if we have a seed address
		var queryString = "";
		if ("" != seedAddress) queryString = "?Addr=" + escape(seedAddress);
		// Open window	
	    popAddrWin = DisplayPopupWindow("Controls/AddressLookup/AddressLookup.aspx" + queryString, "AddressLookup", iWidth, iHeight);
	}
	catch(e)
	{
		alert("OpenAddressWindow() : Unable to launch the address seach dialog.\n" + e);
	}
}

function CloseAddressWindow()
{
	window.close();
}

function PopupSelectAddress()
{
	// Check for the right number of arguments
	// Oragnisation, property, street, town, county, post code
	if (arguments.length < 7)
	{
		alert("PopupSelectAddress() : Wrong number of arguments.");
		return void(0);
	}

	try
	{
		// Store the field values here
		var selOrganisaton = PopupSelectAddress.arguments[0];
		var selProperty = PopupSelectAddress.arguments[1];
		var selStreet = PopupSelectAddress.arguments[2];
		var selStreet2 = PopupSelectAddress.arguments[3];
		var selTown = PopupSelectAddress.arguments[4];
		var selCounty = PopupSelectAddress.arguments[5];
		var selPostCode = PopupSelectAddress.arguments[6];

		// Assign the values to the callers elements
		if ("object" == typeof(window.opener.fldOrganisation)) window.opener.fldOrganisation.value = selOrganisaton;
		if ("object" == typeof(window.opener.fldProperty)) window.opener.fldProperty.value = selProperty;
		if ("object" == typeof(window.opener.fldStreet)) window.opener.fldStreet.value = selStreet;
		if ("object" == typeof(window.opener.fldLocality)) window.opener.fldLocality.value = selStreet2;
		if ("object" == typeof(window.opener.fldTown)) window.opener.fldTown.value = selTown;
		if ("object" == typeof(window.opener.fldCounty)) window.opener.fldCounty.value = selCounty;
		if ("object" == typeof(window.opener.fldPostCode)) window.opener.fldPostCode.value = selPostCode;
		
		// Close this window
		CloseAddressWindow();
	}
	catch(e)
	{
		alert("OpenAddressWindow() : Error.\n" + e);
	}
}
