var mlsID;
var classID;
var savedSearchID;
var arSearchHelp = new Array();
var aGp = new Array();
var searchType = "";
var arrSearchFields = new Array();
var arrSavedQueryObjects = new Array();
var formRowClassName = "formBody2";
var formCellClassName = "formBody2";
var groupCellClassName = "formBody2";
var defaultCellPadding = 3;
var formFontStyle = "";
var controlName = "";
var imageFolder = "";
var selQuery = null;
var RequiredFieldsAlert = "Please enter values for all required fields.\n\nRequired fields appear at the top of the \"Select Fields\"\nlist with \"(Required)\" at the end of the field name.";

function SE_AdvSearch_ToggleVicinity()
{
	var divVSearch = document.getElementById(controlName + "divVicinitySearch");
	var a3 =  document.getElementById(controlName + "a3");
	var a4 =  document.getElementById(controlName + "a4");
	var msLat1 = document.getElementById(controlName + "msLat1")
	
	switch(divVSearch.style.display)
	{
		case "none":
			if(msLat1.value.length > 0)
			{
				if(!confirm("This will clear your existing map search.\n Are you sure you want to do that?"))
					return;
			}			
			SE_AdvSearch_ClearMapSearch();
			if (typeof(GetCount) != undefined) GetCount();			
			divVSearch.style.display = "block";
			a3.innerHTML = "[-] ";
			a4.innerHTML = "Hide Vicinity Search";
			break;
		case "block":					
			divVSearch.style.display = "none";
			a3.innerHTML = "[+] ";
			a4.innerHTML = "Perform a Vicinity Search";
			break;		
	}
}

function SE_AdvSearch_ToggleMap()
{
	var divMapImage = document.getElementById(controlName + "divMapImage");
	var a5 =  document.getElementById(controlName + "a5");
	var a6 =  document.getElementById(controlName + "a6");
	
	switch(divMapImage.style.display)
	{
		case "none":
			if(document.getElementById(controlName + "txtAddress").value.length > 0)
			{
				if(!confirm("This will clear your existing vicinity search.\n Are you sure you want to do that?"))
					return;
			}					
			SE_AdvSearch_ClearVicinitySearch();
			if (typeof(GetCount) != undefined) GetCount();	
			divMapImage.style.display = "block";
			a5 = "[-] ";
			a6.innerHTML = "Hide Map Search";
			SE_AdvSearch_EditMapSearch();
			break;
		case "block":					
			divMapImage.style.display = "none";
			a5.innerHTML = "[+] ";
			a6.innerHTML = "Perform a Map Search";
			break;		
	}
}
	
function SE_AdvSearch_FindGroupByFieldID(fieldID)
{
	for(var x = 0; x < aGp.length; x++)
	{
		for(var y = 0; y < aGp[x].fieldList.length; y++)
		{
			if(aGp[x].fieldList[y].fieldID == fieldID)
				return aGp[x];
		}
	}	
	return null;
}



function SE_AdvSearch_PassOnloadValues(mcontrolName, mDefaultCellPadding, mGroupCellClassName, mFormRowClassName, mFormCellClassName, mFormFontStyle, mImageFolder)
{
	controlName = mcontrolName + "_";
	defaultCellPadding = mDefaultCellPadding;
	groupCellClassName = mGroupCellClassName;
	formRowClassName = mFormRowClassName;
	formCellClassName = mFormCellClassName;
	formFontStyle = mFormFontStyle;
	imageFolder = mImageFolder;
}

function _SE_AdvSearch_FindFieldById(fieldID)
{
	for(var x = 0; x < aGp.length; x++)
	{
		for(var y = 0; y < aGp[x].fieldList.length; y++)
		{
			if(aGp[x].fieldList[y].fieldID == fieldID)
				return aGp[x].fieldList[y];
		}
	}
	
	return null;
}

function _SE_AdvSearch_FindSearchFieldLookupByID(searchField, lookupID)
{
	for (var i = 0; i < searchField.lookupList.length; i++)
	{
		if (searchField.lookupList[i].lookupID == lookupID)
			return searchField.lookupList[i];
	}
}

function SE_AdvSearch_FindInstruction(searchHelpID)
{
	for (var i = 0; i < arSearchHelp.length; i++)
	{
		if (arSearchHelp[i].searchHelpID == searchHelpID)
		{
			return arSearchHelp[i].helpLines;
		}
	}
	return null;
}

function SE_AdvSearch_QueryObject(aSearchField)
{
	this.searchField = aSearchField;
	this.luValueList = new Array(); //Used when the associated field type is either luValue or luFeature
	this.addLuValue = SE_AdvSearch_QueryObject_AddLuValue; //Used when the associated field type is either luValue or luFeature
	this.stringValue = "";
	this.rangeValueFrom = "";
	this.rangeValueTo = "";
	this.binaryOperator = "";
	this.isAllSelected = false;
}

function SE_AdvSearch_QueryObject_AddLuValue(aLookup)
{
	this.luValueList[this.luValueList.length] = aLookup;
}

