/* #############################################################################################################################

	Project	: NRT.Web.CustomModules.Property

	Summary	: This file contains all the scripts to handle the entire client side functionality for property search page(s)

	RevisionHistory: 
	-----------------------------------------------------------------------------------------------------------------------------
	Date		Name		Description
	-----------------------------------------------------------------------------------------------------------------------------

	09/15/2005	ksundar		Created

###############################################################################################################################*/

/*
	--------------------------------------------------------------------------------------
									Global Variables
	--------------------------------------------------------------------------------------
*/

var _sSummaryListboxID = "lstSummary";	

var _divRadiusSearchMap			= document.getElementById("div_RadiusSearch");
var _txtListingCount			= document.getElementById("txtListingCount");

var _txt_RadiusSearch_City		= document.getElementById("txt_RadiusSearch_City");
var _ddl_RadiusSearch_State		= document.getElementById("ddl_RadiusSearch_State");
var _txt_RadiusSearch_Zip		= document.getElementById("txt_RadiusSearch_Zip");

//var _divLeftHeaderText_City		= document.getElementById("divLeftHeaderText_City");
var _divRightHeaderText_City	= document.getElementById("divRightHeaderText_City"); 
//var _divLeftHeaderText_Radius	= document.getElementById("divLeftHeaderText_Radius");
var _divRightHeaderText_Radius	= document.getElementById("divRightHeaderText_Radius");
//var _divLeftHeaderText_General = document.getElementById("divLeftHeaderText_General");
var _divRightHeaderText_General = document.getElementById("divRightHeaderText_General");

var _div_SchoolDistrictSearch_SearchLabel = document.getElementById("div_SchoolDistrictSearch_SearchLabel");
var _div_MLSAreaSearch_SearchLabel = document.getElementById("div_MLSAreaSearch_SearchLabel");

var _ifrm_RadiusSearch_Map = document.getElementById("ifrm_RadiusSearch_Map");
var _div_RadiusSearch_InfoText = document.getElementById("div_RadiusSearch_InfoText");

var _ifrm_CitySearch_Map = document.getElementById("ifrm_CitySearch_Map"); 
var _div_CitySearch_ResizeMapLink = document.getElementById("div_CitySearch_ResizeMap");
var _div_RadiusSearch_ResizeMapLink = document.getElementById("div_RadiusSearch_ResizeMap");
var _div_CitySearch_MapPointLink = document.getElementById("div_CitySearch_MapPointLink");

var _sCityCountyList = document.getElementById("hdnMapCityCountyList").value;
var _sLastNodeName = document.getElementById("hdnLastNodeName").value;

var _lstCities = document.getElementById("lst_SearchOptions_City_Cities_ctl02");		

//var _divMoreSearchOptionsLink = document.getElementById("divMoreSearchOptionsLink");
var _divMoreSearchOptions = document.getElementById("div_MoreSearchOptions");

var _sRadiusSearchSource = "";
var _sRadiusSearchStreet = "";
var _sRadiusSearchCity = "";
var _sRadiusSearchState = "";
var _sRadiusSearchZip = "";
var _nRadiusSearchMiles = 0;
var _sCityStateZip = "";

var _currIFrame = null;
var _sLastIFrameSource = null;

var _nSortValue = 0;
var _sCompleteCityListHtml = null;
var searchXMLFF = null;

/*
	--------------------------------------------------------------------------------------
								Global function pointers
	--------------------------------------------------------------------------------------
*/

this.document.body.onkeyup = function(e)
{
	if(!e)
		e = window.event;
	
	//	Call RemoveSearch() when the Delete key is pressed
	if(e.keyCode == 46)
	{
		RemoveSearch(false);
	}
}

String.prototype.trim = function(trimChar)
{
	var rExpBegin = new RegExp("^" + trimChar + "+", "g");
	var rExpEnd = new RegExp(trimChar + "+$", "g");
	
	return this.replace(rExpBegin, '').replace(rExpEnd, '')
}

String.prototype.remove = function(removeString)
{	
	var oRegex = new RegExp(removeString + "[,]?", "g");		
	return this.replace(oRegex, '');			
}

String.prototype.removeSpaces = function()
{
	return this.replace(/\s+/g, ""); 
}

function LoadMap(iframeWindow, mode)
{
	_currIFrame = iframeWindow;
	
	if(mode == "city")
	{
		_currIFrame.src = _sLastIFrameSource;
		
		document.getElementById("hl_CitySearch_GotoMapPointMap").style.display = "inline";
		document.getElementById("hl_CitySearch_GotoCityCountyMap").style.display = "none";				
	}
	else if(mode == "mappoint")
	{
		_sLastIFrameSource = _currIFrame.src;
		_currIFrame.src = "/custommodules/property/maps/loadmapstatus.html";
		
		document.getElementById("hl_CitySearch_GotoMapPointMap").style.display = "none";
		document.getElementById("hl_CitySearch_GotoCityCountyMap").style.display = "inline";
									
		SearchController.GetMapPointMap(SearchXml.xml, GetMapPointMap_Callback);		
	}
	else if(mode == "radius")
	{
		_sLastIFrameSource = _currIFrame.src;
		_currIFrame.src = "/custommodules/property/maps/loadmapstatus.html";	
						
		SearchController.GetMapPointMap(SearchXml.xml, GetMapPointMap_Callback);		
	}	
}

function GetMapPointMap_Callback(response)
{
  if(response != null && response.value != null)
  {
    if(response.value[0] == "")
    {
      _currIFrame.src = _sLastIFrameSource;
      document.getElementById("hl_CitySearch_GotoMapPointMap").style.display = "inline";
      document.getElementById("hl_CitySearch_GotoCityCountyMap").style.display = "none";                                         
    }
    else
    {
      if (response.value[0] == undefined)
      {
				_currIFrame.src = response.value.Locations[0]; //response.value[0];
      }
      else
      {
				_currIFrame.src = response.value[0]; //response.value[0];
      }     
      if (response.value[0] == undefined)
      {
				_txtListingCount.value = response.value.Locations[1]; //response.value[1];          
      }
      else
      {
				_txtListingCount.value = response.value[1]; //response.value[1];        
      }     
    }
  }
}

function LoadCities(regionID, countyID, areaName, stateID)
{
	_sCityIndexes = "";
	
	if(_sCompleteCityListHtml == null)
	{
		_sCompleteCityListHtml = _lstCities.outerHTML;		
		_sCompleteCityListHtml = _sCompleteCityListHtml.replace(/selected/gi, "");
	}
	
	var sSelectedCities = null;
	var xnCities = SearchXml.childNodes[0].selectSingleNode("Cities");
	if(xnCities != null)
		sSelectedCities = xnCities.xml;		
	else
		sSelectedCities = "";		
	
	if(regionID != 0)
	{
		SearchController.GetCityLookupByRegion(regionID, sSelectedCities, stateID, LoadCities_Callback);
	}
	else if(countyID != 0)
	{
		SearchController.GetCityLookupByCounty(countyID, sSelectedCities, LoadCities_Callback);
	}
	else if(areaName != '0')
	{
		if(areaName == "MinneapolisArea" || areaName == "StPaulArea")
		{
			xnCities = SearchXml.childNodes[0].selectSingleNode("Communities");
			
			if(xnCities != null)
				sSelectedCities = xnCities.xml;		
			else
				sSelectedCities = "";				
		}
	
		SearchController.GetCityLookupByArea(areaName, sSelectedCities, LoadCities_Callback);
	}	
	else if(regionID == 0 && countyID == 0 && areaName == '0')
	{
		_xnCityList = _xnMasterCityList;
			
		_lstCities.outerHTML = _sCompleteCityListHtml;
		_lstCities = document.getElementById('lst_SearchOptions_City_Cities_ctl02');
		
		if(sSelectedCities.length > 0)
			SearchController.GetSelectedCityIndexes(sSelectedCities, GetSelectedCityIndexes_Callback);		
	}
}

if(_xnMasterCityList == null)
	SearchController.GetCityLookup2('', LoadMasterCities_Callback);

var _xnCityList = null;
var _xnMasterCityList = null;

function GetSelectedCityIndexes_Callback(response)
{
	if(response != null && response.value != null)
	{
		var sSelectedIndexArray = response.value;
		
		if(sSelectedIndexArray == null)
			_lstCities.selectedIndex = -1;
		else
		{				
			for(var i=0; i<sSelectedIndexArray.length; i++)
			{
				var opt = _lstCities.options[sSelectedIndexArray[i]];
				if(opt != null)
					opt.selected = true;
			}
		}
	}
}

function LoadMasterCities_Callback(response)
{
	if(response != null && response.value != null)
	{
		var xdCities = response.value;
			
		_xnMasterCityList = xdCities.childNodes[0];
		_xnCityList = _xnMasterCityList;	
	}
}


function LoadCities_Callback(response)
{
	if(response != null && response.value != null)
	{
		var xdCities = response.value;
		 _xnCityList = xdCities.childNodes[0];		
		
		_lstCities.innerHTML = "";		
		var nCurrIndex;
		var i;
		for(i=0; i<_xnCityList.childNodes.length; i++)
		{
			if(spm_browserType() == "ie")
			{
				_lstCities.options[_lstCities.length] = new Option(_xnCityList.childNodes[i].text, _xnCityList.childNodes[i].getAttribute("id"), false, false);
					
				if(_xnCityList.childNodes[i].getAttribute("sel") != null)
				{
					nCurrIndex = _lstCities.length - 1;				
					_lstCities.options[nCurrIndex].selected = true;
				}
			}
			else
			{
			    //Firefox must increment by 2
			    if (spm_browserType() == "mo" && i>0){i++}; 
			    
			    if (_xnCityList.childNodes[i+1] != null)
			    {
			        _lstCities.options[_lstCities.length] = new Option(_xnCityList.childNodes[i+1].textContent, _xnCityList.childNodes[i+1].getAttribute("id"), false, false);
			        
			        if(_xnCityList.childNodes[i+1].getAttribute("sel") != null)
				    {
					    nCurrIndex = _lstCities.length - 1;				
					    _lstCities.options[nCurrIndex].selected = true;
				    }
			    }			
			}
		}	
	}
}

