//========================================================
//  CheckText
//  Ensures that when the user first clicks the sales
//  search, the default text is removed
//========================================================
function CheckText(ele) {
    if (trim(ele.value) == DefaultSalesSearchText) {
        ele.value = "";
    }
}

//========================================================
//  FoeSearch - used for clicking search examples 
//  below Sales Search Text input
//========================================================
function FoeSearch(string) {
    var theForm = document.searchform;
    theForm.txtSearch.value = string;
}

//========================================================
//  FoeContentSearch - used for clicking search examples 
//  below Content Search Text input
//========================================================
function FoeContentSearch(string) {
    var theForm = document.searchform;
    theForm.txtContentSearch.value = string;
}

//========================================================
//   ArticleSearch - called from keyword links
//========================================================
function ArticleSearch(term) {
    var theForm = document.searchform;
    if (theForm.txtContentSearch) {
        theForm.txtContentSearch.value = term;
        DoSearchContentSmall(theForm);
    }
}

//========================================================
//   DoSearchContentSmall
//========================================================
function DoSearchContentSmall(theForm) {
    var term = theForm.txtContentSearch.value;
    if (trim(term).length < 3) {
        search_alert(String.format("<p>Please enter a term or phrase into the search field.</p>"));
    }
    else {
        var strURL = String.format("{0}Controls/AjaxCalls/ContentSearchBar.aspx?N=0&Ntk=ArticleSearch&Ntt={1}&sid={2}",
                                WebRoot,
                                escape(theForm.txtContentSearch.value),
                                sid);

        $AJAX.GetForDelegate(function(AjaxResponse) { eval(AjaxResponse); }, strURL);
    }
}
//========================================================
//   DoSearchCID
//========================================================
function DoSearchCID(theForm) {
    var SearchType = "sale";
    var NavQuery = new Array();
    NavQuery.push(ChannelSectionId);
    var QueryString = "N=" + NavQuery.join("+");
    var strURL = null;

    var txtCIDSearch = theForm.txtCIDSearch.value;
    if (txtCIDSearch == "" || txtCIDSearch == "Enter ID") {
        //search_alert("<p>Please enter CompanyID into the search field.");
        alert("Please enter CompanyID into the WebID field.");
        theForm.txtCIDSearch.focus();
        return false;
    }
    else {
        txtSearch = txtCIDSearch.replace(' ', '');
        QueryString += String.format("&Ntt={0}&Ntk=IdAccount&Ntx=mode matchall", txtCIDSearch);
        //----------------- AJAX CAll 
        strURL = WebRoot + "Controls/AjaxCalls/SearchBar.aspx?SearchType=" + SearchType + "&txtCIDSearch=" + txtCIDSearch + "&" + QueryString;
    }

    if (strURL != null) {
        var ResponseDelegate = function(AjaxResponse) {
            if (AjaxResponse.indexOf("var doEval = true;") > -1) // flag indicating we should evaluate the response
            {
                eval(AjaxResponse);
            }
            else {
                gE("searchformresult").innerHTML = AjaxResponse;
            }
        };

        //-----------------
        $AJAX.GetForDelegate(ResponseDelegate, strURL);
        //window.location.href = strURL; // for testing
        //-----------------}
    }
}
//========================================================
//   DoSearchWebID
//========================================================
function DoSearchWebID(theForm, SEOSection) {
    var SearchType = 'sale';
    var Category = '';
    var NavQuery = new Array();
    var strURL = null;
    var ChannelSelectionID = salesID;

    if (SEOSection == 'for-rent') {
        SearchType = 'rentals';
        Category = '';
        ChannelSelectionID = rentalsID;
    }
    else if (SEOSection == 'apartments') {
        SearchType = 'rentals';
        Category = 'apartments';
        ChannelSelectionID = AptCommunityID;
    }
    else if (SEOSection == 'new-homes') {
        SearchType = 'sale';
        Category = 'community';
        ChannelSelectionID = NewHomesID;
    }
    else {
        SearchType = 'sale';
        Category = '';
        ChannelSelectionID = salesID;
    }

    NavQuery.push(ChannelSelectionID);

    var QueryString = "N=" + NavQuery.join("+");

    var txtIDSearch = theForm.txtIDSearch.value;
    if (txtIDSearch == "" || txtIDSearch == "Enter ID") {
        //search_alert("<p>Please enter MLS ID or Web ID into the search field.");
        alert("Please enter MLS ID or Web ID into the WebID field.");
        theForm.txtIDSearch.focus();
        return false;
    }
    else {
        txtSearch = txtIDSearch.replace(' ', '');
        QueryString += String.format("&Ntt={0}&Ntk=IdSearch&Ntx=mode matchall", txtIDSearch);
        //----------------- AJAX CAll
        strURL = WebRoot + "Controls/AjaxCalls/SearchBar.aspx?SearchType=" + SearchType + "&txtIDSearch=" + txtIDSearch + "&" + QueryString;
        
        if (Category.length > 0) {
            strURL = strURL + "&category=" + Category;
        }
    }

    if (strURL != null) {
        var ResponseDelegate = function(AjaxResponse) {
            //if (AjaxResponse.indexOf("var doEval = true;") > -1) // flag indicating we should evaluate the response
            //{
            eval(AjaxResponse);
            //}
            //else
            //{
            //gE("searchformresult").innerHTML = AjaxResponse;
            //}
        };

        //-----------------
        $AJAX.GetForDelegate(ResponseDelegate, strURL);
        //alert(strURL);
        //window.location.href = strURL; // for testing
        //-----------------}
    }
}
//========================================================
//   DoSearchNewHomesSmall
//========================================================
function DoSearchNewHomesSmall(theForm) {
    var SearchType = "sale";
    var Category = "community";
    var beds = "0";
    var pnum1 = 0;
    var pnum2 = 99000000;
    var txtSearch = "";
    var qrystr = "";
    var qryPrice = "";
    var els = theForm.elements;
    txtSearch = trim(theForm.txtSearch.value);

    var sb = theForm.sb.value; // Passed for Search Bar State ... 
    var NavQuery = new Array();
    NavQuery.push(ChannelSectionId);
    // NavQuery.push(salesID);

    CloseSuggest();

    if (theForm.EndecaId.value.length > 0) {
        NavQuery.push(theForm.EndecaId.value);
    }

    var QueryString = "N=" + NavQuery.join("+");

    txtSearch = trim(txtSearch);

    if (txtSearch == "" || txtSearch == "City and State, or Zip Code") {
        search_alert("<p>Please enter a city/state or a zip code into the search field.  We recommend that you follow the following format:</p><p>City, State (e.g. <a href=\"javascript:FoeSearch('San Francisco, CA');\">San Francisco, CA</a>)<br/>Zip Code (e.g.  <a href=\"javascript:FoeSearch('89148');\">89148</a>)</p>");
        //gE("search_alert").style.display = "block";
        return false;
    }

    //var RegExZip = new RegExp("[ABCEGHJKLMNPRSTVXY]\\d[A-Z]\\s?\\d[A-Z]\\d", "i");
    var RegExZip = new RegExp(/[0-9]{5}/);

    if (RegExZip.exec(txtSearch) != null) // the regex matched a zip code
    {
        txtSearch = txtSearch.replace(' ', '');
        QueryString += String.format("&Ntt={0}&Ntk=AddrZip&Ntx=mode matchall", txtSearch);
    }

    //----------------- AJAX CAll 
    var strURL = WebRoot + "Controls/AjaxCalls/SearchBar.aspx?SearchType=" + SearchType + "&sid=" + sid + "&txtSearch=" + txtSearch + "&sb=" + sb + "&" + QueryString + qryPrice + "&category=" + Category;

    //document.body.innerHTML = strURL;
    //return;

    //==================================================
    //  Response delegate handles conditions, if there are zero results, or if there are more than one result.
    //  If One Result :     redirect to results
    //  If More Than one :  Display dropdown with selection
    //  If zero :           display Zero Results
    //==================================================

    var ResponseDelegate = function(AjaxResponse) {
        if (AjaxResponse.indexOf("var doEval = true;") > -1) // flag indicating we should evaluate the response
        {
            eval(AjaxResponse);
        }
        else {
            gE("searchformresult").innerHTML = AjaxResponse;
        }
    };

    //-----------------
    $AJAX.GetForDelegate(ResponseDelegate, strURL);
    //window.location.href = strURL; // for testing
    //-----------------
}
//========================================================
//   DoSearchSalesSmall
//========================================================
function DoSearchSalesSmall(theForm) {
    var SearchType = "sale";
    var beds = "0";
    var pnum1 = 0;
    var pnum2 = 99000000;
    var txtSearch = "";
    var qrystr = "";
    var qryPrice = "";
    var els = theForm.elements;
    txtSearch = trim(theForm.txtSearch.value);

    var sb = theForm.sb.value; // Passed for Search Bar State ... 
    var NavQuery = new Array();

    NavQuery.push(salesID);
    
    // Until the new widget is launch, Sales Search default to MLS/Broker Listings.
    NavQuery.push(BrokerID);

    CloseSuggest();

    if (theForm.EndecaId.value.length > 0) {
        NavQuery.push(theForm.EndecaId.value);
    }

    if (theForm.list_DataSource) {
        var datasource = theForm.list_DataSource.options;

        if (datasource.selectedIndex > -1) {
            if (datasource[datasource.selectedIndex].value != "") {
                NavQuery.push(datasource[datasource.selectedIndex].value);
            }
        }
    }

    var QueryString = "N=" + NavQuery.join("+");

    txtSearch = trim(txtSearch);

    if (txtSearch == "" || txtSearch == "City and State, or Zip Code") {
        search_alert("<p>Please enter a city/state or a zip code into the search field.  We recommend that you follow the following format:</p><p>City, State (e.g. <a href=\"javascript:FoeSearch('San Francisco, CA');\">San Francisco, CA</a>)<br/>Zip Code (e.g.  <a href=\"javascript:FoeSearch('89148');\">89148</a>)</p>");
        //gE("search_alert").style.display = "block";
        return false;
    }

    //var RegExZip = new RegExp("[ABCEGHJKLMNPRSTVXY]\\d[A-Z]\\s?\\d[A-Z]\\d", "i");
    var RegExZip = new RegExp(/[0-9]{5}/);

    if (RegExZip.exec(txtSearch) != null) // the regex matched a zip code
    {
        txtSearch = txtSearch.replace(' ', '');
        QueryString += String.format("&Ntt={0}&Ntk=AddrZip&Ntx=mode matchall", txtSearch);
    }

    //----------------- AJAX CAll 
    var strURL = WebRoot + "Controls/AjaxCalls/SearchBar.aspx?SearchType=" + SearchType + "&sid=" + sid + "&txtSearch=" + txtSearch + "&sb=" + sb + "&" + QueryString + qryPrice;

    //document.body.innerHTML = strURL;
    //return;

    //==================================================
    //  Response delegate handles conditions, if there are zero results, or if there are more than one result.
    //  If One Result :     redirect to results
    //  If More Than one :  Display dropdown with selection
    //  If zero :           display Zero Results
    //==================================================

    var ResponseDelegate = function(AjaxResponse) {
        //alert(AjaxResponse);
        if (AjaxResponse.indexOf("var doEval = true;") > -1) // flag indicating we should evaluate the response
        {
            eval(AjaxResponse);
        }
        else {
            gE("searchformresult").innerHTML = AjaxResponse;
        }
    };

    //-----------------
    $AJAX.GetForDelegate(ResponseDelegate, strURL);
    //window.location.href = strURL; // for testing
    //-----------------
}
//========================================================
//   DoSearchApartmentsSmall
//========================================================
function DoSearchApartmentsSmall(theForm) {
    var SearchType = "rentals";
    var Category = "apartments";
    var beds = "0";
    var pnum1 = 0;
    var pnum2 = 99000000;
    var txtSearch = "";
    var qrystr = "";
    var qryPrice = "";
    var els = theForm.elements;
    txtSearch = trim(theForm.txtSearch.value);
    var Country = "200"; //theForm.search_country_select.options[theForm.search_country_select.options.selectedIndex].value;
    var sb = theForm.sb.value; // Passed for Search Bar State ... 
    var NavQuery = new Array();
    NavQuery.push(AptCommunityID);
    //NavQuery.push(salesID);  

    CloseSuggest();

    if (theForm.EndecaId.value.length > 0) {
        NavQuery.push(theForm.EndecaId.value);
    }

    if (theForm.search_bedrooms) {
        var bedslist = theForm.search_bedrooms.options;
        if (bedslist.selectedIndex > 0) {
            NavQuery.push(bedslist[bedslist.selectedIndex].value);
        }
    }

    if (theForm.search_bathrooms) {
        var bathslist = theForm.search_bathrooms.options;
        if (bathslist.selectedIndex > 0) {
            NavQuery.push(bathslist[bathslist.selectedIndex].value);
        }
    }

    //var ptypelist = theForm.search_propertytype.options;
    //if (ptypelist.selectedIndex > 0)
    //{
    //  NavQuery.push(ptypelist[ptypelist.selectedIndex].value);
    //} 

    var QueryString = "N=" + NavQuery.join("+");

    qryPrice = "";
    if (theForm.search_pricemin) {
        var minPrice = theForm.search_pricemin.options;
        var maxPrice = theForm.search_pricemax.options;
        if ((minPrice.selectedIndex > 0) || (maxPrice.selectedIndex > 0)) {
            if ((minPrice.selectedIndex > maxPrice.selectedIndex) && (maxPrice.selectedIndex != 0)) {
                search_alert("<p>The minimum price must be less than the maximum price</p>");
                return false;
            }
            else {
                qryPrice = String.format("&Nf=Price_Rentals|BTWN+{0}+{1}",
                                    minPrice[minPrice.selectedIndex].value,
                                    maxPrice[maxPrice.selectedIndex].value);
            }
        }
    }

    txtSearch = trim(txtSearch);

    if (txtSearch == "" || txtSearch == "City and State, or Zip Code") {
        search_alert("<p>Please enter a city/state or a zip code into the search field.  We recommend that you follow the following format:</p><p>City, State (e.g. <a href=\"javascript:FoeSearch('San Francisco, CA');\">San Francisco, CA</a>)<br/>Zip Code (e.g.  <a href=\"javascript:FoeSearch('89148');\">89148</a>)</p>");
        // gE("search_alert").style.display = "block";
        return false;
    }

    var RegExCAZip = new RegExp("[ABCEGHJKLMNPRSTVXY]\\d[A-Z]\\s?\\d[A-Z]\\d", "i");
    var RegExUSZip = new RegExp(/[0-9]{5}/);

    if (RegExUSZip.exec(txtSearch) != null) // the regex matched a us zip code
    {
        txtSearch = txtSearch.replace(' ', '');
        QueryString += String.format("&Ntt={0}&Ntk=AddrZip&Ntx=mode matchall", txtSearch);
    }

    else // users cannot logically search by zip AND keyword without more work... - Jim
    {
        if (theForm.search_keywords) {
            var txtkeyword = trim(theForm.search_keywords.value);
            if (txtkeyword != "") {
                if (txtkeyword.length < 3) {
                    search_alert("<p>Please enter minimum 3 characters.</p>");
                    return false;
                }
                var RegExKW = new RegExp(/[^A-Za-z0-9\-\s]+/);
                if (RegExKW.exec(txtkeyword)) {
                    search_alert("<p>Please enter alpha numeric and space only.</p>");
                    return false;
                }
                QueryString += String.format("&Ntt={0}&Ntk=CommentsLong", theForm.search_keywords.value);
            }
        }
    }

    //----------------- AJAX CAll 
    var strURL = WebRoot + "Controls/AjaxCalls/SearchBar.aspx?SearchType=" + SearchType + "&txtSearch=" + txtSearch + "&sid=" + sid + "&sb=" + sb + "&C=" + Country + "&" + QueryString + qryPrice + "&category=" + Category;
    //alert(strURL);
    //document.body.innerHTML = strURL;
    //return;

    //==================================================
    //  Response delegate handles conditions, if there are zero results, or if there are more than one result.
    //  If One Result :     redirect to results
    //  If More Than one :  Display dropdown with selection
    //  If zero :           display Zero Results
    //==================================================

    var ResponseDelegate = function(AjaxResponse) {
        if (AjaxResponse.indexOf("var doEval = true;") > -1) // flag indicating we should evaluate the response
        {
            //alert(AjaxResponse);
            eval(AjaxResponse);
        }
        else {
            gE("searchformresult").innerHTML = AjaxResponse;
        }
    };


    //-----------------
    $AJAX.GetForDelegate(ResponseDelegate, strURL);
    //window.location.href = strURL; // for testing
    //-----------------
}
//========================================================
//   DoSearchRentalsSmall
//========================================================
function DoSearchRentalsSmall(theForm) {
    var SearchType = "rentals";
    var Category = "";
    var beds = "0";
    var pnum1 = 0;
    var pnum2 = 99000000;
    var txtSearch = "";
    var qrystr = "";
    var qryPrice = "";
    var els = theForm.elements;
    txtSearch = trim(theForm.txtSearch.value);
    var Country = "200"; //theForm.search_country_select.options[theForm.search_country_select.options.selectedIndex].value;
    var sb = theForm.sb.value; // Passed for Search Bar State ... 
    var NavQuery = new Array();
    NavQuery.push(rentalsID);
    NavQuery.push(BrokerID); 

    CloseSuggest();

    if (theForm.Category.value == "") {
        Category = "Apartments";
    }
    else {
        Category = theForm.Category.value;
    }

    if (theForm.EndecaId.value.length > 0) {
        NavQuery.push(theForm.EndecaId.value);
    }

    if (theForm.list_DataSource) {
        var datasource = theForm.list_DataSource.options;

        if (datasource.selectedIndex > -1) {
            if (datasource[datasource.selectedIndex].value != "") {
                NavQuery.push(datasource[datasource.selectedIndex].value);
            }
        }
    }

    if (theForm.search_bedrooms) {
        var bedslist = theForm.search_bedrooms.options;
        if (bedslist.selectedIndex > 0) {
            NavQuery.push(bedslist[bedslist.selectedIndex].value);
        }
    }

    if (theForm.search_bathrooms) {
        var bathslist = theForm.search_bathrooms.options;
        if (bathslist.selectedIndex > 0) {
            NavQuery.push(bathslist[bathslist.selectedIndex].value);
        }
    }

    //var ptypelist = theForm.search_propertytype.options;
    //if (ptypelist.selectedIndex > 0)
    //{
    //  NavQuery.push(ptypelist[ptypelist.selectedIndex].value);
    //} 

    var QueryString = "N=" + NavQuery.join("+");

    qryPrice = "";
    if (theForm.search_pricemin) {
        var minPrice = theForm.search_pricemin.options;
        var maxPrice = theForm.search_pricemax.options;
        if ((minPrice.selectedIndex > 0) || (maxPrice.selectedIndex > 0)) {
            if ((minPrice.selectedIndex > maxPrice.selectedIndex) && (maxPrice.selectedIndex != 0)) {
                search_alert("<p>The minimum price must be less than the maximum price</p>");
                return false;
            }
            else {
                qryPrice = String.format("&Nf=Price_Rentals|BTWN+{0}+{1}",
                                    minPrice[minPrice.selectedIndex].value,
                                    maxPrice[maxPrice.selectedIndex].value);
            }
        }
    }

    txtSearch = trim(txtSearch);

    if (txtSearch == "" || txtSearch == "City and State, or Zip Code") {
        search_alert("<p>Please enter a city/state or a zip code into the search field.  We recommend that you follow the following format:</p><p>City, State (e.g. <a href=\"javascript:FoeSearch('San Francisco, CA');\">San Francisco, CA</a>)<br/>Zip Code (e.g.  <a href=\"javascript:FoeSearch('89148');\">89148</a>)</p>");
        // gE("search_alert").style.display = "block";
        return false;
    }

    var RegExCAZip = new RegExp("[ABCEGHJKLMNPRSTVXY]\\d[A-Z]\\s?\\d[A-Z]\\d", "i");
    var RegExUSZip = new RegExp(/[0-9]{5}/);

    if (RegExUSZip.exec(txtSearch) != null) // the regex matched a us zip code
    {
        txtSearch = txtSearch.replace(' ', '');
        QueryString += String.format("&Ntt={0}&Ntk=AddrZip&Ntx=mode matchall", txtSearch);
    }

    else // users cannot logically search by zip AND keyword without more work... - Jim
    {
        if (theForm.search_keywords) {
            var txtkeyword = trim(theForm.search_keywords.value);
            if (txtkeyword != "") {
                if (txtkeyword.length < 3) {
                    search_alert("<p>Please enter minimum 3 characters.</p>");
                    return false;
                }
                var RegExKW = new RegExp(/[^A-Za-z0-9\-\s]+/);
                if (RegExKW.exec(txtkeyword)) {
                    search_alert("<p>Please enter alpha numeric and space only.</p>");
                    return false;
                }
                QueryString += String.format("&Ntt={0}&Ntk=CommentsLong", theForm.search_keywords.value);
            }
        }
    }

    //----------------- AJAX CAll 
    var strURL = WebRoot + "Controls/AjaxCalls/SearchBar.aspx?SearchType=" + SearchType + "&txtSearch=" + txtSearch + "&sid=" + sid + "&sb=" + sb + "&C=" + Country + "&" + QueryString + qryPrice + "&Category=" + Category;
    //alert(strURL);
    //document.body.innerHTML = strURL;
    //return;

    //==================================================
    //  Response delegate handles conditions, if there are zero results, or if there are more than one result.
    //  If One Result :     redirect to results
    //  If More Than one :  Display dropdown with selection
    //  If zero :           display Zero Results
    //==================================================

    var ResponseDelegate = function(AjaxResponse) {
        if (AjaxResponse.indexOf("var doEval = true;") > -1) // flag indicating we should evaluate the response
        {
            //alert(AjaxResponse);
            eval(AjaxResponse);
        }
        else {
            gE("searchformresult").innerHTML = AjaxResponse;
        }
    };


    //-----------------
    $AJAX.GetForDelegate(ResponseDelegate, strURL);
    //window.location.href = strURL; // for testing
    //-----------------
}
//========================================================
//   DoSearchAgentSmall
//========================================================
function DoSearchAgentSmall(theForm) {
    var SearchType = "agents";
    var txtSearch = ""; //default setting for OCR Agents
    var qrystr = "";
    var els = theForm.elements;
    txtSearch = trim(theForm.txtSearch.value);
    var Country = theForm.search_country_select.options[theForm.search_country_select.options.selectedIndex].value;
    var sb = theForm.sb.value; // Passed for Search Bar State ... 
    var NavQuery = new Array();

    CloseSuggest();

    if (theForm.EndecaId.value.length > 0) {
        NavQuery.push(theForm.EndecaId.value);
    }
    else {
        NavQuery.push("0");
    }

    var QueryString = "N=" + NavQuery.join("+");


    txtSearch = trim(txtSearch);

    if (txtSearch == "" || txtSearch == "City and State, or Zip Code") {
        search_alert("<p>Please enter a city/state or a zip code into the search field.  We recommend that you follow the following format:</p><p>City, State (e.g. <a href=\"javascript:FoeSearch('San Francisco, CA');\">San Francisco, CA</a>)<br/>Zip Code (e.g.  <a href=\"javascript:FoeSearch('89148');\">89148</a>)</p>");
        // gE("search_alert").style.display = "block";
        return false;
    }

    //var RegExZip = new RegExp("[ABCEGHJKLMNPRSTVXY]\\d[A-Z]\\s?\\d[A-Z]\\d", "i");
    var RegExZip = new RegExp(/[0-9]{5}/);

    if (RegExZip.exec(txtSearch) != null) // the regex matched a zip code
    {
        txtSearch = txtSearch.replace(' ', '');
        QueryString += String.format("&Ntt={0}&Ntk=AddrZip&Ntx=mode matchall", txtSearch);
    }

    var txtkeyword = trim(theForm.search_keywords.value);
    var txtBroker = trim(theForm.search_brokerName.value);
    var txtAgent = trim(theForm.search_agentName.value);

    if (txtkeyword != "") {
        if (txtkeyword.length < 3) {
            search_alert("<p>Please enter minimum 3 characters.</p>");
            return false;
        }
        var RegExKW = new RegExp(/[^A-Za-z0-9\-\s]+/);
        if (RegExKW.exec(txtkeyword)) {
            search_alert("<p>Please enter alpha numeric and space only.</p>");
            return false;
        }
        QueryString += String.format("&Ntt={0}&Ntk=KeywordSearch", theForm.search_keywords.value);
    }
    else if ((txtBroker != "") && (txtAgent == "")) {
        if (txtBroker.length < 3) {
            search_alert("<p>Please enter minimum 3 characters.</p>");
            return false;
        }
        var RegExKW = new RegExp(/[^A-Za-z0-9\-\s]+/);
        if (RegExKW.exec(txtBroker)) {
            search_alert("<p>Please enter alpha numeric and space only.</p>");
            return false;
        }
        QueryString += String.format("&Ntt={0}&Ntk=AgentAdvertiser", theForm.search_brokerName.value);
    }
    else if (txtAgent != "") {
        if (txtBroker.length < 3) {
            search_alert("<p>Please enter minimum 3 characters.</p>");
            return false;
        }
        var RegExKW = new RegExp(/[^A-Za-z0-9\-\s]+/);
        if (RegExKW.exec(txtAgent)) {
            search_alert("<p>Please enter alpha numeric and space only.</p>");
            return false;
        }
        QueryString += String.format("&Ntt={0}&Ntk=AgentName", theForm.search_agentName.value);
    }

    //----------------- AJAX CAll 
    var strURL = WebRoot + "Controls/AjaxCalls/SearchBar.aspx?SearchType=" + SearchType + "&txtSearch=" + txtSearch + "&sid=" + sid + "&sb=" + sb + "&C=" + Country + "&" + QueryString;


    //==================================================
    //  Response delegate handles conditions, if there are zero results, or if there are more than one result.
    //  If One Result :     redirect to results
    //  If More Than one :  Display dropdown with selection
    //  If zero :           display Zero Results
    //==================================================

    var ResponseDelegate = function(AjaxResponse) {
        if (AjaxResponse.indexOf("var doEval = true;") > -1) // flag indicating we should evaluate the response
        {
            eval(AjaxResponse);
        }
        else {
            gE("searchformresult").innerHTML = AjaxResponse;
        }
    };

    $AJAX.GetForDelegate(ResponseDelegate, strURL);

}
//========================================================
//   DoSearchClassifiedSmall
//========================================================
function DoSearchClassifiedSmall(theForm) {
    var SearchType = "sale";
    var beds = "0";
    var pnum1 = 0;
    var pnum2 = 99000000;
    var txtSearch = "";
    var qrystr = "";
    var qryPrice = "";
    var els = theForm.elements;
    txtSearch = trim(theForm.txtSearch.value);

    var sb = theForm.sb.value; // Passed for Search Bar State ... 
    var NavQuery = new Array();
    NavQuery.push(ChannelSectionId);
    NavQuery.push(ClassifiedID);
    
    //NavQuery.push(salesID);
    
    
    CloseSuggest();

//    if (theForm.list_DataSource) {
//        var datasource = theForm.list_DataSource.options;
//        NavQuery.push(datasource[datasource.selectedIndex].value);
//    }

    if (theForm.EndecaId.value.length > 0) {
        NavQuery.push(theForm.EndecaId.value);
    }

    var QueryString = "N=" + NavQuery.join("+");

    txtSearch = trim(txtSearch);

    if (txtSearch == "" || txtSearch == "City and State, or Zip Code") {
        search_alert("<p>Please enter a city/state or a zip code into the search field.  We recommend that you follow the following format:</p><p>City, State (e.g. <a href=\"javascript:FoeSearch('San Francisco, CA');\">San Francisco, CA</a>)<br/>Zip Code (e.g.  <a href=\"javascript:FoeSearch('89148');\">89148</a>)</p>");
        //gE("search_alert").style.display = "block";
        return false;
    }

    //var RegExZip = new RegExp("[ABCEGHJKLMNPRSTVXY]\\d[A-Z]\\s?\\d[A-Z]\\d", "i");
    var RegExZip = new RegExp(/[0-9]{5}/);

    if (RegExZip.exec(txtSearch) != null) // the regex matched a zip code
    {
        txtSearch = txtSearch.replace(' ', '');
        QueryString += String.format("&Ntt={0}&Ntk=AddrZip&Ntx=mode matchall", txtSearch);
    }

    //----------------- AJAX CAll 
    var strURL = WebRoot + "Controls/AjaxCalls/SearchBar.aspx?SearchType=" + SearchType + "&sid=" + sid + "&txtSearch=" + txtSearch + "&sb=" + sb + "&" + QueryString + qryPrice;

    //document.body.innerHTML = strURL;
    //return;

    //==================================================
    //  Response delegate handles conditions, if there are zero results, or if there are more than one result.
    //  If One Result :     redirect to results
    //  If More Than one :  Display dropdown with selection
    //  If zero :           display Zero Results
    //==================================================

    var ResponseDelegate = function(AjaxResponse) {
        if (AjaxResponse.indexOf("var doEval = true;") > -1) // flag indicating we should evaluate the response
        {
            eval(AjaxResponse);
        }
        else {
            gE("searchformresult").innerHTML = AjaxResponse;
        }
    };

    //-----------------
    $AJAX.GetForDelegate(ResponseDelegate, strURL);
    //window.location.href = strURL; // for testing
    //-----------------
}

