var CityList = "";
var CityLastValue = "";
var Community_NameList = "";
var Community_NameLastValue = "";
var SubdivisionList = "";
var SubdivisionLastValue = "";
var Community_NameValues = "";
var Community_NameCount = 0;
var Community_NameArray = new Array();
var SubdivisionValues = "";
var SubdivisionCount = 0;
var SubdivisionArray = new Array();

$(document).ready(function() {
	$('#Prop_Type_Description').selectbox();
	$('#Minimum_Price').selectbox();
	$('#Maximum_Price').selectbox();
	$('#Bedrooms').selectbox();
	$('#Bathrooms').selectbox();
	$('#SQFootage').selectbox();
	$('#Pool').selectbox();
	$('#Waterfront').selectbox();
});

document.getElementById('SelectedCity0').style.background = '#c6b898';

function InitialCityLoad(ID) {
	CityToggleOption(ID);
	SendRequestCityCommunity_Name(CityList);
	SendRequestCitySubdivision(CityList);
}

function CityToggleEvent(oEvent,Counter,Type) {
	if ((oEvent.shiftKey) || (oEvent.ctrlKey)) {
		if (oEvent.shiftKey) {
			if(CityLastValue > Counter) {
				CityLastValue--;
				Counter--;
				for(i=CityLastValue;i>Counter;i--){ CityToggleOption(i); }
			} else {
				CityLastValue++;
				Counter++;
				for(i=CityLastValue;i<Counter;i++){ CityToggleOption(i); }
			}
		}
		if (oEvent.ctrlKey) {
			CityToggleOption(Counter);
		}
	} else {
		CitySelectOption(Counter);
	}
	SendRequestCityCommunity_Name(CityList);
	SendRequestCitySubdivision(CityList);
	SendRequestCount();
	document.getElementById('HiddenCity').value = CityList;
	document.getElementById('HiddenCommunity_Name').value = "";
	document.getElementById('HiddenSubdivision').value = "";
}
function CityToggleOption(Counter)
{
	document.getElementById('SelectedCity0').style.background = '#ffffff';
	if(document.getElementById('SelectedCity' + Counter).style.background == '#c6b898') {
		document.getElementById('SelectedCity' + Counter).style.background = '#ffffff';
		if(CityList.indexOf("'" + CityArray[Counter] + "',") !== -1) { 
			var sRegExInput = new RegExp("'" + CityArray[Counter] + "',", "g");
			CityList = CityList.replace(sRegExInput,"");
		}
	} else {
		document.getElementById('SelectedCity' + Counter).style.background = '#c6b898';
		if(CityList.indexOf("'" + CityArray[Counter] + "',") == -1) {
			CityList = CityList + "'" + CityArray[Counter] + "',";
		}
	}
	CityLastValue = Counter;
	SendRequestCount();
}
function CitySelectOption(Counter)
{
	document.getElementById('SelectedCity0').style.background = '#ffffff';
	TempCount = CityCount + 1;
	for(i=1;i<TempCount;i++){ document.getElementById('SelectedCity' + i).style.background = '#ffffff'; }
	document.getElementById('SelectedCity' + Counter).style.background = '#c6b898';
	CityList = "'" + CityArray[Counter] + "',";
	CityLastValue = Counter;
	SendRequestCount();
}
function CityClearList()
{
	document.getElementById('SelectedCity0').style.background = '#c6b898';
	document.getElementById('Community_Name_CTRL_Message').style.visibility = 'hidden';
	document.getElementById('Subdivision_CTRL_Message').style.visibility = 'hidden';	
	TempCount = CityCount + 1;
	for(i=1;i<TempCount;i++){ document.getElementById('SelectedCity' + i).style.background = '#ffffff'; }
	CityList = "";
	CityLastValue = 0;
	Community_NameList = "";
	Community_NameLastValue = 0;
	document.getElementById('Community_NameDropDown').innerHTML = '<table cellpadding="0" cellspacing="0"><tr><td align="center" valign="middle">Please select a City<br />to narrow your search.</td></tr></table>';
	SubdivisionList = "";
	SubdivisionLastValue = 0;
	document.getElementById('SubdivisionDropDown').innerHTML = '<table cellpadding="0" cellspacing="0"><tr><td align="center" valign="middle">Please select a City<br />to narrow your search.</td></tr></table>';
	SendRequestCount();
	document.getElementById('HiddenCity').value = CityList;
	document.getElementById('HiddenCommunity_Name').value = "";
	document.getElementById('HiddenSubdivision').value = "";
}
function Community_NameToggleEvent(oEvent,Counter,Type) {
	if ((oEvent.shiftKey) || (oEvent.ctrlKey)) {
		if (oEvent.shiftKey) {
			if(Community_NameLastValue > Counter) {
				Community_NameLastValue--;
				Counter--;
				for(i=Community_NameLastValue;i>Counter;i--){ Community_NameToggleOption(i); }
			} else {
				Community_NameLastValue++;
				Counter++;
				for(i=Community_NameLastValue;i<Counter;i++){ Community_NameToggleOption(i); }
			}
		}
		if (oEvent.ctrlKey) {
			Community_NameToggleOption(Counter);
		}
	} else {
		Community_NameSelectOption(Counter);
	}
	document.getElementById('HiddenCommunity_Name').value = Community_NameList;
}
function Community_NameToggleOption(Counter)
{
	var LowerCounter = Counter - 1;
	document.getElementById('SelectedCommunity_Name0').style.background = '#ffffff';
	if(document.getElementById('SelectedCommunity_Name' + Counter).style.background == '#c6b898') {
		document.getElementById('SelectedCommunity_Name' + Counter).style.background = '#ffffff';
		if(Community_NameList.indexOf(Community_NameArray[LowerCounter] + ",") !== -1) { 
			var sRegExInput = new RegExp(Community_NameArray[LowerCounter] + ",", "g");
			Community_NameList = Community_NameList.replace(sRegExInput,"");
			document.getElementById('HiddenCommunity_Name').value = Community_NameList;
		}
	} else {
		document.getElementById('SelectedCommunity_Name' + Counter).style.background = '#c6b898';
		if(Community_NameList.indexOf(Community_NameArray[LowerCounter] + ",") == -1) {
			Community_NameList = Community_NameList + Community_NameArray[LowerCounter] + ",";
			document.getElementById('HiddenCommunity_Name').value = Community_NameList;
		}
	}
	Community_NameLastValue = Counter;
	SendRequestCount();
}
function Community_NameSelectOption(Counter)
{
	var LowerCounter = Counter - 1;
	TempCount = Community_NameCount + 1;
	for(i=0;i<TempCount;i++){ document.getElementById('SelectedCommunity_Name' + i).style.background = '#ffffff'; }
	document.getElementById('SelectedCommunity_Name' + Counter).style.background = '#c6b898';
	Community_NameList = Community_NameArray[LowerCounter] + ",";
	document.getElementById('HiddenCommunity_Name').value = Community_NameList;
	Community_NameLastValue = Counter;
	SendRequestCount();
}
function Community_NameClearList()
{
	document.getElementById('SelectedCommunity_Name0').style.background = '#c6b898';
	TempCount = Community_NameCount + 1;
	for(i=1;i<TempCount;i++){ document.getElementById('SelectedCommunity_Name' + i).style.background = '#ffffff'; }
	Community_NameList = "";
	Community_NameLastValue = 0;
	SendRequestCount();
	document.getElementById('HiddenCommunity_Name').value = Community_NameList;
}
function SubdivisionToggleEvent(oEvent,Counter,Type) {
	if ((oEvent.shiftKey) || (oEvent.ctrlKey)) {
		if (oEvent.shiftKey) {
			if(SubdivisionLastValue > Counter) {
				SubdivisionLastValue--;
				Counter--;
				for(i=SubdivisionLastValue;i>Counter;i--){ SubdivisionToggleOption(i); }
			} else {
				SubdivisionLastValue++;
				Counter++;
				for(i=SubdivisionLastValue;i<Counter;i++){ SubdivisionToggleOption(i); }
			}
		}
		if (oEvent.ctrlKey) {
			SubdivisionToggleOption(Counter);
		}
	} else {
		SubdivisionSelectOption(Counter);
	}
	document.getElementById('HiddenSubdivision').value = SubdivisionList;
}
function SubdivisionToggleOption(Counter)
{
	var LowerCounter = Counter - 1;
	document.getElementById('SelectedSubdivision0').style.background = '#ffffff';
	if(document.getElementById('SelectedSubdivision' + Counter).style.background == '#c6b898') {
		document.getElementById('SelectedSubdivision' + Counter).style.background = '#ffffff';
		if(SubdivisionList.indexOf(SubdivisionArray[LowerCounter] + ",") !== -1) { 
			var sRegExInput = new RegExp(SubdivisionArray[LowerCounter] + ",", "g");
			SubdivisionList = SubdivisionList.replace(sRegExInput,"");
			document.getElementById('HiddenSubdivision').value = SubdivisionList;
		}
	} else {
		document.getElementById('SelectedSubdivision' + Counter).style.background = '#c6b898';
		if(SubdivisionList.indexOf(SubdivisionArray[LowerCounter] + ",") == -1) {
			SubdivisionList = SubdivisionList + SubdivisionArray[LowerCounter] + ",";
			document.getElementById('HiddenSubdivision').value = SubdivisionList;
		}
	}
	SubdivisionLastValue = Counter;
	SendRequestCount();
}
function SubdivisionSelectOption(Counter)
{
	var LowerCounter = Counter - 1;
	TempCount = SubdivisionCount + 1;
	for(i=0;i<TempCount;i++){ document.getElementById('SelectedSubdivision' + i).style.background = '#ffffff'; }
	document.getElementById('SelectedSubdivision' + Counter).style.background = '#c6b898';
	SubdivisionList = SubdivisionArray[LowerCounter] + ",";
	document.getElementById('HiddenSubdivision').value = SubdivisionList;
	SubdivisionLastValue = Counter;
	SendRequestCount();
}
function SubdivisionClearList()
{
	document.getElementById('SelectedSubdivision0').style.background = '#c6b898';
	TempCount = SubdivisionCount + 1;
	for(i=1;i<TempCount;i++){ document.getElementById('SelectedSubdivision' + i).style.background = '#ffffff'; }
	SubdivisionList = "";
	SubdivisionLastValue = 0;
	SendRequestCount();
	document.getElementById('HiddenSubdivision').value = SubdivisionList;
}
function SendRequestCitySubdivision(CityList) {
	SubdivisionList = "";
	SubdivisionLastValue = 0;
	document.getElementById('SubdivisionDropDown').innerHTML = '<table cellpadding="0" cellspacing="0"><tr><td align="center" valign="middle"><img src="graphics/loading.gif"></td></tr></table>';
	varUID = GenerateRandomNumber();
	varCriteria = "homesearch-data.asp?Request=Subdivision&UID="  + varUID
	+ "&City=" + CityList.replace(/'/g,"");
	include(varCriteria,"SubdivisionDropDown");

	varUID = GenerateRandomNumber();
	varCriteria = "homesearch-data.asp?Request=SubdivisionArray&UID="  + varUID
	+ "&City=" + CityList.replace(/'/g,"");
	var xmlhttp = false;
  /*@cc_on @*/
  /*@if (@_jscript_version >= 5)
  try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } catch (E) { xmlhttp = false; } }
  /*@end @*/
  if (!xmlhttp && typeof XMLHttpRequest!='undefined') { xmlhttp = new XMLHttpRequest(); }
	xmlhttp.open("GET", varCriteria,true);
  xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4) { 
		SubdivisionValues = xmlhttp.responseText;
		SubdivisionArray = SubdivisionValues.split('|');
		SubdivisionCount = SubdivisionArray.length;
		if (SubdivisionCount > 4) { document.getElementById('Subdivision_CTRL_Message').style.visibility = 'visible'; } else { document.getElementById('Subdivision_CTRL_Message').style.visibility = 'hidden'; }
	} }
  xmlhttp.send(null)
}
function SendRequestCityCommunity_Name(CityList) {
	Community_NameList = "";
	Community_NameLastValue = 0;
	document.getElementById('Community_NameDropDown').innerHTML = '<table cellpadding="0" cellspacing="0"><tr><td align="center" valign="middle"><img src="graphics/loading.gif"></td></tr></table>';
	varUID = GenerateRandomNumber();
	varCriteria = "homesearch-data.asp?Request=Community_Name&UID="  + varUID
	+ "&City=" + CityList.replace(/'/g,"");
	include(varCriteria,"Community_NameDropDown");

	varUID = GenerateRandomNumber();
	varCriteria = "homesearch-data.asp?Request=Community_NameArray&UID="  + varUID
	+ "&City=" + CityList.replace(/'/g,"");
	var xmlhttp = false;
  /*@cc_on @*/
  /*@if (@_jscript_version >= 5)
  try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } catch (E) { xmlhttp = false; } }
  /*@end @*/
  if (!xmlhttp && typeof XMLHttpRequest!='undefined') { xmlhttp = new XMLHttpRequest(); }
	xmlhttp.open("GET", varCriteria,true);
  xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4) { 
		Community_NameValues = xmlhttp.responseText;
		Community_NameArray = Community_NameValues.split('|');
		Community_NameCount = Community_NameArray.length;
		if (Community_NameCount > 4) { document.getElementById('Community_Name_CTRL_Message').style.visibility = 'visible'; } else { document.getElementById('Community_Name_CTRL_Message').style.visibility = 'hidden'; }
	} }
  xmlhttp.send(null)

}
function SendRequestCount() {
	document.getElementById('PropertySearchCount').innerHTML = '<img src="graphics/loading-small.gif" style="margin-top: 3px;">';
	varUID = GenerateRandomNumber();
	varCriteria = "homesearch-count.asp?UID="  + varUID
	if(CityList) {
	varCriteria = varCriteria + "&City=" + CityList.replace(/'/g,"")
	}
	if(Community_NameList) {
	varCriteria = varCriteria + "&Community_Name=" + Community_NameList;
	}
	if(SubdivisionList) {
	varCriteria = varCriteria + "&Subdivision=" + SubdivisionList;
	}
	if(document.getElementById('ZipCode').value) {
	varCriteria = varCriteria + "&ZipCode=" + document.getElementById('ZipCode').value;
	}
	if(document.getElementById('Prop_Type_Description').value) {
	varCriteria = varCriteria + "&Prop_Type_Description=" + document.getElementById('Prop_Type_Description').value;
	}
	if(document.getElementById('Minimum_Price').value) {
	varCriteria = varCriteria + "&Minimum_Price=" + document.getElementById('Minimum_Price').value;
	}
	if(document.getElementById('Maximum_Price').value) {
	varCriteria = varCriteria + "&Maximum_Price=" + document.getElementById('Maximum_Price').value;
	}
	if(document.getElementById('Bedrooms').value) {
	varCriteria = varCriteria + "&Bedrooms=" + document.getElementById('Bedrooms').value;
	}
	if(document.getElementById('Bathrooms').value) {
	varCriteria = varCriteria + "&Bathrooms=" + document.getElementById('Bathrooms').value;
	}
	if(document.getElementById('SQFootage').value) {
	varCriteria = varCriteria + "&SQFootage=" + document.getElementById('SQFootage').value;
	}
	if(document.getElementById('Pool').value) {
	varCriteria = varCriteria + "&Pool=" + document.getElementById('Pool').value;
	}
	if(document.getElementById('Waterfront').value) {
	varCriteria = varCriteria + "&Waterfront=" + document.getElementById('Waterfront').value;
	}
	if(document.getElementById('PhotoOnly').checked) {
	varCriteria = varCriteria + "&PhotoOnly=Yes";
	}
	//alert(varCriteria);
	include(varCriteria,"PropertySearchCount");
}
function include(xUrl,xId) {
	var xmlhttp = false;
  /*@cc_on @*/
  /*@if (@_jscript_version >= 5)
  try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } catch (E) { xmlhttp = false; } }
  /*@end @*/
  if (!xmlhttp && typeof XMLHttpRequest!='undefined') { xmlhttp = new XMLHttpRequest(); }
	xmlhttp.open("GET", xUrl,true);
  xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4) { 
		document.getElementById(xId).innerHTML = xmlhttp.responseText;
	} }
  xmlhttp.send(null)
}

function GenerateRandomNumber() {
	var randomnumber= Math.floor(Math.random()*80000000)+10000000;
	return (randomnumber);
}

function CheckZipCodeLength(field) {
	var ZipCodeValue = field.value;
	var ZipCodeLength = ZipCodeValue.length;
	ZipCodeLength++;
	if(ZipCodeLength >= 3) {
	SendRequestCount();
	}
}
SendRequestCount();