function SE_AdvSearch_GetSearchXml(arrQueryObjects)
{
	var newLine = "\r\n";
	retXml = "<?xml version=\"1.0\"?>" + newLine;
	retXml += "<GetSavedSearch>" + newLine;
	retXml += "<mlsID>" + mlsID + "</mlsID>" + newLine;
	retXml += "<classID>" + classID + "</classID>" + newLine;
	retXml += "<savedSearchID>" + savedSearchID + "</savedSearchID>" + newLine;
	if(searchType.length > 0)
		retXml += "<searchType>" + searchType + "</searchType>" + newLine;
	retXml += "<rows>" + newLine;
	for (var i = 0; i < arrQueryObjects.length; i++)
	{
		retXml += "<row>" + newLine;
		retXml += "<mlsfieldid>" + arrQueryObjects[i].searchField.fieldID + "</mlsfieldid>" + newLine;
		retXml += "<mlsfieldsearchtype>" + arrQueryObjects[i].searchField.searchType + "</mlsfieldsearchtype>" + newLine;
		retXml += "<mlsfielddestcolumn>" + arrQueryObjects[i].searchField.destColumn + "</mlsfielddestcolumn>" + newLine;
		retXml += "<mlsfielddestdatatype>" + arrQueryObjects[i].searchField.destDataType + "</mlsfielddestdatatype>" + newLine;
		retXml += "<stringvalue>" + arrQueryObjects[i].stringValue.XmlEncode() + "</stringvalue>" + newLine;
		retXml += "<rangevaluefrom>" + arrQueryObjects[i].rangeValueFrom + "</rangevaluefrom>" + newLine;
		retXml += "<rangevalueto>" + arrQueryObjects[i].rangeValueTo + "</rangevalueto>" + newLine;
		retXml += "<binaryoperator>" + arrQueryObjects[i].binaryOperator + "</binaryoperator>" + newLine;
		if (arrQueryObjects[i].isAllSelected)
			retXml += "<isallselected>" + "1" + "</isallselected>" + newLine;
		else
			retXml += "<isallselected>" + "0" + "</isallselected>" + newLine;
		if (arrQueryObjects[i].searchField.searchType == "luValue" || arrQueryObjects[i].searchField.searchType == "luFeature")
		{
			retXml += "<lookuplist>" + newLine;
			for (var j = 0; j < arrQueryObjects[i].luValueList.length; j++)
			{
				retXml += "<lookupid>" + arrQueryObjects[i].luValueList[j].lookupID + "</lookupid>" + newLine;
				if (arrQueryObjects[i].searchField.searchType == "luValue")
				{
					retXml += "<lookupvalue>" + arrQueryObjects[i].luValueList[j].fieldValue + "</lookupvalue>" + newLine;
				}
				else
				{
					retXml += "<lookupvalue>" + arrQueryObjects[i].luValueList[j].lookupID + "</lookupvalue>" + newLine;
				}
			}
			retXml += "</lookuplist>" + newLine;
		}
		retXml += "</row>" + newLine;
	}
	retXml += "</rows>" + newLine;
	//check for vicinity search
	if(document.getElementById(controlName + "txtAddress").value.length > 0)
	{
		retXml += SE_AdvSearch_GetVicinitySearchXml() + newLine;
	}
	//check for map search
	if (document.getElementById(controlName + "msCountyID").value.length > 0)
	{
		retXml += SE_AdvSearch_GetMapSearchXml() + newLine;
	}
	retXml += SE_AdvSearch_GetStatusXml() + newLine;
	retXml += "</GetSavedSearch>";
	
	return retXml;
}

function SE_AdvSearch_GetStatusXml()
{
	var newLine = "\r\n";
	var ret = "<StatusCodes>" + newLine;
	ret += "<Active>" + document.getElementById(controlName + "chkActive").checked + "</Active>" + newLine;
	ret += "<Pending>" + document.getElementById(controlName + "chkPending").checked + "</Pending>" + newLine;
	ret += "<Sold>" + document.getElementById(controlName + "chkSold").checked + "</Sold>" + newLine;
	ret += "<SoldDB>" + document.getElementById(controlName + "cboSold").value + "</SoldDB>" + newLine;
	ret += "<Withdrawn>" + document.getElementById(controlName + "chkWithdrawn").checked + "</Withdrawn>" + newLine;
	ret += "<WithdrawnDB>" + document.getElementById(controlName + "cboWithdrawn").value + "</WithdrawnDB>" + newLine;
	ret += "<Expired>" + document.getElementById(controlName + "chkExpired").checked + "</Expired>" + newLine;
	ret += "<ExpiredDB>" + document.getElementById(controlName + "cboExpired").value + "</ExpiredDB>" + newLine;
	ret += "</StatusCodes>";
	return ret;
}

function SE_AdvSearch_GetRequiredFields()
{
	var ret = new Array();
	for (var i = 0; i < arrSearchFields.length; i++)
	{
		if (arrSearchFields[i].searchRequired == 1)
			ret[ret.length] = arrSearchFields[i];
	}
	return ret;
}

function SE_AdvSearch_CheckIfSearchContainsRequiredFields()
{
	var requiredFields = SE_AdvSearch_GetRequiredFields();
	var found;
	for (var i = 0; i < requiredFields.length; i++)
	{
		found = false;
		for (var j = 0; j < arrQueryObjects.length; j++)
		{
			if (arrQueryObjects[j].searchField.fieldID == requiredFields[i].fieldID)
			{
				found = true;
				break;
			}
		}
		if (! found)
			return false;
	}
	return true;
}