//========================================================
//   DoSearchAgentExpanded
//========================================================
function DoSearchAgentExpanded(theForm) {
    var SearchType = "agents";
    var txtSearch = "";
    var qrystr = "";
    var els = theForm.elements;
    txtSearch = trim(theForm.txtSearch.value);
    var Country = theForm.search_country_select.options[theForm.search_country_select.options.selectedIndex].value;
    var sb = theForm.sb.value; // Passed for Search Bar State ... 
    var NavQuery = new Array();

    CloseSuggest();

    if (theForm.EndecaId.value.length > 0) {
        NavQuery.push(theForm.EndecaId.value);
    }
    else {
        NavQuery.push("0");
    }

    var QueryString = "N=" + NavQuery.join("+");

    txtSearch = trim(txtSearch);

    if (txtSearch == "" || txtSearch == "City and State, or Zip Code") {
        search_alert("<p>Please enter a city/state or a zip code into the search field.  We recommend that you follow the following format:</p><p>City, State (e.g. <a href=\"javascript:FoeSearch('San Francisco, CA');\">San Francisco, CA</a>)<br/>Zip Code (e.g.  <a href=\"javascript:FoeSearch('89148');\">89148</a>)</p>");
        // gE("search_alert").style.display = "block";
        return false;
    }

    //var RegExZip = new RegExp("[ABCEGHJKLMNPRSTVXY]\\d[A-Z]\\s?\\d[A-Z]\\d", "i");
    var RegExZip = new RegExp(/[0-9]{5}/);

    if (RegExZip.exec(txtSearch) != null) // the regex matched a zip code
    {
        txtSearch = txtSearch.replace(' ', '');
        QueryString += String.format("&Ntt={0}&Ntk=AddrZip&Ntx=mode matchall", txtSearch);
    }

    var txtkeyword = trim(theForm.search_keywords.value);
    var txtBroker = trim(theForm.search_brokerName.value);
    var txtAgent = trim(theForm.search_agentName.value);
    if ((RegExZip.exec(txtSearch) == null)) {
        if (txtkeyword != "") {
            if (txtkeyword.length < 3) {
                search_alert("<p>Please enter minimum 3 characters.</p>");
                return false;
            }
            var RegExKW = new RegExp(/[^A-Za-z0-9\-\s]+/);
            if (RegExKW.exec(txtkeyword)) {
                search_alert("<p>Please enter alpha numeric and space only.</p>");
                return false;
            }
            QueryString += String.format("&Ntt={0}&Ntk=KeywordSearch", theForm.search_keywords.value);
        }
        else if ((txtBroker != "") && (txtAgent == "")) {
            if (txtBroker.length < 3) {
                search_alert("<p>Please enter minimum 3 characters.</p>");
                return false;
            }
            var RegExKW = new RegExp(/[^A-Za-z0-9\-\s]+/);
            if (RegExKW.exec(txtBroker)) {
                search_alert("<p>Please enter alpha numeric and space only.</p>");
                return false;
            }
            QueryString += String.format("&Ntt={0}&Ntk=AgentAdvertiser", theForm.search_brokerName.value);
        }
        else if (txtAgent != "") {
            if (txtAgent.length < 3) {
                search_alert("<p>Please enter minimum 3 characters.</p>");
                return false;
            }
            var RegExKW = new RegExp(/[^A-Za-z0-9\-\s]+/);
            if (RegExKW.exec(txtAgent)) {
                search_alert("<p>Please enter alpha numeric and space only.</p>");
                return false;
            }
            QueryString += String.format("&Ntt={0}&Ntk=AgentName", theForm.search_agentName.value);
        }
    }
    //----------------- AJAX CAll 
    var strURL = WebRoot + "Controls/AjaxCalls/SearchBar.aspx?SearchType=" + SearchType + "&txtSearch=" + txtSearch + "&sid=" + sid + "&sb=" + sb + "&C=" + Country + "&" + QueryString;


    //==================================================
    //  Response delegate handles conditions, if there are zero results, or if there are more than one result.
    //  If One Result :     redirect to results
    //  If More Than one :  Display dropdown with selection
    //  If zero :           display Zero Results
    //==================================================

    var ResponseDelegate = function(AjaxResponse) {
        if (AjaxResponse.indexOf("var doEval = true;") > -1) // flag indicating we should evaluate the response
        {
            eval(AjaxResponse);
        }
        else {
            gE("searchformresult").innerHTML = AjaxResponse;
        }
    };

    $AJAX.GetForDelegate(ResponseDelegate, strURL);
    //alert(strURL);
    // window.location.href = strURL; // for testing
}
//========================================================
//   DoSearchNewHomesExpanded
//========================================================
function DoSearchNewHomesExpanded(theForm) {
    var SearchType = "sale";
    var Category = "community";
    var beds = "0";
    var pnum1 = 0;
    var pnum2 = 99000000;
    var txtSearch = "";
    var qrystr = "";
    var qryPrice = "";
    var els = theForm.elements;
    txtSearch = trim(theForm.txtSearch.value);
    var Country = theForm.search_country_select.options[theForm.search_country_select.options.selectedIndex].value;
    var sb = theForm.sb.value; // Passed for Search Bar State ... 
    var NavQuery = new Array();
    //NavQuery.push(salesID);
    NavQuery.push(NewHomesID);

    CloseSuggest();

    if (theForm.EndecaId.value.length > 0) {
        NavQuery.push(theForm.EndecaId.value);
    }

    var bedslist = theForm.search_bedrooms.options;
    if (bedslist.selectedIndex > 0) {
        NavQuery.push(bedslist[bedslist.selectedIndex].value);
    }

    var bathslist = theForm.search_bathrooms.options;
    if (bathslist.selectedIndex > 0) {
        NavQuery.push(bathslist[bathslist.selectedIndex].value);
    }

    //var ptypelist = theForm.search_propertytype.options;
    //if (ptypelist.selectedIndex > 0)
    //{
    //  NavQuery.push(ptypelist[ptypelist.selectedIndex].value);
    //} 

    var QueryString = "N=" + NavQuery.join("+");

    var minPrice = theForm.search_pricemin.options;
    var maxPrice = theForm.search_pricemax.options;
    if ((minPrice.selectedIndex > 0) || (maxPrice.selectedIndex > 0)) {
        if ((minPrice.selectedIndex > maxPrice.selectedIndex) && (maxPrice.selectedIndex != 0)) {
            search_alert("<p>The minimum price must be less than the maximum price</p>");
            return false;
        }
        else {
            qryPrice = String.format("&Nf=Price_Sales|BTWN+{0}+{1}",
                                minPrice[minPrice.selectedIndex].value,
                                maxPrice[maxPrice.selectedIndex].value);
        }
    }
    else {
        qryPrice = "";
    }

    txtSearch = trim(txtSearch);

    if (txtSearch == "" || txtSearch == "City and State, or Zip Code") {
        search_alert("<p>Please enter a city/state or a zip code into the search field.  We recommend that you follow the following format:</p><p>City, State (e.g. <a href=\"javascript:FoeSearch('San Francisco, CA');\">San Francisco, CA</a>)<br/>Zip Code (e.g.  <a href=\"javascript:FoeSearch('89148');\">89148</a>)</p>");
        // gE("search_alert").style.display = "block";
        return false;
    }

    //var RegExZip = new RegExp("[ABCEGHJKLMNPRSTVXY]\\d[A-Z]\\s?\\d[A-Z]\\d", "i");
    var RegExZip = new RegExp(/[0-9]{5}/);

    if (RegExZip.exec(txtSearch) != null) // the regex matched a zip code
    {
        txtSearch = txtSearch.replace(' ', '');
        QueryString += String.format("&Ntt={0}&Ntk=AddrZip&Ntx=mode matchall", txtSearch);
    }

    //----------------- AJAX CAll 
    var strURL = WebRoot + "Controls/AjaxCalls/SearchBar.aspx?SearchType=" + SearchType + "&txtSearch=" + txtSearch + "&sid=" + sid + "&sb=" + sb + "&C=" + Country + "&" + QueryString + qryPrice + "&SearchFor=" + Category;

    //document.body.innerHTML = strURL;
    //return;

    //==================================================
    //  Response delegate handles conditions, if there are zero results, or if there are more than one result.
    //  If One Result :     redirect to results
    //  If More Than one :  Display dropdown with selection
    //  If zero :           display Zero Results
    //==================================================

    var ResponseDelegate = function(AjaxResponse) {
        if (AjaxResponse.indexOf("var doEval = true;") > -1) // flag indicating we should evaluate the response
        {
            //alert(AjaxResponse);
            eval(AjaxResponse);
        }
        else {
            gE("searchformresult").innerHTML = AjaxResponse;
        }
    };


    //-----------------
    $AJAX.GetForDelegate(ResponseDelegate, strURL);
    //window.location.href = strURL; // for testing
    //-----------------
}
//========================================================
//   DoSearchSalesExpanded
//========================================================
function DoSearchSalesExpanded(theForm) {
    var SearchType = "sale";
    var Category = "";
    var beds = "0";
    var pnum1 = 0;
    var pnum2 = 99000000;
    var txtSearch = "";
    var qrystr = "";
    var qryPrice = "";
    var els = theForm.elements;
    txtSearch = trim(theForm.txtSearch.value);
    var Country = theForm.search_country_select.options[theForm.search_country_select.options.selectedIndex].value;
    var sb = theForm.sb.value; // Passed for Search Bar State ... 
    var NavQuery = new Array();

    NavQuery.push(salesID);

    // Until the new widget is launch, Sales Search default to MLS/Broker Listings.
    NavQuery.push(BrokerID);

    CloseSuggest();

    if (theForm.EndecaId.value.length > 0) {
        NavQuery.push(theForm.EndecaId.value);
    }

    var bedslist = theForm.search_bedrooms.options;
    if (bedslist.selectedIndex > 0) {
        NavQuery.push(bedslist[bedslist.selectedIndex].value);
    }

    var bathslist = theForm.search_bathrooms.options;
    if (bathslist.selectedIndex > 0) {
        NavQuery.push(bathslist[bathslist.selectedIndex].value);
    }

    var ptypelist = theForm.search_propertytype.options;
    if (ptypelist.selectedIndex > 0) {
        NavQuery.push(ptypelist[ptypelist.selectedIndex].value);
    }

    var QueryString = "N=" + NavQuery.join("+");

    var minPrice = theForm.search_pricemin.options;
    var maxPrice = theForm.search_pricemax.options;
    if ((minPrice.selectedIndex > 0) || (maxPrice.selectedIndex > 0)) {
        if ((minPrice.selectedIndex > maxPrice.selectedIndex) && (maxPrice.selectedIndex != 0)) {
            search_alert("<p>The minimum price must be less than the maximum price</p>");
            return false;
        }
        else {
            qryPrice = String.format("&Nf=Price_Sales|BTWN+{0}+{1}",
                                minPrice[minPrice.selectedIndex].value,
                                maxPrice[maxPrice.selectedIndex].value);
        }
    }
    else {
        qryPrice = "";
    }

    txtSearch = trim(txtSearch);

    if (txtSearch == "" || txtSearch == "City and State, or Zip Code") {
        search_alert("<p>Please enter a city/state or a zip code into the search field.  We recommend that you follow the following format:</p><p>City, State (e.g. <a href=\"javascript:FoeSearch('San Francisco, CA');\">San Francisco, CA</a>)<br/>Zip Code (e.g.  <a href=\"javascript:FoeSearch('89148');\">89148</a>)</p>");
        // gE("search_alert").style.display = "block";
        return false;
    }

    //var RegExZip = new RegExp("[ABCEGHJKLMNPRSTVXY]\\d[A-Z]\\s?\\d[A-Z]\\d", "i");
    var RegExZip = new RegExp(/[0-9]{5}/);

    if (RegExZip.exec(txtSearch) != null) // the regex matched a zip code
    {
        txtSearch = txtSearch.replace(' ', '');
        QueryString += String.format("&Ntt={0}&Ntk=AddrZip&Ntx=mode matchall", txtSearch);
    }

    //----------------- AJAX CAll 
    var strURL = WebRoot + "Controls/AjaxCalls/SearchBar.aspx?SearchType=" + SearchType + "&txtSearch=" + txtSearch + "&sid=" + sid + "&sb=" + sb + "&C=" + Country + "&" + QueryString + qryPrice + "&SearchFor=" + Category;

    //document.body.innerHTML = strURL;
    //return;

    //==================================================
    //  Response delegate handles conditions, if there are zero results, or if there are more than one result.
    //  If One Result :     redirect to results
    //  If More Than one :  Display dropdown with selection
    //  If zero :           display Zero Results
    //==================================================

    var ResponseDelegate = function(AjaxResponse) {
        if (AjaxResponse.indexOf("var doEval = true;") > -1) // flag indicating we should evaluate the response
        {
            //alert(AjaxResponse);
            eval(AjaxResponse);
        }
        else {
            gE("searchformresult").innerHTML = AjaxResponse;
        }
    };


    //-----------------
    $AJAX.GetForDelegate(ResponseDelegate, strURL);
    //window.location.href = strURL; // for testing
    //-----------------
}
//========================================================
//   DoSearchNewHomesHomepage
//========================================================
function DoSearchNewHomesHomepage(theForm) {
    var SearchType = "sale";
    var Category = "community";
    var beds = "0";
    var pnum1 = 0;
    var pnum2 = 99000000;
    var txtSearch = "";
    var qrystr = "";
    var qryPrice = "";
    var els = theForm.elements;
    txtSearch = trim(theForm.txtSearch.value);
    var Country = theForm.search_country_select.options[theForm.search_country_select.options.selectedIndex].value;
    var sb = theForm.sb.value; // Passed for Search Bar State ...
    var SEOSiteID = theForm.SEOSiteID.value;
    var NavQuery = new Array();

    NavQuery.push(NewHomesID);

    // alert(NavQuery);

    CloseSuggest();

//    if (theForm.list_DataSource) {
//        var datasource = theForm.list_DataSource.options;

//        if (datasource.selectedIndex > -1) {
//            if (datasource[datasource.selectedIndex].value != "") {
//                NavQuery.push(datasource[datasource.selectedIndex].value);
//            }
//        }
//    }

    if (theForm.EndecaId.value.length > 0) {
        NavQuery.push(theForm.EndecaId.value);
    }

    // alert(NavQuery);

    var bedslist = theForm.search_bedrooms.options;
    if (bedslist.selectedIndex > 0) {
        NavQuery.push(bedslist[bedslist.selectedIndex].value);
    }

    var bathslist = theForm.search_bathrooms.options;
    if (bathslist.selectedIndex > 0) {
        NavQuery.push(bathslist[bathslist.selectedIndex].value);
    }

    if (theForm.search_propertytype) {
        var ptypelist = theForm.search_propertytype.options;
        if (ptypelist.selectedIndex > 0) {
            NavQuery.push(ptypelist[ptypelist.selectedIndex].value);
        }
    }

    if (theForm.OpenhouseCheckboxState.value == 'true')
        NavQuery.push('1123836');

    var QueryString = "N=" + NavQuery.join("+");

    //  alert(QueryString);

    var minPrice = theForm.search_pricemin.options;
    var maxPrice = theForm.search_pricemax.options;
    if ((minPrice.selectedIndex > 0) || (maxPrice.selectedIndex > 0)) {
        if ((minPrice.selectedIndex > maxPrice.selectedIndex) && (maxPrice.selectedIndex != 0)) {
            search_alert("<p>The minimum price must be less than the maximum price</p>");
            return false;
        }
        else {
            qryPrice = String.format("&Nf=Price_Sales|BTWN+{0}+{1}",
                                minPrice[minPrice.selectedIndex].value,
                                maxPrice[maxPrice.selectedIndex].value);
        }
    }
    else {
        qryPrice = "";
    }

    txtSearch = trim(txtSearch);

    if (txtSearch == "" || txtSearch == "City and State, or Zip Code") {
        search_alert("<p>Please enter a city/state or a zip code into the search field.  We recommend that you follow the following format:</p><p>City, State (e.g. <a href=\"javascript:FoeSearch('San Francisco, CA');\">San Francisco, CA</a>)<br/>Zip Code (e.g.  <a href=\"javascript:FoeSearch('89148');\">89148</a>)</p>");
        // gE("search_alert").style.display = "block";
        return false;
    }

    var RegExCAZip = new RegExp("[ABCEGHJKLMNPRSTVXY]\\d[A-Z]\\s?\\d[A-Z]\\d", "i");
    var RegExUSZip = new RegExp(/[0-9]{5}/);

    if (RegExUSZip.exec(txtSearch) != null) // the regex matched a us zip code
    {
        txtSearch = txtSearch.replace(' ', '');
        QueryString += String.format("&Ntt={0}&Ntk=AddrZip&Ntx=mode matchall", txtSearch);
    }
    //    else if (RegExCAZip.exec(txtSearch) != null) // the regex matched a canadian zip code
    //    {
    //        txtSearch = txtSearch.replace(' ','');
    //        QueryString += String.format("&Ntt={0}&Ntk=AddrZip&Ntx=mode matchall",txtSearch);
    //    }
    else // users cannot logically search by zip AND keyword without more work... - Jim
    {
        if (theForm.search_keywords) {
            var txtkeyword = trim(theForm.search_keywords.value);
            if (txtkeyword != "") {
                if (txtkeyword.length < 3) {
                    search_alert("<p>Please enter minimum 3 characters.</p>");
                    return false;
                }
                var RegExKW = new RegExp(/[^A-Za-z0-9\-\s]+/);
                if (RegExKW.exec(txtkeyword)) {
                    search_alert("<p>Please enter alpha numeric and space only.</p>");
                    return false;
                }
                QueryString += String.format("&Ntt={0}&Ntk=CommentsLong", theForm.search_keywords.value);
            }
        }
    }


    //----------------- AJAX CAll
    var strURL = WebRoot + "Controls/AjaxCalls/SearchBar.aspx?SearchType=" + SearchType + "&txtSearch=" + txtSearch + "&SEOSiteID=" + SEOSiteID + "&sid=" + sid + "&sb=" + sb + "&C=" + Country + "&" + QueryString + qryPrice + "&Category=" + Category;
    
    
    //alert(strURL);
    //document.body.innerHTML = strURL;
    //return;

    //==================================================
    //  Response delegate handles conditions, if there are zero results, or if there are more than one result.
    //  If One Result :     redirect to results
    //  If More Than one :  Display dropdown with selection
    //  If zero :           display Zero Results
    //==================================================

    var ResponseDelegate = function(AjaxResponse) {
        if (AjaxResponse.indexOf("var doEval = true;") > -1) // flag indicating we should evaluate the response
        {
            //alert(AjaxResponse);
            eval(AjaxResponse);
        }
        else {
            gE("searchformresult").innerHTML = AjaxResponse;
        }
    };


    //-----------------
    $AJAX.GetForDelegate(ResponseDelegate, strURL);
    //   alert(strURL);
    //window.location.href = strURL; // for testing
    //-----------------
}

