Array.prototype.indexOf=function(val)
{
	for(i=0;i<this.length;i++)
	{
		if(this[i]==val)
			return i;
	}

	return -1;
}

var psid = 0;
var pendingCount = false;
var counting = false;
var mapSearchLoaded = false;
var ctlID = "";
var companyID = -2147483648;
var officeID = -2147483648;
var homepageID = -2147483648;
var contactID = -2147483648;
var tabSelectedClass = "";
var tabUnselectedClass = "";
var reloNetworkID = "";
var buttonClass = "";
var map = null;
var loadMapFunction = null;
var SelectedCounties = new Array();
var SelectedCities = new Array();
var SelectedMLSAreas = new Array();
var SelectedSchoolDistricts = new Array();
var isIDX = true;
var mapMarkerPopupUrl = "/Common/PublicPropSearch/MapMarkerDetail.aspx";
var allowSoldSearch = false;
var hasMLSsForSoldSearch = false;
var sourceApp = "";
var SE_PS_Count = 0;
var isLoading = true;

function ge(id)
{
	return document.getElementById(id);
}

function Search()
{
	this.publicSearchID = -2147483648;
	this.classStandardNames = "";
	this.hasFireplace = false;
	this.hasPhoto = false;
	this.hasPool = false;
	this.hasTour = false;
	this.includeNulls = false;
	this.isSoldSearch = false;
	this.daysBacksold = 0;
	this.daysBackListed = null;
	this.isCompanyListingsOnly = false;
	this.isCompanySoldSearch = false;
	this.isBankOwned = false;
	this.listingID = "";
	this.maxAge = "";
	this.maxBaths = "";
	this.maxBeds = "";
	this.maxPrice = "";
	this.maxSqft = "";
	this.minBaths = "";
	this.minBeds = "";
	this.minGarageSpaces = "";
	this.minLotSize = "";
	this.maxLotSize = "";
	this.minPrice = "";
	this.minSqft = "";
	this.nwLat = "";
	this.nwLon = "";
	this.sortPrimary = "";
	this.searchType = "";
	this.seLat = "";
	this.seLon = "";
	this.state = "";
	this.stories = "";
	this.streetName = "";
	this.vAddress = "";
	this.vCity = "";
	this.vRadius = "";
	this.vState = "";
	this.vZip = "";
	this.zipCodes = "";
	this.zoomLevel = "";
	this.cities = new Array();
	this.counties = new Array();
	this.mlsAreas = new Array();
	this.schoolDistricts = new Array();
	this.ToXML = Search_ToXML;
	this.LoadFromForm = Search_LoadFromForm;
	this.mlsIdsToSearch = "";
    this.searchPurpose = "";    
    this.OpenHomeTimeFrame = "";
    this.mapBoundaryPoints = new Array();
    this.mapBoundariesTempGUID = "";
    this.regionID = "";
    this.ShowCompanyFeatures = "";
    this.ShowOfficeFeatures = "";
    this.CurrentUserId = "";
}

function countyItem(name,county)
{
	this.name = name;
	this.county = county;
	this.optionValue = name.replace(",", "&comma;") + "|" + county;
}

function Search_ToXML()
{
	var d = createXMLDocument("publicSearch");
	var root = d.documentElement;
	AppendXmlNode(d, root, "publicSearchID", (this.publicSearchID != 0 ? this.publicSearchID : ""));
	AppendXmlNode(d, root, "companyID", companyID);
	AppendXmlNode(d, root, "officeID", officeID);
	AppendXmlNode(d, root, "homepageID", homepageID);
	AppendXmlNode(d, root, "contactID", contactID);
	AppendXmlNode(d, root, "classStandardNames", this.classStandardNames);
	AppendXmlNode(d, root, "hasFireplace", this.hasFireplace ? "true" : "false");
	AppendXmlNode(d, root, "hasPhoto", this.hasPhoto ? "true" : "false");
	AppendXmlNode(d, root, "hasPool", this.hasPool ? "true" : "false");
	AppendXmlNode(d, root, "hasTour", this.hasTour ? "true" : "false");
	AppendXmlNode(d, root, "isBankOwned", this.isBankOwned ? "true" : "false");
	AppendXmlNode(d, root, "idx", isIDX ? "true" : "false");
	AppendXmlNode(d, root, "includeNulls", this.includeNulls ? "true" : "false");
	AppendXmlNode(d, root, "isSoldSearch", this.isSoldSearch ? "1" : "0");
	AppendXmlNode(d, root, "allowSoldSearch", allowSoldSearch ? "1" : "0");
	AppendXmlNode(d, root, "daysBackSold", this.daysBackSold)
	AppendXmlNode(d, root, "daysBackListed", this.daysBackListed)
	AppendXmlNode(d, root, "isCompanyListingsOnly", this.isCompanyListingsOnly ? "1" : "0");
	AppendXmlNode(d, root, "isCompanySoldSearch", this.isCompanySoldSearch ? "1" : "0");
	AppendXmlNode(d, root, "listingID", this.listingID);
	AppendXmlNode(d, root, "maxAge", this.maxAge);
	AppendXmlNode(d, root, "maxBaths", this.maxBaths);
	AppendXmlNode(d, root, "maxPrice", this.maxPrice);
	AppendXmlNode(d, root, "maxSqft", this.maxSqft);	
	AppendXmlNode(d, root, "maxBeds", this.maxBeds);
	AppendXmlNode(d, root, "minBaths", this.minBaths);
	AppendXmlNode(d, root, "minBeds", this.minBeds);
	AppendXmlNode(d, root, "minGarageSpaces", this.minGarageSpaces);
	AppendXmlNode(d, root, "minLotSize", this.minLotSize);
	AppendXmlNode(d, root, "maxLotSize", this.maxLotSize);
	AppendXmlNode(d, root, "minPrice", this.minPrice);
	AppendXmlNode(d, root, "minSqft", this.minSqft);
	AppendXmlNode(d, root, "mlsIdsToSearch", this.mlsIdsToSearch);
	AppendXmlNode(d, root, "zoomLevel", this.zoomLevel);
	AppendXmlNode(d, root, "nwLat", this.nwLat);
	AppendXmlNode(d, root, "nwLon", this.nwLon);	
	AppendXmlNode(d, root, "sortPrimary", this.sortPrimary);
	AppendXmlNode(d, root, "searchType", this.searchType);
	AppendXmlNode(d, root, "seLat", this.seLat);
	AppendXmlNode(d, root, "seLon", this.seLon);
	AppendXmlNode(d, root, "state", this.state);
	AppendXmlNode(d, root, "stories", this.stories);
	AppendXmlNode(d, root, "streetName", this.streetName);
	AppendXmlNode(d, root, "vAddress", this.vAddress);
	AppendXmlNode(d, root, "vCity", this.vCity);
	AppendXmlNode(d, root, "vRadius", this.vRadius);
	AppendXmlNode(d, root, "vState", this.vState);
	AppendXmlNode(d, root, "vZip", this.vZip);
	AppendXmlNode(d, root, "zipCodes", this.zipCodes);
	AppendXmlNode(d, root, "zoomLevel", this.zoomLevel);
	AppendXmlNode(d, root, "searchPurpose", this.searchPurpose);
	AppendXmlNode(d, root, "OpenHomeTimeFrame", this.OpenHomeTimeFrame);
	AppendXmlNode(d, root, "regionID", this.regionID);
	AppendXmlNode(d, root, "ShowCompanyFeatures", this.ShowCompanyFeatures);
	AppendXmlNode(d, root, "ShowOfficeFeatures", this.ShowOfficeFeatures);
	AppendXmlNode(d, root, "CurrentUserId", this.CurrentUserId);
	
	var countiesNode = d.createElement("counties");
	for(var i = 0; i < this.counties.length; i++)
		AppendXmlNode(d, countiesNode, "county", this.counties[i].XmlEncode());
	root.appendChild(countiesNode);
	
	var citiesNode = d.createElement("cities");
	for(var i = 0; i < this.cities.length; i++)
		AppendXmlNode(d, citiesNode, "city", this.cities[i].XmlEncode());
	root.appendChild(citiesNode);

	var mlsAreasNode = d.createElement("mlsAreas");
	for(var i = 0; i < this.mlsAreas.length; i++)
		AppendXmlNode(d, mlsAreasNode, "mlsArea", this.mlsAreas[i].XmlEncode());
	root.appendChild(mlsAreasNode);
	
	var schoolDistrictsNode = d.createElement("schoolDistricts");
	for(var i = 0; i < this.schoolDistricts.length; i++)
		AppendXmlNode(d, schoolDistrictsNode, "schoolDistrict", this.schoolDistricts[i].XmlEncode());
	root.appendChild(schoolDistrictsNode);
	
	AppendXmlNode(d, root, "mapBoundariesTempGUID", this.mapBoundariesTempGUID.XmlEncode());
	var mapBoundaryPointsNode = d.createElement("MapBoundaryPoints");
	for(var i = 0; i < this.mapBoundaryPoints.length; i++)
	{
	    var mapBoundaryPoint = d.createElement("MapBoundaryPoint");
		AppendXmlNode(d, mapBoundaryPoint, "Latitude", this.mapBoundaryPoints[i].latitude);
		AppendXmlNode(d, mapBoundaryPoint, "Longitude", this.mapBoundaryPoints[i].longitude);
		mapBoundaryPointsNode.appendChild(mapBoundaryPoint);	
	}
	root.appendChild(mapBoundaryPointsNode);
	
	return d;
}