function _SE_AdvSearch_GetQueryObjectFromForm()
{
	var arr = new Array();
	var hasError = false;
	
	for(var x = 0 ; x < aGp.length ; x++)
	{
		for(var y = 0; y < aGp[x].fieldList.length; y++)
		{
			var lastQueryObject = null;
			var selectedSearchField = aGp[x].fieldList[y];
			
			if(selectedSearchField.searchType == "string")
			{
				searchUI = document.getElementById("fld" + selectedSearchField.fieldID);
				var val = searchUI.value;
				if(val.length > 0)
				{
					lastQueryObject = new SE_AdvSearch_QueryObject(selectedSearchField);
					lastQueryObject.stringValue = val;
					lastQueryObject.binaryOperator = "OR";
				}
			}
			if(selectedSearchField.searchType == "range")
			{			
				searchUIFrom = document.getElementById("fld" + selectedSearchField.fieldID + "From");
				searchUITo = document.getElementById("fld" + selectedSearchField.fieldID + "To");
				
				if(searchUIFrom.value.trim().length > 0 || searchUITo.value.trim().length > 0)
				{
					lastQueryObject = new SE_AdvSearch_QueryObject(selectedSearchField);
					lastQueryObject.rangeValueFrom = searchUIFrom.value.trim()
					lastQueryObject.rangeValueTo = searchUITo.value.trim();
					lastQueryObject.binaryOperator = "RANGE";
				}
			}
			if(selectedSearchField.searchType == "luValue" || selectedSearchField.searchType == "luFeature")
			{
				searchUI = document.getElementById("fld" + selectedSearchField.fieldID);
				
				if(searchUI.selectedIndex != -1)
				{
					lastQueryObject = new SE_AdvSearch_QueryObject(selectedSearchField);
					lastQueryObject.luValueList.length = 0;
					lastQueryObject.isAllSelected = false;
					lastQueryObject.binaryOperator = "OR";

					for (var i = 0; i < searchUI.options.length; i++)
					{
						if (searchUI.options[i].selected)
							lastQueryObject.addLuValue(selectedSearchField.lookupList[i]);
					}
				}
			}

			if(lastQueryObject != null)
				arr[arr.length] = lastQueryObject;
		}
	}
	return arr;
}

function SE_AdvSearch_RunBSTSearch()
{
	var arrQueryObjects = _SE_AdvSearch_GetQueryObjectFromForm();
	

	if(document.getElementById(controlName + "chkActive").checked != true && document.getElementById(controlName + "chkPending").checked != true && document.getElementById(controlName + "chkSold").checked != true && document.getElementById(controlName + "chkWithdrawn").checked != true && document.getElementById(controlName + "chkExpired").checked != true)
	{
		alert("Please select at least one Property Status.");
		return false;
	}
	
	//Check to see if there are any search criteria defined
	if(!document.getElementById(controlName + "a3").innerHTML == "[-] " && (arrQueryObjects == null || arrQueryObjects.length == 0))
	{
		alert("Please enter at least one search criteria.");
		return false;
	}

	var searchXml = document.getElementById(controlName + "searchXml");
	searchXml.value = SE_AdvSearch_GetSearchXml(arrQueryObjects);
	return true;
}

function SE_AdvSearch_InitiateSearchForm(e)
{
	_SE_AdvSearch_DrawGroups();
	
	setTimeout("SE_AdvSearch_LoadSavedQueryObjects()",1000);
	SE_AdvSearch_ShowHideVicinity();
	
	if (SE_GetXmlHttp() == null)
	{
		if(document.getElementById("btnCount") != null)
			document.getElementById("btnCount").style.display = 'none';
		else
			document.getElementById("trCount").style.display = 'none';
	}
}

function _SE_AdvSearch_DrawGroups()
{
	var searchTbody = document.getElementById("tbdySearch");
	var currentRow = document.getElementById("trGroupPlaceholder");
	
	for(var x = 0 ; x < aGp.length ; x++)
	{
		var group = aGp[x];
	
		var groupRow = document.createElement("TR");
		groupRow.id = "gr" + group.groupID;
		var groupCell = document.createElement("TD");
		groupCell.className = groupCellClassName;
		var fieldTable = document.createElement("TABLE");
		fieldTable.cellPadding = defaultCellPadding;
		fieldTable.cellSpacing = "0";
		fieldTable.width = '100%';
		tbody = document.createElement("TBODY");
		tbody.appendChild(_SE_AdvSearch_GetFieldHeaderRow(group));
		_SE_AdvSearch_DrawFields(tbody,group,x);
		fieldTable.appendChild(tbody);		
		groupCell.appendChild(fieldTable);
		groupRow.appendChild(groupCell);
		searchTbody.insertBefore(groupRow,document.getElementById("trVicinitySearch"));
		currentRow = groupRow;
	}
	
	document.getElementById("trGroupPlaceholder").style.display = "none";
}

function _SE_AdvSearch_GetFieldHeaderRow(group)
{
	var headerRow = document.createElement("TR")
	headerRow.className = formRowClassName;
	var headerCell = document.createElement("TD");
	headerCell.colSpan = 2;
	headerCell.className = formCellClassName;
	headerCell.id = "gh" + group.groupID;
	if(!group.exposedOnOpen)
		headerCell.innerHTML = "<a href=\"javascript:SE_AdvSearch_ToggleGroup('gf" + group.groupID + "');\" style=\"TEXT-DECORATION:none;font-weight:bold;" + formFontStyle + "\" id=\"gf" + group.groupID + "a\">[+] </a><a href=\"javascript:SE_AdvSearch_ToggleGroup('gf" + group.groupID + "');\" style=\"font-weight:bold;" + formFontStyle + "\"id=\"gf" + group.groupID + "b\">" + group.groupName + "</a>";
	else
		headerCell.innerHTML = "<strong>" + group.groupName + ":</strong>";
	if(group.instructions.length > 0)
		headerCell.innerHTML += "<br>" + group.instructions;
	headerRow.appendChild(headerCell);
	
	return headerRow;
}

