﻿function DisableAllControls ()
{
var statectrl = GetObj(GetObj('cboStateClientID').value);
if (statectrl != null)
statectrl.disabled = true;

var metroctrl = GetObj(GetObj('cboMetroClientID').value);
if (metroctrl != null)
metroctrl.disabled = true;

var listingctrl = GetObj(GetObj('cboSearchForClientID').value);
if (listingctrl != null)
listingctrl.disabled = true;

var pricelowctrl = GetObj(GetObj('cboLowPriceClientID').value);
if (pricelowctrl != null)
pricelowctrl.disabled = true;

var pricehighctrl = GetObj(GetObj('cboHighPriceClientID').value);
if (pricehighctrl != null)
pricehighctrl.disabled = true;

var defaultlocationctrl = GetObj('cbDefaultLocation');
if (defaultlocationctrl != null)
defaultlocationctrl.disabled = true;    

if (GetObj ('rbHomePlan' != null))
{
var homeplanctrl = GetObj(GetObj ('rbHomePlan').value);
if (homeplanctrl != null)
homeplanctrl.disabled = true;

var communityctrl = GetObj(GetObj ('rbCommunity').value);
if (communityctrl != null)
communityctrl.disabled = true;

var builderctrl = GetObj(GetObj ('rbBuilder').value);
if (builderctrl != null)
builderctrl.disabled = true;

var inventoryctrl = GetObj(GetObj ('rbInventory').value);
if (inventoryctrl != null)
inventoryctrl.disabled = true;
}

var cmdsearchbtn = GetObj(GetObj('cmdSearchBtn').value);
if (cmdsearchbtn != null)
cmdsearchbtn.disabled = true;
}

function SaveSettings()
{
if (GetObj('cboSearchForClientID') != null)
{
var searchforctrl = GetObj(GetObj('cboSearchForClientID').value);
var selectedIndex = searchforctrl.selectedIndex;
if (selectedIndex != -1)
{
var searchforid = searchforctrl.options[selectedIndex].value;
GetObj('searchforid').value = searchforid;
}
}

if (GetObj('cboLowPriceClientID') != null)
{
var pricelowctrl = GetObj(GetObj('cboLowPriceClientID').value);
var selectedIndex = pricelowctrl.selectedIndex;
var pricelowid = pricelowctrl.options[selectedIndex].value;
GetObj('pricelowid').value = pricelowid;
}

if (GetObj('cboHighPriceClientID') != null)
{
var pricehighctrl = GetObj(GetObj('cboHighPriceClientID').value);
var selectedIndex = pricehighctrl.selectedIndex;
var pricehighid = pricehighctrl.options[selectedIndex].value;
GetObj('pricehighid').value = pricehighid;
}

if (GetObj ('cboBedsClientID') != null)
{
var bedsctrl = GetObj(GetObj('cboBedsClientID').value);
if (bedsctrl != null)
{
var bedsid = bedsctrl.options[bedsctrl.selectedIndex].value;
GetObj("bedsid").value = bedsid;
}
}

if (GetObj ('cboBathsClientID') != null)
{
var bathsctrl = GetObj(GetObj('cboBathsClientID').value);
if (bathsctrl != null)
{
var bathsid = bathsctrl.options[bathsctrl.selectedIndex].value;
GetObj("bathsid").value = bathsid;
}
}

if (GetObj ('cboRadiusClientID') != null)
{
var radiusctrl = GetObj(GetObj('cboRadiusClientID').value);
if (radiusctrl != null)
{
var radius = radiusctrl.options[radiusctrl.selectedIndex].value;
GetObj("radius").value = radius;
}
}

if (GetObj ('cboSchoolDistrictClientID') != null)
{
var schooldistrictctrl = GetObj(GetObj('cboSchoolDistrictClientID').value);
var selectedIndex = schooldistrictctrl.selectedIndex;
if (selectedIndex != -1)
{
var schooldistrict = schooldistrictctrl.options[selectedIndex].value;
GetObj('schooldistrict').value = schooldistrict;
}
}

if (GetObj ('cboBuilderClientID') != null)
{
var builderctrl = GetObj(GetObj('cboBuilderClientID').value);
var selectedIndex = builderctrl.selectedIndex;
if (selectedIndex != -1)
{
var builderid = builderctrl.options[selectedIndex].value;
GetObj('builderid').value = builderid;
}
}

if (GetObj ('cboRadiusClientID') != null)
{
var radiusctrl = GetObj(GetObj('cboRadiusClientID').value);
var selectedIndex = radiusctrl.selectedIndex;
var radius = radiusctrl.options[selectedIndex].value;
GetObj('radius').value = radius;
}

if (GetObj ('txtSearchClientID') != null)
{
var txtSearch = GetObj(GetObj('txtSearchClientID').value);
GetObj('freetextval').value = txtSearch.value;
}

if (GetObj ('hdnSearchIDClientID') != null)
{
GetObj('searchid').value = GetObj(GetObj ('hdnSearchIDClientID').value).value;
}
}