function Search_LoadFromForm()
{
	this.publicSearchID = psid;
	this.classStandardNames = GetSelectedClasses();
	this.hasFireplace = ge(ctlID + "chkHasFireplace").checked;
	this.hasPhoto = ge(ctlID + "chkHasPhoto").checked;
	this.hasPool = ge(ctlID + "chkHasPool").checked;
	this.hasTour = ge(ctlID + "chkHasTour").checked;
	this.isBankOwned = ge(ctlID + "chkIsBankOwned").checked;
	this.includeNulls = ge(ctlID + "chkIncludeNulls").checked;
	if (ge(ctlID + "trSoldSearch"))
	{
		this.isSoldSearch =  SE_GetSelectedValue(ge(ctlID + "cboBasicSearchStatus")) == "S";
		this.daysBackSold = SE_GetSelectedValue(ge(ctlID + "cboDaysBackSold"));
	}
	else
		this.isSoldSearch = false;
	if (ge(ctlID + "chkCompanyListingsOnly"))
		this.isCompanyListingsOnly = ge(ctlID + "chkCompanyListingsOnly").checked;
	this.isCompanySoldSearch = (ge(ctlID + "hdnIsCompanySoldSearch").value == "true");
	if (this.isCompanySoldSearch)
	    this.daysBackSold = ge(ctlID + "hdnDaysBackSold").value;
	this.daysBackListed = SE_GetSelectedValue(ge(ctlID + "cboDaysBackListed"));
	this.listingID = ge(ctlID + "txtMLSID").value;
	this.maxAge = SE_GetSelectedValue(ge(ctlID + "cboAge"));
	this.maxBaths = SE_GetSelectedValue(ge(ctlID + "cboMaxBathrooms"));
	this.maxBeds = SE_GetSelectedValue(ge(ctlID + "cboMaxBedrooms"));
	this.maxPrice = SE_GetSelectedValue(ge(ctlID + "cboMaxPrice"));
	this.maxSqft = SE_GetSelectedValue(ge(ctlID + "cboMaxSQFT"));
	this.minBaths = SE_GetSelectedValue(ge(ctlID + "cboMinBathrooms"));
	this.minBeds = SE_GetSelectedValue(ge(ctlID + "cboMinBedrooms"));
	this.minGarageSpaces = SE_GetSelectedValue(ge(ctlID + "cboMinGarageSpaces"));
	this.minLotSize = SE_GetSelectedValue(ge(ctlID + "cboMinLotSize"));
	this.maxLotSize = SE_GetSelectedValue(ge(ctlID + "cboMaxLotSize"));
	this.minPrice = SE_GetSelectedValue(ge(ctlID + "cboMinPrice"));
	this.minSqft = SE_GetSelectedValue(ge(ctlID + "cboMinSQFT"));	
	if (this.isSoldSearch)
		this.mlsIdsToSearch = ge(ctlID + "hdnMLSsToSoldSearch").value;	
	else
		this.mlsIdsToSearch = ge(ctlID + "hdnMLSsToSearch").value;	
	this.sortPrimary = SE_GetSelectedValue(ge(ctlID + "cboSortPrimary"));
	this.searchType = ge(ctlID + "txtSearchType").value;
	this.state = SE_GetSelectedValue(ge(ctlID + "cboState"));
	this.stories = SE_GetSelectedValue(ge(ctlID + "cboStories"));
	this.streetName = ge(ctlID + "txtStreetName").value;
	this.vAddress = ge(ctlID + "txtAddress").value;
	this.vCity = ge(ctlID + "txtCity").value;
	this.vRadius = SE_GetSelectedValue(ge(ctlID + "cboVicinityRadius"));
	this.vState = ge(ctlID + "txtState").value;
	this.vZip = ge(ctlID + "txtZipCode").value;
	this.zipCodes = ge(ctlID + "txtZipCodes").value;
	this.searchPurpose = ge(ctlID + "hdnsearchPurpose").value;	
	this.regionID = ge(ctlID + "hdnregionID").value;
	this.ShowCompanyFeatures = ge(ctlID + "hdnShowCompanyFeatures").value;
	this.ShowOfficeFeatures = ge(ctlID + "hdnShowOfficeFeatures").value;
	this.CurrentUserId = ge(ctlID + "hdnCurrentUserId").value;
	
	if (ge(ctlID + "cboTimeFrame"))
	    this.OpenHomeTimeFrame = ge(ctlID + "cboTimeFrame").value;	
	
	if (ge(ctlID + "txtSearchType").value.toLowerCase() == "map")
	{
	    var bounds = map.getBounds();
	    this.nwLat = bounds.northWest.latitude;
        this.nwLon = bounds.northWest.longitude;
        this.seLat = bounds.southEast.latitude;
        this.seLon = bounds.southEast.longitude;    
        this.zoomLevel = map.zoomLevel;
        this.mapBoundaryPoints = map.drawGetPoints();
        this.mapBoundariesTempGUID = map.mapBoundariesTempGUID;
        map.map.ShowDashboard();
        map._reSize();
    }   
	
	for(var i = 0; i < ge(ctlID + "lstCounties").options.length; i++)
		if(ge(ctlID + "lstCounties").options[i].selected)
			this.counties[this.counties.length] = ge(ctlID + "lstCounties").options[i].value;
			
	for(var i = 0; i < ge(ctlID + "lstCities").options.length; i++)
		if(ge(ctlID + "lstCities").options[i].selected)
			this.cities[this.cities.length] = ge(ctlID + "lstCities").options[i].value;
	
	for(var i = 0; i < ge(ctlID + "lstMLSAreas").options.length; i++)
		if(ge(ctlID + "lstMLSAreas").options[i].selected)
			this.mlsAreas[this.mlsAreas.length] = ge(ctlID + "lstMLSAreas").options[i].value.replace("&comma;", ",");

	for(var i = 0; i < ge(ctlID + "lstSchoolDistricts").options.length; i++)
		if(ge(ctlID + "lstSchoolDistricts").options[i].selected)
			this.schoolDistricts[this.schoolDistricts.length] = ge(ctlID + "lstSchoolDistricts").options[i].value.replace("&comma;", ",");
}

