var bSelectUseSelect = false;
function onXMLSearchCancel()
{
	RemoveFormContainer(this.eContainer)
}

function elFirstChild(n)
{
	var o = n.firstChild;
	while (o && o.nodeType!=1)
		o = o.nextSibling;
	return o;
}

function elNextSibling(n)
{
	var o = n.nextSibling;
	while (o && o.nodeType!=1)
		o = o.nextSibling;
	return o;
}

function onXMLShowAddress()
{
	var node = this.node;
	var sURL = "Pin=" + node.getAttribute("Pin");
	var sCounty = node.getAttribute("County");
	if (sCounty && sCounty.length>0)
		sURL += "&County=" + sCounty;

	PostFindData(sURL);
	RemoveFormContainer(this.parentNode.eContainer);
}

function onXMLSearchChange()
{
	var sel = this.selectedIndex;
	if (sel>0)
	{
		var node = this.options[this.selectedIndex].node;
		RemoveFormContainer(this.eContainer)
	}
}

function onXMLSelectMouseOver() { this.style.color = "red"; }
function onXMLSelectMouseOut() { this.style.color = ""; }


function prepareMultiMatch()
{
	var eMM = document.createElement("DIV");
	eMM.className = "AddressList";
	eMM.style.margin = "4px 8px";

	return eMM;
}

function addToMultiMatch(eMM, curnode, fClick, sLine)
{
	var div = document.createElement("DIV");
	div.style.cursor = "pointer";
	div.style.fontsize = "smaller";
	div.onmouseover = onXMLSelectMouseOver;
	div.onmouseout = onXMLSelectMouseOut;

	div.node = curnode;
	div.onclick = fClick;

	for (var i=3; i<arguments.length; i++)
	{
		if (i>3)
			div.appendChild(document.createElement("BR"));

		div.appendChild(document.createTextNode(arguments[i]));
	}

	eMM.appendChild(div);
}

function displayMultiMatch(eMM)
{
	var eDiv = document.createElement("DIV");

	eDiv.appendChild(document.createTextNode("Multiple matches.  Select from following list:"));
	eDiv.appendChild(document.createElement("BR"));

	eDiv.appendChild(eMM);
	eDiv.appendChild(document.createElement("BR"));

	var eButton = document.createElement("BUTTON");
	eButton.onclick = onXMLSearchCancel;
	eButton.appendChild(document.createTextNode("Cancel"));
	eDiv.appendChild(eButton);

	eButton.eContainer = eMM.eContainer = MakeDialog("Multiple Matches", eDiv, 300);
	eButton.focus();
}

function OnClickBlock()
{
	var node = this.node;
	var sBlock = node.parentNode.getAttribute("bid");
	var sCounty = node.getAttribute("fips");
	PostFindData("Pin=" + sBlock + "&County=" + sCounty);
	RemoveFormContainer(this.parentNode.eContainer);
}

function ShowBlockResults(nodeBlocks)
{
	var eMM = prepareMultiMatch();
	var bid = nodeBlocks.getAttribute("bid");
	var fips = Number(nodeBlocks.firstChild.getAttribute("fips"));
	var sBlock = ReDashPin(bid,fips);
	var node = nodeBlocks.firstChild;
	while (node)
	{
		if (node.nodeType==1)
		{
			var sLegend = "Block " + sBlock + " in " + node.getAttribute("CountyName") + " county.";
			addToMultiMatch(eMM, node, OnClickBlock, sLegend);
		}

		node = node.nextSibling;
	}

	displayMultiMatch(eMM);
}


function prepareSelect()
{
	var eSel;
	if (bSelectUseSelect)
	{
		eSel = document.createElement("SELECT");
		eSel.onchange = onXMLSearchChange;
	}
	else
	{
		eSel = document.createElement("DIV");
		eSel.className = "AddressList";
		eSel.style.margin = "4px 8px";
	}

	return eSel;
}

function addSelectOption(sAddress, eSelect, curnode)
{
	if (bSelectUseSelect)
	{
		opt = new Option(sAddress, "", false, false);
		eSelect.options[eSelect.options.length] = opt;
		opt.node = curnode;

		eSelect.size = eSelect.options.length;
	}
	else
	{
		var nFips = Number(curnode.getAttribute("County"));
		var sPin = curnode.getAttribute("Pin");
		var sPin = ReDashPin(sPin,nFips);
		var div = document.createElement("DIV");
		div.onclick = onXMLShowAddress;
		div.style.cursor = "pointer";
		div.style.fontsize = "smaller";
		div.onmouseover = onXMLSelectMouseOver;
		div.onmouseout = onXMLSelectMouseOut;
		div.node = curnode;
		div.appendChild(document.createTextNode(sAddress));
		div.appendChild(document.createElement("BR"));
		div.appendChild(document.createTextNode("(" + sPin + ")"));

		eSelect.appendChild(div);
	}
}