//========================================================
//   DoSearchRentalsHomepage
//========================================================
function DoSearchRentalsHomepage(theForm) {
    var SearchType = "rentals";
    var Category = "";
    var beds = "0";
    var pnum1 = 0;
    var pnum2 = 99000000;
    var txtSearch = "";
    var qrystr = "";
    var qryPrice = "";
    var els = theForm.elements;
    txtSearch = trim(theForm.txtSearch.value);
    var Country = "200"; //theForm.search_country_select.options[theForm.search_country_select.options.selectedIndex].value;
    var sb = theForm.sb.value; // Passed for Search Bar State ... 
    var SEOSiteID = theForm.SEOSiteID.value;
    var NavQuery = new Array();
    var isCommunitySearch = false;
    
    NavQuery.push(rentalsID);
       
    for (var i = 0; i < theForm.list_DataSource.length; i++) {
        if (theForm.list_DataSource[i].checked) {
            var datasource = theForm.list_DataSource[i].value;
            if (datasource != 1058959) {
                NavQuery.push(datasource);
                isCommunitySearch = false;
            }
            else {
                Category = "apartments";
                NavQuery.push(AptCommunityID);
                isCommunitySearch = true;
            }
            break;
        }
    }
    CloseSuggest();
    var search_bedrooms = null;
    var search_bathrooms = null;
    var search_propertytype = null;

    if (isCommunitySearch == true) {
        search_bedrooms = theForm.search_bedrooms_community;
        search_bathrooms = theForm.search_bathrooms_community;
        search_propertytype = theForm.search_propertytype_community;
    }
    else {
        search_bedrooms = theForm.search_bedrooms_listing;
        search_bathrooms = theForm.search_bathrooms_listing;
        search_propertytype = theForm.search_propertytype_listing;
    }

    if (theForm.EndecaId.value.length > 0) {
        NavQuery.push(theForm.EndecaId.value);
    }

    if (search_bedrooms != null) {
        var bedslist = search_bedrooms.options;
        if (bedslist.selectedIndex > 0) {
            NavQuery.push(bedslist[bedslist.selectedIndex].value);
        }
    }
    if (search_bathrooms != null) {
        var bathslist = search_bathrooms.options;
        if (bathslist.selectedIndex > 0) {
            NavQuery.push(bathslist[bathslist.selectedIndex].value);
        }
    }
    if (search_propertytype != null) {
        var ptypelist = search_propertytype.options;
        if (ptypelist.selectedIndex > 0) {
            NavQuery.push(ptypelist[ptypelist.selectedIndex].value);
        }
    }
    
    if (theForm.OpenhouseCheckboxState.value == 'true')
        NavQuery.push('1123836');

    var QueryString = "N=" + NavQuery.join("+");

    qryPrice = "";
    if (theForm.search_pricemin) {
        var minPrice = theForm.search_pricemin.options;
        var maxPrice = theForm.search_pricemax.options;
        if ((minPrice.selectedIndex > 0) || (maxPrice.selectedIndex > 0)) {
            if ((minPrice.selectedIndex > maxPrice.selectedIndex) && (maxPrice.selectedIndex != 0)) {
                search_alert("<p>The minimum price must be less than the maximum price</p>");
                return false;
            }
            else {
//                if (isCommunitySearch == true) {
//                    qryPrice = String.format("&Nf=PriceMin_Rentals|GTEQ+{0}|PriceMax_Rentals|LTEQ+{1}",
//                                    minPrice[minPrice.selectedIndex].value,
//                                    maxPrice[maxPrice.selectedIndex].value);
//                }
//                else {             
                qryPrice = String.format("&Nf=Price_Rentals|BTWN+{0}+{1}",
                                    minPrice[minPrice.selectedIndex].value,
                                    maxPrice[maxPrice.selectedIndex].value);
//                }                                     
            }
        }
    }

    txtSearch = trim(txtSearch);

    if (txtSearch == "" || txtSearch == "City and State, or Zip Code") {
        search_alert("<p>Please enter a city/state or a zip code into the search field.  We recommend that you follow the following format:</p><p>City, State (e.g. <a href=\"javascript:FoeSearch('San Francisco, CA');\">San Francisco, CA</a>)<br/>Zip Code (e.g.  <a href=\"javascript:FoeSearch('89148');\">89148</a>)</p>");
        // gE("search_alert").style.display = "block";
        return false;
    }

    var RegExCAZip = new RegExp("[ABCEGHJKLMNPRSTVXY]\\d[A-Z]\\s?\\d[A-Z]\\d", "i");
    var RegExUSZip = new RegExp(/[0-9]{5}/);

    if (RegExUSZip.exec(txtSearch) != null) // the regex matched a us zip code
    {
        txtSearch = txtSearch.replace(' ', '');
        QueryString += String.format("&Ntt={0}&Ntk=AddrZip&Ntx=mode matchall", txtSearch);
    }

    else // users cannot logically search by zip AND keyword without more work... - Jim
    {
        if (theForm.search_keywords) {
            var txtkeyword = trim(theForm.search_keywords.value);
            if (txtkeyword != "") {
                if (txtkeyword.length < 3) {
                    search_alert("<p>Please enter minimum 3 characters.</p>");
                    return false;
                }
                var RegExKW = new RegExp(/[^A-Za-z0-9\-\s]+/);
                if (RegExKW.exec(txtkeyword)) {
                    search_alert("<p>Please enter alpha numeric and space only.</p>");
                    return false;
                }
                QueryString += String.format("&Ntt={0}&Ntk=CommentsLong", theForm.search_keywords.value);
            }
        }
    }

    //----------------- AJAX CAll 
    var strURL = WebRoot + "Controls/AjaxCalls/SearchBar.aspx?SearchType=" + SearchType + "&txtSearch=" + txtSearch + "&SEOSiteID=" + SEOSiteID + "&sid=" + sid + "&sb=" + sb + "&C=" + Country + "&" + QueryString + qryPrice + "&Category=" + Category; ;
    //alert(strURL);
    //document.body.innerHTML = strURL;
    //return;

    //==================================================
    //  Response delegate handles conditions, if there are zero results, or if there are more than one result.
    //  If One Result :     redirect to results
    //  If More Than one :  Display dropdown with selection
    //  If zero :           display Zero Results
    //==================================================

    var ResponseDelegate = function(AjaxResponse) {
        if (AjaxResponse.indexOf("var doEval = true;") > -1) // flag indicating we should evaluate the response
        {
            //alert(AjaxResponse);
            eval(AjaxResponse);
        }
        else {
            gE("searchformresult").innerHTML = AjaxResponse;
        }
    };


    //-----------------
    $AJAX.GetForDelegate(ResponseDelegate, strURL);
    //window.location.href = strURL; // for testing
    //-----------------
}