var forceIn = false;  // Used To Force Into FillListValues

function SimpleListBoxOnEnterTextCheck(textBoxID, listBoxID, evt)
{
	var lstMyList = document.getElementById(listBoxID);
	var txtMyText = document.getElementById(textBoxID);

    if (evt.keyCode==13)
    {
		// Check if there is anything in the text box, if not, don't do anything
		if (txtMyText.value.length > 0)
		{
			// Check If Something is Selected
			var idx = lstMyList.selectedIndex;
			if(idx >= 0)
			{
				// Force Into Function
				forceIn = true;
				
				// Refresh List Now
				FillListValues(lstMyList, evt);
				
				// Turn Off Force In
				forceIn = false;
			}
		}
	}
}

var onChangeFired = false;
var firstTimeIn = true;
function FillListValues(CONTROL,evt)
{
	if(evt.type == "change" || forceIn || evt.type == "click" )
	{
		
		// Do Only On Click
		if (evt.type == "click")
		{
			// See If More Than 1 Is Selected
			var counter = 0;
			var x = 0;
			while (x < CONTROL.options.length)
			{
				if (CONTROL.options[x].selected)
				{
					counter = counter + 1;
				} 
				if (counter > 1)
				{
					break;
				}
				x = x + 1;
			}
			
			if (!onChangeFired && CONTROL.selectedIndex >= 0 && counter == 1 && !firstTimeIn)
			{
				// Remove Last Element From List
				RemoveSearch(true);
			} 

			// Reset Change Fired and FirstTime in
			firstTimeIn = false;
			onChangeFired = false;
			
			// Done
			return;
		}
		
		// Change Fired If Not Forced
		if (!forceIn)
		{
			onChangeFired = true;
		}
		firstTimeIn = false;
	
		// Do All The Time
		var oParentNode = _xnCityList.selectSingleNode("city[@id='" + CONTROL.options[CONTROL.selectedIndex].value + "']");		
		if(oParentNode == null)
			return;
			
		UpdateSearchXMLWithMapInput("Cities", "Cities", "", CONTROL.options[CONTROL.selectedIndex].text, CONTROL.options[CONTROL.selectedIndex].value, oParentNode.getAttribute("par"), CONTROL.id);

		oParentNode = SearchXml.selectSingleNode("//Cities");
		if(oParentNode == null)
			return;

		for(var i=0; i<oParentNode.childNodes.length; i++)
		{
			var oChildNode = oParentNode.childNodes[i];
			
			var xnCity = _xnCityList.selectSingleNode("city[@id='" + oChildNode.getAttribute("value") + "']");
			if(xnCity != null)
			{
				var optCity = _lstCities.options[xnCity.getAttribute("pos")];
				if(optCity != null)
					optCity.selected = true;
			}								
		}		

		_nCurrListBoxIndex = -1;
			
	}
}

/*
	----------------------------------------------------------------------------------------------------------------------				
	Name	: UpdateSearchXMLWithMapInput	
	
	Purpose	: This function will 
			1. Update the search XML with region/county/city list selected via map
			2. Sends a Javascript RPC request to server
			3. Update the contents of SummaryList Control based on the latest search XML											
	----------------------------------------------------------------------------------------------------------------------
*/

function UpdateSearchXMLWithMapInput(nodeName, displayName, mapHtmlName, text, value, parentID, controlID)
{
	var oTempNode = null;
	var oTempNode1 = null;
	var oParentNode = null;	
	var oChildNode = null;	
	var script = null;
	
	/* If a drilldown county is clicked, then we need to load the next level map and update the search xml accordingly */
	if(mapHtmlName.length > 0)
	{	
		// Load the drilldown map
		if (spm_browserType() == "mo")
		{
		    script = "_ifrm_CitySearch_Map.src = '/custommodules/property/maps/" + mapHtmlName + ".html';"
            if (window.status != "complete")
            {
                setTimeout(script, 100);
            }
            else
            {
                setTimeout(script, 1);
            }
		}
		else
		{
		    _ifrm_CitySearch_Map.src = "/custommodules/property/maps/" + mapHtmlName + ".html";
		}
		
		
		//
		// REGION: If this is region drilldown, then add a "<Region> node		
		//
		if(nodeName == 'Region')
		{
		   //remove any existing counties, cities, communities...
            RemoveChildNodeByName(SearchXml.childNodes[0], "Counties");
           // RemoveChildNodeByName(SearchXml.childNodes[0], "Communities");	
            //RemoveChildNodeByName(SearchXml.childNodes[0], "Cities");	
            
		    oTempNode = AddParentNode("Region", displayName, "map", _ifrm_CitySearch_Map.src, "MapSearch", text, value);						
			
	        if(oTempNode != null)
	        { 
		        oTempNode.setAttribute("StateID", parentID);
	        }	
		}
		//
		// Index..we're back to the main map.....
		//
		else if(nodeName == 'Index')	
		{
		RemoveChildNodeByName(SearchXml.childNodes[0], "Communities");	
	    RemoveChildNodeByName(SearchXml.childNodes[0], "Cities");
	    RemoveChildNodeByName(SearchXml.childNodes[0], "Counties");
	    RemoveChildNodeByName(SearchXml.childNodes[0], "Region");
		}
		//
		// COUNTIES: If this is county drilldown.....
		//
		else if(nodeName == 'Counties')	
		{
			// Remove the current county's parent Region node from DOM
			oTempNode = SearchXml.selectSingleNode("//Region[@value='" + parentID + "']");
			if(oTempNode != null)
				SearchXml.childNodes[0].removeChild(oTempNode);
			
			// Search if there is any existing parent "Counties" node. (e.g) <Counties><child>....</Counties>
			// this node will be present only if the user has already clicked on a non-drilldown county in some map
			oTempNode = SearchXml.selectSingleNode("//Counties/child[0]");
			
			// If a county list is already present, then we need to add our current county as a child of the existing list
			if(oTempNode != null)
			{
				// pointer to parent <counties> node
				var oParentNode = SearchXml.selectSingleNode("//Counties");				
				
				// CHECK1: Before we add our current county to the county list, make sure it is not already present in there
				oChildNode = SearchXml.selectSingleNode("//Counties/child[@value='" + value + "']");				
				if(oChildNode == null)
				{
					// CHECK2: If the county is not already present, then check if there is any another county that belonged to the 
					//				  same region as our current county has already been added to the list or not?
					oChildNode = SearchXml.selectSingleNode("//Counties/child[@position='" + parentID + "']");				
					
					// if another county matching our current region is present, then remove it from the tree
					if(oChildNode != null)
						oParentNode.removeChild(oChildNode);
						
					// Add the current county as child county of existing Counties Node
					AddChildNode(oParentNode, _ifrm_CitySearch_Map.src, text, value, parentID);	
				}					
			}
			else
			{
				// Add as a parent county Node
				AddParentNode2("Counties", displayName, "map", _ifrm_CitySearch_Map.src, "MapSearch", text, "," + value + ",", parentID);			
			}
		}
		//
		// CITIES: If this is city drilldown.....into communities
		//
		else if(nodeName == 'Cities')	
		{
			// Remove the current county's parent Region node from DOM
			oTempNode = SearchXml.selectSingleNode("//Counties[@value='," + parentID + ",']");
			if(oTempNode != null)
				SearchXml.childNodes[0].removeChild(oTempNode);
			
			// Search if there is any existing parent "Counties" node. (e.g) <Counties><child>....</Counties>
			// this node will be present only if the user has already clicked on a non-drilldown county in some map
			oTempNode = SearchXml.selectSingleNode("//Cities/child[0]");
			
			// If a county list is already present, then we need to add our current county as a child of the existing list
			if(oTempNode != null)
			{
				// pointer to parent <counties> node
				var oParentNode = SearchXml.selectSingleNode("//Cities");				
				
				// CHECK1: Before we add our current county to the county list, make sure it is not already present in there
				oChildNode = SearchXml.selectSingleNode("//Cities/child[@value='" + value + "']");				
				if(oChildNode == null)
				{
					// CHECK2: If the county is not already present, then check if there is any another county that belonged to the 
					//				  same region as our current county has already been added to the list or not?
					oChildNode = SearchXml.selectSingleNode("//Cities/child[@position='" + parentID + "']");				
					
					// if another county matching our current region is present, then remove it from the tree
					if(oChildNode != null)
						oParentNode.removeChild(oChildNode);
						
						//alert("stop");
					// Add the current county as child county of existing Counties Node
					AddChildNode(oParentNode, _ifrm_CitySearch_Map.src, text, value, parentID);	
				}					
			}
			else
			{
				// Add as a parent county Node
				AddParentNode2("Cities", displayName, "map", _ifrm_CitySearch_Map.src, "MapSearch", text, "," + value + ",", parentID);			
			}			
		}
		
	}
	/* If a non drilldown county or city is clicked */	
	else
	{
		if(nodeName == 'Counties')
		{
			// Remove the current county's parent Region node from DOM		
			oTempNode = SearchXml.selectSingleNode("//Region[@value='" + parentID + "']");
			if(oTempNode != null)
				SearchXml.childNodes[0].removeChild(oTempNode);
			
			// Search for any existing parent "Counties" node
			oTempNode = SearchXml.selectSingleNode("//Counties/child[0]");
			
			// Search for any standalone "Counties" node			
			oTempNode1 = SearchXml.selectSingleNode("//Counties");
			
			// If there is no county node at all...then add the current county as the first parent county
			if(oTempNode == null && oTempNode1 == null)
			{
				oParentNode = AddParentNode("Counties", "", "map", _ifrm_CitySearch_Map.src, "MapSearch", null, null);	
				oParentNode.setAttribute("displayName", displayName);				
			}
			// else if a single/standalone County node is already present
			else if(oTempNode1 != null && oTempNode == null)
			{
				// Remove the standalone counties node from DOM
				SearchXml.childNodes[0].removeChild(oTempNode1);
			
				// Create a parent county node
				oParentNode = AddParentNode("Counties", "Counties", "map", _ifrm_CitySearch_Map.src, "MapSearch", null, null);	
				oParentNode.setAttribute("displayName", displayName);
				
				// Add the removed county as a child to this newly created parent county node
				AddChildNode(oParentNode, _ifrm_CitySearch_Map.src, oTempNode1.getAttribute("text"), oTempNode1.getAttribute("value"), oTempNode1.getAttribute("position"));			
			}
			
			// Pointer to parent county node
			if(oParentNode == null)
				oParentNode = SearchXml.selectSingleNode("//Counties");				
		}
		else if(nodeName == 'Cities')
		{
			//	
			// Remove the current city's parent county node from DOM		
			//
			
			var nodeName;
			
			if(_ifrm_CitySearch_Map.src.indexOf("maps/LakeMinnetonkaArea.html") >= 0 || _ifrm_CitySearch_Map.src.indexOf("maps/WhiteBearLakeArea.html") >= 0)
				nodeName = "Cities";
			else
				nodeName = "Counties";
				
			oTempNode = SearchXml.selectSingleNode("//" + nodeName + "[@value='," + parentID + ",']");
			if(oTempNode != null)
			{
				SearchXml.childNodes[0].removeChild(oTempNode);
			}
			else
			{
				var oRegionNode = SearchXml.selectSingleNode("//Region[@value='" + parentID + "']");
				if(oRegionNode != null)
				{
					SearchXml.childNodes[0].removeChild(oRegionNode);
				}
				else
				{				
					// Search for any existing parent "Counties" node	that has the current city's county as its child		
					oTempNode = SearchXml.selectSingleNode("//" + nodeName + "/child[@value='" + parentID + "']");
				
					// Search for any standalone "Counties" node
					oTempNode1 = SearchXml.selectSingleNode("//" + nodeName);
			
					// if the current city's parent county is already part of a county list, then remove it from list
					if(oTempNode != null && oTempNode1 != null)
						oTempNode1.removeChild(oTempNode);
				}
			}
			
			// pointer to cities parent node
			oParentNode = SearchXml.selectSingleNode("//Cities");
			
			// add the current city as part of cities child list
			if(oParentNode == null)
				oParentNode = AddParentNode("Cities", "Cities", "map", _ifrm_CitySearch_Map.src, "MapSearch", null, null);			
		}
		else if(nodeName == 'Communities')
		{
			//	
			// Remove the current city's parent county node from DOM		
			//
			
			oTempNode = SearchXml.selectSingleNode("//Cities[@value='," + parentID + ",']");
			if(oTempNode != null)
				SearchXml.childNodes[0].removeChild(oTempNode);
			else
			{
				// Search for any existing parent "Counties" node	that has the current city's county as its child		
				oTempNode = SearchXml.selectSingleNode("//Cities/child[@value='" + parentID + "']");
			
				// Search for any standalone "Counties" node
				oTempNode1 = SearchXml.selectSingleNode("//Cities");
		
				// if the current city's parent county is already part of a county list, then remove it from list
				if(oTempNode != null && oTempNode1 != null)
					oTempNode1.removeChild(oTempNode);
			}
			
			// pointer to cities parent node
			oParentNode = SearchXml.selectSingleNode("//Communities");
			
			// add the current city as part of cities child list
			if(oParentNode == null)
				oParentNode = AddParentNode("Communities", "Communities", "map", _ifrm_CitySearch_Map.src, "MapSearch", null, null);			
		}
		
		// Check if the currently clicked county/city is already in the list
		oChildNode = oParentNode.selectSingleNode("child[@value='" + value + "']");
		
		//
		//	The following section swaps the county/city node in and out of the DOM tree when a user clicks 
		//	and reclicks on the same county or city
		//
		
		// If not in list, then add as a child County Node							
		if(oChildNode == null)
		{		
			var pos = -1;
			
			var xnCity = _xnCityList.selectSingleNode("city[@id='" + value + "']");
			if(xnCity != null)
			{
				pos = xnCity.getAttribute("pos");
				
				var optCity = _lstCities.options[pos];
				if(optCity != null)
				{
					optCity.selected = true;
					//AddToSelectedCityList(xnCity.getAttribute("pos"));
				}
			}
			
			AddChildNode(oParentNode, _lstCities.id, text, value, pos, parentID);			
			
		}
		// If in list, then remove it from DOM
		else
		{
			oParentNode.removeChild(oChildNode);			
			
			var xnCity = _xnCityList.selectSingleNode("city[@id='" + value + "']");
			if(xnCity != null)
			{
				var optCity = _lstCities.options[xnCity.getAttribute("pos")];
				if(optCity != null)
				{
					optCity.selected = false;
					//AddToSelectedCityList(xnCity.getAttribute("pos"));
				}
			}			
		}
		
		//	Remove parent from DOM tre if it doesn't contain any childs				
		RemoveNodeIfEmpty(oParentNode);							
	}	
	
	//	Initiate the server RPC request	
	if(spm_browserType() == "ie")
		SearchController.GetSearchResultCount(SearchXml.xml, GetSearchResultCount_Callback);	
	else
		SearchController.GetSearchResultCount(SearchXml.childNodes[0].xml, GetSearchResultCount_Callback);				
	
	// Refresh Summary Box
	RefreshSummaryBox();		
}