function FindMetroList(url)
{ 
try 
{
var statectrl = GetObj(GetObj('cboStateClientID').value);
var stateid = statectrl.options[statectrl.selectedIndex].value; 

if (stateid == "0")
{
var metroctrl = GetObj(GetObj('cboMetroClientID').value);
if (metroctrl != null)
{
//Clear the Metro box                
for(var i=metroctrl.options.length-1;i>=0;i--)
metroctrl.options[i] = null;

//Add instructions
var newOpt = new Option('Select a Metro', '0')                  
metroctrl.options[metroctrl.options.length] = newOpt;

// Since we have re-loaded the metro now update the Property Type
FindSearchForList(metroctrl.getAttribute('searchforurl')); 
}
}
else
{
xmlMetroHttp = GetXmlHttpObject(CallbackMetro);
SendXmlHttpRequest(xmlMetroHttp, url + '?stateid=' + stateid + "&searchtype=" + statectrl.getAttribute ("searchtype"));
}
}
catch(e){} 
} 

function FindStateList(url)
{
try 
{
var statectrl = GetObj(GetObj('cboStateClientID').value);
xmlStateHttp = GetXmlHttpObject(CallbackState);
SendXmlHttpRequest(xmlStateHttp, url + "?searchtype=" + statectrl.getAttribute ("searchtype"));
}
catch(e){} 
}

function FindLowPriceList(url)
{
try 
{
xmlLowPriceHttp = GetXmlHttpObject(CallbackLowPrice);
SendXmlHttpRequest(xmlLowPriceHttp, url);
}
catch(e){} 
}

function FindHighPriceList(url)
{
try 
{
xmlHighPriceHttp = GetXmlHttpObject(CallbackHighPrice);
SendXmlHttpRequest(xmlHighPriceHttp, url);
}
catch(e){} 
}

function FindRadiusList(url)
{
try 
{
var radius = GetObj('cboRadiusClientID');
if( radius != null )
{
xmlRadiusHttp = GetXmlHttpObject(CallbackRadius);
SendXmlHttpRequest(xmlRadiusHttp, url);
}
}
catch(e){} 
}

function FindSearchForList ( url )
{
FindSearchForListWithCallback ( url, CallbackSearchFor );
}

function FindSearchForListWithCallback ( url, callback )
{ 
try 
{ 
var statectrl = GetObj(GetObj('cboStateClientID').value);
var stateid = statectrl.options[statectrl.selectedIndex].value;        

var metroctrl = GetObj(GetObj('cboMetroClientID').value);
var metroid = metroctrl.options[metroctrl.selectedIndex].value;

var zipcode = "";
if (GetObj ('txtZipClientID') != null)
{
var zipcodectrl = GetObj(GetObj('txtZipClientID').value);
zipcode = zipcodectrl.value;
}
var radius = "";
if (GetObj ('cboRadiusClientID') != null)
{
var radiusctrl = GetObj(GetObj('cboRadiusClientID').value);
if (radiusctrl.selectedIndex != -1)
radius = radiusctrl.options[radiusctrl.selectedIndex].value;
}

var searchforctrl = GetObj(GetObj('cboSearchForClientID').value);
var IncludeBuilderTypes = searchforctrl.getAttribute ('includebuildertypes');

if (metroid == "0")
{
//Clear the SearchFor box                
for(var i=searchforctrl.options.length-1;i>=0;i--)
searchforctrl.options[i] = null;

//Add instructions
var newOpt = new Option('All Property Types', '0')                  
searchforctrl.options[searchforctrl.options.length] = newOpt;

// Since we have re-loaded the metro now update the School District and Builder dropdowns
if (GetObj('cboSchoolDistrictClientID') != null)
FindSchoolDistrictList(searchforctrl.getAttribute('schooldistricturl'));
if (GetObj('cboBuilderClientID') != null)
FindBuilderList(searchforctrl.getAttribute('builderurl'));
}
else
{
xmlSearchForHttp = GetXmlHttpObject(callback); 
SendXmlHttpRequest(xmlSearchForHttp, url + '?stateid=' + stateid + '&metroid=' + metroid + "&zip=" + zipcode + "&radius=" + radius + "&IncludeBuilderTypes=" + IncludeBuilderTypes + "&searchtype=" + statectrl.getAttribute("searchtype"));
}

// Disable searchOptions for Plan, Community, Builder and Inventory
DisableSearchOptions();         

}
catch(e){} 
} 

function FindSchoolDistrictList ( url )
{
FindSchoolDistrictListWithCallback ( url, CallbackSchoolDistrict );
}

function FindSchoolDistrictListWithCallback ( url, callback )
{ 
try 
{ 
var statectrl = GetObj(GetObj('cboStateClientID').value);
var stateid = statectrl.options[statectrl.selectedIndex].value;        

var metroctrl = GetObj(GetObj('cboMetroClientID').value);
var metroid = metroctrl.options[metroctrl.selectedIndex].value;

var searchforctrl = GetObj(GetObj('cboSearchForClientID').value);
var searchforid = searchforctrl.options[searchforctrl.selectedIndex].value;

var zipcodectrl = GetObj(GetObj('txtZipClientID').value);
var zipcode = zipcodectrl.value;

var radiusctrl = GetObj(GetObj('cboRadiusClientID').value);
var radius = radiusctrl.options[radiusctrl.selectedIndex].value;

xmlSchoolDistrictHttp = GetXmlHttpObject(callback);   
SendXmlHttpRequest(xmlSchoolDistrictHttp, url + '?stateid=' + stateid + '&metroid=' + metroid + '&searchforid=' + searchforid + "&zip=" + zipcode + "&radius=" + radius + "&searchtype=" + statectrl.getAttribute ("searchtype"));
}
catch(e){} 
} 