//========================================================
//   DoSearchApartmentsHomepage
//========================================================
function DoSearchApartmentsHomepage(theForm) {
    var SearchType = "rentals";
    var Category = "apartments";
    var beds = "0";
    var pnum1 = 0;
    var pnum2 = 99000000;
    var txtSearch = "";
    var qrystr = "";
    var qryPrice = "";
    var els = theForm.elements;
    txtSearch = trim(theForm.txtSearch.value);
    var Country = "200"; //theForm.search_country_select.options[theForm.search_country_select.options.selectedIndex].value;
    var sb = theForm.sb.value; // Passed for Search Bar State ... 
    var SEOSiteID = theForm.SEOSiteID.value;
    var NavQuery = new Array();

    NavQuery.push(AptCommunityID);

    if (theForm.list_DataSource) {
        var datasource = theForm.list_DataSource.options;

        if (datasource.selectedIndex > -1) {
            if (datasource[datasource.selectedIndex].value != "") {
                NavQuery.push(datasource[datasource.selectedIndex].value);
            }
        }
    }


    CloseSuggest();

    if (theForm.EndecaId.value.length > 0) {
        NavQuery.push(theForm.EndecaId.value);
    }

    if (theForm.search_bedrooms) {
        var bedslist = theForm.search_bedrooms.options;
        if (bedslist.selectedIndex > 0) {
            NavQuery.push(bedslist[bedslist.selectedIndex].value);
        }
    }

    if (theForm.search_bathrooms) {
        var bathslist = theForm.search_bathrooms.options;
        if (bathslist.selectedIndex > 0) {
            NavQuery.push(bathslist[bathslist.selectedIndex].value);
        }
    }

    //    if (theForm.search_propertytype)
    //    {
    //        var ptypelist = theForm.search_propertytype.options;
    //        if (ptypelist.selectedIndex > 0)
    //        {
    //          NavQuery.push(ptypelist[ptypelist.selectedIndex].value);
    //        }
    //    }

    if (theForm.OpenhouseCheckboxState.value == 'true')
        NavQuery.push('1123836');

    var QueryString = "N=" + NavQuery.join("+");

    qryPrice = "";
    if (theForm.search_pricemin) {
        var minPrice = theForm.search_pricemin.options;
        var maxPrice = theForm.search_pricemax.options;
        if ((minPrice.selectedIndex > 0) || (maxPrice.selectedIndex > 0)) {
            if ((minPrice.selectedIndex > maxPrice.selectedIndex) && (maxPrice.selectedIndex != 0)) {
                search_alert("<p>The minimum price must be less than the maximum price</p>");
                return false;
            }
            else {
                qryPrice = String.format("&Nf=Price_Rentals|BTWN+{0}+{1}",
                                    minPrice[minPrice.selectedIndex].value,
                                    maxPrice[maxPrice.selectedIndex].value);
            }
        }
    }

    txtSearch = trim(txtSearch);

    if (txtSearch == "" || txtSearch == "City and State, or Zip Code") {
        search_alert("<p>Please enter a city/state or a zip code into the search field.  We recommend that you follow the following format:</p><p>City, State (e.g. <a href=\"javascript:FoeSearch('San Francisco, CA');\">San Francisco, CA</a>)<br/>Zip Code (e.g.  <a href=\"javascript:FoeSearch('89148');\">89148</a>)</p>");
        // gE("search_alert").style.display = "block";
        return false;
    }

    var RegExCAZip = new RegExp("[ABCEGHJKLMNPRSTVXY]\\d[A-Z]\\s?\\d[A-Z]\\d", "i");
    var RegExUSZip = new RegExp(/[0-9]{5}/);

    if (RegExUSZip.exec(txtSearch) != null) // the regex matched a us zip code
    {
        txtSearch = txtSearch.replace(' ', '');
        QueryString += String.format("&Ntt={0}&Ntk=AddrZip&Ntx=mode matchall", txtSearch);
    }

    else // users cannot logically search by zip AND keyword without more work... - Jim
    {
        if (theForm.search_keywords) {
            var txtkeyword = trim(theForm.search_keywords.value);
            if (txtkeyword != "") {
                if (txtkeyword.length < 3) {
                    search_alert("<p>Please enter minimum 3 characters.</p>");
                    return false;
                }
                var RegExKW = new RegExp(/[^A-Za-z0-9\-\s]+/);
                if (RegExKW.exec(txtkeyword)) {
                    search_alert("<p>Please enter alpha numeric and space only.</p>");
                    return false;
                }
                QueryString += String.format("&Ntt={0}&Ntk=CommentsLong", theForm.search_keywords.value);
            }
        }
    }

    //----------------- AJAX CAll 
    var strURL = WebRoot + "Controls/AjaxCalls/SearchBar.aspx?SearchType=" + SearchType + "&txtSearch=" + txtSearch + "&SEOSiteID=" + SEOSiteID + "&sid=" + sid + "&sb=" + sb + "&C=" + Country + "&" + QueryString + qryPrice + "&category=" + Category; ;
    //alert(strURL);
    //document.body.innerHTML = strURL;
    //return;

    //==================================================
    //  Response delegate handles conditions, if there are zero results, or if there are more than one result.
    //  If One Result :     redirect to results
    //  If More Than one :  Display dropdown with selection
    //  If zero :           display Zero Results
    //==================================================

    var ResponseDelegate = function(AjaxResponse) {
        if (AjaxResponse.indexOf("var doEval = true;") > -1) // flag indicating we should evaluate the response
        {
            //alert(AjaxResponse);
            eval(AjaxResponse);
        }
        else {
            gE("searchformresult").innerHTML = AjaxResponse;
        }
    };


    //-----------------
    $AJAX.GetForDelegate(ResponseDelegate, strURL);
    //window.location.href = strURL; // for testing
    //-----------------
}
//========================================================
//   DoSearchSalesHomepage
//========================================================
function DoSearchSalesHomepage(theForm) {
    var SearchType = "sales";
    var beds = "0";
    var pnum1 = 0;
    var pnum2 = 99000000;
    var txtSearch = "";
    var qrystr = "";
    var qryPrice = "";
    var els = theForm.elements;
    txtSearch = trim(theForm.txtSearch.value);
    var Country = theForm.search_country_select.options[theForm.search_country_select.options.selectedIndex].value;
    var sb = theForm.sb.value; // Passed for Search Bar State ... 
    var SEOSiteID = theForm.SEOSiteID.value;
    var NavQuery = new Array();

    NavQuery.push(salesID);

    // Until the new widget is launch, Sales Search default to MLS/Broker Listings.
    NavQuery.push(BrokerID);

    CloseSuggest();
    
//    for (var i = 0; i < theForm.list_DataSource.length; i++) {
//        if (theForm.list_DataSource[i].checked) {
//            var datasource = theForm.list_DataSource[i].value;
//            if (datasource != 1058959) {
//                NavQuery.push(datasource);
//            }
//            else {
//                Category = "apartments";
//                NavQuery.push(AptCommunityID);
//            }
//        }
//    }

    if (theForm.EndecaId.value.length > 0) {
        NavQuery.push(theForm.EndecaId.value);
    }

    if (theForm.search_bedrooms) {
        var bedslist = theForm.search_bedrooms.options;
        if (bedslist.selectedIndex > 0) {
            NavQuery.push(bedslist[bedslist.selectedIndex].value);
        }
    }

    if (theForm.search_bathrooms) {
        var bathslist = theForm.search_bathrooms.options;
        if (bathslist.selectedIndex > 0) {
            NavQuery.push(bathslist[bathslist.selectedIndex].value);
        }
    }

    if (theForm.search_propertytype) {
        var ptypelist = theForm.search_propertytype.options;
        if (ptypelist.selectedIndex > 0) {
            NavQuery.push(ptypelist[ptypelist.selectedIndex].value);
        }
    }

    if (theForm.OpenhouseCheckboxState.value == 'true')
        NavQuery.push('1123836');

    var QueryString = "N=" + NavQuery.join("+");

    //alert(QueryString);

    qryPrice = "";
    if (theForm.search_pricemin) {
        var minPrice = theForm.search_pricemin.options;
        var maxPrice = theForm.search_pricemax.options;
        if ((minPrice.selectedIndex > 0) || (maxPrice.selectedIndex > 0)) {
            if ((minPrice.selectedIndex > maxPrice.selectedIndex) && (maxPrice.selectedIndex != 0)) {
                search_alert("<p>The minimum price must be less than the maximum price</p>");
                return false;
            }
            else {
                qryPrice = String.format("&Nf=Price_Sales|BTWN+{0}+{1}",
                                    minPrice[minPrice.selectedIndex].value,
                                    maxPrice[maxPrice.selectedIndex].value);
            }
        }
    }

    txtSearch = trim(txtSearch);

    if (txtSearch == "" || txtSearch == "City and State, or Zip Code") {
        search_alert("<p>Please enter a city/state or a zip code into the search field.  We recommend that you follow the following format:</p><p>City, State (e.g. <a href=\"javascript:FoeSearch('San Francisco, CA');\">San Francisco, CA</a>)<br/>Zip Code (e.g.  <a href=\"javascript:FoeSearch('89148');\">89148</a>)</p>");
        // gE("search_alert").style.display = "block";
        return false;
    }

    var RegExCAZip = new RegExp("[ABCEGHJKLMNPRSTVXY]\\d[A-Z]\\s?\\d[A-Z]\\d", "i");
    var RegExUSZip = new RegExp(/[0-9]{5}/);

    if (RegExUSZip.exec(txtSearch) != null) // the regex matched a us zip code
    {
        txtSearch = txtSearch.replace(' ', '');
        QueryString += String.format("&Ntt={0}&Ntk=AddrZip&Ntx=mode matchall", txtSearch);
    }
    else if (RegExCAZip.exec(txtSearch) != null) // the regex matched a canadian zip code
    {
        txtSearch = txtSearch.replace(' ', '');
        QueryString += String.format("&Ntt={0}&Ntk=AddrZip&Ntx=mode matchall", txtSearch);
    }

    else // users cannot logically search by zip AND keyword without more work... - Jim
    {
        if (theForm.search_keywords) {
            var txtkeyword = trim(theForm.search_keywords.value);
            if (txtkeyword != "") {
                if (txtkeyword.length < 3) {
                    search_alert("<p>Please enter minimum 3 characters.</p>");
                    return false;
                }
                var RegExKW = new RegExp(/[^A-Za-z0-9\-\s]+/);
                if (RegExKW.exec(txtkeyword)) {
                    search_alert("<p>Please enter alpha numeric and space only.</p>");
                    return false;
                }
                QueryString += String.format("&Ntt={0}&Ntk=CommentsLong", theForm.search_keywords.value);
            }
        }
    }

    //----------------- AJAX CAll 
    //var strURL = WebRoot + "Controls/AjaxCalls/SearchBar.aspx?SearchType=" + SearchType + "&txtSearch=" + txtSearch + "&sid=" + sid + "&sb=" + sb + "&C=" + Country + "&" + QueryString + qryPrice; //+ "&Ns=Priority|1";
    var strURL = WebRoot + "Controls/AjaxCalls/SearchBar.aspx?SearchType=" + SearchType + "&txtSearch=" + txtSearch + "&SEOSiteID=" + SEOSiteID + "&sid=" + sid + "&sb=" + sb + "&C=" + Country + "&" + QueryString + qryPrice; //+ "&Ns=Priority|1";

    //alert(strURL);
    //document.body.innerHTML = strURL;
    //return;

    //==================================================
    //  Response delegate handles conditions, if there are zero results, or if there are more than one result.
    //  If One Result :     redirect to results
    //  If More Than one :  Display dropdown with selection
    //  If zero :           display Zero Results
    //==================================================

    var ResponseDelegate = function(AjaxResponse) {
        if (AjaxResponse.indexOf("var doEval = true;") > -1) // flag indicating we should evaluate the response
        {
            //alert(AjaxResponse);
            eval(AjaxResponse);
        }
        else {
            gE("searchformresult").innerHTML = AjaxResponse;
        }
    };


    //-----------------
    $AJAX.GetForDelegate(ResponseDelegate, strURL);
    // alert(strURL);
}
//========================================================
//   DoSearchClassifiedsHomepage
//========================================================
function DoSearchClassifiedsHomepage(theForm) {
    var SearchType = "sales"
    var beds = "0";
    var pnum1 = 0;
    var pnum2 = 99000000;
    var txtSearch = "";
    var qrystr = "";
    var qryPrice = "";
    var els = theForm.elements;
    txtSearch = trim(theForm.txtSearch.value);
    var Country = theForm.search_country_select.options[theForm.search_country_select.options.selectedIndex].value;
    var sb = theForm.sb.value; // Passed for Search Bar State ... 
    var SEOSiteID = theForm.SEOSiteID.value;
    var NavQuery = new Array();
    //NavQuery.push(ChannelSectionId);
    NavQuery.push(ClassifiedID);    

//    if (theForm.list_DataSource) {
//        var datasource = theForm.list_DataSource.options;
//        NavQuery.push(datasource[datasource.selectedIndex].value);
//    }

    var list_Source = document.getElementsByName("list_DataSource");

    for (i = 0; i < list_Source.length; i++) {
        if (list_Source[i].checked) {
            var datasource = list_Source[i].value;
            if (datasource != 1058959) {
                NavQuery.push(datasource);
            }
            else {
                Category = "apartments";
                NavQuery.push(AptCommunityID);
            }
            break;
        }
    }
    CloseSuggest();

    if (theForm.EndecaId.value.length > 0) {
        NavQuery.push(theForm.EndecaId.value);
    }

    if (theForm.search_bedrooms) {
        var bedslist = theForm.search_bedrooms.options;
        if (bedslist.selectedIndex > 0) {
            NavQuery.push(bedslist[bedslist.selectedIndex].value);
        }
    }

    if (theForm.search_bathrooms) {
        var bathslist = theForm.search_bathrooms.options;
        if (bathslist.selectedIndex > 0) {
            NavQuery.push(bathslist[bathslist.selectedIndex].value);
        }
    }

    //var ptypelist = theForm.search_propertytype.options;
    //if (ptypelist.selectedIndex > 0)
    //{
    //  NavQuery.push(ptypelist[ptypelist.selectedIndex].value);
    //}
    if (theForm.OpenhouseCheckboxState.value == 'true')
        NavQuery.push('1123836');

    var QueryString = "N=" + NavQuery.join("+");

    qryPrice = "";
    if (theForm.search_pricemin) {
        var minPrice = theForm.search_pricemin.options;
        var maxPrice = theForm.search_pricemax.options;
        if ((minPrice.selectedIndex > 0) || (maxPrice.selectedIndex > 0)) {
            if ((minPrice.selectedIndex > maxPrice.selectedIndex) && (maxPrice.selectedIndex != 0)) {
                search_alert("<p>The minimum price must be less than the maximum price</p>");
                return false;
            }
            else {
                qryPrice = String.format("&Nf=Price_Sales|BTWN+{0}+{1}",
                                    minPrice[minPrice.selectedIndex].value,
                                    maxPrice[maxPrice.selectedIndex].value);
            }
        }
    }

    txtSearch = trim(txtSearch);

    if (txtSearch == "" || txtSearch == "City and State, or Zip Code") {
        search_alert("<p>Please enter a city/state or a zip code into the search field.  We recommend that you follow the following format:</p><p>City, State (e.g. <a href=\"javascript:FoeSearch('San Francisco, CA');\">San Francisco, CA</a>)<br/>Zip Code (e.g.  <a href=\"javascript:FoeSearch('89148');\">89148</a>)</p>");
        // gE("search_alert").style.display = "block";
        return false;
    }

    var RegExCAZip = new RegExp("[ABCEGHJKLMNPRSTVXY]\\d[A-Z]\\s?\\d[A-Z]\\d", "i");
    var RegExUSZip = new RegExp(/[0-9]{5}/);

    if (RegExUSZip.exec(txtSearch) != null) // the regex matched a us zip code
    {
        txtSearch = txtSearch.replace(' ', '');
        QueryString += String.format("&Ntt={0}&Ntk=AddrZip&Ntx=mode matchall", txtSearch);
    }
    else if (RegExCAZip.exec(txtSearch) != null) // the regex matched a canadian zip code
    {
        txtSearch = txtSearch.replace(' ', '');
        QueryString += String.format("&Ntt={0}&Ntk=AddrZip&Ntx=mode matchall", txtSearch);
    }

    else // users cannot logically search by zip AND keyword without more work... - Jim
    {
        if (theForm.search_keywords) {
            var txtkeyword = trim(theForm.search_keywords.value);
            if (txtkeyword != "") {
                if (txtkeyword.length < 3) {
                    search_alert("<p>Please enter minimum 3 characters.</p>");
                    return false;
                }
                var RegExKW = new RegExp(/[^A-Za-z0-9\-\s]+/);
                if (RegExKW.exec(txtkeyword)) {
                    search_alert("<p>Please enter alpha numeric and space only.</p>");
                    return false;
                }
                QueryString += String.format("&Ntt={0}&Ntk=CommentsLong", theForm.search_keywords.value);
            }
        }
    }

    //----------------- AJAX CAll 
    var strURL = WebRoot + "Controls/AjaxCalls/SearchBar.aspx?SearchType=" + SearchType + "&txtSearch=" + txtSearch + "&SEOSiteID=" + SEOSiteID + "&sid=" + sid + "&sb=" + sb + "&C=" + Country + "&" + QueryString + qryPrice; //+ "&Ns=Priority|1";
    //alert(strURL);
    //document.body.innerHTML = strURL;
    //return;

    //==================================================
    //  Response delegate handles conditions, if there are zero results, or if there are more than one result.
    //  If One Result :     redirect to results
    //  If More Than one :  Display dropdown with selection
    //  If zero :           display Zero Results
    //==================================================

    var ResponseDelegate = function(AjaxResponse) {
        if (AjaxResponse.indexOf("var doEval = true;") > -1) // flag indicating we should evaluate the response
        {
            //alert(AjaxResponse);
            eval(AjaxResponse);
        }
        else {
            gE("searchformresult").innerHTML = AjaxResponse;
        }
    };


    //-----------------
    $AJAX.GetForDelegate(ResponseDelegate, strURL);
    // alert(strURL);
}