/*
	--------------------------------------------------------------------------------------								
	Name	: UpdateSearch	
	
	Purpose	: This function will 
			1. Update the search XML
			2. Sends a Javascript RPC request to server
			3. Update the contents of SummaryList Control based on the latest search XML														
	--------------------------------------------------------------------------------------
*/

function UpdateSearch(nodeName, displayName, controlID, e)
{	
	var aTextValue;	
	var ctrlSource;
	var category;
	
	if(controlID != null)
		ctrlSource = document.getElementById(controlID);
	else
		ctrlSource = GetEventSource(e);
		
	if(ctrlSource == null || !IsValidEventSource(ctrlSource))
		return;
	
	if(ctrlSource.attributes["category"])
		category = ctrlSource.attributes["category"].value;	
	
	if(displayName == null || displayName.length == 0)
	{			
		//	dropdownlist
		if(ctrlSource.type == "select" || ctrlSource.type == "select-one")
		{		
			//	Add the parent Node if it doesn't exist
			AddParentNode(nodeName, "", ctrlSource.type, ctrlSource.id, category, "", ctrlSource.value);
		}
		else if(ctrlSource.type == "checkbox")
		{
			//	Add the parent Node if it doesn't exist
			AddParentNode(nodeName, "", ctrlSource.type, ctrlSource.id, category, "", ctrlSource.checked + "");
		}
		return;
	}
	
	// MULTI-SELECT LISTBOX | CHECKBOX
	if(CanHaveChildren(ctrlSource))
	{
		//	Add the parent Node is it doesn't exist
		var oParentNode = AddParentNode(nodeName, displayName, ctrlSource.type, null, category, null, null);
		
		//	ListBox (Multi-Select)
		if(ctrlSource.type == "select-multiple")
		{
			//	Remove all the child nodes and add childs based on the 
			//	current selected items
			AddChildNodes(oParentNode, ctrlSource, true);
		}			
		//	CheckBox
		else if(ctrlSource.type == "checkbox")
		{
			//	Get the text and value
			aTextValue = GetTextValue(ctrlSource);			
			
			// CHECKED
			if(ctrlSource.checked)
			{				
				//	Add this as a child node
				AddChildNode(oParentNode, ctrlSource.id, aTextValue[0], aTextValue[1], aTextValue[1]);
				
				if(aTextValue[0].indexOf("Previews") >= 0)
				{
					RemoveSearchNode("MinimumPrice");
					RemoveSearchNode("MaximumPrice");								
									
					var minPriceIndex = document.getElementById("hdnPreviewMinPriceIndex").value;
					var maxPriceIndex = document.getElementById("hdnPreviewMaxPriceIndex").value;										
					
					document.getElementById("ddl_SearchOptions_General_MinPrice").options[minPriceIndex].selected = true;
					document.getElementById("ddl_SearchOptions_General_MaxPrice").options[maxPriceIndex].selected = true;					
				}				
			}
			// UN-CHECKED
			else
			{
				RemoveChildNodeByValue(oParentNode, ctrlSource.id);
				
				if(aTextValue[0].indexOf("Previews") >= 0)
				{				
					document.getElementById("ddl_SearchOptions_General_MinPrice").options[0].selected = true;
					document.getElementById("ddl_SearchOptions_General_MaxPrice").options[0].selected = true;
					
					RemoveSearchNode("MinimumPrice");
					RemoveSearchNode("MaximumPrice");								
				}		
									
			}			
		}
		//	TextBox with Comma Seperated Values
		else if(ctrlSource.type == "text" && ctrlSource.isMultiple != null)
		{
			//	Remove all the child nodes and add childs based on the 
			//	current selected items
			AddChildNodes(oParentNode, ctrlSource);			
		}		
	}
	//	DROPDOWN LISTBOX | RADIO BUTTON | TEXT BOX
	else
	{
		//	Get the text and value
		aTextValue = GetTextValue(ctrlSource);
	
		if (ctrlSource.id == "rbtnl_MoreSearchOptions_SingleFamily_ListingType_0" || ctrlSource.id == "rbtnl_MoreSearchOptions_SingleFamily_ListingType_2" || ctrlSource.id == "rbtnl_MoreSearchOptions_SingleFamily_ListingType_3" || ctrlSource.id == "rbtnl_MoreSearchOptions_SingleFamily_ListingType_4") 
		{
			RemoveSearchNode("//child[@controlID='chkl_MoreSearchOptions_SingleFamily_ListingType_10']");
			RemoveSearchNode("//child[@controlID='chkl_MoreSearchOptions_SingleFamily_ListingType_11']");
			RemoveSearchNode("//child[@controlID='chkl_MoreSearchOptions_SingleFamily_ListingType_12']");
			RemoveSearchNode("//child[@controlID='chkl_MoreSearchOptions_SingleFamily_ListingType_13']");
			
		}
		
		//multi family
		if (ctrlSource.id == "rbtnl_MoreSearchOptions_MultiFamily_ListingType_0" || ctrlSource.id == "rbtnl_MoreSearchOptions_MultiFamily_ListingType_2") 
		{
			RemoveSearchNode("//child[@controlID='chkl_MoreSearchOptions_MultiFamily_ListingType_0']");
			RemoveSearchNode("//child[@controlID='chkl_MoreSearchOptions_MultiFamily_ListingType_1']");
			RemoveSearchNode("//child[@controlID='chkl_MoreSearchOptions_MultiFamily_ListingType_2']");
			RemoveSearchNode("//child[@controlID='chkl_MoreSearchOptions_MultiFamily_ListingType_3']");
			
		}
		
		//Commercial
		if (ctrlSource.id == "rbtnl_MoreSearchOptions_Commercial_ListingType_0" || ctrlSource.id == "rbtnl_MoreSearchOptions_Commercial_ListingType_2") 
		{
			RemoveSearchNode("//child[@controlID='chkl_MoreSearchOptions_Commercial_ListingType_0']");
			RemoveSearchNode("//child[@controlID='chkl_MoreSearchOptions_Commercial_ListingType_1']");
			RemoveSearchNode("//child[@controlID='chkl_MoreSearchOptions_Commercial_ListingType_2']");
			RemoveSearchNode("//child[@controlID='chkl_MoreSearchOptions_Commercial_ListingType_3']");
		}

		//lots and land
		if (ctrlSource.id == "rbtnl_MoreSearchOptions_LotLand_ListingType_0" || ctrlSource.id == "rbtnl_MoreSearchOptions_LotLand_ListingType_2") 
		{
			RemoveSearchNode("//child[@controlID='chkl_MoreSearchOptions_LotLand_ListingType_0']");
			RemoveSearchNode("//child[@controlID='chkl_MoreSearchOptions_LotLand_ListingType_1']");
			RemoveSearchNode("//child[@controlID='chkl_MoreSearchOptions_LotLand_ListingType_2']");
			RemoveSearchNode("//child[@controlID='chkl_MoreSearchOptions_LotLand_ListingType_3']");
		}
		
		
		if(aTextValue != null)
		{
			//	Add this as a parent node			
			AddParentNode(nodeName, displayName, ctrlSource.type, ctrlSource.id, category, aTextValue[0], aTextValue[1]);
		}
		else
		{		
			RemoveSearchNode(nodeName);	
		}
	}
	
	//	Initiate the server RPC request
	if(spm_browserType() == "ie")
		SearchController.GetSearchResultCount(SearchXml.xml, GetSearchResultCount_Callback);	
	else
		SearchController.GetSearchResultCount(SearchXml.childNodes[0].xml, GetSearchResultCount_Callback);	
	
	//	Refresh summary listbox
	RefreshSummaryBox();
}		