function FindBuilderList ( url )
{
FindBuilderListWithCallback ( url, CallbackBuilder );
}

function FindBuilderListWithCallback ( url, callback )
{ 
try 
{ 
var statectrl = GetObj(GetObj('cboStateClientID').value);
var stateid = statectrl.options[statectrl.selectedIndex].value;        

var metroctrl = GetObj(GetObj('cboMetroClientID').value);
var metroid = metroctrl.options[metroctrl.selectedIndex].value;

var searchforctrl = GetObj(GetObj('cboSearchForClientID').value);
var searchforid = searchforctrl.options[searchforctrl.selectedIndex].value;

var zipcodectrl = GetObj(GetObj('txtZipClientID').value);
var zipcode = zipcodectrl.value;

var radiusctrl = GetObj(GetObj('cboRadiusClientID').value);
var radius = radiusctrl.options[radiusctrl.selectedIndex].value;

xmlBuilderHttp = GetXmlHttpObject(callback);   
SendXmlHttpRequest(xmlBuilderHttp, url + '?stateid=' + stateid + '&metroid=' + metroid + '&searchforid=' + searchforid + "&zip=" + zipcode + "&radius=" + radius + "&searchtype=" + statectrl.getAttribute ("searchtype"));
}
catch(e){} 
} 

//CallbackMethod will fire when the state 
//has changed, i.e. data is received back 
function CallbackMetro() 
{ 
try
{
//readyState of 4 or 'complete' represents 
//that data has been returned 
if (xmlMetroHttp.readyState == 4 || xmlMetroHttp.readyState == 'complete')
{
var response = xmlMetroHttp.responseText;
if (response.length > 0)
{
//Determine if there is a initialization value
var initial_metroidCtrl = GetObj('initial_metroid');
var initialvalue = -1;
if (initial_metroidCtrl != null)
{
initialvalue = parseInt(initial_metroidCtrl.value);
}

var metroctrl = GetObj(GetObj('cboMetroClientID').value);
var selectedIndex = metroctrl.selectedIndex;
var metroid = 0;
if (selectedIndex != -1)
metroid = metroctrl.options[selectedIndex].value;

if( metroctrl.getAttribute('lastValue') != null )
initialvalue = parseInt(metroctrl.getAttribute('lastValue'));

var doc = xmlMetroHttp.responseXML;
metroqty = doc.documentElement.getElementsByTagName('item');
var name = '';
var id = '';

//Clear the Metro box                
for(var i=metroctrl.options.length-1;i>=0;i--)
metroctrl.options[i] = null;

//Load the option values
for(var i = 0; i < metroqty.length; i++)
{                  
if(metroqty[i].attributes["name"] + "" == "undefined")    // IE
{                
id = metroqty[i].attributes.getNamedItem("id").nodeValue;
name = metroqty[i].attributes.getNamedItem("name").nodeValue;
}    
else // MOZ
{
id = metroqty[i].attributes["id"].value;
name = metroqty[i].attributes["name"].value;
}

//Perform the actual add
if (id != '')
{
var newOpt = new Option(name, id)                  
metroctrl.options[metroctrl.options.length] = newOpt;

//If there is an initial value and this is it then set the selectedindex
if (initialvalue == parseInt(id))
metroctrl.selectedIndex = i;
}
}

// Since we have re-loaded the metro now update the Property Type
FindSearchForList(metroctrl.getAttribute('searchforurl')); 
} 
}
}
catch(e){}
}

//CallbackMethod will fire when the data is received back 
function CallbackState() 
{ 
try
{
//readyState of 4 or 'complete' represents 
//that data has been returned 
if (xmlStateHttp.readyState == 4 || xmlStateHttp.readyState == 'complete')
{
var response = xmlStateHttp.responseText;
if (response.length > 0)
{
//Determine if there is a initialization value
var initial_stateCtrl = GetObj('initial_stateid');
var initialvalue = -1;
if (initial_stateCtrl != null)
initialvalue = parseInt(initial_stateCtrl.value);

var statectrl = GetObj(GetObj('cboStateClientID').value);
var selectedIndex = statectrl.selectedIndex;

if( statectrl.getAttribute('lastValue') != null )
initialvalue = parseInt(statectrl.getAttribute('lastValue'));

var stateid = 0;
if (selectedIndex != -1)
stateid = statectrl.options[selectedIndex].value;

var doc = xmlStateHttp.responseXML;
stateqty = doc.documentElement.getElementsByTagName('item');
var name = '';
var id = '';

//Load the option values
for(var i = 0; i < stateqty.length; i++)
{                  
if(stateqty[i].attributes["name"] + "" == "undefined")    // IE
{                
id = stateqty[i].attributes.getNamedItem("id").nodeValue;
name = stateqty[i].attributes.getNamedItem("name").nodeValue;
}    
else // MOZ
{
id = stateqty[i].attributes["id"].value;
name = stateqty[i].attributes["name"].value;
}

//Perform the actual add
if (id != '')
{
var newOpt = new Option(name, id)   
statectrl.options[statectrl.options.length] = newOpt;

//If there is an initial value and this is it then set the selectedindex
if (initialvalue == parseInt(id))
statectrl.selectedIndex = i;
}
}
// Since we have re-loaded the metro now update the Property Type
FindMetroList(statectrl.getAttribute('metrourl'));                    
} 
}
}
catch(e){}
}

