
var prefix1 = parentControlID + "_";
var prefix2 = parentControlID + ":";

//get browser 
var isIE = navigator.userAgent.indexOf("MSIE") > -1;

function getElementByBaseId(oBase)
{
	try
	{
		var oControl = document.getElementById(prefix1 + oBase);
		if(oControl != null)
		{
			return oControl;
		}
		else
		{
			var oControl2 = document.getElementById(prefix2 + oBase);
			return oControl2;
		}
	}
	catch(oError)
	{
		return null;
	}
}
function CloseScreenDivs()
{
	prepareIE('auto', 'auto');
	var dFull = document.getElementById("overlay");
	var dSub = document.getElementById("lightbox");
		
	toggleDDL('inline');

	if(dFull != null)
		dFull.style.display = "none";
	if(dSub != null)
	{
		dSub.style.display = "none";
		dSub.innerHTML = "";	
	}
}

function toggleDDL(dis)
{
	//loop through controls and all selects
	//compensate for an IE bug called burnthrough where select boxes do not respect z-index
	//document.forms[0]["giftCertificate5:ddlRegion"].style.display = "none";
	//document.forms[0]["giftCertificate5:ddlRegion"].style.display = "inline";
	var elem = document.forms[0].elements; 	
	for(var i = 0; i < elem.length; i++)
	{
		if(elem[i].type == 'select-one')
		{
			elem[i].style.display = dis;
		}
	}		
}

function showModal()
{	
	prepareIE('100%', 'hidden');
	toggleDDL('none');
	
	var dFull = document.getElementById("overlay");
	var dSub = document.getElementById("lightbox");	
	
	if(dFull != null)
		dFull.style.display = "block";
	if(dSub != null)
	{
		dSub.style.display = "block";	
		
		dSub.innerHTML = dSub.innerHTML + "<div class='close'><a href='javascript:CloseScreenDivs();'><img src='/common/img/closebutton.gif'></a></div><br /><h3>Getting Search Results...</h3><br />";		
	}			
}

function prepareIE(height, overflow)
{
	try
	{
		var htm = document.getElementsByTagName('html')[0];
		htm.scrollTop = 0;
		htm.style.height = height;
		htm.style.overflow = overflow;
	}
	catch(ex)
	{
		//alert(ex);	
	}
}

function FindAHome()
{
	showModal();
	getSearchResults();
	return false;
}

function getDDLByBaseId(baseID)
{
	var elem = document.forms[0].elements; 	
	for(var i = 0; i < elem.length; i++)
	{
		if(elem[i].type == 'select-one')
		{
			if(elem[i].id.indexOf(baseID) > -1)
				return elem[i];
		}
	}	
}

function getSearchResults()
{
	var dType = getDDLByBaseId("ddlHomeType");
	var dPrice = getDDLByBaseId("ddlStartPrice");
	var dLocation = getDDLByBaseId("ddlLocation");
	
	if(dType != null && dPrice != null && dLocation != null)
	{
		var oQuery;
		if(dType.value != "")
			oQuery += "&type=" + dType.value;
		if(dPrice.value != "")
			oQuery += "&price=" + dPrice.value;
		if(dLocation.value != "")
			oQuery += "&location=" + dLocation.value;
		
		requestInfo(oQuery);
	}
	else
	{
		parseDisplayResponse("Unable to find search parameters.");
	}
}

function parseDisplayResponse(oResponse)
{
	var dSub = document.getElementById("lightbox");
	if(dSub != null)
	{
		dSub.style.display = "block";	
		
		dSub.innerHTML = "<div class='close'><a href='javascript:CloseScreenDivs();'><img src='/common/img/closebutton.gif'></a></div><br /><h3>Homes that match your search</h3><br />";
		if(oResponse.length < 110)
		{
			dSub.innerHTML += "<div id='scrollDiv'>No results match your search.</div>";
		}
		else
		{
			dSub.innerHTML += "<div id='scrollDiv'>" + oResponse + "</div>";
		}
	}	
}

function requestInfo(oQuery)
{
	var oXmlHttp = createXMLHttp();
	var strPage = "/findahomeresults.aspx?";
	
	oXmlHttp.open("GET", strPage + oQuery, true);
	//alert(strPage + oQuery);
	oXmlHttp.onreadystatechange = function () { 
		try
		{
			if(oXmlHttp.readyState == 4)
			{
				if(oXmlHttp.status == 200)
				{
					parseDisplayResponse(oXmlHttp.responseText);
				}
			}
		}
		catch(oError)
		{
			//alert(oError);
		}
	}
	oXmlHttp.setRequestHeader("Cache-Control", "no-cache");
	oXmlHttp.send(null);	
}