/*
	--------------------------------------------------------------------------------------								
	Name	: RemoveSearch	
	
	Purpose	: This function gets called when an item is selected in the Summary Listbox and 
			  the delete key is pressed.
			  
			  This function will 
			  1. Remove every selected item from summary listbox 
			  2. Sends a Javascript RPC request to server
			  3. Update the contents of SummaryList Control based on the latest search XML			
	--------------------------------------------------------------------------------------
*/

function RemoveSearch(resetTree)
{	
	var lstSummary = document.getElementById("lstSummary");
	
	//	Only proceed if there are any items inside the Summary listbox
	if(lstSummary.options.length == 0)
		return;
	
	var oSummaryOptions = lstSummary.options;
	var oRootNode = SearchXml.childNodes[0];		
	
	for(var i=0; i<oSummaryOptions.length; i++)
	{
		// Continue iterating through the option loop and remove if
		// the current option is selected
		if (resetTree || oSummaryOptions[i].selected)
		{
			RemoveSummaryOption(lstSummary, oRootNode, oSummaryOptions[i]);
			
			if(oRootNode.childNodes.length == 1)
				break;
		}
	}
						
	//	Initiate the server RPC request
	if(spm_browserType() == "ie")	
		SearchController.GetSearchResultCount(SearchXml.xml, GetSearchResultCount_Callback);
	else
		SearchController.GetSearchResultCount(SearchXml.childNodes[0].xml, GetSearchResultCount_Callback);			

	//	Refresh summary listbox
	RefreshSummaryBox();
}


/*
	--------------------------------------------------------------------------------------								
	Name	: RemoveSummaryOption	
	
	Purpose	: This function will
			1. Remove the passed-in option from from Search XML
			2. Remove matching node (for the passed-in option) from Search XML
			3. Reset the controls that match the removed node
	--------------------------------------------------------------------------------------
*/

function RemoveSummaryOption(lstSummary, oRootNode, oCurrOption)
{	
	var oParentNode;
	var oChildNode;
	var oCurrOption;
	var aOptionValues;
			
	//
	// If a child option was deleted										
	//
	if(oCurrOption.value.indexOf(":Child") > 0)
	{
		//	Get the name of the parent that this child belongs
		//	array[0] - Parent Node Name
		//	array[1] - Child Node Position in DOM tree
		aOptionValues = oCurrOption.value.split(":Child");

		//	Get the parent & child nodes
		oParentNode = oRootNode.selectSingleNode(aOptionValues[0]);
		if(oParentNode != null)
		{			
			oChildNode = GetChildNodeByPosition(oParentNode, aOptionValues[1]);

			if(oChildNode != null)
			{
				var ctrl = InstantiateControl(oChildNode);
				
				if(ctrl != null)
				{
					// Reset control - multiselect listbox & checkbox
					ResetControl(ctrl, oChildNode.getAttribute("position"), oChildNode.getAttribute("value"));
				}
			}
					
			// Reset node in DOM
			RemoveChildNodeByPosition(oParentNode, aOptionValues[1]);
		}
	}
	//
	//	If a parent option was deleted
	//
	else
	{
		//	Get the parent node					
		oParentNode = oRootNode.selectSingleNode(oCurrOption.value);											

		if(oParentNode.childNodes.length > 0)
		{		
			//	Reset control - multiselect listbox				
			if(oParentNode.getAttribute("controlType") == "select-multiple")
			{					
				ResetControl(InstantiateControl(oParentNode), null, null);
			}
			//	Reset control - textbox
			else if(oParentNode.getAttribute("controlType") == "text")
			{
				ResetControl(InstantiateControl(oParentNode), null, null);
			}
			else if(oParentNode.getAttribute("controlType") == "map")
			{
				for(var j=0; j<oParentNode.childNodes.length; j++)
				{
					var ctrl = InstantiateControl(oParentNode.childNodes[j]);				
					if(ctrl != null)
						ResetControl(ctrl, oParentNode.childNodes[j].getAttribute("position"), oParentNode.childNodes[j].getAttribute("value"));										
				}				
			}									
			//	Reset control - Check Boxes									
			else
			{
				for(var j=0; j<oParentNode.childNodes.length; j++)
					ResetControl(InstantiateControl(oParentNode.childNodes[j]), null, null);
			}			
		}
		//	Reset control - Dropdown list and Radio button
		else
		{
			ResetControl(InstantiateControl(oParentNode), null, null);			
		}
		
		// Reset node in DOM				
		RemoveChildNodeByName(oRootNode, oParentNode.nodeName)					
	}
}	

/*
	--------------------------------------------------------------------------------------								
	Name	: RefreshSummaryBox	
	
	Purpose	: This function will walk through the DOM structure of Search XML and transform 
			  the nodes as options of SummaryList Control
	--------------------------------------------------------------------------------------
*/
	
function RefreshSummaryBox()
{
	
	//	Clear all the items in the existing listbox	
	var lstSummary = document.getElementById("lstSummary");
	lstSummary.innerHTML = "";	
	
	//	Get the root XML node
	var oRootNode = SearchXml.childNodes[0];
							
	var oParentNode;
	var oChildNode;		
	var sOptionText;
	var sOptionValue;	
		
	//	Iterate through every child node of root node
	for(var i=0; i<oRootNode.childNodes.length; i++)
	{
		// Current parent node
		oParentNode = oRootNode.childNodes[i];
	
		if(oParentNode.childNodes.length == 0)
		{
			// For Invisible Nodes (like PageSize) we should not show the Node values in summary box
			if(oParentNode.getAttribute("displayName").length > 0)
			{				
				sOptionText = oParentNode.getAttribute("displayName") + " : " + oParentNode.getAttribute("text");
				sOptionValue = oParentNode.nodeName;
			
				AddOption(lstSummary, sOptionText, sOptionValue);
				if(sOptionText == 'Search For : Open Houses')
				{
					lstSummary.options[lstSummary.length-1].style.color = "#009640";
					//lstSummary.options[lstSummary.length-1].style.color = "black";
				}
				else
				{
					lstSummary.options[lstSummary.length-1].style.color = "black";
				}
			}
		}
		else
		{
			sOptionText = oParentNode.getAttribute("displayName");
			sOptionValue = oParentNode.nodeName;
			
			AddOption(lstSummary, sOptionText, sOptionValue);
			lstSummary.options[lstSummary.length-1].style.color = "black";
			
			for(var j=0; j<oParentNode.childNodes.length; j++)
			{
				// Current child node
				oChildNode = oParentNode.childNodes[j];
				
				sOptionText = oChildNode.getAttribute("text");


				if(sOptionText.indexOf("Previews") >= 0)
					sOptionText = oChildNode.getAttribute("text");
				else
					sOptionText = "\xa0\xa0\xa0\xa0\xa0\xa0\xa0" + oChildNode.getAttribute("text");
					
				sOptionValue = oParentNode.nodeName + ":Child" + oChildNode.getAttribute("position");
				
				AddOption(lstSummary, sOptionText, sOptionValue);
				lstSummary.options[lstSummary.length-1].style.color = "black";
			}
		}
	}	
}

/*
	--------------------------------------------------------------------------------------
	Name	: DisplayHSA
	
	Purpose	: This function will show or hide the Home Search Alert link based on the 
			  search type.  Sold Properties can not show the link.
	--------------------------------------------------------------------------------------
*/

function DisplayHSA(e)
{
	var ctrlSource = GetEventSource(e);
	var aTextValue = GetTextValue(ctrlSource);

	if (aTextValue[0] == "Sold Properties") 
	{
		document.getElementById("cpTopLinks_ctl00_hlHomeSearchAlert").style.display = "none";
	}
	else
	{
		document.getElementById("cpTopLinks_ctl00_hlHomeSearchAlert").style.display = "inline";
	}
}