function _SE_AdvSearch_DrawFields(tbody, group, groupIndex)
{
	var totalColumns = 2;
	var currentColumn = 1;
	var fieldList = group.fieldList;
	
	var fieldRow = document.createElement("TR");
	fieldRow.className = formRowClassName;
	
	//Fix problem with Netscape
	var divFix = document.createElement("DIV");
	divFix.id = "gf" + group.groupID;
	var blockTable = document.createElement("TABLE")
	blockTable.width = "100%";
	var blockTbody = document.createElement("TBODY");
	var blockTableRow = document.createElement("TR");
	var blockTableCell = document.createElement("TD");
	blockTableCell.width = "100%";
	blockTableCell.appendChild(divFix);
	divFix.appendChild(blockTable);
	blockTbody.appendChild(blockTableRow);	
	blockTable.appendChild(blockTbody);
	fieldRow.appendChild(blockTableCell);	
	
	if(!group.exposedOnOpen)
		divFix.style.display = "none";
	else
		divFix.style.display = "block";
	
	for(var y = 1; y <= totalColumns; y++)
	{	
		var fieldCell = document.createElement("TD");
		fieldCell.className = formCellClassName;
		fieldCell.vAlign = "top";
		fieldCell.width = (100 / totalColumns).toString() + "%";
	
		for(var x = y-1; x < fieldList.length; x += totalColumns)
		{
			var searchUI;
			var objField = fieldList[x];
			var spn = document.createElement("SPAN");
			var displayName = objField.displayName + ":"
			
			// add help link if needed
			if(objField.searchHelpID != null && objField.searchHelpID > 0)
			    displayName = displayName + "&nbsp;&nbsp; <a href=\"javascript:SE_AdvSearch_GetHelp('" + objField.searchHelpID +  "');\">help</a>"
			    
			spn.innerHTML = displayName;
			fieldCell.appendChild(spn);
			fieldCell.appendChild(document.createElement("BR"));
			
			//draw the field
			if (objField.searchType == "string")
			{
				searchUI = document.createElement("INPUT");
				searchUI.type = "text";
				searchUI.id = "fld" + objField.fieldID;
				searchUI.onblur = _SE_AdvSearch_ValidateField;
				searchUI.onkeydown = SE_ValidateOnEnter;
                fieldCell.appendChild(searchUI);	
			}
			if (objField.searchType == "range")
			{
				searchUI = document.createElement("INPUT");
				searchUI.id = "fld" + objField.fieldID + "From";
				searchUI.onblur = _SE_AdvSearch_ValidateField;
				searchUI.onkeydown = SE_ValidateOnEnter;
				fieldCell.appendChild(searchUI);	
				var elemTo = document.createTextNode(" to ");
				fieldCell.appendChild(elemTo);
				searchUI = document.createElement("INPUT");
				searchUI.id = "fld" + objField.fieldID + "To";
				searchUI.onblur = _SE_AdvSearch_ValidateField;
				searchUI.onkeydown = SE_ValidateOnEnter;
				fieldCell.appendChild(searchUI);	
			}
			if (objField.searchType == "luValue" || objField.searchType == "luFeature")
			{
				searchUI = document.createElement("SELECT");
				searchUI.id = "fld" + objField.fieldID;
				searchUI.style.width = "260";
				searchUI.multiple = true;
				searchUI.size = (objField.lookupList.length > 2 ? (4 + (objField.lookupList.length / 8) > 8 ? 8 : 4 + (objField.lookupList.length / 8)) : 2);
												
				var optionElem;
				for (var i = 0; i < objField.lookupList.length; i++)
				{
					optionElem = document.createElement("OPTION");
					optionElem.text = objField.lookupList[i].fieldDescription
					optionElem.selected = false;
					if (objField.searchType == "luValue")
					{
						optionElem.value = objField.lookupList[i].fieldValue
					}
					else
					{
						optionElem.value = objField.lookupList[i].mappedFeatureID
					}
					searchUI[searchUI.options.length] = optionElem;
				}								
	
				setTimeout("document.getElementById(\"fld" + objField.fieldID + "\").selectedIndex = -1",2);
				searchUI.onclick = function(){if(typeof(GetCount)=='function')GetCount()};
				fieldCell.appendChild(searchUI);
			}
			
			
			fieldCell.appendChild(document.createElement("BR"));
			fieldCell.appendChild(document.createElement("BR"));
		}
		
		blockTableRow.appendChild(fieldCell);
	}
	
	tbody.appendChild(fieldRow);
}

    function SE_ValidateOnEnter(e)
    {
        if(!e) e = window.event;
        if (e.keyCode == 13 && e.srcElement)
        {
            return _SE_AdvSearch_ValidateField(e);
        }
    }