//CallbackMethod will fire when the data is received back 
function CallbackLowPrice() 
{ 
try
{
//readyState of 4 or 'complete' represents 
//that data has been returned 
if (xmlLowPriceHttp.readyState == 4 || xmlLowPriceHttp.readyState == 'complete')
{
var response = xmlLowPriceHttp.responseText;
if (response.length > 0)
{
var lowpricectrl = GetObj(GetObj('cboLowPriceClientID').value);

var doc = xmlLowPriceHttp.responseXML;
lowpriceqty = doc.documentElement.getElementsByTagName('item');
var name = '';
var id = '';

//Load the option values
for(var i = 0; i < lowpriceqty.length; i++)
{                  
if(lowpriceqty[i].attributes["name"] + "" == "undefined")    // IE
{                
id = lowpriceqty[i].attributes.getNamedItem("id").nodeValue;
name = lowpriceqty[i].attributes.getNamedItem("name").nodeValue;
}    
else // MOZ
{
id = lowpriceqty[i].attributes["id"].value;
name = lowpriceqty[i].attributes["name"].value;
}

//Perform the actual add
if (id != '')
{
var newOpt = new Option(name, id)   
lowpricectrl.options[lowpricectrl.options.length] = newOpt;
if ((lowpricectrl.getAttribute('lastValue') != null) && (id == lowpricectrl.getAttribute('lastValue')))
lowpricectrl.selectedIndex=lowpricectrl.options.length-1;
}
}
} 
}
}
catch(e){}
}

//CallbackMethod will fire when the data is received back 
function CallbackHighPrice() 
{ 
try
{
//readyState of 4 or 'complete' represents 
//that data has been returned 
if (xmlHighPriceHttp.readyState == 4 || xmlHighPriceHttp.readyState == 'complete')
{
var response = xmlHighPriceHttp.responseText;
if (response.length > 0)
{
var highpricectrl = GetObj(GetObj('cboHighPriceClientID').value);

var doc = xmlHighPriceHttp.responseXML;
highpriceqty = doc.documentElement.getElementsByTagName('item');
var name = '';
var id = '';

//Load the option values
for(var i = 0; i < highpriceqty.length; i++)
{                  
if(highpriceqty[i].attributes["name"] + "" == "undefined")    // IE
{                
id = highpriceqty[i].attributes.getNamedItem("id").nodeValue;
name = highpriceqty[i].attributes.getNamedItem("name").nodeValue;
}    
else // MOZ
{
id = highpriceqty[i].attributes["id"].value;
name = highpriceqty[i].attributes["name"].value;
}

//Perform the actual add
if (id != '')
{
var newOpt = new Option(name, id)   
highpricectrl.options[highpricectrl.options.length] = newOpt;
if ((highpricectrl.getAttribute('lastValue') != null) && (id == highpricectrl.getAttribute('lastValue')))
highpricectrl.selectedIndex=highpricectrl.options.length-1;
}
}
//set the selectedindex for option any
if (highpricectrl.getAttribute('lastValue') == null)
highpricectrl.selectedIndex=highpricectrl.options.length - 1;
} 
}
}
catch(e){}
}

//CallbackMethod will fire when the data is received back 
function CallbackRadius() 
{ 
try
{
//readyState of 4 or 'complete' represents 
//that data has been returned 
if (xmlRadiusHttp.readyState == 4 || xmlRadiusHttp.readyState == 'complete')
{
var response = xmlRadiusHttp.responseText;
if (response.length > 0)
{
var radiusctrl = GetObj(GetObj('cboRadiusClientID').value);

var doc = xmlRadiusHttp.responseXML;
radiusqty = doc.documentElement.getElementsByTagName('item');
var name = '';
var id = '';

//Load the option values
for(var i = 0; i < radiusqty.length; i++)
{                  
if(radiusqty[i].attributes["name"] + "" == "undefined")    // IE
{                
id = radiusqty[i].attributes.getNamedItem("id").nodeValue;
name = radiusqty[i].attributes.getNamedItem("name").nodeValue;
}    
else // MOZ
{
id = radiusqty[i].attributes["id"].value;
name = radiusqty[i].attributes["name"].value;
}

//Perform the actual add
if (id != '')
{
var newOpt = new Option(name, id)   
radiusctrl.options[radiusctrl.options.length] = newOpt;
if ((radiusctrl.getAttribute('lastValue') != null) && (id == radiusctrl.getAttribute('lastValue')))
radiusctrl.selectedIndex=radiusctrl.options.length-1;
}
}
//Set the default value for Radius search.
if( statectrl.getAttribute('lastValue') == null )
radiusctrl.selectedIndex = GetObj('radiusDefault').value;            
} 
}
}
catch(e){}
}