function ClearSearch()
{
	ge(ctlID + "chkHasFireplace").checked = false;
	ge(ctlID + "chkHasPhoto").checked = false;
	ge(ctlID + "chkHasPool").checked = false;
	ge(ctlID + "chkHasTour").checked = false;
	ge(ctlID + "chkIsBankOwned").checked = false;
	ge(ctlID + "cboBasicSearchStatus").selectedIndex = 0;
	if (ge(ctlID + "chkCompanyListingsOnly"))
		ge(ctlID + "chkCompanyListingsOnly").checked = false;
	ge(ctlID + "chkIncludeNulls").checked = false;
	ge(ctlID + "txtMLSID").value = "";
	ge(ctlID + "cboAge").selectedIndex = 0;
	ge(ctlID + "cboMaxBathrooms").selectedIndex = 0;
	ge(ctlID + "cboMaxBedrooms").selectedIndex = 0;
	ge(ctlID + "cboMaxPrice").selectedIndex = 0;
	ge(ctlID + "cboMaxSQFT").selectedIndex = 0;
	ge(ctlID + "cboMinBathrooms").selectedIndex = 0;
	ge(ctlID + "cboMinBedrooms").selectedIndex = 0;
	ge(ctlID + "cboMinGarageSpaces").selectedIndex = 0;
	ge(ctlID + "cboMinLotSize").selectedIndex = 0;
	ge(ctlID + "cboMaxLotSize").selectedIndex = 0;
	ge(ctlID + "cboMinPrice").selectedIndex = 0;
	ge(ctlID + "cboMinSQFT").selectedIndex = 0;
	ge(ctlID + "cboStories").selectedIndex = 0;
	ge(ctlID + "txtStreetName").value = "";
	ge(ctlID + "txtAddress").value = "";
	ge(ctlID + "txtCity").value = "";
	ge(ctlID + "cboVicinityRadius").selectedIndex = 0;
	ge(ctlID + "txtState").value = "";
	ge(ctlID + "txtZipCode").value = "";
	ge(ctlID + "txtZipCodes").value = "";
	ge(ctlID + "lstCounties").selectedIndex = -1;
	ge(ctlID + "lstCities").selectedIndex = -1;
	ge(ctlID + "lstMLSAreas").selectedIndex = -1;
	ge(ctlID + "lstSchoolDistricts").selectedIndex = -1;
	SelectSearchType("Regional");
	map.drawDone();
	GetCount();
}


function DisableFormElements()
{
	if(ge(ctlID + "cboMapLocations") != null)
		ge(ctlID + "cboMapLocations").disabled = true;
		
	if (map)
		map.showUpdatingMsg(true);
}

function EnableFormElements()
{
	if(ge(ctlID + "cboMapLocations") != null)
		ge(ctlID + "cboMapLocations").disabled = false;
	
	if (map) 
		map.showUpdatingMsg(false);

	
}
function EnableTabs()
{
	EnableTab("tdRegionalSearchTab");
	EnableTab("tdMapSearchTab");
	EnableTab("tdVicinitySearchTab");
	EnableTab("tdMlsIdSearchTab");
	EnableTab("tdListingAreaSearchTab");
	EnableTab("tdSchoolDistrictSearchTab");
	EnableTab("tdAdvancedMLSSearchTab");
	EnableTab("tdNationalSearchTab");
}

function EnableTab(tabName)
{
	tab = document.getElementById(ctlID + tabName);
	if (tab)
		tab.className = tab.className.replace("disabled", "");
}

var vicinityCheck = "";
function GetCount()
{
    var strDisplayText = "Properties";
    if (ge(ctlID + "cboTimeFrame"))
        strDisplayText = "Open Homes";
    
	if (isLoading) return;
	SE_PS_Count = 0;
	if(ge("divCount") != null)
	{
		if(!counting && ctlID)
		{
			var st = ge(ctlID + "txtSearchType").value.toLowerCase();

			DisableFormElements();
			var btn = "<br/><br/><input type=button class=" + buttonClass 
				+ " onclick=ge('" + ctlID + "btnSearch').click(); value='" + ge(ctlID + "btnSearch").value + "'>"; 
			if(st == "vicinity")
			{
			    ge(ctlID + "btnSearch").disabled = true;
				vicinityCheck = checkVicinity();
				ge(ctlID + "btnSearch").disabled = false;
				if (vicinityCheck != "")
				{
					ge("divCount").innerHTML = "<em>" + vicinityCheck + "</em>";
					return;
				}
			}
			else
			    ge(ctlID + "btnSearch").disabled = false;
			
			if(st == "mlsid" && ge(ctlID + "txtMLSID").value.length == 0)
				ge("divCount").innerHTML = strDisplayText + " that match your search:<br/><br/><em>Please enter an MLS ID.</em>" + btn;
			else if (st == "advancedmls")
			{
				ge("divCount").innerHTML = strDisplayText +" that match your search:<br/><br/><em>Updating...</em>" + btn;
				pendingCount = false;
				counting = true;
				count = SE_AdvSearch_GetListingCount();
				SE_PS_Count = count;
				if (SE_IsInteger(count))
					ge("divCount").innerHTML = strDisplayText + " that match your search:<br/><br/> " + count.format("N0") + btn ;
				counting = false;				
			}
			else
			{
				var s = new Search();
				s.LoadFromForm();
				ge("divCount").innerHTML = strDisplayText + " that match your search:<br/><br/><em>Updating...</em>" + btn;
				pendingCount = false;
				counting = true;
				SendRequest('/Common/PublicPropSearch/ActionHandler.aspx?companyID=' + companyID + "&sourceApp=" + sourceApp, s.ToXML());				
			}
		}
		else
			pendingCount = true;
	}
}

function checkVicinity()
{
	if ((ge(ctlID + "txtAddress").value == "")
		|| 
			(!(	(ge(ctlID + "txtZipCode").value != "") 
				|| (ge(ctlID + "txtCity").value != "" && ge(ctlID + "txtState").value != "")
			))
		)
		return "Please enter an address.";
	else
	{
		var xml = createXMLDocument("VicinitySearch");
		var root = xml.documentElement;
		
		AppendXmlNode(xml, root, "address", ge(ctlID + "txtAddress").value);
		AppendXmlNode(xml, root, "city", ge(ctlID + "txtCity").value);
		AppendXmlNode(xml, root, "state", ge(ctlID + "txtState").value);
		AppendXmlNode(xml, root, "zip", ge(ctlID + "txtZipCode").value);
	
		if (SE_PostHttpText("/Common/PublicPropSearch/ActionHandler.aspx?", xml, "RETURN_STATUS").toLowerCase().indexOf('failed') != -1)
			return "The address you entered could not be mapped."
		else
			return "";
	}
}