function SE_AdvSearch_LoadSavedQueryObjects()
{
	for(var x = 0; x < arrSavedQueryObjects.length; x++)
	{
		var query = arrSavedQueryObjects[x];
		var field = query.searchField;
		var group = SE_AdvSearch_FindGroupByFieldID(field.fieldID);
		
		if(field != null)
		{
			var groupFieldRow = document.getElementById("gf" + group.groupID);
			if(groupFieldRow.style.display == "none")
				SE_AdvSearch_ToggleGroup("gf" + group.groupID);
				
			if(field.searchType == "string")
				document.getElementById("fld" + field.fieldID).value = query.stringValue;
			if(field.searchType == "range")
			{
				document.getElementById("fld" + field.fieldID + "From").value = query.rangeValueFrom;
				document.getElementById("fld" + field.fieldID + "To").value = query.rangeValueTo;
			}
			if(field.searchType == "luValue" || field.searchType == "luFeature")
			{
				var select = document.getElementById("fld" + field.fieldID);
								
				if (query.isAllSelected)
				{
					//skip this, since all selects are "all" by default
				}
				else
				{
					var tempLuValueList = query.luValueList;
					
					if(tempLuValueList.length != field.lookupList.length)
					{
						for(var j = 0; j < field.lookupList.length; j++)
						{
							for(var i = 0; i < tempLuValueList.length; i++)
							{
								if(typeof(tempLuValueList[i]) != 'undefined' && field.lookupList[j].lookupID == tempLuValueList[i].lookupID)
								{
									select.options[j].selected = true;
									tempLuValueList = _SE_AdvSearch_DeleteArrayItem(tempLuValueList,i)
									break;
								}
							}
						}
					}
				}	
			}
		}
	}
}

function _SE_AdvSearch_DeleteArrayItem(array,deleteIndex)
{
	if(deleteIndex == 0)
		return array.splice(1,array.length - 1);
	else if(deleteIndex == array.length - 1)
		return array.splice(0,array.length - 2)
	else
	{
		var beforeIndex = array.splice(0,deleteIndex - 1);
		var afterIndex = array.splice(0,deleteIndex + 1);
		return beforeIndex.concat(afterIndex);
	}	
}

function SE_AdvSearch_ShowHideVicinity()
{
	var addr = document.getElementById(controlName + "txtAddress");
	var countyID = document.getElementById(controlName + "msCountyID");
	
	if(countyID.value.length > 0)
	{
		if(addr.value.length > 0)
		{
			if(confirm("This will clear your existing map search.\n Are you sure you want to do that?"))
			{
				divMap.style.display = "none";
				chkMapSearch_Click();
			}
			else
			{	
				divVicinity.style.display = "none";
				return;
			}
		}
	}
	else
	{
		if(addr.value.length > 0)
		{
			SE_AdvSearch_ToggleVicinity();
		}
	}
}

function SE_AdvSearch_ValidateLocation()
{
	document.getElementById("centerLocationCheck").innerHTML = "<strong>One moment, attempting to map address...</strong>";
	setTimeout('SE_AdvSearch_CheckCenterLocation(true)',1);
}

function SE_AdvSearch_CheckCenterLocation(showSuccessMessage)
{
	var serverURL = "/Common/PublicPropSearch/ActionHandler.aspx";
	result = SE_PostHttpText(serverURL,  SE_AdvSearch_GetVicinitySearchFullXml(), "RETURN_STATUS")
	if (result == "SUCCESS" && showSuccessMessage)
		alert("The address you have entered can be mapped.");
	if (result == "FAILED")
		alert("AgentAchieve is unable to map the address you have specified. If you run this search, this address will be ignored.");
	
	document.getElementById("centerLocationCheck").innerHTML = "<a href=\"javascript:SE_AdvSearch_ValidateLocation()\">Click here</a> to test that your center point is a \"map-able\" property address.";
}

function SE_AdvSearch_GetVicinitySearchFullXml()
{
	var newLine = "\r\n";
	var retXml = "<?xml version=\"1.0\"?>" + newLine;
	retXml += SE_AdvSearch_GetVicinitySearchXml()
	return retXml;
}

function SE_AdvSearch_GetVicinitySearchXml()
{
	var newLine = "\r\n";
	var ret = "<VicinitySearch>" + newLine;
	ret += "<address>" + document.getElementById(controlName + "txtAddress").value.XmlEncode() + "</address>" + newLine;
	ret += "<city>" + document.getElementById(controlName + "txtCity").value.XmlEncode() + "</city>" + newLine;
	ret += "<state>" + document.getElementById(controlName + "txtState").value.XmlEncode() + "</state>" + newLine;
	ret += "<zip>" + document.getElementById(controlName + "txtZipCode").value.XmlEncode() + "</zip>" + newLine;
	ret += "<radius>" + document.getElementById(controlName + "cboVicinityRadius").value + "</radius>" + newLine;
	ret += "</VicinitySearch>";
	return ret;
}

function _SE_AdvSearch_ValidateField(e)
{
	var src = _SE_AdvSearch_ValidateField_GetSrcElement(e);
	
	if(src != null)
	{
		var field = _SE_AdvSearch_FindFieldById(src.id.toString().replace(new RegExp('[A-Za-z]','g'),''));
		
		if(field != null)
			return _SE_AdvSearch_ValidFieldValue(field, src);
	}
}

function _SE_AdvSearch_ValidateField_GetSrcElement(e) 
{
	if ( typeof( window.event ) != "undefined" ) {
	return window.event.srcElement;
	}
	if ( e != null && typeof( e.target ) != "undefined" ) {
	return e.target;
	}
	return null;
} 