//CallbackMethod will fire when the metro 
//has changed, i.e. data is received back 
function CallbackSearchFor() 
{ 
try
{
//readyState of 4 or 'complete' represents 
//that data has been returned 
if (xmlSearchForHttp.readyState == 4 || xmlSearchForHttp.readyState == 'complete')
{
var response = xmlSearchForHttp.responseText; 
if (response.length > 0)
{
// Determine if there is a initialization value
var initial_searchforidCtrl = GetObj('initial_searchforid');
var initialvalue = -1;
if (initial_searchforidCtrl != null)
initialvalue = parseInt(initial_searchforidCtrl.value);

var searchforctrl = GetObj(GetObj('cboSearchForClientID').value);
var selectedIndex = searchforctrl.selectedIndex;
var searchforid = 0;
if (selectedIndex != -1)
initialvalue = searchforctrl.options[selectedIndex].value;

var doc = xmlSearchForHttp.responseXML;
searchqty = doc.documentElement.getElementsByTagName('item');
var name = '';
var id = '';

if( searchforctrl.getAttribute('lastValue') != null )
initialvalue = parseInt(searchforctrl.getAttribute('lastValue'));

// Clear the Search For box                
for (var i = searchforctrl.options.length-1; i >= 0; i--)
searchforctrl.options[i] = null;

// Load the option values
for(var i = 0; i < searchqty.length; i++)
{                  
if(searchqty[i].attributes["name"] + "" == "undefined")    // IE
{                
id = searchqty[i].attributes.getNamedItem("id").nodeValue;
name = searchqty[i].attributes.getNamedItem("name").nodeValue;
}    
else // MOZ
{
id = searchqty[i].attributes["id"].value;
name = searchqty[i].attributes["name"].value;
}

// Perform the actual add
if (id != '')
{
var newOpt = new Option(name, id)                  
searchforctrl.options[searchforctrl.options.length] = newOpt;

// If there is an initial value and this is it then set the selectedindex
if (initialvalue == parseInt(id))
searchforctrl.selectedIndex = i;
}
}
// Since we have re-loaded the metro now update the School District and Builder dropdowns
if (GetObj('cboSchoolDistrictClientID') != null)
FindSchoolDistrictList(searchforctrl.getAttribute('schooldistricturl'));
if (GetObj('cboBuilderClientID') != null)
FindBuilderList(searchforctrl.getAttribute('builderurl'));
} 
}
}
catch(e){}
}

//CallbackMethod will fire when the metro 
//has changed, i.e. data is received back 
function CallbackSchoolDistrict() 
{ 
try
{
//readyState of 4 or 'complete' represents 
//that data has been returned 
if (xmlSchoolDistrictHttp.readyState == 4 || xmlSchoolDistrictHttp.readyState == 'complete')
{
var response = xmlSchoolDistrictHttp.responseText;
if (response.length > 0)
{
// Determine if there is a initialization value
var initial_schooldistrictidCtrl = GetObj('initial_schooldistrictid');
var initialvalue = -1;
if (initial_schooldistrictidCtrl != null)
initialvalue = parseInt(initial_schooldistrictidCtrl.value);

var schooldistrictctrl = GetObj(GetObj('cboSchoolDistrictClientID').value);
var selectedIndex = schooldistrictctrl.selectedIndex;
var schooldistrictid = 0;
if (selectedIndex != -1)
initialvalue = schooldistrictctrl.options[selectedIndex].value;

var doc = xmlSchoolDistrictHttp.responseXML;
searchqty = doc.documentElement.getElementsByTagName('item');
var name = '';
var id = '';

if( schooldistrictctrl.getAttribute('lastValue') != null )
initialvalue = schooldistrictctrl.getAttribute('lastValue');

// Clear the School District box                
for (var i = schooldistrictctrl.options.length-1; i >= 0; i--)
schooldistrictctrl.options[i] = null;

// Load the option values
for(var i = 0; i < searchqty.length; i++)
{                  
if(searchqty[i].attributes["name"] + "" == "undefined")    // IE
{                
id = searchqty[i].attributes.getNamedItem("id").nodeValue;
name = searchqty[i].attributes.getNamedItem("name").nodeValue;
}    
else // MOZ
{
id = searchqty[i].attributes["id"].value;
name = searchqty[i].attributes["name"].value;
}

// Perform the actual add
var newOpt = new Option(name, id)                  
schooldistrictctrl.options[schooldistrictctrl.options.length] = newOpt;

// If there is an initial value and this is it then set the selectedindex
if (initialvalue == name)
schooldistrictctrl.selectedIndex = i;
}
} 
}
}
catch(e){}
}

//CallbackMethod will fire when the metro 
//has changed, i.e. data is received back 
function CallbackBuilder() 
{ 
try
{
//readyState of 4 or 'complete' represents 
//that data has been returned 
if (xmlBuilderHttp.readyState == 4 || xmlBuilderHttp.readyState == 'complete')
{
var response = xmlBuilderHttp.responseText;
if (response.length > 0)
{
// Determine if there is a initialization value
var initial_builderidCtrl = GetObj('initial_builderid');
var initialvalue = -1;
if (initial_builderidCtrl != null)
initialvalue = parseInt(initial_builderidCtrl.value);

var builderctrl = GetObj(GetObj('cboBuilderClientID').value);
var selectedIndex = builderctrl.selectedIndex;
var builderid = 0;
if (selectedIndex != -1)
initialvalue = builderctrl.options[selectedIndex].value;

var doc = xmlBuilderHttp.responseXML;
searchqty = doc.documentElement.getElementsByTagName('item');
var name = '';
var id = '';

if( builderctrl.getAttribute('lastValue') != null )
initialvalue = builderctrl.getAttribute('lastValue');

// Clear the Builder box 
for (var i = builderctrl.options.length-1; i >= 0; i--)
builderctrl.options[i] = null;

// Load the option values
for(var i = 0; i < searchqty.length; i++)
{                  
if(searchqty[i].attributes["name"] + "" == "undefined")    // IE
{                
id = searchqty[i].attributes.getNamedItem("id").nodeValue;
name = unescape(searchqty[i].attributes.getNamedItem("name").nodeValue);
}    
else // MOZ
{
id = searchqty[i].attributes["id"].value;
name = unescape(searchqty[i].attributes["name"].value);
}

// Perform the actual add
var newOpt = new Option(name, id)                  
builderctrl.options[builderctrl.options.length] = newOpt;

// If there is an initial value and this is it then set the selectedindex
if ((initialvalue == name) || (initialvalue == id))
builderctrl.selectedIndex = i;
}
} 
}
}
catch(e){}
}