var SE_HttpIsBusy = false;
function GetHTTPRequestObject()
{
	var httpRequest = null;
	
	if (window.XMLHttpRequest)
		httpRequest = new XMLHttpRequest();
	else if (window.ActiveXObject)
		httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
	
	return httpRequest;
}

function SendRequest(serverURL, xml)
{
	var httpRequest = GetHTTPRequestObject();

	if(httpRequest != null)
	{
		try
		{
			if (SE_HttpIsBusy)
			{
			    httpRequest.onreadystatechange = function () {}
	            httpRequest.abort();
			}
			SE_HttpIsBusy = true;
			httpRequest.open("POST", serverURL, true);
			httpRequest.setRequestHeader("Content-Type", "text/xml");
			httpRequest.onreadystatechange = function(){
				watchState(httpRequest);
			}

			httpRequest.send(xml);
			return httpRequest;
		}
		catch(e)
		{
			return null;
		}
	}
	else
		return null;
}

function watchState(Response)
{//debugger;
//	if(pendingCount)
//	{
//		counting=false;
		//Response.onreadystatechange = function(){};
//		Response.abort();
//		GetCount();
//		return;
//	}
	
	if(Response.readyState == 4)
	{
		counting = false;
		SE_HttpIsBusy = false;
		if (Response.status == 200)
		{		
			if(Response != null)
			{
				var xmlDoc = createXMLFromString(Response.responseText);
				SE_PS_Count = GetNodeAttribute(xmlDoc, "count", "value");

                var strDisplayText = "Properties";
                if (ge(ctlID + "cboTimeFrame"))
                    strDisplayText = "Open Homes";

				ge("divCount").innerHTML = strDisplayText + " that match your search:<br/><br/>" 
					+ GetNodeAttribute(xmlDoc, "count", "value").format("N0") 
					+ "<br/><br/><input type=button class=" + buttonClass 
					+ " onclick=ge('" + ctlID + "btnSearch').click(); value='" + ge(ctlID + "btnSearch").value + "'>" 
					+ (ge(ctlID + "txtSearchType").value.toLowerCase() == "map" && parseInt(GetNodeAttribute(xmlDoc, "count", "value"))  > 100 ? 
						"<br/><br/><div style='font-size:8pt;font-weight:normal;padding:7px;'>Note: The first 100 properties are shown at a time. Zoom in to see more properties.</div>" 
						: "");
				LoadProperties(xmlDoc);
				EnableFormElements();
			}
		}
		else
		{
			Response.onreadystatechange = function(){}
			alert("An error was encountered while executing your search.\n\n" + Response.status + ": " + Response.statusText + "\n\n" );// + Response.responseText);
			pendingCount = false;
			counting = false;
		}
		if(pendingCount)
			GetCount();
	}
} 

function LoadProperties(xmlDoc)
{
	newMarker = new SEMarker();
	map.markers = new Array();	
	nodes = xmlDoc.getElementsByTagName("prop");
	
	for(var i = 0; i < nodes.length; i++)
	{
		var node = nodes.item(i);
		newMarker = new SEMarker();
		newMarker.map = map;
		newMarker.location = new SEPoint(node.getAttribute("lat"), node.getAttribute("lon"));
		newMarker.tooltipHtml = node.getAttribute("addr");
		newMarker.htmlPageUrl = "http://" + window.location.hostname + mapMarkerPopupUrl + (mapMarkerPopupUrl.indexOf("?") >= 0 ? "&" : "?") + "listingID=" + node.getAttribute("id");
		newMarker.htmlBirdsEyeDetailUrl = "http://" + window.location.hostname + mapMarkerPopupUrl + (mapMarkerPopupUrl.indexOf("?") >= 0 ? "&" : "?") + "listingID=" + node.getAttribute("id");
		map.markers[map.markers.length] = newMarker;
	}
	
	map.addMarkers();
}

function GetSelectedClasses()
{
	var classList = "";
	classList = AppendSelectedClassValue(ge(ctlID + "chkClassSFR"), classList);
	classList = AppendSelectedClassValue(ge(ctlID + "chkClassCON"), classList);
	classList = AppendSelectedClassValue(ge(ctlID + "chkClassLTL"), classList);
	classList = AppendSelectedClassValue(ge(ctlID + "chkClassMOB"), classList);
	classList = AppendSelectedClassValue(ge(ctlID + "chkClassRNT"), classList);
	classList = AppendSelectedClassValue(ge(ctlID + "chkClassFRM"), classList);
	classList = AppendSelectedClassValue(ge(ctlID + "chkClassINC"), classList);
	classList = AppendSelectedClassValue(ge(ctlID + "chkClassCOM"), classList);
	return classList;
}

function AppendSelectedClassValue(checkbox, currentList)
{
	return ((checkbox != null && checkbox.checked) ? currentList + (currentList.length > 0 ? "," : "") + checkbox.value : currentList);
}

function PreviousType()
{
	if (ge(ctlID + "tdRegionalSearchTab") && ge(ctlID + "tdRegionalSearchTab").className == tabSelectedClass)
		return "Regional"
	else if (ge(ctlID + "tdVicinitySearchTab") && ge(ctlID + "tdVicinitySearchTab").className == tabSelectedClass)
		return "Vicinity"
	else if (ge(ctlID + "tdMapSearchTab") && ge(ctlID + "tdMapSearchTab").className == tabSelectedClass)
		return "Map"
	else if (ge(ctlID + "tdVicinitySearchTab") && ge(ctlID + "tdVicinitySearchTab").className == tabSelectedClass)
		return "tbMlsIdSearch"
	else if (ge(ctlID + "tdMlsIdSearchTab") && ge(ctlID + "tdMlsIdSearchTab").className == tabSelectedClass)
		return "MLSID"
	else if (ge(ctlID + "tdListingAreaSearchTab") && ge(ctlID + "tdListingAreaSearchTab").className == tabSelectedClass)
		return "MLSArea"
	else if (ge(ctlID + "tdSchoolDistrictSearchTab") && ge(ctlID + "tdSchoolDistrictSearchTab").className == tabSelectedClass)
		return "SchoolDistrict"
	else if (ge(ctlID + "tdAdvancedMLSSearchTab") && ge(ctlID + "tdAdvancedMLSSearchTab").className == tabSelectedClass)
		return "AdvancedMLS"

}