function DoSearchSalesMini(theForm) {
    var SearchType = "sales";
    var beds = "0";
    var pnum1 = 0;
    var pnum2 = 99000000;
    var txtSearch = "";
    var qrystr = "";
    var qryPrice = "";
    var els = theForm.elements;
    txtSearch = trim(theForm.txtSearch.value);
    var Country = theForm.search_country_select.options[theForm.search_country_select.options.selectedIndex].value;
    var sb = theForm.sb.value; // Passed for Search Bar State ... 
    var NavQuery = new Array();

    // For sale.
    NavQuery.push('829246');

    // MLS/Broker
    NavQuery.push('829166');

    CloseSuggest();

    if (theForm.EndecaId.value.length > 0) {
        NavQuery.push(theForm.EndecaId.value);
    }



    var QueryString = "N=" + NavQuery.join("+");


    txtSearch = trim(txtSearch);

    if (txtSearch == "" || txtSearch == "Search Homes for Sale. Enter City, State or Zip.") {
        search_alert("&nbsp;&nbsp;Please enter a <b>City</b>, <b>State</b> or a <b>Zip Code</b> into the search field.</b>");
        // gE("search_alert").style.display = "block";
        return false;
    }

    var RegExCAZip = new RegExp("[ABCEGHJKLMNPRSTVXY]\\d[A-Z]\\s?\\d[A-Z]\\d", "i");
    var RegExUSZip = new RegExp(/[0-9]{5}/);

    if (RegExUSZip.exec(txtSearch) != null) // the regex matched a us zip code
    {
        txtSearch = txtSearch.replace(' ', '');
        QueryString += String.format("&Ntt={0}&Ntk=AddrZip&Ntx=mode matchall", txtSearch);
    }
    else if (RegExCAZip.exec(txtSearch) != null) // the regex matched a canadian zip code
    {
        txtSearch = txtSearch.replace(' ', '');
        QueryString += String.format("&Ntt={0}&Ntk=AddrZip&Ntx=mode matchall", txtSearch);
    }

    //----------------- AJAX CAll 
    var strURL = WebRoot + "Controls/AjaxCalls/SearchBar.aspx?SearchType=" + SearchType + "&txtSearch=" + txtSearch + "&sid=" + sid + "&sb=" + sb + "&C=" + Country + "&" + QueryString + qryPrice + "&Ns=Priority|1";
    //alert(strURL);
    //document.body.innerHTML = strURL;
    //return;

    //==================================================
    //  Response delegate handles conditions, if there are zero results, or if there are more than one result.
    //  If One Result :     redirect to results
    //  If More Than one :  Display dropdown with selection
    //  If zero :           display Zero Results
    //==================================================

    var ResponseDelegate = function(AjaxResponse) {
        if (AjaxResponse.indexOf("var doEval = true;") > -1) // flag indicating we should evaluate the response
        {
            //alert(AjaxResponse);
            eval(AjaxResponse);
        }
        else {
            gE("searchformresult").innerHTML = AjaxResponse;
        }
    };


    //-----------------
    $AJAX.GetForDelegate(ResponseDelegate, strURL);
    //alert(strURL);
    //window.location.href = strURL; // for testing
    //-----------------
}

