    var updateInterval = null;
    function ontextfocus(which) 
    {
        clearText(which);
		updateInterval = setInterval("OnDataChange()", 100);
    }
    function clearText(which) 
    {
	    if ((which == "MinPrice") && ( document.getElementById(minPriceID).value == minPriceString ))
		    document.getElementById(minPriceID).value = "";
	    if ((which == "MaxPrice") && ( document.getElementById(maxPriceID).value == maxPriceString ))
		    document.getElementById(maxPriceID).value = "";
	    if ((which == "MinSqFt") && ( document.getElementById(minSqFtID).value == minSqFtString ))
		    document.getElementById(minSqFtID).value = "";
	    if ((which == "MaxSqFt") && ( document.getElementById(maxSqFtID).value == maxSqFtString ))
		    document.getElementById(maxSqFtID).value = "";
	    if ((which == "ZipCode") && ( document.getElementById(zipCodeID).value == zipCodeString ))
		    document.getElementById(zipCodeID).value = "";
    }
    
    function ontextblur(which) 
    {
        resetText(which);
        if(updateInterval != null)
        {
            clearInterval(updateInterval);
            updateInterval = null;
        }
    }
    function resetText(which) 
    {
        trimSpaces(minPriceID);
        trimSpaces(maxPriceID);
        trimSpaces(minSqFtID);
        trimSpaces(maxSqFtID);
        trimSpaces(zipCodeID);

	    if ((which == "MinPrice") && ( document.getElementById(minPriceID).value == "" ))
		    document.getElementById(minPriceID).value = minPriceString;
	    if ((which == "MaxPrice") && ( document.getElementById(maxPriceID).value == "" ))
		    document.getElementById(maxPriceID).value = maxPriceString;
	    if ((which == "MinSqFt") && ( document.getElementById(minSqFtID).value == "" ))
		    document.getElementById(minSqFtID).value = minSqFtString;
	    if ((which == "MaxSqFt") && ( document.getElementById(maxSqFtID).value == "" ))
		    document.getElementById(maxSqFtID).value = maxSqFtString;
	    if ((which == "ZipCode") && ( document.getElementById(zipCodeID).value == "" ))
		    document.getElementById(zipCodeID).value = zipCodeString;
    }    

    function OnPropertyTypeChange()
    {
        updateCityDropDown();
        updateLandSelections(document.getElementById(propertyTypeID).value == "Land");
    }    

    function updateLandSelections(landSelected)
    {
        document.getElementById(minSqFtID).disabled = landSelected;
        document.getElementById(maxSqFtID).disabled = landSelected;
        document.getElementById(bedsID).disabled = landSelected;
        document.getElementById(bathsID).disabled = landSelected;

        if(landSelected)
        {
		    document.getElementById(minSqFtID).value = minSqFtString;
		    document.getElementById(maxSqFtID).value = maxSqFtString;
		    document.getElementById(bedsID).value = "Beds";
		    document.getElementById(bathsID).value = "Baths";
		}
    }
    
    function OnCountyChange()
    {
        updateCityDropDown();
    }

    function OnCityChange()
    {
        OnDataChange();
    }
    
    function updateCityDropDown()
    {
        Command = document.getElementById(propertyTypeID).value + ":'" + document.getElementById(countyID).value + "'";
        context = 'GetCityCall';
        sendXMLHttpRequest(requestUrl,'GetCities',Command,ReceiveCityData,context,true);
    }
    
    function ReceiveCityData(result, context)
    {
        var dropDown = document.getElementById(cityID);
        var currentCityValue = dropDown.value;
        if(currentCityValue == "")
            currentCityValue = anyCityString;
        dropDown.length = 0;
        dropDown.options[0] = new Option(anyCityString,"Any City");
        dropDown.value = "Any City";
        if(result == "")
            return;
        else if(result == "none")
        {
            OnDataChange();
            return;
        }
            
        var cityarray=result.split(",");
        for(x=0;x<cityarray.length;x++)
        {
            dropDown.options[x+1] = new Option(cityarray[x],cityarray[x]);
            if(cityarray[x] == currentCityValue)
                dropDown.value = currentCityValue;
        }
        OnDataChange();
    }

    function OnDataChange()
    {    
        var valid = true;
        valid &= checkvalid(minPriceID, minPriceValidatorID);
        valid &= checkvalid(maxPriceID, maxPriceValidatorID);
        valid &= checkvalid(minSqFtID, minSqFtValidatorID);
        valid &= checkvalid(maxSqFtID, maxSqFtValidatorID);
        valid &= checkvalid(zipCodeID, zipCodeValidatorID);
 
        if(!valid)
        {
            document.getElementById(matchedHomesID).innerHTML = invalidDataString;
            document.getElementById(matchedHomesID).style.color = '#ff0000';
            document.getElementById(matchedHomesLabelID).innerHTML = "";
            updateDataCommand = "";
            return;
        }
        else
        {
            document.getElementById(matchedHomesID).style.color = '#3F517F';
            document.getElementById(matchedHomesLabelID).innerHTML = propertiesMatchString;
        }
        
        PostData();
    }
    var updateDataCommand = "";
    function PostData()
    {
        Command = document.getElementById(propertyTypeID).value + ":";

        Command += " where 1=1";
        BuildQuery("MinPrice", minPriceID, minPriceString);
        BuildQuery("MaxPrice", maxPriceID, maxPriceString);
        BuildQuery("MinSqFt", minSqFtID, minSqFtString);
        BuildQuery("MaxSqFt", maxSqFtID, maxSqFtString);
        BuildQuery("Beds", bedsID, "Beds");
        BuildQuery("Baths", bathsID, "Baths");
        BuildQuery("County", countyID, "Any County");
        BuildQuery("ZipCode", zipCodeID, zipCodeString);
        BuildQuery("City", cityID, "Any City");
		
        context = 'QuickSearchCall';

        if(updateDataCommand == Command)
            return;
            
        updateDataCommand = Command;

        //Send changes to server
        sendXMLHttpRequest(requestUrl,'QuickSearch',Command,ReceiveServerData,context,true);
    }
    function checkvalid(clientID, validatorid)
    {
        var obj = document.getElementById(clientID);
        var validateobject = document.getElementById(validatorid);
        if(!validateobject.evaluationfunction(validateobject))
        {
            obj.style.backgroundColor = '#ffff00'; 
            return false;
        }
        obj.style.backgroundColor = '#ffffff'; 
        return true;
    }
    function BuildQuery(id, clientID, defaultvalue)
    {
        var obj = document.getElementById(clientID);
        value = obj.value;
  
	    if ((value != defaultvalue) && (value != ""))
	    {
            Command+=" AND ";
	        if(id=="County")
		        Command += "(" + id + "='" + value + "')";
	        if(id=="City")
		        Command += "(" + id + "='" + value + "')";
	        if(id=="MinPrice")
		        Command += "(PRICE >= " + removeCommas(value) + ")";
	        if(id=="MaxPrice")
		        Command += "(PRICE <= " + removeCommas(value) + ")";
	        if(id=="MinSqFt")
		        Command += "(TOTALSQUAREFT >= " + removeCommas(value) + ")";
	        if(id=="MaxSqFt")
		        Command += "(TOTALSQUAREFT <= " + removeCommas(value) + ")";
	        if(id=="Beds")
		        Command += "(BEDROOMSTOTAL >= " + value + ")";
	        if(id=="Baths")
		        Command += "(BATHSTOTAL >= " + value + ")";
	        if(id=="ZipCode")
		        Command += "(" + id + "=" + value + ")";
		    return true;
		}
		return false;
    }
    function removeCommas( strValue ) 
    {
        objRegExp = /,/g; //search for commas globally

        return strValue.replace(objRegExp,'');
    }    
    function ReceiveServerData(result, context)
    {
        //don't set it if it is the same to avoid flicker
        if(document.getElementById(matchedHomesID).innerHTML != result)
            document.getElementById(matchedHomesID).innerHTML = result;
    }

    function trimSpaces(clientID)
    {
        var obj = document.getElementById(clientID);
        sString = obj.value;

        //Trim Left
        while (sString.substring(0,1) == ' ')
        {
            sString = sString.substring(1, sString.length);
        }
        //Trim Right
        while (sString.substring(sString.length-1, sString.length) == ' ')
        {
            sString = sString.substring(0,sString.length-1);
        }
        obj.value = sString;
    }