function SelectSearchType(type)
{	
	var previousType= PreviousType();
	if(type.length == 0) type = ge(ctlID + "txtSearchType").value;
	if(type.length == 0) type = "Regional";
	
	if (isLoading) return;
	
	
	ShowFeatures();
	ShowClasses();
	ShowSoldSearchOptions();
	
	ge(ctlID + "tdRegionalSearchTab").className = tabUnselectedClass;
	ge(ctlID + "tbRegionalSearch").style.display = "none";
	ge(ctlID + "pnlCityList").style.display = "none";
	ge(ctlID + "pnlAreaList").style.display = "none";
	ge(ctlID + "pnlSchoolDistrict").style.display = "none";
	ge(ctlID + "trZipCodes").style.display = "none";	
	HideAdvancedMLSSearch();				
	
	if(ge(ctlID + "tdVicinitySearchTab") != null)
	{
		ge(ctlID + "tdVicinitySearchTab").className = tabUnselectedClass;
		ge(ctlID + "tbVicinitySearch").style.display = "none";
	}
	else if(type == "Vicinity")
		type = "Regional";		
	
	if(ge(ctlID + "tdMapSearchTab") != null)
	{
		ge(ctlID + "tdMapSearchTab").className = tabUnselectedClass;
		ge(ctlID + "tbMapSearch").style.display = "none";
	}
	else if(type == "Map")
		type = "Regional";
	
	if(ge(ctlID + "tdMlsIdSearchTab") != null)
	{
		ge(ctlID + "tdMlsIdSearchTab").className = tabUnselectedClass;
		ge(ctlID + "tbMlsIdSearch").style.display = "none";	
	}
	else if(type == "MLSID")
		type = "Regional";
		
	if(ge(ctlID + "tdListingAreaSearchTab") != null)
	{
		ge(ctlID + "tdListingAreaSearchTab").className = tabUnselectedClass;
		ge(ctlID + "pnlAreaList").style.display = "none";
	}
	else if(type == "MLSArea")
		type = "Regional";
	
	if(ge(ctlID + "tdSchoolDistrictSearchTab") != null)
	{
		ge(ctlID + "tdSchoolDistrictSearchTab").className = tabUnselectedClass;
		ge(ctlID + "pnlSchoolDistrict").style.display = "none";
	}
	else if(type == "SchoolDistrict")
		type = "Regional";
	
	if(ge(ctlID + "tdAdvancedMLSSearchTab") != null)
	{
		ge(ctlID + "tdAdvancedMLSSearchTab").className = tabUnselectedClass;
		HideAdvancedMLSSearch();	
	}
	else if(type == "AdvancedMLS")
		type = "Regional";	

	ge(ctlID + "txtSearchType").value = type;
	
	if (ge(ctlID + "trListingsPerPage"))
		ge(ctlID + "trListingsPerPage").style.display = "";

    HandleTitleFocus()
	
	switch(type)
	{
		case "Regional":
			ge(ctlID + "spnCitiesAreas").innerText = "cities";
			ge(ctlID + "tbRegionalSearch").style.display = "block";
			ge(ctlID + "tdRegionalSearchTab").className = tabSelectedClass;
			ge(ctlID + "pnlCityList").style.display = "block";
			ge(ctlID + "trZipCodes").style.display = "";
			ClearMapSearch();
			ClearVicinitySearch();
			ClearListingAreaSearch();
			ge(ctlID + "pnlCityList").style.display = "block";
			ClearMLSIDSearch();
			ClearSchoolDistrictSearch();
			if (previousType != "AdvancedMLS")
				ChangeCounty(ge(ctlID + "lstCounties"));
			break;
		
		case "Vicinity":
			ge(ctlID + "tbVicinitySearch").style.display = "block";
			ge(ctlID + "tdVicinitySearchTab").className = tabSelectedClass;
			ClearMapSearch();
			ClearRegionalSearch();
			ClearListingAreaSearch();
			ClearMLSIDSearch();
			ClearSchoolDistrictSearch();
			ClearAdvancedMLSSearch();
			HandleVicinityStateChange(true)
			if (!focusSet) SE_SetFieldFocus(ctlID + "txtAddress");
			break;
		
		case "Map":
		    ge(ctlID + "tbMapSearch").style.display = "block";
		    ge(ctlID + "tdMapSearchTab").className = tabSelectedClass;		    
			if(!mapSearchLoaded)
				loadMapFunction();
			mapSearchLoaded = true;		
			ClearRegionalSearch();
			ClearVicinitySearch();
			ClearListingAreaSearch();
			ClearMLSIDSearch();
			ClearSchoolDistrictSearch();
			ClearAdvancedMLSSearch();
			HandleMapStateChange();
			map.map.ShowDashboard();
			map._reSize();		
			break;
		
		case "MLSID":
			ge(ctlID + "tbMlsIdSearch").style.display = "block";
			ge(ctlID + "tdMlsIdSearchTab").className = tabSelectedClass;
			ClearRegionalSearch();
			ClearMapSearch();
			ClearVicinitySearch();
			ClearListingAreaSearch();
			HideClasses();
			HideFeatures();
			HideSoldSearchOptions();
			ClearSchoolDistrictSearch();
			ClearAdvancedMLSSearch();
			if (!focusSet) SE_SetFieldFocus(ctlID + "txtMLSID");
			break;
		
		case "MLSArea":
			ge(ctlID + "spnCitiesAreas").innerText = "areas";
			ge(ctlID + "tbRegionalSearch").style.display = "block";
			ge(ctlID + "tdListingAreaSearchTab").className = tabSelectedClass;
			ge(ctlID + "pnlAreaList").style.display = "block";
			ClearMapSearch();
			ClearVicinitySearch();
			ClearMLSIDSearch();
			ClearCitySearch();
			ClearSchoolDistrictSearch();
			ClearAdvancedMLSSearch();
			if (previousType != "AdvancedMLS")
				ChangeCounty(ge(ctlID + "lstCounties"));
			break;
		
		case "SchoolDistrict":
			ge(ctlID + "spnCitiesAreas").innerText = "school districts";
			ge(ctlID + "tbRegionalSearch").style.display = "block";
			ge(ctlID + "tdSchoolDistrictSearchTab").className = tabSelectedClass;
			ge(ctlID + "pnlSchoolDistrict").style.display = "block";
			ClearListingAreaSearch();
			ClearMapSearch();
			ClearVicinitySearch();
			ClearMLSIDSearch();
			ClearCitySearch();
			ClearAdvancedMLSSearch();
			if (previousType != "AdvancedMLS")
				ChangeCounty(ge(ctlID + "lstCounties"));
			break;
			
		case "AdvancedMLS":
			ShowAdvancedMLSSearch();
			ge(ctlID + "tdAdvancedMLSSearchTab").className = tabSelectedClass;
			//ClearRegionalSearch();
			//ClearMapSearch();
			//ClearVicinitySearch();
			//ClearListingAreaSearch();
			//ClearMLSIDSearch();
			//ClearSchoolDistrictSearch();
			HideSoldSearchOptions();
			HideClasses();
			HideFeatures();
			break;
	}
	GetCount();
}

function HandleTitleFocus()
{
    if(ge(ctlID + "txtProfileName") != null
        && ge(ctlID + "tbProfileName").className != "hidden" 
        && ge(ctlID + "txtProfileName").value.trim().length == 0)
    {
        focusSet = true;
        SE_SetFieldFocus(ctlID + "txtProfileName");
    }
    else
        focusSet = false;  
}

function LoadSelectedMapLocation()
{
	if (ge(ctlID + "cboMapLocations").options.length > 0)
	{
		var s = ge(ctlID + "cboMapLocations").options[ge(ctlID + "cboMapLocations").selectedIndex].value.split("|");
		map.setCenterZoom(new SEPoint(parseFloat(s[0]), parseFloat(s[1])), parseInt(s[2]));
		map.clearDrawing();
		return HandleMapStateChange(parseFloat(s[0]), parseFloat(s[1]))
	}
}
function ReLoadSelectedMapLocation()
{
  LoadSelectedMapLocation();
}