//========================================================
//   SwitchSearch
//========================================================
function SwitchSearch(type, state, Query) {
    if (Query == undefined) {
        Query = "";
    }
    var UGen = new UrlGen(Query);

    if (state == undefined) {
        state = "small";
    }

    var AfterLoad = function() { void (null); }; // to be defined where needed, called after new search is loaded

    switch (type) {
        case "Apartments":
            switch (state) {
                case "Homepage":
                    UGen.RemoveParam("sb");
                    UGen.AddParam("sb", "14");
                    var strURL = WebRoot + "Controls/AjaxCalls/GetSearchBar.aspx?" + UGen.ToString();
                    break;
                case "Expanded":
                    UGen.RemoveParam("sb");
                    UGen.AddParam("sb", "13");
                    var strURL = WebRoot + "Controls/AjaxCalls/GetSearchBar.aspx?" + UGen.ToString();
                    break;
                default:
                    UGen.RemoveParam("sb");
                    UGen.AddParam("sb", "12");
                    var strURL = WebRoot + "Controls/AjaxCalls/GetSearchBar.aspx?" + UGen.ToString();
                    break;
            }
            break;

        case "Rentals":
            switch (state) {
                case "Homepage":
                    UGen.RemoveParam("sb");
                    UGen.AddParam("sb", "11");
                    var strURL = WebRoot + "Controls/AjaxCalls/GetSearchBar.aspx?" + UGen.ToString();
                    break;
                case "Expanded":
                    UGen.RemoveParam("sb");
                    UGen.AddParam("sb", "10");
                    var strURL = WebRoot + "Controls/AjaxCalls/GetSearchBar.aspx?" + UGen.ToString();
                    break;
                default:
                    UGen.RemoveParam("sb");
                    UGen.AddParam("sb", "9");
                    var strURL = WebRoot + "Controls/AjaxCalls/GetSearchBar.aspx?" + UGen.ToString();
                    break;
            }
            break;

        case "NewHomes":
            switch (state) {
                case "Homepage":
                    UGen.RemoveParam("sb");
                    UGen.AddParam("sb", "8");
                    var strURL = WebRoot + "Controls/AjaxCalls/GetSearchBar.aspx?" + UGen.ToString();
                    break;
                case "Expanded":
                    UGen.RemoveParam("sb");
                    UGen.AddParam("sb", "7");
                    var strURL = WebRoot + "Controls/AjaxCalls/GetSearchBar.aspx?" + UGen.ToString();
                    break;
                default:
                    UGen.RemoveParam("sb");
                    UGen.AddParam("sb", "6");
                    var strURL = WebRoot + "Controls/AjaxCalls/GetSearchBar.aspx?" + UGen.ToString();
                    break;
            }
            break;

        case "Agents":
            switch (state) {
                case "Homepage":
                    UGen.RemoveParam("sb");
                    UGen.AddParam("sb", "5");
                    var strURL = WebRoot + "Controls/AjaxCalls/GetSearchBar.aspx?" + UGen.ToString();
                    break;
                case "Expanded":
                    UGen.RemoveParam("sb");
                    UGen.AddParam("sb", "4");
                    var strURL = WebRoot + "Controls/AjaxCalls/GetSearchBar.aspx?" + UGen.ToString();
                    break;
                default:
                    UGen.RemoveParam("sb");
                    UGen.AddParam("sb", "3");
                    var strURL = WebRoot + "Controls/AjaxCalls/GetSearchBar.aspx?" + UGen.ToString();
                    break;
            }
            break;

        default:
            switch (state) {
                case "Homepage":
                    UGen.RemoveParam("sb");
                    UGen.AddParam("sb", "2");
                    var strURL = WebRoot + "Controls/AjaxCalls/GetSearchBar.aspx?" + UGen.ToString();
                    break;
                case "Expanded":
                    UGen.RemoveParam("sb");
                    UGen.AddParam("sb", "1");
                    var strURL = WebRoot + "Controls/AjaxCalls/GetSearchBar.aspx?" + UGen.ToString();
                    break;
                default:
                    UGen.RemoveParam("sb");
                    UGen.AddParam("sb", "0");
                    var strURL = WebRoot + "Controls/AjaxCalls/GetSearchBar.aspx?" + UGen.ToString();
                    break;
            }
            break;

    }
    $AJAX.GetForDelegate(function(AjaxResponse) {
        gE("search_bar").innerHTML = AjaxResponse;
        AfterLoad();
    }, strURL);
}