function createXMLHttp()
{
	if(typeof XMLHttpRequest != "undefined")
	{
		return new XMLHttpRequest();
	}
	else if(window.ActiveXObject)
	{
		var aVersions = [ "MSXML2.XMLHttp.5.0", 
		"MSXML2.XMLHttp.4.0","MSXML2.XMLHttp.3.0", 
		"MSXML2.XMLHttp", "Microsoft.XXMLHttp"];
		
		for (var i = 0; i < aVersions.length; i ++)
		{
			try
			{
				var oXmlHttp = new ActiveXObject(aVersions[i]);
				return oXmlHttp;
			}
			catch(oError)
			{
			
			}
		}
	}
	throw new Error("XmlHttp object could not be created.");
}

function requestListItems(oControl, oQuery)
{
	var oXmlHttp = createXMLHttp();
	var strPage = "/getListItems.aspx?";
	oXmlHttp.open("GET", strPage + oQuery + "&control=" + oControl, true);
	oXmlHttp.onreadystatechange = function () { 
		try
		{
			if(oXmlHttp.readyState == 4)
			{
				if(oXmlHttp.status == 200)
				{
					parseListItems(oXmlHttp.responseText, oControl);
				}
			}
		}
		catch(oError)
		{
			//alert(oError);
		}
	}
	oXmlHttp.setRequestHeader("Cache-Control", "no-cache");
	oXmlHttp.send(null);	
}

function parseListItems(items, oControl)
{
	var ddl = null;
	if(oControl == "type")
	{
		ddl = getDDLByBaseId("ddlHomeType");
	}
	else if (oControl == "price")
	{
		ddl = getDDLByBaseId("ddlStartPrice");
	}
	else if (oControl == "location")
	{
		ddl = getDDLByBaseId("ddlLocation");
	}
	
	if(ddl != null)
	{
		ddl.options.length = 0;		
		ddl.options[0] = new Option("--SELECT--", "");	
		var itemArray = items.split("|");
		for(var i = 0; i < itemArray.length; i ++)
		{
			//parse out items and clear options and re-add new options
			if(itemArray[i].substring(0, itemArray[i].indexOf("=")) != '')
			{
				ddl.options[i + 1] = new Option(itemArray[i].substring(0, itemArray[i].indexOf("=")),itemArray[i].substring(itemArray[i].indexOf("=") + 1));
			}
		}
		ddl.disabled = false;
	}
	
	//var bFind = getElementByBaseId("btnFind");
	
	//if(bFind != null)
	//{
	//	bFind.value = "Find >";
	//}
}

function FilterDropDowns(oControl, oValuesPrice, oValuesType, oValuesLocation)
{
	var dType = getDDLByBaseId("ddlHomeType");
	var dPrice = getDDLByBaseId("ddlStartPrice");
	var dLocation = getDDLByBaseId("ddlLocation");
	//var bFind = getElementByBaseId("btnFind");
	
	//if(bFind != null)
	//{
	//	bFind.value = "Wait..";
	//}
	
	if(dType != null && dPrice != null && dLocation != null)
	{
		var oQuery = "";
		if(dType.value != "")
			oQuery += "&type=" + dType.value;
		if(dPrice.value != "")
			oQuery += "&price=" + dPrice.value;
		if(dLocation.value != "")
			oQuery += "&location=" + dLocation.value;
		
		if(dType.value == "")
		{
			dType.disabled = true;
			dType.options.length = 0;		
			dType.options[0] = new Option("Filtering..", "");
			requestListItems("type", oQuery + "&values=" + oValuesType);
		}
		if(dPrice.value == "")
		{
			dPrice.disabled = true;
		    dPrice.options.length = 0;		
			dPrice.options[0] = new Option("Filtering..", "");
			requestListItems("price", oQuery + "&values=" + oValuesPrice);
		}
		if(dLocation.value == "")
		{
			dLocation.disabled = true;
		    dLocation.options.length = 0;		
			dLocation.options[0] = new Option("Filtering..", "");
			requestListItems("location", oQuery + "&values=" + oValuesLocation);
		}
	}
}