function _SE_AdvSearch_ValidFieldValue(selSearchField, searchUI)
{
	var ret = true;
	if(selSearchField.destDataType.toLowerCase() == 'int' && selSearchField.searchType=='string')
	{
		var re = new RegExp("[^0-9, ]");
		if(re.test(searchUI.value))
		{
			alert('Illeagal characters found.  Only digits and commas are valid.');
			searchUI.value = '';
			ret = false;
		}
		else
			ret = true;
	}	
	else if(searchUI != null)
	{
		if(searchUI.value.length > 0)
		{
			switch(selSearchField.destDataType.toLowerCase())
			{
				case "decimal":
				case "double":
				case "money":
				case "float":
				    searchUI.value = searchUI.value.replace(new RegExp('[^0-9\.]','g'), "");
					if(isNaN(searchUI.value))
					{
						alert("The value that you entered contains non-numeric characters.\n\nPlease enter only numeric characters (0-9) and decimal point in this field.");
						searchUI.value = '';
						ret = false;
					}
					break;
				case "int":
				case "tiny":
				case "small":
					if (searchUI.value.indexOf(".") > -1)
					{
					    alert("This field cannot accept decimal points.");
					    searchUI.value = '';
					    ret = false;
					    break;
					}
				    searchUI.value = searchUI.value.replace(new RegExp('[^0-9]','g'), "");    					
					if(isNaN(searchUI.value))
					{
						alert("The value that you entered contains non-numeric characters.\n\nPlease enter only numeric characters (0-9) in this field.");
						searchUI.value = '';
						ret = false;
					}
					break;
				case "datetime":
				case "date":
					if(!IsValidDate(searchUI.value,''))
					{
						alert("The value that you entered is an invalid date format.\n\nPlease enter a date in the format of MM/DD/YY.");
						searchUI.value = '';
						ret = false;
					}
					break;
			}
		}
	}	
	if (typeof(GetCount) == 'function') GetCount();
			
	if (ret == false)
	    SE_CancelEvent();
	
	return ret;
}


function SE_AdvSearch_ShowListingCount()
{
	var result = SE_AdvSearch_GetListingCount()
	if(!SE_IsInteger(result))
		alert("There was an error obtaining the count of listings for the current search criteria.");
	else
		alert(result + " listing(s) will be returned by the current search criteria.");
	return result;
}
function SE_AdvSearch_GetListingCount()
{
	var arrQueryObjects = _SE_AdvSearch_GetQueryObjectFromForm();	
	var serverURL = "/Common/PublicPropSearch/ActionHandler.aspx";	
	var result = SE_PostHttpText(serverURL, SE_AdvSearch_GetSearchXml(arrQueryObjects), "RESULT")
	return result;
}

function SE_AdvSearch_GetHelp(searchHelpID)
{
	var s = SE_AdvSearch_FindInstruction(searchHelpID);
	
	if(s != null)
	{
		var msg = s[0];
		
		for(var i = 1; i < s.length; i++)
		{
			msg += "\n" + s[i];
		}
		
		alert(msg);
	}
}

function SE_AdvSearch_ToggleGroup(groupFieldRowID)
{
	var groupFieldRow = document.getElementById(groupFieldRowID);
	var groupFieldAnchorIndicator = document.getElementById(groupFieldRowID + "a");
	var groupFieldAnchorCopy = document.getElementById(groupFieldRowID + "b");
	
	if(groupFieldRow.style.display == "none")
	{
		groupFieldRow.style.display = "block";
		groupFieldAnchorIndicator.innerText = "[-] ";
		groupFieldAnchorCopy.innerText = "Hide " + groupFieldAnchorCopy.innerText;
	}
	else
	{
		groupFieldRow.style.display = "none";
		groupFieldAnchorIndicator.innerText = "[+] ";
		groupFieldAnchorCopy.innerText = groupFieldAnchorCopy.innerText.substring(5,groupFieldAnchorCopy.innerText.length);
	}
}

function SE_AdvSearch_ClearMapSearch()
{
	//unchecked - clear map search objects
	document.getElementById(controlName + "msCountyID").value = "";
	document.getElementById(controlName + "msLat1").value = "";
	document.getElementById(controlName + "msLat2").value = "";
	document.getElementById(controlName + "msLon1").value = "";
	document.getElementById(controlName + "msLon2").value = "";
	document.getElementById(controlName + "msCLat").value = "";
	document.getElementById(controlName + "msCLon").value = "";
	document.getElementById(controlName + "msViewScale").value = "";
	document.getElementById(controlName + "msViewScale").value = "";
	document.getElementById(controlName + "imgMapSearch").src = imageFolder + "/pleaseSelectCounty.gif";
	document.getElementById(controlName + "divMapImage").style.display = "none";
	document.getElementById(controlName + "a5").innerHTML = "[+] ";
	document.getElementById(controlName + "a6").innerHTML = "Perform a Map Search";
	SE_MapSearch_BoundaryPoints = new Array();
    SE_MapSearch_BoundaryGUID = "";		        
}

function SE_AdvSearch_ClearVicinitySearch()
{
	document.getElementById(controlName + "txtAddress").value = "";
	document.getElementById(controlName + "txtCity").value = "";
	document.getElementById(controlName + "txtState").value = "";
	document.getElementById(controlName + "txtZipCode").value = "";
	document.getElementById(controlName + "divVicinitySearch").style.display = "none";
	document.getElementById(controlName + "a3").innerHTML = "[+] ";
	document.getElementById(controlName + "a4").innerHTML = "Perform a Vicinity Search";
}