function HandleMapStateChange(latitude, longitude)
{
	if (!latitude)
	{
		latitude = map.centerPoint.latitude;
		longitude = map.centerPoint.longitude;
	}
	map._mapDivWidth = map._mapDiv.offsetWidth;
	map._mapDivHeight = map._mapDiv.offsetHeight;
	
	var xml = createXMLDocument("GetStateFromLatLong");
	var root = xml.documentElement;	
	AppendXmlNode(xml, root, "latitude", latitude);
	AppendXmlNode(xml, root, "longitude", longitude);
	var mapState =  SE_PostHttpText("/Common/PublicPropSearch/ActionHandler.aspx?", xml, "STATE")
	
	if (SE_GetSelectedValue(ge(ctlID + "cboState")) != mapState)
	{	
		SE_SetSelectedValue(ge(ctlID + "cboState"), mapState)
		if (ge(ctlID + "cboState").value.toLowerCase() == SE_GetSelectedValue(ge(ctlID + "cboState")).toLowerCase())
			ge(ctlID + "cboState").onchange()
		EnableFormElements();
		return true;
	}
	return false;
}
function HandleVicinityStateChange(tabClick)
{
	if (ge(ctlID + "txtState").value.toLowerCase() != SE_GetSelectedValue(ge(ctlID + "cboState")).toLowerCase())
	{
		if (tabClick)
			ge(ctlID + "txtState").value = SE_GetSelectedValue(ge(ctlID + "cboState"));
		else
		{
			ge(ctlID + "txtState").value = ge(ctlID + "txtState").value.toUpperCase();
			SE_SetSelectedValue(ge(ctlID + "cboState"), ge(ctlID + "txtState").value.toUpperCase());
			ge(ctlID + "cboState").onchange();
			var xxx = ctlID + "$cboState";
			__doPostBack(xxx,'');
		}
	}
}

function ClearRegionalSearch()
{
	ge(ctlID + "lstCounties").selectedIndex = -1;
	ge(ctlID + "lstCities").selectedIndex = -1;
	ge(ctlID + "txtZipCodes").value = "";
}

function ShowFeatures()
{
	ge(ctlID + "tbFeatures").style.display = "block";
}

function HideFeatures()
{
	ge(ctlID + "tbFeatures").style.display = "none";
}

function ShowClasses()
{
	ge(ctlID + "tbClasses").style.display = "block";
}

function HideClasses()
{
	ge(ctlID + "tbClasses").style.display = "none";
}

function ShowSoldSearchOptions()
{
	if(ge(ctlID + "trSoldSearch") && allowSoldSearch && hasMLSsForSoldSearch)
	{
		ge(ctlID + "trSoldSearch").style.display = "";
		if(ge(ctlID + "trSoldSearchMessage") != null)
		{
			ge(ctlID + "trSoldSearchMessage").style.display = "";
			UpdateSoldSearchMessage();
		}
	}
}

function HideSoldSearchOptions()
{
	if (ge(ctlID + "trSoldSearch"))
	{
		ge(ctlID + "trSoldSearch").style.display = "none";
		if(ge(ctlID + "trSoldSearchMessage") != null)
			ge(ctlID + "trSoldSearchMessage").style.display = "none";
	}
}

function UpdateSoldSearchMessage()
{
	if (ge(ctlID + "trSoldSearch") && allowSoldSearch)
	{
		cboStatus = document.getElementById(ctlID + 'cboBasicSearchStatus');
		spnDaysBackSold = document.getElementById(ctlID + 'spnDaysBackSold');
		divAlertSold1 = document.getElementById('divAlertSold');
		if (cboStatus.options(cboStatus.selectedIndex).value == "A")
		{
			spnDaysBackSold.style.display = "none";
			if (divAlertSold1)
				divAlertSold1.style.display = "none";
		}
		else
		{
			spnDaysBackSold.style.display = "block";
			if (divAlertSold1)
				divAlertSold1.style.display = "block";
		}	
	}	
}
function ShowAdvancedMLSSearch()
{	
	ge(ctlID + "tbAdvancedSearch").style.display = "block";

}
function HideAdvancedMLSSearch()
{	
	ge(ctlID + "tbAdvancedSearch").style.display = "none";
}
function ClearMapSearch()
{
    this.mapBoundaryPoints = new Array();
    this.mapBoundariesTempGUID = "";
    
    if (map.map != null)
        map.drawInit();
}

function ClearVicinitySearch()
{
	ge(ctlID + "txtAddress").value = "";
	ge(ctlID + "txtCity").value = "";
	ge(ctlID + "txtState").value = "";
	ge(ctlID + "txtZipCode").value = "";
}

function ClearMLSIDSearch(){}

function ClearSchoolDistrictSearch()
{
	ge(ctlID + "lstSchoolDistricts").selectedIndex = -1;
}			

function ClearListingAreaSearch()
{
	ge(ctlID + "lstMLSAreas").selectedIndex = -1;
}

function ClearCitySearch()
{
	ge(ctlID + "lstCities").selectedIndex = -1;
	ge(ctlID + "txtZipCode").value = "";
}

function ClearAdvancedMLSSearch()
{}

function GoToRelo(companyID,reloNetworkID)
{
	var f = document.createElement("form");
			
	var url = document.createElement("input");
	url.type = "hidden";
	url.name = "toUrl";
	url.value = "http://www.relohomesearch.com/default.asp";
	f.appendChild(url);
	
	var args = document.createElement("input");
	args.type = "hidden";
	args.name = "args";
	args.value = "COID|" + reloNetworkID;
	f.appendChild(args);

	document.body.appendChild(f);
	
	f.action = "/ExternalPost.aspx";
	f.method = "POST";
	f.target = "_blank";
	f.submit();
}

var _tmpCounties = "";
var _tmpSelectedCounties = new Array();
var _tmpCurrentListCities = new Array();
var _tmpCurrentListMLSAreas = new Array();
var _tmpCurrentListSchoolDistricts = new Array();
var lastCompletedSearch = "";

function ChangeCounty(obj)
{
	ge(ctlID + "txtZipCodes").value = "";
	
	//build counties list
	var _tmpCounties = "";
	_tmpSelectedCounties = new Array();
	
	for(var i=0;i<obj.options.length;i++)
	{
		if(obj.options[i].selected)
		{
			_tmpCounties += (_tmpCounties.length > 0 ? "," : "") + obj.options[i].value;
			_tmpSelectedCounties[_tmpSelectedCounties.length] = obj.options[i].value;
		}
	}
	
	lastCompletedSearch = "";
	UpdateCountyItems("citySearch");
	UpdateCountyItems("mlsAreaSearch");
	UpdateCountyItems("schoolDistrictSearch");
	
	GetCount();
}