/*
	--------------------------------------------------------------------------------------
	Name	:
	
	Purpose	: this function will change the search type label
	--------------------------------------------------------------------------------------
*/

function ChangeSearchLabel(e)
{
	
	var ctrlSource = GetEventSource(e);
	var aTextValue = GetTextValue(ctrlSource);
	
	switch(aTextValue[0]) 
	{
		case "Sold Properties":
		{
			document.getElementById("imgGenOpenHouse").style.display = "none";
			document.getElementById("imgGenNewListing").style.display = "none";
			document.getElementById("imgGenForSale").style.display = "none";
			document.getElementById("imgGenSold").style.display = "inline";
			break;
		}
		case "Open Houses":
		{
			document.getElementById("imgGenSold").style.display = "none";
			document.getElementById("imgGenNewListing").style.display = "none";
			document.getElementById("imgGenForSale").style.display = "none";
			document.getElementById("imgGenOpenHouse").style.display = "inline";
			break;
		}
		case "New Listings":
		{
			document.getElementById("imgGenSold").style.display = "none";
			document.getElementById("imgGenOpenHouse").style.display = "none";
			document.getElementById("imgGenForSale").style.display = "none";
			document.getElementById("imgGenNewListing").style.display = "inline";
			break;
		}
		case "For Sale Properties":
		{
			document.getElementById("imgGenSold").style.display = "none";
			document.getElementById("imgGenOpenHouse").style.display = "none";
			document.getElementById("imgGenNewListing").style.display = "none";
			document.getElementById("imgGenForSale").style.display = "inline";
			break;
		}
	}
}

/*
	--------------------------------------------------------------------------------------
	
						Functions related to Search Tab	
						
	--------------------------------------------------------------------------------------
*/

function LoadDiv(divID)
{
	if(this.event)
	{
		try
		{
			if(oTransContainer.filters) // IE Only
				oTransContainer.filters[0].Apply();
		}
		catch(e){}
	}
   				  					
	// Hide all DIV's
	ResetTabs();
	
	//	Set the Tab Image of the currently clicked Div to "Highlighted" state
	var srcElement;
	if(!this.event)
		srcElement = arguments.callee.caller.arguments[0].explicitOriginalTarget;
	else
		srcElement = this.event.srcElement;
		
	ToggleImageDisplay(null, srcElement, 1);	
		
	//	Show the currently clicked DIV
	document.getElementById(divID).style.display = "inline";	
	
	PositionSearchButton();
	
	//
	//	Special Handling 
	//	For the dIV's listed below, we need to perform additional operations
	//
	
   // _divMoreSearchOptionsLink.style.display = "inline";
    ToggleSearchOptions(true);
    
	switch(divID)
	{
		case "div_CitySearch":
		{
			//_divLeftHeaderText_City.style.display = "inline";	
			//_divRightHeaderText_City.style.display = "inline";	
			
			_ifrm_CitySearch_Map.src = "maps/index.html";
			
			document.getElementById("div_SearchOptions_City").style.display = "inline";															
			
			break;
		}
		case "div_RadiusSearch":
		{
			//_divLeftHeaderText_Radius.style.display = "inline";	
			_divRightHeaderText_Radius.style.display = "inline";						
			
			document.getElementById("div_SearchOptions_Radius").style.display = "inline";			
			var rgxUrl = new RegExp("[&|?]IsBranded=1&AgentID=\\d+", "gi");
			_ifrm_RadiusSearch_Map.src = document.getElementById("hdnRadiusUrl").value.replace(rgxUrl, "");			
		
			break;
		}
		case "div_SchoolDistrictSearch":
		{
			//_divLeftHeaderText_General.style.display = "inline";
			_divRightHeaderText_General.style.display = "inline";
			
			var txtBox = document.getElementById("lst_SchoolDistrictSearch_DistrictList_ctl01");
			txtBox.style.display = "none";
					
			var lstBox = document.getElementById("lst_SchoolDistrictSearch_DistrictList_ctl02");
			lstBox.style.display = "none";
					
			if(lstBox.length == 0)
			{
				setTimeout('SearchController.GetSchoolDistrictLookup(0, GetSchoolDistrictLookup_Callback)', 500);
			}
			else
			{
				var expr =	"document.getElementById('lst_SchoolDistrictSearch_DistrictList_ctl01').style.display = 'inline';" + 
							"document.getElementById('lst_SchoolDistrictSearch_DistrictList_ctl02').style.display = 'inline';"
				
				setTimeout(expr, 500);
			}		
					
			break;
		}
		case "div_MLSAreaSearch":
		{
			//_divLeftHeaderText_General.style.display = "inline";
			_divRightHeaderText_General.style.display = "inline";			
					
			var txtBox = document.getElementById("lst_MLSAreaSearch_AreaList_ctl01");
			txtBox.style.display = "none";
					
			var lstBox = document.getElementById("lst_MLSAreaSearch_AreaList_ctl02");
			lstBox.style.display = "none";
			
			if(lstBox.length == 0)
			{	
				setTimeout('SearchController.GetMLSAreaLookup(0, GetMLSAreaLookup_Callback)', 500);
			}
			else
			{
				var expr =	"document.getElementById('lst_MLSAreaSearch_AreaList_ctl01').style.display = 'inline';" + 
							"document.getElementById('lst_MLSAreaSearch_AreaList_ctl02').style.display = 'inline';"
										
				setTimeout(expr, 500);
			}
					
			break;
		}
		case "div_MLSNumberSearch":
		{	
			// Hide the Search More Options Button for MLS Number Search
			//_divMoreSearchOptionsLink.style.display = "none";
			ToggleSearchOptions(false);
			
			//_divLeftHeaderText_General.style.display = "inline";
			_divRightHeaderText_General.style.display = "inline";			
					
			break;
		}				
		default:
		{
			//_divLeftHeaderText_General.style.display = "inline";
			_divRightHeaderText_General.style.display = "inline";			
			break;
		}
	}

	if(this.event)			
	{
		try
		{
			if(oTransContainer.filters) // IE Only
				oTransContainer.filters[0].Play();				
		}
		catch(e){}
	}
						
	//	Get Results Count
	SearchController.GetSearchResultCount(SearchXml.xml, GetSearchResultCount_Callback);		
		
	//	Refresh summary listbox
	RefreshSummaryBox();	
}

function ResetTabs()
{
	//	City Search Tab
	document.getElementById("div_CitySearch").style.display = "none";
	document.getElementById("div_SearchOptions_City").style.display = "none";	
	document.getElementById("div_CitySearch_MapPointLink").style.display = "none";
	document.getElementById("hl_CitySearch_GotoCityCountyMap").style.display = "none";	
	document.getElementById("hl_CitySearch_GotoMapPointMap").style.display = "inline";	
	ToggleImageDisplay("img_CitySearch_Tab", null, 0);	
	
	//_divLeftHeaderText_City.style.display = "none";	
	_divRightHeaderText_City.style.display = "none";		
	
	RemoveChildNodeByName(SearchXml.childNodes[0], "Communities");	
	RemoveChildNodeByName(SearchXml.childNodes[0], "Cities");
	RemoveChildNodeByName(SearchXml.childNodes[0], "Counties");
	RemoveChildNodeByName(SearchXml.childNodes[0], "Region");
	
	//	Radius Search Tab	
/*	document.getElementById("div_RadiusSearch").style.display = "none";
	document.getElementById("div_SearchOptions_Radius").style.display = "none";			
	ToggleImageDisplay("img_RadiusSearch_Tab", null, 0);	
	
	_divLeftHeaderText_Radius.style.display = "none";	
	_divRightHeaderText_Radius.style.display = "none";
	
	RemoveSearchNode("Radius_Latitude");
	RemoveSearchNode("Radius_Longitude");
	RemoveSearchNode("Radius_Miles");
	
	if(_currIFrame != null)
	{
		_currIFrame.width = "360px";	
		_currIFrame.height = "100%";	
	}
	*/
	
	//	SchoolDistrict Tab
	document.getElementById("div_SchoolDistrictSearch").style.display = "none";
	ToggleImageDisplay("img_SchoolDistrictSearch_Tab", null, 0);
	
	RemoveSearchNode("SchoolDistrict");	
	
	//	Address Search Tab	
	document.getElementById("div_AddressSearch").style.display = "none";
	ToggleImageDisplay("img_AddressSearch_Tab", null, 0);
	
	RemoveSearchNode("Address_HouseNumber");
	RemoveSearchNode("Address_Street");
	RemoveSearchNode("Address_City");
	RemoveSearchNode("Address_ZipCode");	
	
	//	MLSArea Search Tab		
	document.getElementById("div_MLSAreaSearch").style.display = "none";
	ToggleImageDisplay("img_MLSAreaSearch_Tab", null, 0);
	
	RemoveSearchNode("MLSArea");			
	
	//	MLSNumber/InfoLine Search Tab	
	document.getElementById("div_MLSNumberSearch").style.display = "none";				
	ToggleImageDisplay("img_MLSNumberSearch_Tab", null, 0);
	
	RemoveSearchNode("MLSNumber");			
	
	// Search More Options Button
	//_divMoreSearchOptionsLink.style.display = "inline";
}

function ToggleImageDisplay(imgName, imgCtrl, flag)
{
	if(imgCtrl == null)
		imgCtrl = document.getElementById(imgName);

	if (imgCtrl.src == null)
	{
		return;
	}
	if(flag == 0)	
		imgCtrl.src = imgCtrl.src.replace("_on", "_off");
	else
		imgCtrl.src = imgCtrl.src.replace("_off", "_on");
}

function SortSchoolDistrictLookup()
{
	//	Refresh summary listbox
	RemoveSearchNode("SchoolDistrict");
	RefreshSummaryBox();
	
	_div_SchoolDistrictSearch_SearchLabel.innerHTML = "Re-Sorting.....";		
	
	var lstBox = document.getElementById("lst_SchoolDistrictSearch_DistrictList_ctl02");
	lstBox.innerHTML = "";
	
	var srcElement;
	if(!this.event)
		srcElement = arguments.callee.caller.arguments[0].explicitOriginalTarget;
	else
		srcElement = this.event.srcElement;
		
	_nSortValue = srcElement.value;
			
	SearchController.GetSchoolDistrictLookup(srcElement.value, GetSchoolDistrictLookup_Callback);
	
	//	Initiate the server RPC request
	SearchController.GetSearchResultCount(SearchXml.xml, GetSearchResultCount_Callback);					
}