// iMode: 0=low wins;1=high wins
function validatePriceRange(iMode)
{
var cboHigh = GetObj(GetObj('cboHighPriceClientID').value);
var cboLow = GetObj(GetObj('cboLowPriceClientID').value);
var dHighPrice = getDdIdVal(cboHigh);
var dLowPrice = getDdIdVal(cboLow);
if (dHighPrice == -1) return;
if (dLowPrice == -1) return;

if(dHighPrice < dLowPrice )
{
if (iMode==1)
cboLow.selectedIndex = cboHigh.selectedIndex;  
else
cboHigh.selectedIndex = cboLow.selectedIndex;
}
return;
}

function checkHighPriceMethod(sender, args)
{
args.IsValid = true;
validatePriceRange(1);
return;
}

function checkZip(sender, args)
{
var txtZip = GetObj(GetObj ('txtZipClientID').value);
var dZip = txtZip.value;
if(GetObj ('txtZipCode').value == "") 
return false;

return;
}

function checkLowPriceMethod(sender, args)
{
args.IsValid = true;
validatePriceRange(0);
return;
}

function validateStateDD(sender, args)
{ 
validateDD(GetObj('cboStateClientID').value, args);
return;
}

function validateMetroDD(sender, args)
{ 
validateDD(GetObj('cboMetroClientID').value, args);
return;
}
function validateBuilderDD(sender, args)
{ 
validateDD(GetObj('ddlBuilderName').value, args);
return;
}
function validateRadius(sender, args)
{ 
validateDD(GetObj('cboRadiusClientID').value, args);
return;
}

function validatePropertyTypeDD(sender, args)
{ 
validateDD(GetObj('cboSearchForClientID').value, args);
return;
}

function validateSearchSelections (sender, args)
{
if ((dropDownIsValid (GetObj (GetObj('cboMetroClientID').value))) ||
((GetObj ('txtZipClientID') != null) &&
(GetObj (GetObj('txtZipClientID').value).value.length == 5)))
args.IsValid = true;
else
args.IsValid = false;

return;
}

function EnableMapResults (bEnable)
{
var chkMapSearchResults = GetObj (GetObj ("chkMapSearchResults").value);
chkMapSearchResults.disabled = !bEnable;
if (!bEnable)
chkMapSearchResults.checked = false;
}

//Disable Search Option
function DisableSearchOptions()
{
try 
{ 
var statectrl = GetObj(GetObj('cboStateClientID').value);
var selectedIndex = statectrl.selectedIndex;
var stateid = statectrl.options[selectedIndex].value;        

var metroctrl = GetObj(GetObj('cboMetroClientID').value);
var selectedIndex = metroctrl.selectedIndex;
var metroid = metroctrl.options[selectedIndex].value;

var searchforctrl = GetObj(GetObj('cboSearchForClientID').value);
var selectedIndex = searchforctrl.selectedIndex;
var searchforid = 0;
if (selectedIndex != -1)
searchforid = searchforctrl.options[selectedIndex].value;

if (stateid > 0 && metroid > 0)
{
var chkCommunitySearchResults = GetObj (GetObj ("rbCommunity").value);
chkCommunitySearchResults.checked = true;

DisablePlans(stateid, metroid, searchforid);
DisableCommunity(stateid, metroid, searchforid);
DisableBuilder(stateid, metroid, searchforid);
DisableInventory(stateid, metroid, searchforid); 
}   
}
catch(e){} 
}

//Plans
function DisablePlans(stateid, metroid, searchforid)
{    
Csi.NewHomeGuide.Web.UserControls_SearchControl.CheckForPlans(stateid, metroid, searchforid, callbackEnablePlans);
}
function callbackEnablePlans(response)
{
if(response.error != null) 
alert(response.error.Message);
else    
{
if (response.value == 'false')
EnablePlansResults(false);
else
EnablePlansResults(true);
}
}

//Community
function DisableCommunity(stateid, metroid, searchforid)
{
Csi.NewHomeGuide.Web.UserControls_SearchControl.CheckForCommunities(stateid, metroid, searchforid, callbackEnableCommunities);
}
function callbackEnableCommunities(response)
{
if (response.error != null) 
alert(response.error.Message);
else    
{
if (response.value == 'false')
EnableCommunityResults(false);
else
EnableCommunityResults(true);
}
}