var MissingCounties = new Array();
function UpdateCountyItems(searchType)
{
	switch (searchType.toLowerCase())
	{	
		case "citysearch":
		    SelectedItems = SelectedCities;
			lstItems = ge(ctlID + "lstCities");
			items = Cities;
			_tmpCurrentItems = _tmpCurrentListCities;
			break;
		case "mlsareasearch":
		    if (lastCompletedSearch != "citySearch")
		    {
		        setTimeout('UpdateCountyItems("mlsAreaSearch")', 100);
		        return;
		    }
			SelectedItems = SelectedMLSAreas;
			lstItems = ge(ctlID + "lstMLSAreas");
			items = MLSAreas;
			_tmpCurrentItems = _tmpCurrentListMLSAreas;
			break;
		case "schooldistrictsearch":
			 if (lastCompletedSearch != "mlsAreaSearch")
		    {
		        setTimeout('UpdateCountyItems("schoolDistrictSearch")', 100);
		        return;
		    }
			SelectedItems = SelectedSchoolDistricts;
			lstItems = ge(ctlID + "lstSchoolDistricts");
			items = SchoolDistricts;
			_tmpCurrentItems = _tmpCurrentListSchoolDistricts;
			break;
	}
	
	// remember the selected items so we can reset them
	SelectedItems.length = 0;
	for (i=0; i<lstItems.options.length; i++)
	{
		if (lstItems.options[i].selected)
			SelectedItems[i] = lstItems.options[i];
	}
	
	// clone the counties array so we don't damage the original
	MissingCounties = new Array();
	for (i=0; i< _tmpSelectedCounties.length; i++)
	{
		MissingCounties[i] = _tmpSelectedCounties[i];
	}
	
	// reload the listbox with any items that we have previously downloaded
	lstItems.options.length = 0;
	var optionNum = 0;
	for(var i=0; i < items.length; i++)
	{
		if(_tmpCounties.indexOf(items[i].county)>-1)
		{
			if(MissingCounties.indexOf(items[i].county) != -1)
			{
				// we don't need to get items for this county - they are already in the items array
				MissingCounties.splice(MissingCounties.indexOf(items[i].county),1);
			}
			lstItems.options[optionNum] = new Option(items[i].name, items[i].optionValue);
			tmpCurrentItems[optionNum] = lstItems.options[optionNum];
			optionNum++;
		}
	}
	
	if(MissingCounties.length > 0)
	{
		lstItems.options.length = 0;			
		lstItems.options[0] = new Option("Loading...", "");
		lstItems.disabled = true;		
		LoadCountyItems(searchType,lstItems.id );
	}
	else
	{
		// we are done - re-sort and reselect
		sortOptions(lstItems);
		ReselectItems(lstItems, SelectedItems);
	}
	lastCompletedSearch = searchType;
}

function ReselectItems(lstItems, SelectedItems)
{
	for (i=0; i< lstItems.options.length; i++)
	{
		for (j=0; j < SelectedItems.length; j++)
		{
			if (lstItems.options[i] && lstItems.options[i].value && SelectedItems[j] && SelectedItems[j].value && lstItems.options[i].value == SelectedItems[j].value)
			{
				lstItems.options[i].selected = true;
				break;
			}
		}
	}
}

function LoadCountyItems(searchType, lstItemsId)
{
	var lstItems = ge(lstItemsId);
	var lstCounties = ge(ctlID + "lstCounties");
	var mlsIdsToSearch = ge(ctlID + "hdnMLSsToSearch").value;
	var Items;
	var SelectedItems;
	var PreviousItems;
	var DisplayType = ""
	switch (searchType.toLowerCase())
	{
		case "citysearch":
			Items = Cities;
			SelectedItems = SelectedCities;
			PreviousItems = _tmpCurrentListCities;
			DisplayType = "Cities";
			break;
		case "mlsareasearch":
			SelectedItems = SelectedMLSAreas;
			Items = MLSAreas;
			PreviousItems = _tmpCurrentListMLSAreas;
			DisplayType = "MLS Areas";
			break;
		case "schooldistrictsearch":
			SelectedItems = SelectedSchoolDistricts;
			Items = SchoolDistricts;
			PreviousItems = _tmpCurrentListSchoolDistricts;
			DisplayType = "School Districts";
			break;
	}
	
	var xml = createXMLDocument(searchType);
	var root = xml.documentElement;
	
	AppendXmlNode(xml, root, "state", SE_GetSelectedValue(ge(ctlID + "cboState")));
	AppendXmlNode(xml, root, "officeID", officeID);
	AppendXmlNode(xml, root, "homepageID", homepageID);
	AppendXmlNode(xml, root, "contactID", contactID);
	AppendXmlNode(xml, root, "mlsIdsToSearch", mlsIdsToSearch);
	
	for(var i=0; i < MissingCounties.length; i++)
		AppendXmlNode(xml, root, "county", MissingCounties[i]);
				
	var xmlDoc = createXMLFromString(SE_PostHttpText("/Common/PublicPropSearch/ActionHandler.aspx?companyID=" + companyID, xml, ""));
	var optionNum = 0;
		
	// add the items we already had before calling the server for more items
	for(var i = 0; i < PreviousItems.length; i++)
	{
		lstItems.options[optionNum] = PreviousItems[i];
		optionNum++;
	}
	PreviousItems.length = 0;
	
	// this should only add the items for the missing counties and should not overlap with previsouitesm
	for(var i = 0; i < xmlDoc.getElementsByTagName("ci").length; i++)
	{
		var node = xmlDoc.getElementsByTagName("ci").item(i);
		var ci = node.getAttribute("v").split("|");
		var thisItem = new countyItem(ci[0],ci[1]);
		Items[Items.length] = thisItem;
		
		var strValue = thisItem.optionValue;
		
		while (strValue.indexOf(",") > -1)
		    strValue = strValue.replace(",","&comma;");
		
		lstItems.options[optionNum] = new Option(thisItem.name, strValue);
		optionNum++;		
	}
		
	if (optionNum > 0)
	{
		// we are done - re-sort and re-select
		sortOptions(lstItems);	
		ReselectItems(lstItems, SelectedItems);				
		lstItems.disabled = false;
	}
	else
	{
		lstItems.options.length = 0;			
		lstItems.options[0] = new Option("No " + DisplayType + " found for this county.", "");
		lstItems.disabled = true;	
	}
}

function sortOptions(s)
{
    var t = new Array();
    
    for(var i = 0; i < s.options.length; i++)
        t[i] = new Array(s.options[i].text, s.options[i].value);

	if(t.length > 1)
		t.sort(function(a, b){if(a < b)return -1;if (a > b)return 1;return 0;});

    s.options.length = 0;

    for(var i = 0; i < t.length; i++)
        s.options[s.options.length] = new Option(t[i][0],t[i][1]);
}

function UpdateZipCodes()
{
	ge(ctlID + "lstCities").options.length = 0;
	
	for(var x = 0; x < ge(ctlID + "lstCounties").options.length; x++)
		ge(ctlID + "lstCounties").options[x].selected = false;
	
	var za = ge(ctlID + "txtZipCodes").value.split(",");
	var gz = new Array();

	for(var x = 0; x < za.length; x++)
		if(za[x].trim().length == 5)
			gz[gz.length] = za[x].trim();
	
	ge(ctlID + "txtZipCodes").value = gz.join(",");
	
	GetCount();
}

function ShowHidesStatus()
{
	var chk = ge(ctlID + "chkSendDroppedListings");
	
	if(chk != null)
	{
		if(chk.checked)
		{
			ge(ctlID + "chkActiveAllowed").disabled = false;
			ge(ctlID + "chkActiveAllowed").parentNode.disabled = false;
			ge(ctlID + "chkPendingAllowed").disabled = false;
			ge(ctlID + "chkPendingAllowed").parentNode.disabled = false;
			ge(ctlID + "chkSoldAllowed").disabled = false;
			ge(ctlID + "chkSoldAllowed").parentNode.disabled = false;
			ge(ctlID + "chkWithdrawnAllowed").disabled = false;
			ge(ctlID + "chkWithdrawnAllowed").parentNode.disabled = false;
			ge(ctlID + "chkExpiredAllowed").disabled = false;
			ge(ctlID + "chkExpiredAllowed").parentNode.disabled = false;
		}
		else
		{
			ge(ctlID + "chkActiveAllowed").disabled = true;
			ge(ctlID + "chkActiveAllowed").parentNode.disabled = true;
			ge(ctlID + "chkPendingAllowed").disabled = true;
			ge(ctlID + "chkPendingAllowed").parentNode.disabled = true;
			ge(ctlID + "chkSoldAllowed").disabled = true;
			ge(ctlID + "chkSoldAllowed").parentNode.disabled = true;
			ge(ctlID + "chkWithdrawnAllowed").disabled = true;
			ge(ctlID + "chkWithdrawnAllowed").parentNode.disabled = true;
			ge(ctlID + "chkExpiredAllowed").disabled = true;
			ge(ctlID + "chkExpiredAllowed").parentNode.disabled = true;
		}
	}
}