function SortMLSAreaLookup()
{
	//	Refresh summary listbox
	RemoveSearchNode("MLSArea");
	RefreshSummaryBox();	
	
	_div_MLSAreaSearch_SearchLabel.innerHTML = "Re-Sorting.....";		
	
	var lstBox = document.getElementById("lst_MLSAreaSearch_AreaList_ctl02");
	lstBox.innerHTML = "";
	
	var srcElement;
	if(!this.event)
		srcElement = arguments.callee.caller.arguments[0].explicitOriginalTarget;
	else
		srcElement = this.event.srcElement;
	
	_nSortValue = srcElement.value;
	
	SearchController.GetMLSAreaLookup(srcElement.value, GetMLSAreaLookup_Callback);	
	
	//	Initiate the server RPC request
	SearchController.GetSearchResultCount(SearchXml.xml, GetSearchResultCount_Callback);						
}

function GetSchoolDistrictLookup_Callback(response)
{
	if(response != null && response.value != null)
	{
		var alDist = response.value;
		var txtBox = document.getElementById("lst_SchoolDistrictSearch_DistrictList_ctl01");		
		var lstBox = document.getElementById("lst_SchoolDistrictSearch_DistrictList_ctl02");
		
		for(var i=0; i<alDist.length; i++)
		{
			lstBox.options[lstBox.length] = new Option(alDist[i].Value, alDist[i].Key, false, false);
		}
		
		_div_SchoolDistrictSearch_SearchLabel.innerHTML = "Select a School District:";
		
		txtBox.style.display = "inline";
		lstBox.style.display = "inline";
	}	
}

function GetMLSAreaLookup_Callback(response)
{	
	if(response != null && response.value != null)
	{
		var alArea = response.value;
		var txtBox = document.getElementById("lst_MLSAreaSearch_AreaList_ctl01");		
		var lstBox = document.getElementById("lst_MLSAreaSearch_AreaList_ctl02");
			
		for(var i=0; i<alArea.length; i++)
		{
			lstBox.options[lstBox.length] = new Option(alArea[i].Value, alArea[i].Key, false, false);		
		}

		_div_MLSAreaSearch_SearchLabel.innerHTML = "Select an MLS Area:";
		
		txtBox.style.display = "inline";		
		lstBox.style.display = "inline";		
	}	
}

function GetSearchResultCount_Callback(response)
{
	if(response != null && response.value != null)
	{
		var txtBox = document.getElementById("txtListingCount");
		txtBox.value = response.value;
	}	
}

/*
	--------------------------------------------------------------------------------------											
				Control Utility Functions	
	--------------------------------------------------------------------------------------
*/

function InstantiateControl(node)
{
	var sControlID = node.getAttribute("controlID")
	
	return document.getElementById(sControlID);		
}	

function ResetControl(ctrl, optionIndex, optionValue)
{
	if(ctrl == null)
		return;
		
	switch(ctrl.type)
	{
		case "select-one":
		{
			ctrl.selectedIndex = 0;
			break;
		}
		case "select-multiple":
		{
			if(optionIndex == null || optionIndex == -1)
				ctrl.selectedIndex = -1;
			else
			{
				if(ctrl.options[optionIndex] != null && ctrl.options[optionIndex].value == optionValue)
					ctrl.options[optionIndex].selected = false;			
			}
				
			break;
		}
		case "radio":
		{
			ctrl.checked = false;			
			
			if(ctrl.id.indexOf("rbtnl_SearchOptions_General_PropType") >= 0)
			{
				document.getElementById("rbtnl_SearchOptions_General_PropType_0").checked = true;							
				TogglePropertyOptions("0")
			}

			break;
		}
		case "checkbox":
		{
			if(ctrl.id == "chkl_MoreSearchOptions_SingleFamily_ListingType_3")
			{
				document.getElementById("ddl_SearchOptions_General_MinPrice").options[0].selected = true;
				document.getElementById("ddl_SearchOptions_General_MaxPrice").options[0].selected = true;
				
				RemoveSearchNode("MinimumPrice");
				RemoveSearchNode("MaximumPrice");			
			}
			
			ctrl.checked = false;			
			break;
		}
		case "text":
		{
			if(ctrl.isMultiple && optionValue != null)
			{
				ctrl.value = ctrl.value.remove(optionValue)
			}
		/*	else if(ctrl.isRadius && ctrl.isMultiple)
			{
				if(_txt_RadiusSearch_City != null)
				{
					_txt_RadiusSearch_City.value = "";
					_ddl_RadiusSearch_State.selectedIndex = 0;
					_txt_RadiusSearch_Zip.value = "";
				}
			}  */
			else
			{
				ctrl.value = "";
			}

			break;
		}
	}
}

function CanHaveChildren(ctrl)
{
	if(ctrl.type == "select-multiple")
	{
		return true;
	}
	else if(ctrl.type == "checkbox")
	{
		return true;
	}
	// Textbox holding comma seperated values
	else if(ctrl.isMultiple != null)
	{
		return true;
	}
	
	return false;
}

function IsValidEventSource(ctrl)
{
	if(ctrl.tagName == "SELECT" || ctrl.tagName == "INPUT")
	{
		return true;
	}
	
	return false;
}	

function GetTextValue(ctrl)
{
	var aTextValue = new Array(2);
	
	switch(ctrl.type)
	{
		case "checkbox":
		{		
			aTextValue[0] = ctrl.parentNode.childNodes[1].innerHTML;
			aTextValue[1] = ctrl.id.substring(ctrl.id.lastIndexOf("_")+1);			
			
			break;
		}
		case "radio":
		{
			if(spm_browserType() == "ie")
			{
				aTextValue[0] = ctrl.parentElement.children[1].innerText;
				aTextValue[1] = ctrl.value;				
			}
			else
			{
				aTextValue[0] = ctrl.parentNode.childNodes[1].innerHTML;
				aTextValue[1] = ctrl.value;				
			}
			
			break;
		}
		case "select-one":
		{
			aTextValue[0] = ctrl.options[ctrl.selectedIndex].text;
			aTextValue[1] = ctrl.value;
						
			break;
		}
		case "text":
		{
			ctrl.value = ctrl.value.trim(" ");					
			if(ctrl.value.length == 0)
				return null;
										
			aTextValue[0] = ctrl.value;
			aTextValue[1] = ctrl.value;			
					
			break;
		}
	}
	
	return aTextValue;
}

function AddOption(lstBox, text, value)
{
	lstBox.options[lstBox.length] = new Option(text, value, false, false);		
}


function ToggleSearchOptions(toggleState)
{
	//var hlSearchMoreOptions = document.getElementById("hlSearchMoreOptions");
	
	if(toggleState != null)
	{
		if(toggleState == true)
		{
			//hlSearchMoreOptions.innerText = "Hide Search Options";		
			_divMoreSearchOptions.style.display = "inline";					
		}
		else
		{
			//hlSearchMoreOptions.innerText = "More Search Options";
			_divMoreSearchOptions.style.display = "none";
		}		
	}
	else
	{
		if(_divMoreSearchOptions.style.display == "inline")
		{
			//hlSearchMoreOptions.innerText = "More Search Options";
			_divMoreSearchOptions.style.display = "none";
		}
		else
		{
			//hlSearchMoreOptions.innerText = "Hide Search Options";		
			_divMoreSearchOptions.style.display = "inline";
		}		
	}
}

function ResetSingleFamily(showHideFlag, isSold, isOpenHouse, isNewListing)
{
	if(showHideFlag)
	{		
		document.getElementById("div_SearchOptions_SingleFamily").style.display = "inline";				
		document.getElementById("div_MoreSearchOptions_SingleFamily").style.display = "inline";
		document.getElementById("cpTopLinks_ctl00_hlHomeSearchAlert").style.display = "inline";	
	}
	else
	{
		document.getElementById("div_SearchOptions_SingleFamily").style.display = "none";				
		document.getElementById("div_MoreSearchOptions_SingleFamily").style.display = "none";
		//document.getElementById("cpTopLinks_ctl00_hlHomeSearchAlert").style.display = "none";
			
		RemoveSearchNode("Bedrooms");
		RemoveSearchNode("Bathrooms");
		RemoveSearchNode("Garage");		
		RemoveSearchNode("MinimumSquareFeet");			
		RemoveSearchNode("ListingSearchType");
		RemoveSearchNode("ListingSearchTypeAdditional");						
		RemoveSearchNode("SingleFamilyStyles");			
		RemoveSearchNode("InteriorFeatures");
		RemoveSearchNode("PropertyFeatures");
		RemoveSearchNode("Acreage");		
	}
	if (isSold == true)
	{
		document.getElementById("cpTopLinks_ctl00_hlHomeSearchAlert").style.display = "none";			
		document.getElementById("rbtnl_MoreSearchOptions_SingleFamily_ListingType_3").checked = true;
		UpdateSearch('ListingSearchType', 'Search For', 'rbtnl_MoreSearchOptions_SingleFamily_ListingType_3', null)
	}
	else
	{
		document.getElementById("cpTopLinks_ctl00_hlHomeSearchAlert").style.display = "inline";
	
		if (isOpenHouse == true)
		{
			document.getElementById("rbtnl_MoreSearchOptions_SingleFamily_ListingType_1").checked = true;
			UpdateSearch('ListingSearchType', 'Search For', 'rbtnl_MoreSearchOptions_SingleFamily_ListingType_1', null)
		}
		else if (isNewListing == true)
		{
			document.getElementById("rbtnl_MoreSearchOptions_SingleFamily_ListingType_2").checked = true;
			UpdateSearch('ListingSearchType', 'Search For', 'rbtnl_MoreSearchOptions_SingleFamily_ListingType_2', null)			
		}			
	}
	//HideShowOpenHouserbtnl_MoreSearchOptions_SingleFamily_ListingType(document.getElementById("rbtnl_MoreSearchOptions_SingleFamily_ListingType_1"));
		
}