//Builder
function DisableBuilder(stateid, metroid, searchforid)
{  
Csi.NewHomeGuide.Web.UserControls_SearchControl.CheckForBuilders(stateid, metroid, searchforid, callbackEnableBuilders);
}
function callbackEnableBuilders(response)
{
if(response.error != null) 
alert(response.error.Message);
else    
{
if (response.value == 'false')
EnableBuilderResults(false);
else
EnableBuilderResults(true);
}
}

//Inventory
function DisableInventory(stateid, metroid, searchforid)
{   
Csi.NewHomeGuide.Web.UserControls_SearchControl.CheckForInventory(stateid, metroid, searchforid, callbackEnableInventory);
}
function callbackEnableInventory(response)
{
if(response.error != null) 
alert(response.error.Message);
else    
{
if (response.value == 'false')
EnableInventoryResults(false);
else
EnableInventoryResults(true);
}
}

// Builders And Communities
function LoadBuildersAndCommunities (stateid, metroid, searchforid)
{
//AjaxPro.timeoutPeriod = 90000;
Csi.NewHomeGuide.Web.UserControls_SearchControl.GetBuildersAndCommunities (stateid, metroid, searchforid, callbackBuildersAndCommunities);
}
function callbackBuildersAndCommunities(response)
{
// String of Builders and Communities should be in the following format
//
// BuilderName1^BuilderID1|BuilderName2^BuilderID2|...|BuilderNameN^BuilderIDN#
// CommunityName1^CommunityID1|CommunityName2^CommunityID2|...|CommunityNameN^CommunityIDN

if (response.error != null)
alert (response.error.Message);
else
{
var ddlBuilders = null; 
if (GetObj ('ddlBuilderName') != null)
ddlBuilders = GetObj (GetObj ('ddlBuilderName').value);
else if (GetObj ('cboBuilderClientID') != null)
ddlBuilders = GetObj (GetObj ('cboBuilderClientID').value);

var ddlCommunities = null;
if (GetObj ('ddlCommunityName') != null)
ddlCommunities = GetObj (GetObj ('ddlCommunityName').value);

var saBuildersAndCommunities = response.value.split ('#');
var saBuilders = saBuildersAndCommunities [0].split ('|');
var saCommunities = saBuildersAndCommunities [1].split ('|');

if (ddlBuilders != null)
{
ddlBuilders.options[0] = new Option ('Select a Builder', '-1');
for (var iBuilder = 0; iBuilder < saBuilders.length; iBuilder++)
{
var saBuilder = saBuilders[iBuilder].split ('^');
ddlBuilders.options[iBuilder + 1] = new Option (saBuilder[0], saBuilder[1]);
}
}

if (ddlCommunities != null)
{
ddlCommunities.options[0] = new Option ('Select a Community', '-1');
for (var iCommunity = 0; iCommunity < saCommunities.length; iCommunity++)
{
var saCommunity = saCommunities[iCommunity].split ('^');
ddlCommunities.options[iCommunity + 1] = new Option (saCommunity[0], saCommunity[1]);
}
}
}

// Hide Animated gif
var divBouncyBall = GetObj ("divBouncyBall");
if (divBouncyBall != null)
divBouncyBall.style.display = "none";   
}

//Enable radio buttons
function EnableInventoryResults (bEnable)
{
var chkInvSearchResults = GetObj (GetObj ('rbInventory').value);
chkInvSearchResults.disabled = !bEnable;
}
function EnablePlansResults (bEnable)
{
var chkPlanSearchResults = GetObj (GetObj ('rbHomePlan').value);
chkPlanSearchResults.disabled = !bEnable;
}
function EnableBuilderResults (bEnable)
{
var chkBuilderSearchResults = GetObj (GetObj ('rbBuilder').value);
chkBuilderSearchResults.disabled = !bEnable;
}
function EnableCommunityResults (bEnable)
{
var chkCommunitySearchResults = GetObj (GetObj ('rbCommunity').value);
chkCommunitySearchResults.disabled = !bEnable;
}

function ShowHideAdditionalSearch ()
{    
var divCommunityBuilderSearch = GetObj ('divCommunityBuilderSearch');

if (divCommunityBuilderSearch.style.display == "none")
ShowAdditionalSearch ();
else
HideAdditionalSearch ();
}

function ShowAdditionalSearch ()
{
// Don't allow the Additional search options to be displayed unless a state and metro are selected
var statectrl = GetObj(GetObj('cboStateClientID').value);
var selectedIndex = statectrl.selectedIndex;
var stateid = statectrl.options[selectedIndex].value;

var metroctrl = GetObj(GetObj('cboMetroClientID').value);
selectedIndex = metroctrl.selectedIndex;
var metroid = metroctrl.options[selectedIndex].value;
if ((stateid <= 0) || (metroid <= 0))
{
var divAdditionalError = GetObj ('divAdditionalError');
if (divAdditionalError != null)
divAdditionalError.style.display = "";
else 
GetObj(GetObj('lblErrorClientID').value).innerHTML = "You must select a State and City/Metro first";

return;
}

var divCommunityBuilderSearch = GetObj ('divCommunityBuilderSearch');
var imgShowHide = GetObj ('imgShowHide');

divCommunityBuilderSearch.style.display = "";
imgShowHide.className = "imgCollapseArrow";

// Display the animated gif
var divBouncyBall = GetObj ("divBouncyBall");
if (divBouncyBall != null)
divBouncyBall.style.display = "";

// Load the dropdowns
var statectrl = GetObj(GetObj('cboStateClientID').value);
var selectedIndex = statectrl.selectedIndex;
var stateid = statectrl.options[selectedIndex].value;

var metroctrl = GetObj(GetObj('cboMetroClientID').value);
var selectedIndex = metroctrl.selectedIndex;
var metroid = metroctrl.options[selectedIndex].value;

var searchforctrl = GetObj(GetObj('cboSearchForClientID').value);
var selectedIndex = searchforctrl.selectedIndex;
var searchforid = searchforctrl.options[selectedIndex].value;

LoadBuildersAndCommunities(stateid, metroid, searchforid);
}