function ShowXMLGeocode(node)
{
	UpdateResults(node);
	oMapObj.getByLatLon(node.getAttribute("Lat"), node.getAttribute("Lon"));
	showPrintPageButton();
}

function ShowXMLRowResults(doc)
{
	var eSelect, opt;
	var sCounty, sPin, sAddress, sLat, sLon, nodeRow;
	var nRows = 0;

	var node = doc.firstChild;
	while (node)
	{
		if (node.nodeType==1 && node.nodeName=="row")
		{
			// process prev. node:
			if (nRows)
			{
				if (!eSelect)
					eSelect = prepareSelect();

				addSelectOption(sAddress, eSelect, nodeRow);
			}

			nRows++; 
			sCounty = node.getAttribute("County");
			sPin = node.getAttribute("Pin");
			sAddress = node.getAttribute("Addr");
			sLat = node.getAttribute("Lat");
			sLon = node.getAttribute("Lon");
			nodeRow = node;
		}

		node = node.nextSibling;
	}

	switch(nRows)
	{
		case 0:
			alert("Empty search result list.");
			break;
		case 1:
			UpdateResults(nodeRow);
//			var sMsg = sAddress + "\nPIN: " + sPin + "\nLatitude: " + sLat + "\nLongitude: " + sLon;
			if (nodeRow.hasChildNodes())
			{
				oMapObj.updateInfoXML(nodeRow.firstChild);
				showPrintPageButton();
			}
			else
			{
				oMapObj.getByLatLon(Number(sLat), Number(sLon));
				showPrintPageButton();
			}

			break;
		default:
			addSelectOption(sAddress, eSelect, nodeRow);

			var eButton = document.createElement("BUTTON");
			eButton.onclick = onXMLSearchCancel;
			eButton.appendChild(document.createTextNode("Cancel"));

			var eDiv = document.createElement("DIV");

			eDiv.appendChild(document.createTextNode("Multiple matches.  Select from following list:"));
			eDiv.appendChild(document.createElement("BR"));

			eDiv.appendChild(eSelect);
			eDiv.appendChild(document.createElement("BR"));

			eDiv.appendChild(eButton);

			eButton.eContainer = eSelect.eContainer = MakeDialog("Multiple Matches", eDiv, 300);
			eButton.focus();
			break;
	}
}

function AlertNewSearchXML()
{
	var xmlDoc, docEl, node, nNodes;
	var nCounty, sPin, sAddress, nLat, nLon;

	if (oNewSearchForm.oReq.readyState==4)
	{
		RemoveSearchForm();

		xmlDoc = oNewSearchForm.oReq.responseXML;
		docEl = xmlDoc.documentElement;
		node = docEl.firstChild;

		if (node==null)
			alert("Search returned no results.");
		else
		{
			switch(node.nodeName)
			{
				case "Error":
					alert(node.firstChild.data);
					break;
				case "GeoCodeList":
					oMapObj.setRegion(Number(docEl.getAttribute("region")));
					ShowXMLGeocode(node.firstChild);
					break;
				case "PinSearch":
					ShowXMLPinSearchResults(node);
					break;
				case "Blocks":
					debugger;
					break;
				case "row":
					ShowXMLRowResults(docEl);
					break;
			}
		}
	}
}

function PostXMLSearch()
{
	var sPostData = oNewSearchForm.getPostData();
	var oReq = oNewSearchForm.oReq = GetXMLReq();
	var sURL = oNewSearchForm.eForm.action;

	oReq.open("POST", sURL, true); 
	oReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	oReq.setRequestHeader("Accept", "text/xml");
	oReq.setRequestHeader("Cache-Control", "no-cache");
	oReq.onreadystatechange = oNewSearchForm.fOnReady;
	oReq.send(sPostData);

	return false;
}


function DoXMLSearch(fOnReady)
{
	if (oNewSearchForm==null)
	{
		oNewSearchForm = new FormObject(CreateSearchFormArray(), "Scripts/Server/GetFindData.asp", "post", null, RemoveSearchForm);
		oNewSearchForm.setSubmit("Search", PostXMLSearch);

		var eForm = oNewSearchForm.makeForm("Address");
		oNewSearchForm.eForm = eForm;
		oNewSearchForm.fOnReady = fOnReady;
	}

	oNewSearchForm.eContainer = MakeFormContainer("Search for Property", oNewSearchForm.eForm);
}