function ResetMultiFamily(showHideFlag, isSold, isOpenHouse)
{
	if(showHideFlag)
	{
		document.getElementById("div_SearchOptions_MultiFamily").style.display = "inline";
		document.getElementById("div_MoreSearchOptions_MultiFamily").style.display = "inline";
		document.getElementById("cpTopLinks_ctl00_hlHomeSearchAlert").style.display = "inline";
	}
	else
	{
		document.getElementById("div_SearchOptions_MultiFamily").style.display = "none";
		document.getElementById("div_MoreSearchOptions_MultiFamily").style.display = "none";
		//document.getElementById("cpTopLinks_ctl00_hlHomeSearchAlert").style.display = "none";		
				
		RemoveSearchNode("MinimumSquareFeet");
		RemoveSearchNode("Units");
		RemoveSearchNode("Garage");		
		RemoveSearchNode("MultiFamilyStyles");
		RemoveSearchNode("MultiFamilyTerms");
	}
	if (isSold == true)
	{
		document.getElementById("cpTopLinks_ctl00_hlHomeSearchAlert").style.display = "none";
		document.getElementById("rbtnl_MoreSearchOptions_MultiFamily_ListingType_2").checked = true;
		UpdateSearch('ListingSearchType', 'Search For', 'rbtnl_MoreSearchOptions_MultiFamily_ListingType_2', null)
	}
	else
	{
		document.getElementById("cpTopLinks_ctl00_hlHomeSearchAlert").style.display = "inline";
		if (isOpenHouse == true)
		{
			document.getElementById("rbtnl_MoreSearchOptions_MultiFamily_ListingType_1").checked = true;
			UpdateSearch('ListingSearchType', 'Search For', 'rbtnl_MoreSearchOptions_MultiFamily_ListingType_1', null)
		}
	}	
	//HideShowOpenHouserbtnl_MoreSearchOptions_MultiFamily_ListingType(document.getElementById("rbtnl_MoreSearchOptions_MultiFamily_ListingType_1"));	
}

function ResetCommercial(showHideFlag, isSold, isOpenHouse)
{
	if(showHideFlag)
	{
		document.getElementById("div_SearchOptions_Commercial").style.display = "inline";			
		document.getElementById("div_MoreSearchOptions_Commercial").style.display = "inline";
	}
	else
	{
		document.getElementById("div_SearchOptions_Commercial").style.display = "none";			
		document.getElementById("div_MoreSearchOptions_Commercial").style.display = "none";
		//document.getElementById("cpTopLinks_ctl00_hlHomeSearchAlert").style.display = "none";
				
		RemoveSearchNode("Zoning");				
		RemoveSearchNode("CurrentUse");
		RemoveSearchNode("CommercialTerms");
	}
	if (isSold == true)
	{
		document.getElementById("cpTopLinks_ctl00_hlHomeSearchAlert").style.display = "none";
		document.getElementById("rbtnl_MoreSearchOptions_Commercial_ListingType_2").checked = true;
		UpdateSearch('ListingSearchType', 'Search For', 'rbtnl_MoreSearchOptions_Consumer_ListingType_2', null)
	}
	else
	{
		document.getElementById("cpTopLinks_ctl00_hlHomeSearchAlert").style.display = "inline";
		if (isOpenHouse == true)
		{
			document.getElementById("rbtnl_MoreSearchOptions_Commercial_ListingType_1").checked = true;
			UpdateSearch('ListingSearchType', 'Search For', 'rbtnl_MoreSearchOptions_Commercial_ListingType_1', null)
		}
	}
//	HideShowOpenHouserbtnl_MoreSearchOptions_Commercial_ListingType(document.getElementById("rbtnl_MoreSearchOptions_Commercial_ListingType_1"));

}

function ResetLandLots(showHideFlag, isSold, isOpenHouse)
{
	if(showHideFlag)
	{
		document.getElementById("div_SearchOptions_LotLand").style.display = "inline";
		document.getElementById("div_MoreSearchOptions_LotLand").style.display = "inline";
		document.getElementById("cpTopLinks_ctl00_hlHomeSearchAlert").style.display = "inline";
	}
	else
	{
		document.getElementById("div_SearchOptions_LotLand").style.display = "none";
		document.getElementById("div_MoreSearchOptions_LotLand").style.display = "none";
		//document.getElementById("cpTopLinks_ctl00_hlHomeSearchAlert").style.display = "none";
				
		RemoveSearchNode("MinimumAcreage");			
		RemoveSearchNode("MaximumAcreage");			
		RemoveSearchNode("Topography");
		RemoveSearchNode("Zoning");
	}
	if (isSold == true)
	{
		document.getElementById("cpTopLinks_ctl00_hlHomeSearchAlert").style.display = "none";
		document.getElementById("rbtnl_MoreSearchOptions_LotLand_ListingType_2").checked = true;
		UpdateSearch('ListingSearchType', 'Search For', 'rbtnl_MoreSearchOptions_LotLand_ListingType_2', null)
	}
	else
	{
		document.getElementById("cpTopLinks_ctl00_hlHomeSearchAlert").style.display = "inline";
		if (isOpenHouse == true)
		{
			document.getElementById("rbtnl_MoreSearchOptions_LotLand_ListingType_1").checked = true;
			UpdateSearch('ListingSearchType', 'Search For', 'rbtnl_MoreSearchOptions_LotLand_ListingType_1', null)
		}
	}
//	HideShowOpenHouserbtnl_MoreSearchOptions_LotLand_ListingType(document.getElementById("rbtnl_MoreSearchOptions_LotLand_ListingType_1"));
}

function TogglePropertyOptions(selectedValue, isSold, e)
{
	var isOpenHouse = false;
	var isNewListing = false;
	if(selectedValue == null)
	{
		var ctrlSource = GetEventSource(e);
		selectedValue = ctrlSource.value;		
	}
	
	if (document.getElementById("imgGenSold").style.display == "inline")
		isSold = true;

	if (document.getElementById("imgGenOpenHouse").style.display == "inline")
		isOpenHouse = true;

	PositionSearchButton();
	
	switch(selectedValue)
	{
		case "0":
		{		
			if (document.getElementById("imgGenNewListing").style.display == "inline")
				isNewListing = true;
			ResetMultiFamily(false, isSold, isOpenHouse);
			ResetCommercial(false, isSold, isOpenHouse);
			ResetLandLots(false, isSold, isOpenHouse);
			ResetSingleFamily(true, isSold, isOpenHouse, isNewListing);
	        if (ctrlSource != null)
		        HideShowOpenHouserbtnl_MoreSearchOptions_SingleFamily_ListingType(document.getElementById("rbtnl_MoreSearchOptions_SingleFamily_ListingType_1"));
	 		break;
		}
		case "1":
		{
			ResetSingleFamily(false, isSold, isOpenHouse, isNewListing);
			ResetCommercial(false, isSold, isOpenHouse);
			ResetLandLots(false, isSold, isOpenHouse);
			ResetMultiFamily(true, isSold, isOpenHouse);
			if (ctrlSource != null)
	  	        HideShowOpenHouserbtnl_MoreSearchOptions_MultiFamily_ListingType(document.getElementById("rbtnl_MoreSearchOptions_MultiFamily_ListingType_1"));							
			break;
		}
		case "2":
		{
			ResetSingleFamily(false, isSold, isOpenHouse, isNewListing);
			ResetMultiFamily(false, isSold, isOpenHouse);
			ResetLandLots(false, isSold, isOpenHouse);
			ResetCommercial(true, isSold, isOpenHouse);
		    if (ctrlSource != null)
	    	    HideShowOpenHouserbtnl_MoreSearchOptions_Commercial_ListingType(document.getElementById("rbtnl_MoreSearchOptions_Commercial_ListingType_1"));
			break;
		}
		case "3":
		{
			ResetSingleFamily(false, isSold, isOpenHouse, isNewListing);
			ResetMultiFamily(false, isSold, isOpenHouse);
			ResetCommercial(false, isSold, isOpenHouse);
			ResetLandLots(true, isSold, isOpenHouse);
			if (ctrlSource != null)
				HideShowOpenHouserbtnl_MoreSearchOptions_LotLand_ListingType(document.getElementById("rbtnl_MoreSearchOptions_LotLand_ListingType_1"));		
			break;
		}			
	}
}

function SetMaximumPrice(minimumPriceIndex)
{
	var ddlMaxPrice = document.getElementById("ddl_SearchOptions_General_MaxPrice");
	
	if((minimumPriceIndex + 4) < ddlMaxPrice.options.length - 1)
		ddlMaxPrice.options[minimumPriceIndex + 4].selected = true;
	else
		ddlMaxPrice.options[ddlMaxPrice.options.length - 1].selected = true;

	UpdateSearch('MinimumPrice', 'Minimum Price', 'ddl_SearchOptions_General_MinPrice')
	UpdateSearch('MaximumPrice', 'Maximum Price', 'ddl_SearchOptions_General_MaxPrice')
}

function GetCurrentTabName()
{
	if(document.getElementById("div_CitySearch").style.display == "inline")
		return "MapCity";
	
	//if(document.getElementById("div_RadiusSearch").style.display == "inline")
	//	return "MapRadius";
		
	if(document.getElementById("div_SchoolDistrictSearch").style.display == "inline")
		return "SchoolDistrict";
		
	if(document.getElementById("div_AddressSearch").style.display == "inline")
		return "AddressZip";
		
	if(document.getElementById("div_MLSAreaSearch").style.display == "inline")
		return "Area";
		
	if(document.getElementById("div_MLSNumberSearch").style.display == "inline")
		return "PropertyID";	
}

function GetCurrentPropertyType()
{
	if(document.getElementById("rbtnl_SearchOptions_General_PropType_0").checked)
		return "0";
		
	if(document.getElementById("rbtnl_SearchOptions_General_PropType_1").checked)
		return "1";
		
	if(document.getElementById("rbtnl_SearchOptions_General_PropType_2").checked)
		return "2";
		
	if(document.getElementById("rbtnl_SearchOptions_General_PropType_3").checked)
		return "3";						
}