function HideAdditionalSearch ()
{
var ddlBuilders = GetObj (GetObj ('ddlBuilderName').value);
ddlBuilders.options.length = 0;

var ddlCommunities = GetObj (GetObj ('ddlCommunityName').value);
ddlCommunities.options.length = 0;

var divCommunityBuilderSearch = GetObj ('divCommunityBuilderSearch');
var imgShowHide = GetObj ('imgShowHide');

divCommunityBuilderSearch.style.display = "none";
imgShowHide.className = "imgExpandArrow";

var divAdditionalError = GetObj ('divAdditionalError');
if (divAdditionalError != null)
divAdditionalError.style.display = "none";
}

function QuickSaveSettings()
{
var radiusctrl = GetObj(GetObj('cboRadiusClientID').value);
var selectedIndex = radiusctrl.selectedIndex;
var radius = radiusctrl.options[selectedIndex].value;
GetObj('radius').value = radius;

var zipcode = GetObj(GetObj('txtZipClientID').value);
GetObj('zipval').value = zipcode.value;
}

function validateLength(oSrc, args)
{
if ( args.Value.length < 5 )
args.IsValid = false;
else
args.IsValid = true;
}

function BuilderSaveSettings()
{
var statectrl = GetObj(GetObj('cboStateClientID').value);
GetObj('stateid').value = statectrl.options[statectrl.selectedIndex].value;

var metroctrl = GetObj(GetObj('cboMetroClientID').value);
GetObj('metroid').value = metroctrl.options[metroctrl.selectedIndex].value;

var builderctrl = GetObj(GetObj('cboBuilderClientID').value);
GetObj('builderid').value = builderctrl.options[builderctrl.selectedIndex].value;
}

function ShowBuilderList ()
{
var builderctrl = GetObj(GetObj('cboBuilderClientID').value);
builderctrl.options.length = 1;

var statectrl = GetObj(GetObj('cboStateClientID').value);
var stateid = statectrl.options[statectrl.selectedIndex].value;

var metroctrl = GetObj(GetObj('cboMetroClientID').value);
var metroid = metroctrl.options[metroctrl.selectedIndex].value;

var listingid = 0;
LoadBuildersAndCommunities(stateid, metroid, listingid);
}

function GetObj (sObjId)
{
return document.getElementById (sObjId);
}

function ValidateSelections ()
{
var freetextctrl = null;
if (GetObj ('txtSearchClientID') != null)
freetextctrl = GetObj(GetObj('txtSearchClientID').value);

return true;
}

function ValidateBuilderSelections ()
{
var metroctrl = GetObj(GetObj('cboMetroClientID').value);
var metroid = metroctrl.options[metroctrl.selectedIndex].value;

if (metroid == 0)
{
GetObj(GetObj('lblErrorClientID').value).innerHTML = "You must select a State and City/Metro";
return false;
}
else
return true;
}

function HandleCarriageReturn(e)
{
var keynum;
var keychar;
var numcheck;
if(window.event) // IE
keynum = e.keyCode;
else if(e.which) // Netscape/Firefox/Opera
keynum = e.which;

if ((keynum == 13) && (GetObj ('cmdSearchBtnClientID') != null))
    GetObj(GetObj('cmdSearchBtnClientID').value).click();
}

function ClearZipCode ()
{
if (GetObj('txtZipClientID') != null)
GetObj(GetObj('txtZipClientID').value).value = "";
}

function ChangeZip (e)
{
HandleCarriageReturn(e);
if (GetObj(GetObj('txtZipClientID').value).value.length == 5)
{
FindSearchForList('/WebHandlers/SearchFor.ashx');
ShowHideArea();
}
}

function ShowHideArea ()
{
var metroctrl = GetObj(GetObj('cboMetroClientID').value);
var selectedIndex = metroctrl.selectedIndex;
var metroid = 0;

var zipctrl = GetObj(GetObj('txtZipClientID').value);

if (selectedIndex != -1)
metroid = metroctrl.options[selectedIndex].value;

var initialMetroID = metroctrl.getAttribute('lastValue');

if ((metroid == initialMetroID) && (zipctrl.value == "") && (GetObj ("divAreas") != null))
GetObj ("divAreas").style.display = "";
else if (GetObj ("divAreas") != null)
{
DisableCheckboxes (GetObj ("divAreas"));
GetObj ("divAreas").style.display = "none";
}      
}

function ClearError ()
{
if (GetObj ("lblErrorClientID") != null)
GetObj (GetObj ("lblErrorClientID").value).innerHTML = "";
}

function DisableValidators ()
{
    try
    {
        Page_Validators = new Array ();
    }
    catch(e)
    {
    }
}