var SE_MapSearch_ModalDialog = new SE_ModalDialog('SE_MapSearch_ModalDialog', SE_AdvSearch_MapSearchCallback);
var SE_MapSearch_BoundaryPoints = new Array();
var SE_MapSearch_BoundaryGUID = "";
function SE_AdvSearch_EditMapSearch()
{
	var url = "MapSearch.aspx?mlsID=" + document.getElementById(controlName + "ddlbMLS").value
	 + "&lat1=" + document.getElementById(controlName + "msLat1").value
	 + "&lon1=" + document.getElementById(controlName + "msLon1").value
	 + "&lat2=" + document.getElementById(controlName + "msLat2").value
	 + "&lon2=" + document.getElementById(controlName + "msLon2").value
	 + "&viewScale=" + document.getElementById(controlName + "msViewScale").value
	 + "&county=" + document.getElementById(controlName + "msCountyID").value
	 + "&state=" + document.getElementById(controlName + "msState").value;
	 	
	SE_MapSearch_ModalDialog.getMyCount = GetPopupRowsCount;	
	SE_MapSearch_ModalDialog.returnValue = new Object();
	SE_MapSearch_ModalDialog.show(url, 700, 500, 'MapSearch')
}
function SE_AdvSearch_MapSearchCallback()
{	
	MapSearchGetModalSettings()
}

function GetPopupRowsCount()
{
    SaveMapSettings()
    MapSearchGetModalSettings();
    var count = SE_AdvSearch_GetListingCount();
    RestoreMapSettings();
    return count;
}

function MapSearchGetModalSettings()
{
    var ret = SE_MapSearch_ModalDialog.returnValue;
	
	if (ret.saved && ret.msLat1)
    {
	    document.getElementById(controlName + "msLat1").value = ret.msLat1;
	    document.getElementById(controlName + "msLat2").value = ret.msLat2;
	    document.getElementById(controlName + "msLon1").value = ret.msLon1;
	    document.getElementById(controlName + "msLon2").value = ret.msLon2;
	    document.getElementById(controlName + "msCLat").value = ret.msCLat;
	    document.getElementById(controlName + "msCLon").value = ret.msCLon;
	    document.getElementById(controlName + "msViewScale").value = ret.msViewScale;
	    document.getElementById(controlName + "msCountyID").value = ret.msCountyID;
	    document.getElementById(controlName + "msState").value = ret.msState;
	    SE_MapSearch_BoundaryGUID = ret.mapBoundariesTempGUID
        SE_MapSearch_BoundaryPoints = new Array();
	    for(i = 0; i < ret.msBoundaryPoints.length; i++)
		{
		    var latLong = new Object();
		    latLong.latitude = ret.msBoundaryPoints[i].latitude;
		    latLong.longitude = ret.msBoundaryPoints[i].longitude;
		    SE_MapSearch_BoundaryPoints[i] = latLong;
        }
	    if (ret.msLon1 
	        && ret.msLon1 != ""
	        && ret.msLon1 > -180)
        {
            UpdateMapImage(document.getElementById(controlName + "imgMapSearch"));	
            return;
        }
    }
    if ((!ret.saved || !ret.msLat1) && (!document.getElementById(controlName + "msLat1").value))
    {
        SE_AdvSearch_ClearMapSearch();
    }
}

var MapSearchPreviousValues;
function SaveMapSettings()
{
    MapSearchPreviousValues = new Object()
    MapSearchPreviousValues.msLat1 = document.getElementById(controlName + "msLat1").value; 
    MapSearchPreviousValues.msLat2 = document.getElementById(controlName + "msLat2").value;
    MapSearchPreviousValues.msLon1 = document.getElementById(controlName + "msLon1").value;
    MapSearchPreviousValues.msLon2 = document.getElementById(controlName + "msLon2").value;
    MapSearchPreviousValues.msCLat = document.getElementById(controlName + "msCLat").value;
    MapSearchPreviousValues.msCLon = document.getElementById(controlName + "msCLon").value;
    MapSearchPreviousValues.msViewScale = document.getElementById(controlName + "msViewScale").value; 
    MapSearchPreviousValues.msCountyID = document.getElementById(controlName + "msCountyID").value;
    MapSearchPreviousValues.msState = document.getElementById(controlName + "msState").value; 
}
function RestoreMapSettings()
{
    document.getElementById(controlName + "msLat1").value = MapSearchPreviousValues.msLat1; 
    document.getElementById(controlName + "msLat2").value  = MapSearchPreviousValues.msLat2;
    document.getElementById(controlName + "msLon1").value = MapSearchPreviousValues.msLon1;
    document.getElementById(controlName + "msLon2").value = MapSearchPreviousValues.msLon2;
    document.getElementById(controlName + "msCLat").value = MapSearchPreviousValues.msCLat;
    document.getElementById(controlName + "msCLon").value = MapSearchPreviousValues.msCLon;
    document.getElementById(controlName + "msViewScale").value = MapSearchPreviousValues.msViewScal; 
    document.getElementById(controlName + "msCountyID").value = MapSearchPreviousValues.msCountyID;
    document.getElementById(controlName + "msState").value = MapSearchPreviousValues.msState; 
    MapSearchPreviousValues = null;
}