$().ready(function() {
    var $scrollingDiv = $("#divCount");
    var $countColumn = $("td[id$='tdCountColumn']");

    $(window).scroll(ScrollCount);
    //$(window).resize(ScrollCount);
    function ScrollCount() 
    {
        $scrollingDiv.stop()
        $scrollingDiv.animate({ "marginTop": Math.max(20, ($(window).scrollTop() - $countColumn.offset().top) + 20) + "px" }, "slow");
    }
});

function GetDocumentOffsetLeft(obj)
{
	var offset = 0;
	while(obj != null && obj.offsetParent != null)
	{
		offset += obj.offsetLeft;
		obj = obj.offsetParent;
	}
	return offset;
}

function GetDocumentOffsetTop(obj)
{
	var offset = 0;
	while(obj != null && obj.offsetParent != null)
	{
		offset += obj.offsetTop;
		obj = obj.offsetParent;
	}
	return offset;
}

function ClickSearchButton()
{
    if (ValidateSearch())
    {
        document.getElementById(ctlID + "btnSearch").click();
        return true;
    }
    else
        return false;       
}

function ValidateSearch()
{
	var hasError = false;
	var errorMessage = "";
	var st = ge(ctlID + "txtSearchType").value.toLowerCase();
	
	if (st == "vicinity" && vicinityCheck != "")
	{
	    alert('Unable to continue with your vicinity search.\n' + vicinityCheck);
	    return false;
	}
	
	if (SE_PS_Count > 1000)
	{
		alert('Your search returned more than 1000 listings. '
		    + '\nThe maximum listing count allowed is 1000. '
		    + '\nPlease narrow your search. ')
		return false;
	}
	
	if(ge(ctlID + "txtProfileName") != null && ge(ctlID + "txtProfileName").value.trim().length == 0)
	{
		hasError = true;
		errorMessage += "Please enter a name for your search.";
	}

	if(hasError)
		alert(errorMessage);
	else if (typeof(SE_AdvSearch_RunBSTSearch) != 'undefined') 
		return SE_AdvSearch_RunBSTSearch();
	return !hasError;
}

function ChangeClass()
{
  // get a handle to the min and max price controls
  var ctlMin = ge(ctlID + "cboMinPrice");
  var ctlMax = ge(ctlID + "cboMaxPrice");
  
  // and the check boxes
  var chkClassRNT = ge(ctlID + "chkClassRNT");
  var chkClassSFR = ge(ctlID + "chkClassSFR");
  var chkClassCON = ge(ctlID + "chkClassCON");
  var chkClassLTL = ge(ctlID + "chkClassLTL");
  var chkClassMOB = ge(ctlID + "chkClassMOB");
  var chkClassFRM = ge(ctlID + "chkClassFRM");
  var chkClassINC = ge(ctlID + "chkClassINC");
  var chkClassCOM = ge(ctlID + "chkClassCOM");
  
  // save current price selections so we can restore them later
  var minValue = ctlMin.options[ctlMin.selectedIndex].value;
  var maxValue = ctlMax.options[ctlMax.selectedIndex].value;
  var minIndex = ctlMin.selectedIndex;
  var maxIndex = ctlMax.selectedIndex;    
  
  ctlMin.options.length = 0;
  ctlMin.options[0] = new Option("Any", "");  
  
  ctlMax.options.length = 0;
  ctlMax.options[0] = new Option("Any", "");
  
  // load the rental prices if needed
  if(!chkClassRNT)
    PopulateRegularPrices();
  else
  {
      if(chkClassRNT.checked)
        PopulateRentalPrices();
      
      // load the regular prices if needed
      if((chkClassSFR && chkClassSFR.checked) || (chkClassCON && chkClassCON.checked) ||
         (chkClassLTL && chkClassLTL.checked) || (chkClassMOB && chkClassMOB.checked) ||
         (chkClassFRM && chkClassFRM.checked) || (chkClassINC && chkClassINC.checked) ||
         (chkClassCOM && chkClassCOM.checked))
        PopulateRegularPrices();
      else if(!chkClassRNT.checked)
        PopulateRegularPrices();
  }
  
  // restore previous minValue selection if possible
  if(ctlMin.options.length < minIndex)
    ctlMin.selectedIndex = 0;
  else
    ctlMin.selectedIndex = minIndex;
    
  for(i = 0; i < ctlMin.options.length; i++)
  {
    if(ctlMin.options[i].value == minValue)
    {
      ctlMin.selectedIndex = i;
      break;
    }
  }
  
  // restore previous maxValue selection if possible
  if(ctlMax.options.length < maxIndex)
    ctlMax.selectedIndex = 0;
  else
    ctlMax.selectedIndex = maxIndex;
    
  for(i = 0; i < ctlMax.options.length; i++)
  {
    if(ctlMax.options[i].value == maxValue)
    {
      ctlMax.selectedIndex = i;
      break;
    }
  }
  
  // see how many properties match this criteria  
  GetCount();
}

var _RentalPrices = new Array(0, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 
    1100, 1200, 1300, 1400, 1500, 1600, 1700, 1800, 1900, 2000, 2500, 3000, 4000, 
    5000, 7500, 10000, 15000, 20000);
var _ListingPrices = new Array(25000, 50000, 75000, 100000, 125000, 150000, 175000, 200000,
    225000, 250000, 275000, 300000, 325000, 350000, 375000, 400000, 425000, 450000, 475000, 500000,
    525000, 550000, 575000, 600000, 625000, 650000, 675000, 700000, 725000, 750000, 775000, 800000,
    825000, 850000, 875000, 900000, 925000, 950000, 975000, 1000000, 1100000, 1200000, 1300000, 1400000, 1500000,
    1600000, 1700000, 1800000, 1900000, 2000000, 2250000, 2500000, 2750000, 3000000, 3250000, 3500000,
    3750000, 4000000, 4250000, 4500000, 4750000, 5000000, 6000000, 7000000, 8000000, 9000000, 10000000, 15000000,
    20000000, 25000000, 30000000);

function PopulateRentalPrices()
{
    ctlMin = ge(ctlID + "cboMinPrice");
    ctlMax = ge(ctlID + "cboMaxPrice");
    
    for (var i = 0; i < _RentalPrices.length; i++)
    {
        var price = new String(_RentalPrices[i]).format("C0");
        ctlMin.options[ctlMin.options.length] = new Option(price, _RentalPrices[i]);
        ctlMax.options[ctlMax.options.length] = new Option(price, _RentalPrices[i]);
    }
}
 
function PopulateRegularPrices(minID, maxID)
{
    if (!minID && ctlID)
        minID = ctlID + "cboMinPrice";
    if (!maxID && ctlID)
        maxID = ctlID + "cboMaxPrice";
    
    ctlMin = ge(minID);
    ctlMax = ge(maxID);
    
    for (var i =0; i < _ListingPrices.length; i++)
    {
        var price = new String(_ListingPrices[i]).format("C0");
        ctlMin.options[ctlMin.options.length] = new Option(price, _ListingPrices[i]);
        ctlMax.options[ctlMax.options.length] = new Option(price, _ListingPrices[i]);
    }
}