function PositionSearchButton()
{
	var tabName = GetCurrentTabName();
	var propertyType = GetCurrentPropertyType();
	var divSearchButton = document.getElementById("divSearchButton");
	//_divMoreSearchOptionsLink.style.display = "none";
	
	if(tabName == "MapCity")
	{			
		switch(propertyType)
		{						
			case "0":
			{
				divSearchButton.style.top = "10px";				
				divSearchButton.style.left = "-115px";					
				break;
			}
			case "1":
			{
				divSearchButton.style.top = "10px";				
				divSearchButton.style.left = "-115px";				
				break;
			}
			case "2":
			{
				if(spm_browserType() == "ie")
				{
					divSearchButton.style.top = "5px";				
					divSearchButton.style.left = "-225px";							
				}
				else
				{
					divSearchButton.style.top = "8px";				
					divSearchButton.style.left = "-225px";			
				}
				break;
			}
			case "3":
			{
				if(spm_browserType() == "ie")
				{
					divSearchButton.style.top = "5px";				
					divSearchButton.style.left = "-225px";
				}
				else
				{
					divSearchButton.style.top = "8px";				
					divSearchButton.style.left = "-225px";					
				}
				break;
			}												
		}
	}
	else if(tabName == "MapRadius")
	{			
		switch(propertyType)
		{
			case "0":
			{
				divSearchButton.style.top = "0px";								
				divSearchButton.style.left = "-115px";		
				break;
			}
			case "1":
			{
				divSearchButton.style.top = "5px";								
				divSearchButton.style.left = "-115px";		
				break;
			}
			case "2":
			{
				divSearchButton.style.top = "5px";				
				divSearchButton.style.left = "-225px";							
				break;
			}
			case "3":
			{
				divSearchButton.style.top = "5px";				
				divSearchButton.style.left = "-225px";				
				break;
			}												
		}	
	}
	else if(tabName == "SchoolDistrict" || tabName == "AddressZip" || tabName == "Area")
	{
		switch(propertyType)
		{
			case "0":
			{
				divSearchButton.style.top = "-80px";						
				divSearchButton.style.left = "-225px";
				break;
			}
			case "1":
			{
				divSearchButton.style.top = "-80px";						
				divSearchButton.style.left = "-225px";
				break;
			}
			case "2":
			{
				divSearchButton.style.top = "-115px";						
				divSearchButton.style.left = "-225px";				
				break;
			}
			case "3":
			{
				divSearchButton.style.top = "-115px";						
				divSearchButton.style.left = "-225px";	
				break;
			}												
		}
	}
	else if(tabName == "PropertyID")
	{
	   // _divMoreSearchOptionsLink.style.display = "none";
		switch(propertyType)
		{
			case "0":
			{
				if(spm_browserType() == "ie")
				{
					divSearchButton.style.top = "-75px";						
					divSearchButton.style.left = "-225px";
				}
				else
				{
					divSearchButton.style.top = "-80px";						
					divSearchButton.style.left = "-225px";					
				}
				break;
			}
			case "1":
			{
				if(spm_browserType() == "ie")
				{
					divSearchButton.style.top = "-125px";						
					divSearchButton.style.left = "-245px";
				}
				else
				{
					divSearchButton.style.top = "-110px";						
					divSearchButton.style.left = "-245px";					
				}
				break;
			}
			case "2":
			{
				if(spm_browserType() == "ie")
				{
					divSearchButton.style.top = "-125px";						
					divSearchButton.style.left = "-360px";				
				}
				else
				{
					divSearchButton.style.top = "-110px";						
					divSearchButton.style.left = "-360px";				
				}
				
				break;
			}
			case "3":
			{
				if(spm_browserType() == "ie")
				{
					divSearchButton.style.top = "-125px";						
					divSearchButton.style.left = "-360px";	
				}
				else
				{
					divSearchButton.style.top = "-110px";						
					divSearchButton.style.left = "-360px";	
				}				
				break;
			}												
		}		
	}	
}

function SaveHomeSearchAlert(evt)
{
    script = document.getElementById('cpTopLinks_ctl00_hlHomeSearchAlert').getAttribute('actionScript')
	var response; 

	if(spm_browserType() == "ie")
	    response = SearchController.PersistInCache(SearchXml.xml);
    else
	    response = SearchController.PersistInCache(SearchXml.childNodes[0].xml);

	script = script.replace("SearchID=0", "SearchID=" + response.value);
	
	if(script.indexOf("window.open") < 0)
		script = "window.document.location = '" + script + "'";
		
	eval(script);
}


function CheckTownHomeStyle(e)
{
	var ctrlSource = GetEventSource(e);

    if((ctrlSource.id == "chkl_MoreSearchOptions_SingleFamily_ListingType_0"))
		 {
		 if (ctrlSource.checked ==false)
		    {
		    document.getElementById("div_MoreSearchOptions_SingleFamily_Styles").style.display = "none";
           RemoveSearchNode("SingleFamilyStyles");
           /* RemoveSearchNode("//SingleFamilyStyles/child[@controlID='chkl_MoreSearchOptions_SingleFamily_Style_0']");
            RemoveSearchNode("//SingleFamilyStyles/child[@controlID='chkl_MoreSearchOptions_SingleFamily_Style_1']");
            RemoveSearchNode("//SingleFamilyStyles/child[@controlID='chkl_MoreSearchOptions_SingleFamily_Style_2']");
            RemoveSearchNode("//SingleFamilyStyles/child[@controlID='chkl_MoreSearchOptions_SingleFamily_Style_3']");
            RemoveSearchNode("//SingleFamilyStyles/child[@controlID='chkl_MoreSearchOptions_SingleFamily_Style_4']");
            RemoveSearchNode("//SingleFamilyStyles/child[@controlID='chkl_MoreSearchOptions_SingleFamily_Style_5']");
            RemoveSearchNode("//SingleFamilyStyles/child[@controlID='chkl_MoreSearchOptions_SingleFamily_Style_6']");
            RemoveSearchNode("//SingleFamilyStyles/child[@controlID='chkl_MoreSearchOptions_SingleFamily_Style_7']");
            RemoveSearchNode("//SingleFamilyStyles/child[@controlID='chkl_MoreSearchOptions_SingleFamily_Style_8']");
            RemoveSearchNode("//SingleFamilyStyles/child[@controlID='chkl_MoreSearchOptions_SingleFamily_Style_9']");
            RemoveSearchNode("//SingleFamilyStyles/child[@controlID='chkl_MoreSearchOptions_SingleFamily_Style_10']");
	       */
	        }
	      else
	        {
	        document.getElementById("div_MoreSearchOptions_SingleFamily_Styles").style.display = "inline";
	        }
		 }
			
	if((ctrlSource.id == "chkl_MoreSearchOptions_SingleFamily_ListingType_1") || (ctrlSource.id == "chkl_MoreSearchOptions_SingleFamily_ListingType_2"))
	{
		if(ctrlSource.checked)
		{
		  //  if (document.getElementById("chkl_MoreSearchOptions_SingleFamily_ListingType_0").checked == false)
			//    {
			//    document.getElementById("div_MoreSearchOptions_SingleFamily_Styles").style.display = "none";
		    //    }	
		    document.getElementById("div_MoreSearchOptions_SingleFamily_CondoTownHome_Styles").style.display = "inline";			
		}
		else
		{
			 if ((document.getElementById("chkl_MoreSearchOptions_SingleFamily_ListingType_1").checked == false) && (document.getElementById("chkl_MoreSearchOptions_SingleFamily_ListingType_2").checked == false))
			 {
			 document.getElementById("div_MoreSearchOptions_SingleFamily_CondoTownHome_Styles").style.display = "none";
			    RemoveSearchNode("SingleFamilyStyles");
                /*RemoveSearchNode("//SingleFamilyStyles/child[@controlID='chkl_MoreSearchOptions_SingleFamily_CondoTownHome_Style_0']");
                RemoveSearchNode("//SingleFamilyStyles/child[@controlID='chkl_MoreSearchOptions_SingleFamily_CondoTownHome_Style_1']");
                RemoveSearchNode("//SingleFamilyStyles/child[@controlID='chkl_MoreSearchOptions_SingleFamily_CondoTownHome_Style_2']");
                RemoveSearchNode("//SingleFamilyStyles/child[@controlID='chkl_MoreSearchOptions_SingleFamily_CondoTownHome_Style_3']");
                RemoveSearchNode("//SingleFamilyStyles/child[@controlID='chkl_MoreSearchOptions_SingleFamily_CondoTownHome_Style_4']");
                RemoveSearchNode("//SingleFamilyStyles/child[@controlID='chkl_MoreSearchOptions_SingleFamily_CondoTownHome_Style_5']");
                RemoveSearchNode("//SingleFamilyStyles/child[@controlID='chkl_MoreSearchOptions_SingleFamily_CondoTownHome_Style_6']");
                RemoveSearchNode("//SingleFamilyStyles/child[@controlID='chkl_MoreSearchOptions_SingleFamily_CondoTownHome_Style_7']");
	            */
			 }
		}
	}
}

function pause(milliseconds)
{
    var date = new Date();
    var curDate = null;

    do { curDate = new Date(); }
    while(curDate-date < milliseconds);
}

if( document.implementation.hasFeature("XPath", "3.0") )
{
	HTMLElement.prototype.__defineSetter__("outerHTML", function (sHTML) {
	var r = this.ownerDocument.createRange();
	r.setStartBefore(this);
	var df = r.createContextualFragment(sHTML);
	this.parentNode.replaceChild(df, this);
	});


	var _emptyTags = {
	"IMG":   true,
	"BR":    true,
	"INPUT": true,
	"META":  true,
	"LINK":  true,
	"PARAM": true,
	"HR":    true
	};

	HTMLElement.prototype.__defineGetter__("outerHTML", function () {
	var attrs = this.attributes;
	var str = "<" + this.tagName;
	for (var i = 0; i < attrs.length; i++)
		str += " " + attrs[i].name + "=\"" + attrs[i].value + "\"";

	if (_emptyTags[this.tagName])
		return str + ">";

	return str + ">" + this.innerHTML + "</" + this.tagName + ">";
	});
}