function SE_AdvSearch_GetMapSearchXml()
{
	var ret = "";
	
	if(document.getElementById(controlName + "msLat1").value.length > 0)
	{
		var newLine = "\r\n";
		ret += "<MapSearch>" + newLine;
		ret += "<countyID>" + document.getElementById(controlName + "msCountyID").value + "</countyID>" + newLine;
		ret += "<centerPointLat>" + document.getElementById(controlName + "msCLat").value + "</centerPointLat>" + newLine;
		ret += "<centerPointLon>" + document.getElementById(controlName + "msCLon").value + "</centerPointLon>" + newLine;
		ret += "<viewScale>" + document.getElementById(controlName + "msViewScale").value + "</viewScale>" + newLine;
		ret += "<lat1>" + document.getElementById(controlName + "msLat1").value + "</lat1>" + newLine;
		ret += "<lat2>" + document.getElementById(controlName + "msLat2").value + "</lat2>" + newLine;
		ret += "<lon1>" + document.getElementById(controlName + "msLon1").value + "</lon1>" + newLine;
		ret += "<lon2>" + document.getElementById(controlName + "msLon2").value + "</lon2>" + newLine;
		
		if (SE_MapSearch_BoundaryPoints && SE_MapSearch_BoundaryPoints.length > 0)
		{
		    ret += "<MapBoundariesTempGUID>"  + SE_MapSearch_BoundaryGUID + "</MapBoundariesTempGUID>" + newLine;
            ret += "<MapBoundaryPoints>"  + newLine;
		    for(var i = 0; i < SE_MapSearch_BoundaryPoints.length; i++)
	        {
	            ret += "<MapBoundaryPoint>"  + newLine;
		        ret += "<Latitude>"  + SE_MapSearch_BoundaryPoints[i].latitude + "</Latitude>"  + newLine;
		        ret += "<Longitude>"  + SE_MapSearch_BoundaryPoints[i].longitude + "</Longitude>"  +  newLine;
                ret += "</MapBoundaryPoint>"  + newLine;
	        }
		    ret += "</MapBoundaryPoints>"  + newLine;
		}		
		
		ret += "</MapSearch>";
	}
	return ret;
}

function UpdateMapImage(img)
{
	if(document.getElementById(controlName + "msCLat").value.length > 0)
	{
		var s = imageFolder + "/mapPoint.aspx?";
		s += "latitude=" + document.getElementById(controlName + "msCLat").value;
		s += "&longitude=" + document.getElementById(controlName + "msCLon").value;
		s += "&viewScale=" + document.getElementById(controlName + "msViewScale").value;
		s += "&width=120&height=120";
		img.src = s;
	}
}


// -------------------------------------------------------------
// ---------------- Methods used by sharedjavascript/xx-xx-BST.js files
// -------------------------------------------------------------
function Group(groupID,groupName,instructions,exposedOnOpen)
{
	this.groupID = groupID;
	this.groupName = groupName;
	this.instructions = instructions;
	this.exposedOnOpen = exposedOnOpen;
	this.fieldList = new Array();
	this.addField = Group_addField;
}

function Group_addField(aField)
{
	this.fieldList[this.fieldList.length] = aField
}
function LU(lookupID,lookupName,lookupDisplay,lookupValue)
{
	this.lookupID = lookupID;
	this.fieldValue = lookupValue; //use this field if searchType = luValue
	this.fieldDescription = lookupDisplay;
}

function addGSF(fldID, longName, systemName, dataType, searchtype, lookupName, interpretation, searchHelpID, searchRequired )
{
	// add search field
	field = new SF(fldID,longName,systemName,dataType,searchtype,lookupName,interpretation,searchHelpID,searchRequired); 
}

function addLU(lookupID,lookupDisplay,lookupValue)
{
	// add lookup
	lookupName = s2;
	field.aLU(new LU(lookupID,lookupName,lookupDisplay,lookupValue));
}

function FieldInstruction(searchHelpID,helpText)
{
	this.searchHelpID = searchHelpID
	this.helpText = helpText;
	this.helpLines = this.helpText.split("\r\n");
}

function SF(fieldID, longName, systemName, dataType, searchType, lookupName, isMultiSelect, searchHelpID, isRequired)
{
	this.fieldID = fieldID;
	this.lookupName = lookupName;
	this.searchHelpID = searchHelpID;
	this.displayName = longName;
	this.destColumn = systemName;
	this.destDataType = dataType;
	this.searchRequired = (isRequired==1);
	this.searchType = searchType;
	
	if(searchType=='' || searchType == 'search')
	{
		if(lookupName!='')
		{
			if(isMultiSelect==1)
				this.searchType = 'luFeature';
			else
				this.searchType = 'luValue';
		}
		else
		{
			if(dataType=='Character')
				this.searchType = 'string';
			else
				this.searchType = 'range';
		}	
	}
	
	if(lookupName!='')
	{
		this.searchAnd = true;
		this.searchOr = true;
		this.searchNot = true;	
	}
	else
	{
		this.searchAnd = false;
		this.searchOr = false;
		this.searchNot = false;
	}
	
	this.lookupList = new Array();
	this.aLU = SE_AdvSearch_SearchField_addLookup;
}

function SE_AdvSearch_SearchField_addLookup(aLookup)
{
	this.lookupList[this.lookupList.length] = aLookup
}