//========================================================
//  GetC - Can be used for multinational search
//  See LivingChoices
//========================================================
function GetC() {
    //var CVal = gE("CVal");
    //return CVal.options[CVal.options.selectedIndex].value;
    //return 200; // USA
    var CVal = gE("search_country_select")
    return CVal.options[CVal.options.selectedIndex].value;
}

document.body.onclick = function() { CloseSuggest(); search_alert_close(); };

//========================================================
//  SuggestDown
//  Used for Suggest functionality
//========================================================
function SuggestDown(e, ele) {
    if (gE("search_alert").style.display == "block") {
        return false;
    }
    var val = ele.value;
    if (window.event) {
        keynum = e.keyCode;
    }
    else if (e.which) {
        keynum = e.which;
    }
    switch (keynum) {
        case 13: // enter key
            if (gE("suggest").style.display == "block") {
                if (CurrentSuggestIndex() > -1) {
                    //var content = gE("ila" + CurrentSuggestIndex()).innerHTML.replace(/<[^>]+>/g,"").replace(/\s\/\s/g,"/");
                    //AddSuggest(content.toUpperCase());
                    //var content = gE("ila" + CurrentSuggestIndex()).innerHTML.replace(/<[^>]+>/g,"").replace(/\s\/\s/g,"/");
                    AddSuggest(CurrentSuggestIndex());
                    StopProcessingEvents(e);
                    return false;
                }
            }

            return true;
            break;

        case 27: // escape
            CloseSuggest();
            ele.value = val;
            return false;
            break;
        default:
            return true;
            break;
    }
}

//========================================================
//  SuggestUp
//  Used for Suggest functionality
//========================================================
function SuggestUp(e, ele) {
    if (gE("search_alert").style.display == "block") {
        return false;
    }
    var val = ele.value;
    if (window.event) {
        keynum = e.keyCode;
    }
    else if (e.which) {
        keynum = e.which;
    }
    switch (keynum) {
        case 13: // enter key
            if (gE("suggest").style.display == "block") {
                CloseSuggest();
                StopProcessingEvents(e);
                return false;
            }
            return true;
            break;
        case 27: // escape
            CloseSuggest();
            ele.value = val;
            return false;
            break;
        case 40:
            SuggestSelect(CurrentSuggestIndex() + 1);
            break;
        case 38:
            SuggestSelect(CurrentSuggestIndex() - 1);
            break;
        default:
            if (trim(ele.value).length > 1) {
                //CloseSuggest();
                var Query = String.format("{0}Controls/AjaxCalls/Suggest.aspx?C={1}&term={2}", WebRoot, GetC(), trim(ele.value));
                $AJAX.GetForDelegate(ShowSuggest, Query);
            }
            else {
                CloseSuggest();
            }
            break;
    }
}

//========================================================
//  Stops processing Events
//========================================================
function StopProcessingEvents(e) {
    if (e && e.preventDefault) {
        e.preventDefault();
    }
    else if (window.event && window.event.returnValue) {
        window.eventReturnValue = false;
    }
}


//========================================================
//  CurrentSuggestIndex
//  Used for Suggest functionality
//========================================================
function CurrentSuggestIndex() {
    var LIS = gE("suggest").getElementsByTagName("div");
    var T = -1;
    for (var x = 0; x < LIS.length; x++) {
        if (LIS[x].className == 'hot') {
            T = x;
            break;
        }
    }
    return T;
}

//========================================================
//  ScrollToItem
//  Used for Suggest functionality
//========================================================
function ScrollToItem(elepos) {
    var eleE = gE('ila' + elepos);
    var eleS = gE("suggest");
    var Etop = eleE.offsetTop;
    var Stop = eleS.scrollTop;

    if (Etop < gE("suggest").scrollTop) {
        gE("suggest").scrollTop = Etop;
    }
    else if ((Etop + eleE.offsetHeight) > (Stop + eleS.offsetHeight)) {
        gE("suggest").scrollTop = Etop - eleS.offsetHeight + eleE.offsetHeight;
    }
}

//========================================================
//  SuggestSelect
//  Used for Suggest functionality
//========================================================
function SuggestSelect(num) {
    var LIS = gE("suggest").getElementsByTagName("div");
    var item = null;

    for (var x = 0; x < LIS.length; x++) {
        if (x == num) {
            LIS[x].className = 'hot';
            item = x;
        }
        else {
            LIS[x].className = 'non';
        }
    }
    if (item == null) {
        if (num > LIS.length - 1) {
            item = LIS.length - 1;
            LIS[item].className = 'hot';
        }
        else {
            item = 0;
        }
    }

    if (item > 0) {
        ScrollToItem(item);
    }
}

//========================================================
//  ShowSuggest
//  Used for Suggest functionality
//========================================================
function ShowSuggest(AjaxResponse) {
    if (trim(AjaxResponse).length > 0) {
        if (gE("suggest").style.display != "block") {
            gE("suggest").style.display = "block";
        }
        search_alert_close();
        ToggleSelects("hidden");
        gE("suggest").innerHTML = AjaxResponse;
    }
    else {
        CloseSuggest();
    }
}

//========================================================
//  ToggleSelects
//  Used for Suggest functionality
//========================================================
function ToggleSelects(visibility) {
    var Sel = document.getElementsByTagName("select");
    for (var x = 0; x < Sel.length; x++) {
        Sel[x].style.visibility = visibility;
    }
}

//========================================================
//  CloseSuggest
//  Used for Suggest functionality
//========================================================
function CloseSuggest() {
    gE("suggest").style.display = "none";
    ToggleSelects("visible");
}

//========================================================
//  AddSuggest
//  Used for Suggest functionality
//========================================================
function AddSuggest(index) {
    var CityName = gE("ila" + index).innerHTML.replace(/<[^>]+>/g, "").replace(/\s\/\s/g, "/");
    var EndecaId = gE("ila" + index).getAttribute('name');
    gE("txtSearch").value = CityName.toUpperCase();
    gE("EndecaId").value = EndecaId;
    //CloseSuggest();
}

//========================================================
//  search_alert
//========================================================
function search_alert(strMessage, strClassName) {
    window.setTimeout(function() {
        if (strClassName == undefined) {
            strClassName = "search_alert";
        }
        if (BarStatus.value != BevtQueue) {
            gE("txtSearch").blur(); // cause the sales search bar to close
            window.setTimeout(function() {
                search_alert(strMessage, strClassName); // recall oops after it's closed
            }, 300);
            return;
        }
        // alert(strMessage);
        // window.setTimeout(function()
        // {
        CloseSuggest();
        ToggleSelects("hidden");
        gE("search_alert").style.display = "block";
        gE("search_alert").className = strClassName;
        gE("search_alert_message").innerHTML = strMessage;
        // },10);
    }, 300);
}

function search_alert_close() {
    if (gE("suggest").style.display == "none") {
        ToggleSelects("visible");
    }
    gE("search_alert").style.display = "none";
}

//========================================================
//  SwitchSearchType(strStatus)
//  Controls the toggle dropdown between "property" and 
//  "content" search
//  If no value is passed for strStatus, the options
//  toggle.
//========================================================
function SwitchSearchType(strStatus) {
    window.setTimeout(function() {
        if (strStatus == undefined) {
            if (gE("search_type_list").style.display != 'block') {
                strStatus = "open";
            }
            else {
                strStatus = "close";
            }
        }

        //window.location.href += "#" + strStatus;
        if (strStatus == "open") {
            if (BarStatus.value != BevtQueue) // search bar is open
            {
                gE("txtSearch").blur(); // cause the sales search bar to close
                window.setTimeout(function() {
                    SwitchSearchType(strStatus); // recall SwitchSearchType after it's closed
                }, 300);
                return;
            }
            gE("search_type_list").style.display = 'block';
            gE("search_list_backOpen").style.display = 'block';
            //  Next line For Debugging
            //  gE("search_list_backOpen").style.backgroundColor = '#fff';

            var G = new GetWindowBunds();
            gE("search_list_backOpen").style.left = "1px";
            gE("search_list_backOpen").style.top = "1px";
            gE("search_list_backOpen").style.width = (G.VisibleWidth - 20) + "px";
            gE("search_list_backOpen").style.height = (G.PageHeight - 20) + "px";
            //window.location.href += "#open";
            //BevtStasis('STOpen');
        }
        else {

            gE("search_type_list").style.display = 'none';
            gE("search_list_backOpen").style.display = 'none';
            //window.location.href += "#closed";
            //        if (BarStatus.value != BevtQueue)
            //        {
            //            gE("txtSearch").focus();
            //            //window.location.href += "#focused";
            //        }
        }
    }, 300);
}

//========================================================
//  Bevt(e) 
//  Property SEarch Bar Close Events
//  Manages the opened state of the nav bar
//  if clicked outside any form element, and that element
//  loses focus, close the bar
//  only called from Property Search, Expanded
//========================================================
var BevtQueue = "clicked off"; // constant value to determine if it's time to close bar
function Bevt(e) {

    var theTarget;
    if (!e) {
        e = window.event;
    }
    if (e.target) {
        theTarget = e.target;
    }
    else if (e.srcElement) {
        theTarget = e.srcElement;
    }
    if (theTarget.nodeType == 3) // defeat Safari bug
    {
        theTarget = theTarget.parentNode;
    }

    switch (e.type) {
        case "focus":
            BarStatus.value = "event: " + e.type + "\nTarget ID: " + theTarget.id;
            break;
        case "blur":
            BarStatus.value = BevtQueue;
            window.clearTimeout(BarStatus.timeout);
            BarStatus.timeout = window.setTimeout(function() {
                if (BarStatus.value == BevtQueue) // status value has not changed
                {
                    if (PageCache != null) {
                        // PageCache defined in /results.aspx
                        SwitchSearch('sales', 'small', PageCache.QueryString);
                    }
                    else {
                        // DefaultQueryString defined in /controls/search/Global.ascx
                        SwitchSearch('sales', 'small', DefaultQueryString);
                    }
                }
            }, 200);
            break;
    }
    //gE("NavStatus").value = BarStatus.value;
}

var BarStatus = new BevtValue(BevtQueue);

function BevtValue(Bevt) {
    this.value = Bevt;
    this.timeout = window.setTimeout(function() { void (null); }, 1); // moot timeout to load variable
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//  used when a value needs to be set for BarStatus so the nav 
//  won't close. Called when the user clicks the "switch search
//  type" toggle, and the Expanded Sales SEarch is open.
//  Called with a SetTimeout so it fires AFTER the last onBlur()
//  event is called.
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function BevtStasis(val) {
    window.setTimeout(function() {
        BarStatus.value = val;
    }, 100);
}
