var Search = Class.create();

Search.prototype = 
{
    initialize: function(listing_count, last_home_detail, search_start, search_results) 
    {
        this.table_prefix = null;
        this.homeQuery = null;
        this.show_summary = false;
        this.only_featured = false;
        
        this.listing_count = listing_count;
        this.last_home_detail = last_home_detail;
        this.search_start = search_start;
        this.home_types = new Array();
        this.list_classes = new Array();
        this.districts = new Array();
        
        this.search_results = search_results;
        this.ext_search = false;
        this.only_featured = false;
        this.filter_on_city = true;
        this.filter_on_county = false;
        if(this.search_results == null)
        {
            this.search_results = new SearchResults();
        }
        
        this.setDefaults(null);
    },
    setHomeQuery:function(query)
    {
        this.homeQuery = query;
    },
    resetSearchForm: function()
    {
        this.setSearchValues(null);
        
        var sa = new SimpleAjax('reset', "/ajax/search_reset.php");
        sa.sendGet(null);
        if (_mdc != null)
        {
            _mdc.clearHomes();
        }
    },
    DoMLSSearch:function(mls)
    {
        $('home_num').value = mls;
        this.doSearch();
    },
    /*** these are functions that will be overwritten by child classes ****/
    setDefaults: function(defaults) 
    {
      this.defaults = {
              min_price: 0,
            max_price: 2000000,
            bedrooms: 0,
            bathrooms: 0,
            garage: 0,
            min_year: 1,
            max_year: 2000,
            home_type: '',
            list_class: '',
            district: '',
            sq_feet: 0,
            acreage: 0,
		part_address: '',
		part_address_fields: ''
      }
      Object.extend(this.defaults, defaults || {});
    },
    createQuery:function()
    {
        var min_price = $('min_price').options[$('min_price').selectedIndex].value;
        var max_price = $('max_price').options[$('max_price').selectedIndex].value;
        var bedrooms = $('bedrooms').options[$('bedrooms').selectedIndex].value;
        var bathrooms = $('bathrooms').options[$('bathrooms').selectedIndex].value;
        var garage = $('garage').options[$('garage').selectedIndex].value;
        var min_year = $('min_year').options[$('min_year').selectedIndex].value;
        var max_year = $('max_year').options[$('max_year').selectedIndex].value;
        var home_type = $('home_type').options[$('home_type').selectedIndex].value;
        var sq_feet = $('sq_feet').options[$('sq_feet').selectedIndex].value;
        var list_class = $('list_class').options[$('list_class').selectedIndex].value;
	var part_address = $('part_address') ? $('part_address').value : "";
	var part_address_fields = $('part_address_fields') ? $('part_address_fields').value : "";


        var district = '';
        if($('district') != null)
        {
           district = $('district').options[$('district').selectedIndex].value;
        }
        var acreage = $('acreage').options[$('acreage').selectedIndex].value;
        this.ext_search = false;
    
        //create homeQuery
        var search_query = "home.price >= " + min_price;

	if (part_address != "")
	{
		part_address_cleaned = part_address.replace(/[^a-zA-Z0-9]/, "%");

		fields = new Array('street', 'city', 'zip');
		if (part_address_fields != "")
			fields = part_address_fields.split(',');

		search_query += " AND (";

		clauses = new Array();
		for (i = 0; i < fields.length; i++)
			clauses[i] = "home."+fields[i]+" LIKE '%"+part_address_cleaned+"%' ";

		search_query += clauses.join(' OR ');
		search_query += ") ";

	}

        if(max_price != 2000000)
        {
            search_query += " AND home.price <= " + max_price;
        }        
        
        if(bedrooms > 0)
        {        
            search_query += " AND home.bedrooms >= " + bedrooms;
        }
        
        if(bathrooms > 0)
        {
            search_query += " AND home.bathrooms >= " + bathrooms;
        }
        
        if(sq_feet > 0)
        {
            search_query += " AND home.sq_feet >= " + sq_feet;
        }
            
        if(garage > 0)
        {
            search_query += " AND home.garage_cap >= " + garage;
        }
        
        if(acreage > 0)
        {
            search_query += " AND home.acreage >= " + acreage;
        }
        
        search_query += " AND home.year >= " + min_year;
        
        if(max_year != 2000)
        {
            search_query += " AND home.year <= " + max_year;
        }
        
        if(home_type.length > 0)
        {
            search_query += " AND home.home_type = '" + home_type+"'"; 
        }

        if (list_class.length> 0)
        {
            search_query += " AND home.list_class = '" + list_class+"'";
        }
        
        if (district.length> 0)
        {
            search_query += " AND ext.district = '" + district +"'";
            this.ext_search = true;
        }
        //create location section

        if ($('city_name') != null && $('city_name').value != '')
        {
            var re = new RegExp('^[0-9]{5}$');
            if ($('city_name').value.match(re))
            {
                search_query += " AND home.zip = '"+$('city_name').value+"'";
            }
            else
            {
                search_query += " AND home.city like '"+$('city_name').value+"%'";
            }
        }

        var city = getSelectedCity();
        if(city == null)
        {
            var county = getSelectedCounty();
            if(county != null)
            {
//                if (county.cities.length < 100)
//                {
                    search_query += " AND home.zip IN (";
                    var cities = county.cities;
                    var zips = new Array();
                    for(var i = 0; i < cities.length; i++)
                    {
                        zips[i] = cities[i].zip;
                    }
                    
                    search_query += zips.join(',') + ')';
//                }
            }
        }
        else
        {
            search_query += " AND home.zip = " + city.zip;
        }
        return search_query;
    },
    appendQueryValue:function(name_value, value_array)
    {
	//part address stuff: parse the actual data out of the query
	if (name_value.substring(0, 6) == "(home.")
	{
		//There could be multiple clauses, but just look at the first to find the value
		value = name_value.match(/'%(.*?)%'/)[1];

		//Clean it up and save it
		value = value.replace(/%/, " ");
		name = "part_address";
		value_array[name] = value;

		//Get the list of fields we're searching (street/community/subdivision/etc)
		fields = name_value.match(/home.(.+?)\b/g);
		for (i = 0; i < fields.length; i++)
			fields[i] = fields[i].substring(5);

		if (fields)
			value_array['part_address_fields'] = fields.join(',');
	}

        var start = 0;
        var index = name_value.indexOf(' ', start);
        var name = name_value.substring(5, index);
        
        start = name.length+5+1;
        index = name_value.indexOf(' ', start);
        var op = name_value.substring(start, index);
        
        start += op.length + 1;
        var value = name_value.substring(start, name_value.length);
        
        if(name == 'price' || name == 'year')
        {
            if(op == '>=')
            {
                name = 'min_'+name;
            }
            else
            {
                name = 'max_'+name;
            }
            
        }
        else if(name == 'garage_cap')
        {
            name = 'garage';
        }
        else if(name == 'home_type')
        {
            value = value.substring(1, value.length-1);
        }
        else if(name == 'list_class')
        {
            value = value.substring(1, value.length-1);
        }
        else if(name == 'istrict')
        {
            name = 'district';
            value = value.substring(1, value.length-1);
        }
        
        value_array[name] = value;
    },
    setSearchValues:function(query)
    {
        var values = new Array();
        Object.extend(values, this.defaults);
        if(query != null)
        {
            var parts = query.split(' AND ');
            var newValues = new Array();
            for(var i = 0;i < parts.length; i++)
            {
                this.appendQueryValue(parts[i], newValues);
            }
            
            Object.extend(values, newValues);
        }
        
        util_setSelectValue($('min_price'), values.min_price);
        util_setSelectValue($('max_price'), values.max_price);
        util_setSelectValue($('bedrooms'), values.bedrooms);
        util_setSelectValue($('bathrooms'), values.bathrooms);
        util_setSelectValue($('sq_feet'), values.sq_feet);
        util_setSelectValue($('garage'), values.garage);
        util_setSelectValue($('min_year'), values.min_year);
        util_setSelectValue($('max_year'), values.max_year);
        util_setSelectValue($('home_type'), values.home_type);
        util_setSelectValue($('list_class'), values.list_class);
        if ($('district') != null)
        {
            util_setSelectValue($('district'), values.district);
        }
        util_setSelectValue($('acreage'), values.acreage);
        
	if ($('part_address_fields') && values.part_address_fields)
	{
		$('part_address_fields').value = values.part_address_fields;
	}
	if ($('part_address') && values.part_address)
	{
		$('part_address').value = values.part_address;
	}

        $('home_num').value = "";
        
        this.setTablePrefix(null);
    },
    getSummaryTable:function(query)
    {
        var summary = document.createElement("table");
        summary.cellpadding=0;
        summary.cellspacing=0;
        summary.border=0;
        summary.className = 'cdgrey';
        var min_price = $('min_price').options[$('min_price').selectedIndex].value;
        var max_price = $('max_price').options[$('max_price').selectedIndex].value;
        var bedrooms = $('bedrooms').options[$('bedrooms').selectedIndex].value;
        var bathrooms = $('bathrooms').options[$('bathrooms').selectedIndex].value;
        var garage = $('garage').options[$('garage').selectedIndex].value;
        var min_year = $('min_year').options[$('min_year').selectedIndex].value;
        var max_year = $('max_year').options[$('max_year').selectedIndex].value;
        var home_type = $('home_type').options[$('home_type').selectedIndex].value;
        var list_class = $('list_class').options[$('list_class').selectedIndex].value;
        var district = '';
        if ($('district') != null)
            district = $('district').options[$('district').selectedIndex].value;
        var sq_feet = $('sq_feet').options[$('sq_feet').selectedIndex].value;
        var acreage = $('acreage').options[$('acreage').selectedIndex].value;
        
        var search_type = this.getTablePrefix();
        
        // Set min and max price in the save search pannel
        var format = new NumberFormat(min_price);
        format.setCurrency(true);        
        var price = format.toFormatted();
        format = new NumberFormat(max_price);
        format.setCurrency(true);
        price += " - " + format.toFormatted();
    
        var tr = summary.insertRow(0);
        this.appendSummaryCells('Price:&nbsp;', price, tr, 3);
        
        tr = summary.insertRow(1);
        this.appendSummaryCells('Square feet:&nbsp;', (sq_feet  == '0')? 'Any':sq_feet, tr, 3);
        
        tr = summary.insertRow(2);
        this.appendSummaryCells('Bedrooms:&nbsp;', (bedrooms == '0')? 'Any':bedrooms, tr);
        this.appendSummaryCells('Baths:&nbsp;', (bathrooms == '0')? 'Any':bathrooms, tr);
        
        tr = summary.insertRow(3);
        this.appendSummaryCells('Garage Size:&nbsp;', (garage == '0')? 'Any':garage+'+ cars', tr);
        this.appendSummaryCells('Lot Size:&nbsp;', (acreage == '0')? 'Any':acreage+'+ acres', tr);
        
        tr = summary.insertRow(4);
        var year = '';
        if (min_year == 1)
        {
            if (max_year == 2000)
            {
                year = "Any";
            }
            else
            {
                year = "before "+max_year;
            }
        }
        else
        {
            year = "after "+min_year;
            if (max_year != 2000)
            {
                year += " - before "+max_year;
            }
        }
        this.appendSummaryCells('Year Built:&nbsp;', year, tr, 3);
        
        tr = summary.insertRow(5);
        this.appendSummaryCells('Home Type:&nbsp;', (home_type == '')? "Any":home_type, tr, 3);
        
        tr = summary.insertRow(6);
        this.appendSummaryCells('List Class:&nbsp;', (list_class == '')? "Any":list_class, tr, 3);

        tr = summary.insertRow(7);
        this.appendSummaryCells('Search For:&nbsp;', $(search_type+'_name').value, tr, 3);
                
        if ($('district') != null)
        {
            tr = summary.insertRow(8);
            this.appendSummaryCells('School Dist:&nbsp;', (district == '')? "Any":district, tr, 3);
        }

        return summary;
    },
    /*** end custom methos overwrites ***/
    appendSummaryCells:function(name, value, tr, colspan)
    {
        var c1 = document.createElement("td");
        c1.className = 'form_label';
        c1.style.fontWeight = 'bold';
        c1.setAttribute("valign", "top");
        c1.innerHTML = name;
        
        var c2 = document.createElement("td");
        c2.className = 'form_text';
        c2.innerHTML = value;
        
        if(colspan != null)
        {
            c2.setAttribute("colspan", colspan);
        }
        
        tr.appendChild(c1);
        tr.appendChild(c2);
    },
    doSearch:function()
    {
        var home_num = $('home_num').value;
        var ajax_request = "/ajax/search_stats.php?";
    
        $('resultsListContent').scrollTop = 0;
        $('resultsListContent').style.display = "none";
        $('loading_div').style.display = "none";
        _mdc.hideDisclaimers();
        
        this.table_prefix = this.getTablePrefix();
        
        if(this.table_prefix == null)
        {
            alert("Please select a listing source");
            return false;
        }
        this.homeQuery = 'search_query='+escape(this.createQuery());
        if (this.ext_search)
        {
            ajax_request = "/ajax/search_stats_ext.php?";
        }
        
        $(this.table_prefix).style.display = "block";
        
        if(home_num.length > 0)
        {
            this.homeQuery = "home_id=" + home_num + "&table_prefix=" + this.table_prefix+"&forget=1";
            GDownloadUrl(ajax_request + this.homeQuery, this.processStatsResponse.bind(this));
            return;
        }
        
        
        var bounds = _mdc.map.getBounds();
        var center = _mdc.map.getCenter();
        var ne_bound = bounds.getNorthEast();
        var sw_bound = bounds.getSouthWest();
        
        var search_query = this.createQuery();
      
        if (this.city_id_filter && this.city_id_filter.length>0)  // a field that holds actual city id 
        {
            this.homeQuery += "&city="+this.city_id_filter+"&no_geo=true";
        } 
        else if (this.filter_on_city)  // city and county drop downs
        { 
            var county_id = -1;
            var city_id = -1;
            var county = getSelectedCounty();
        
            if (county != null && county.updateCity != -1)
            {
                this.homeQuery += "&city=" + county.updateCity + "&county="+county.id;
            }
            else
            {
                var city = getSelectedCity();
                if(city != null)
                {
                    this.homeQuery += "&city=" + city.id;
                }
                if(county != null)
                {
                    this.homeQuery += "&county=" + county.id;
                }
            }
        }

        
        this.homeQuery += "&center_lng=" + center.lng() + "&center_lat="+center.lat() + "&zoom=" + _mdc.map.getZoom();
        this.homeQuery += "&min_lng=" + sw_bound.lng() + "&min_lat=" + sw_bound.lat() + "&max_lng=" + ne_bound.lng() + "&max_lat=" + ne_bound.lat() + "&table_prefix=" + this.table_prefix;
        this.homeQuery += "&forget=1";
        if (this.no_geo)
        {
            this.homeQuery += "&no_geo=true&";
        }
    
        if (this.only_featured)
        {
            this.homeQuery += "&featured=true";
        }
        GDownloadUrl(ajax_request + this.homeQuery, this.processStatsResponse.bind(this));
    },
    processStatsResponse:function(data, responseCode)
    {
        _mdc.clearHomes();
        var xml = GXml.parse(data);
        var total = this.parseStatsResponse(xml.documentElement);
        var ajax_request = "/ajax/search_results.php?";
        if (this.ext_search)
        {
            ajax_request = "/ajax/search_results_ext.php?";
        }
    
        var max = parseInt($(this.table_prefix+"_max_listings").value, 10);
                
        this.show_summary = true;
        $('load_percent').innerHTML = total;        
        $('loading_div').style.display = "block";
        $('result_count').innerHTML = 'Searching ...';
        $('result_pages').innerHTML = '&nbsp;';
        var sort_by = $('sort_by');
        var value = sort_by.options[sort_by.selectedIndex].value;

        if(value.length == 0)
        {
            GDownloadUrl(ajax_request+"start="+this.search_start+"&max="+max+"&" + this.homeQuery, this.parseSearchResponse.bind(this));
        }
        else
        {
            GDownloadUrl(ajax_request+"start="+this.search_start+"&max="+max+"&sort_by_order="+value+'&'+this.homeQuery, this.parseSearchResponse.bind(this));
        }
        
        this.search_start = 0;
    },
    setDevelopmentsOnly:function()
    {
        var ajax_request = "/ajax/search_stats.php?";
        
        this.table_prefix = this.getTablePrefix();

        this.homeQuery = "developments_only=1&table_prefix="+this.table_prefix;//'search_query='+escape(this.createQuery());
        
        if(typeof(_mdc) != "undefined")
        {
            var bounds = _mdc.map.getBounds();
            var center = _mdc.map.getCenter();
            var ne_bound = bounds.getNorthEast();
            var sw_bound = bounds.getSouthWest();
            this.homeQuery += "&center_lng=" + center.lng() + "&center_lat="+center.lat() + "&zoom=" + _mdc.map.getZoom();
            this.homeQuery += "&min_lng=" + sw_bound.lng() + "&min_lat=" + sw_bound.lat() + "&max_lng=" + ne_bound.lng() + "&max_lat=" + ne_bound.lat() + "&table_prefix=" + this.table_prefix;
        }
        else
        {
            this.homeQuery += "&center_lng=" + center_lng + "&center_lat="+ center_lat + "&zoom=" + center_zoom + "&table_prefix=" + this.table_prefix;
        }
        
        GDownloadUrl(ajax_request + this.homeQuery, this.dumpStatsResponse.bind(this));
    },
    setCityFilter : function(bool_filter)
    {
        this.filter_on_city = bool_filter;
    },
    setDefaultSearch:function()
    {
        var home_num = $('home_num').value;
        
        this.table_prefix = this.getTablePrefix();
        
        if(this.table_prefix == null)
        {
            alert("Please select a listing source");
            return false;
        }
            
        var ajax_request = "/ajax/search_stats.php?";
        if(home_num.length > 0)
        {
            this.homeQuery = "home_id=" + home_num + "&table_prefix=" + this.table_prefix;
            GDownloadUrl(ajax_request + this.homeQuery, this.dumpStatsResponse.bind(this));
            return;
        }
        
        this.homeQuery = 'search_query='+escape(this.createQuery());
        
        if (this.ext_search)
        {
            ajax_request = "/ajax/search_stats_ext.php?";
        }
        

        if (this.filter_on_city)        
        {
            var county_id = -1;
            var city_id = -1;
            var county = getSelectedCounty();
        
            if (county != null && county.updateCity != -1)
            {
                this.homeQuery += "&city=" + county.updateCity+ "&county="+county.id;
            }
            else
            {
                var city = getSelectedCity();
                if(city != null)
                {
                    this.homeQuery += "&city=" + city.id;
                } 
                if(county != null)
                {
                    this.homeQuery += "&county=" + county.id;
                }
            }
       } 
        
        if(typeof(_mdc) != "undefined")
        {
            var bounds = _mdc.map.getBounds();
            var center = _mdc.map.getCenter();
            var ne_bound = bounds.getNorthEast();
            var sw_bound = bounds.getSouthWest();
            this.homeQuery += "&center_lng=" + center.lng() + "&center_lat="+center.lat() + "&zoom=" + _mdc.map.getZoom();
            this.homeQuery += "&min_lng=" + sw_bound.lng() + "&min_lat=" + sw_bound.lat() + "&max_lng=" + ne_bound.lng() + "&max_lat=" + ne_bound.lat() + "&table_prefix=" + this.table_prefix;
        }
        else
        {
            this.homeQuery += "&center_lng=" + center_lng + "&center_lat="+ center_lat + "&zoom=" + center_zoom + "&table_prefix=" + this.table_prefix;
        }
        if (this.no_geo)
        {
            this.homeQuery += "&no_geo=true";
        }
        
        GDownloadUrl(ajax_request + this.homeQuery, this.dumpStatsResponse.bind(this));
    },

    setDefaultQuery:function(query, params)
    {
        var ajax_request = "/ajax/search_stats.php?search_query="+escape(query)+"&"+params;
        GDownloadUrl(ajax_request, this.dumpStatsResponse.bind(this));
    },

    dumpStatsResponse:function(data, responseCode)
    {
        this.search_start = 0;
        jumpToSearch();
    },
    getNextResultsPage:function(start)
    {
        this.show_summary = false;
        $('resultsListContent').scrollTop = 0;
        $('resultsListContent').style.display = "none";
        $('loading_div').style.display = "block";
        $('result_count').innerHTML = 'Loading ...';
        $('result_pages').innerHTML = '&nbsp;';
        
        _mdc.clearHomes();
                
        var max = parseInt($(this.table_prefix+"_max_listings").value, 10);
        var sort_by = $('sort_by');
        var value = sort_by.options[sort_by.selectedIndex].value;
        var ajax_search;
        if (this.ext_search)
        {
            ajax_search = "/ajax/search_results_ext.php";
        }
        else
        {
            ajax_search = "/ajax/search_results.php";
        }
        if(value.length == 0)
        {
            GDownloadUrl(ajax_search+"?start="+start+"&max="+max+"&" + this.homeQuery, this.parseSearchResponse.bind(this));
        }
        else
        {
            GDownloadUrl(ajax_search+"?start="+start+"&max="+max+"&sort_by_order="+value+'&'+this.homeQuery, this.parseSearchResponse.bind(this));
        }
    },
    parseStatsResponse:function(home_avg)
    {
        var total = home_avg.getAttribute("total");
        var bathrooms = home_avg.getAttribute("bathrooms");
        var bedrooms = home_avg.getAttribute("bedrooms");
        var price = home_avg.getAttribute("price");
        var price_psf = home_avg.getAttribute("price_psf");
        var sq_feet = home_avg.getAttribute("sq_feet");
        
        $('stats_listing_count').innerHTML = total;
        $('stats_price').innerHTML = price;
        $('stats_price_psf').innerHTML = price_psf;
        $('stats_beds').innerHTML = bedrooms;
        $('stats_baths').innerHTML = bathrooms;
        $('stats_sq_feet').innerHTML = sq_feet;
        
        //remove table values
        this.search_results.clearResultsList();
    
        if(window.switchTab != null)
        {
            switchTab("results", "info");
        }
        
        return parseInt(total, 10);
    },
    parseSearchResponse:function(data, responseCode)
    {
        var xml = GXml.parse(data);
        var elHomes = xml.documentElement;
        
        this.search_results.clearResultsList();
        
        var start = parseInt(elHomes.getAttribute("start"));
        var homeCount = parseInt(elHomes.getAttribute("count"));
        var total = parseInt($('stats_listing_count').innerHTML);
                var top = (total < start+homeCount) ? total : start+homeCount;
        if(total > 0)
        {
            $('result_count').innerHTML = (start+1)+' - '+top+' of '+total;
        }
        else
        {
            $('result_count').innerHTML = '0 homes found';
        }
        var max = parseInt($(this.table_prefix+"_max_listings").value, 10);
        if(max >= total)
        {
            $('result_pages').innerHTML = '&nbsp;';
        }
        else
        {
            var inner = '';
            var pages = Math.ceil(total/max);
            var page = (start/max) + 1;
            
            var chapters = Math.ceil(pages/4);
            var chapter = Math.ceil(page/4)-1;
        
            var pageEnd = pages+1;
            var pageStart = (chapter*4)+1;
            if(pageStart + 4 <= pages)
            {
                pageEnd = pageStart+4;
            }
        
            for(var i = pageStart; i < pageEnd; i++)
            {
                var startCount = (i-1)*max;
                if(i == page)
                {
                    inner += '<a href="next_results.html" onclick="_search.getNextResultsPage('+startCount+'); return false" class="body_contrast_color unlink">'+i+'</a>&nbsp;';
                }
                else
                {
                    inner += '<a href="next_results.html" onclick="_search.getNextResultsPage('+startCount+'); return false" class="corange unlink">'+i+'</a>&nbsp;';
                }
            }
            if(pageStart > 1)
            {
                var startCount = (pageStart-2)*max;
                inner = '[&nbsp;<a href="next_results.html" onclick="_search.getNextResultsPage('+startCount+'); return false" class="corange unlink">prev</a>&nbsp;' + inner;
            }
            else
            {
                inner = '[&nbsp;' + inner;
            }
            
            if(pageEnd <= pages)
            {
                var startCount = (pageEnd-1)*max;
                inner += '<a href="next_results.html" onclick="_search.getNextResultsPage('+startCount+'); return false" class="corange unlink">next</a>&nbsp;]';
            }
            else
            {
                inner += '&nbsp;]';
            }
            $('result_pages').innerHTML = inner;
        }
        
        var homes = elHomes.getElementsByTagName("home");
        if(homes.length > 0)
        {
			// check to see if tis is relevence based.
			var relevence = homes[0].getAttribute('relevence');
			if (relevence != null && relevence.length > 0)
			{
				$('sorted_by_relevence').style.display='inline';
				$('sort_by_table').style.display = 'none';
			}
			else
			{
				$('sorted_by_relevence').style.display='none';
				$('sort_by_table').style.display = '';
			}
            //<home id="42" mls="589006" price="430000" beds="3" baths="3" sold="0" lat="40.6288" lng="-111.877"/>
            for (var i = 0; i < homes.length; i++) 
            {
                var home = homes[i];
                var address = home.getElementsByTagName("address")[0];
    
                var street = null;
                if(address.firstChild != null)
                {
                    street = address.firstChild.nodeValue;
                }
                
                var photo_url = null;
                var photo = home.getElementsByTagName("photo");
                if(photo != null && photo.length > 0)
                {
                    if(photo[0].firstChild != null)
                    {
                        photo_url = photo[0].firstChild.nodeValue;
                    }
                }
                
                var agentInfo = null;
                var listing_agent = home.getElementsByTagName("listing_agent");
                if(listing_agent != null && listing_agent.length > 0 && listing_agent[0].firstChild != null)
                {
                    var la = "";
                    var lo = "";
                    var my_listing = listing_agent[0].getAttribute("my_listing");
                   
                    
                    la = listing_agent[0].firstChild.nodeValue;
                    var listing_office = home.getElementsByTagName("listing_office");
                    if(listing_office != null && listing_office.length > 0)
                    {
                        var my_brokerage = listing_office[0].getAttribute("my_brokerage");
                        {
                        }
                        if(listing_office[0].firstChild != null)
                        {
                            lo = listing_office[0].firstChild.nodeValue
                        }
                    }
                    
                    agentInfo = new Agent(0, la, lo, my_listing == 'true', my_brokerage == 'true');
                }
                
                var addr = new Address(street, address.getAttribute("city"), address.getAttribute("state"), address.getAttribute("zip"), address.getAttribute("lat"), address.getAttribute("lng"));
                var info = new HomeInfo(home.getAttribute("listing_type"), home.getAttribute("mls"), home.getAttribute("price"), home.getAttribute("realtor"), 
                                        home.getAttribute("beds"), home.getAttribute("baths"), home.getAttribute("sq_feet"), home.getAttribute("sold"), 
                                        home.getAttribute("open_house"), home.getAttribute("relevence"));

                if (info.listing_type == '23') // developments
                {
                    info.develInfo(home.getAttribute("price_range"), home.getAttribute("title"));
                }
                
                if(photo_url != null)
                {
                    info.photos[0] = new Image();
                    info.photos[0].src = photo_url;
                }
                var snhs = home.getElementsByTagName("snhs");
                if (snhs != null && snhs.length > 0)
		{
                    snhs = snhs[0];
                    info.mls_num = snhs.getAttribute("snhs_msl_num");
                    info.snhs = {"advertiser":snhs.getAttribute("advertiser"), "build_status":snhs.getAttribute("build_status"), "re_header":snhs.getAttribute("re_header"), "community":snhs.getAttribute("community"), "high_school":snhs.getAttribute("high_school"), "jr_high":snhs.getAttribute("jr_high"), "elementary":snhs.getAttribute("elementary"), "bldrweb":snhs.getAttribute("bldrweb"), "profile":snhs.getAttribute("profile"), "spcl_msg":snhs.getAttribute("spcl_msg"), "commweb":snhs.getAttribute("commweb"), "snh_header":snhs.getAttribute("snh_header"), "half_baths":snhs.getAttribute("half_baths"), "list_class":snhs.getAttribute("list_class")};
                }
                
                var marker = new Home(home.getAttribute("id"), info, addr, agentInfo);
                
                _mdc.mapHomes[_mdc.mapHomes.length] = marker;
            }
        }
        
        _mdc.mapHomesCount = _mdc.mapHomes.length;
        
        //add to the list as is
        for(var i = 0; i < _mdc.mapHomes.length; i++)
        {
            var marker = _mdc.mapHomes[i];
            this.search_results.addListViewRow(marker);
        }
        
        _mdc.mapHomes.sort(_mdc.sortHomes);

        homeBounds = null;
        homesChecked = 0;
        for(var i = 0; i < _mdc.mapHomes.length; i++)
        {
            var marker = _mdc.mapHomes[i];
            var newMarker = marker.createMarker(i);
            _mdc.map.addOverlay(newMarker);
            var latlng = newMarker.getPoint();
            if ((latlng.lat() > 15) && (latlng.lng() < -30)) // make sure we are in the north western corner of the earth
            {
                homesChecked++;
                if (homeBounds == null)
                {
                    homeBounds = new MapBounds(latlng);
                }
                else
                {
                    homeBounds.extend(latlng);
                }
            }
    
        }
        if (homeBounds != null)
        {
            var mapBounds = _mdc.map.getBounds();
            var msw = mapBounds.getSouthWest();
            var mne = mapBounds.getNorthEast();

            if (this.no_geo)
//          if ((mne.lat()-homeBounds.maxLat <0) || (mne.lng()-homeBounds.maxLng < 0) || (homeBounds.minLat-msw.lat() < 0) || (homeBounds.minLng-msw.lng()<0))
            {
                var center = _mdc.map.getCenter();
                var skew = (mne.lat() - center.lat())/(mne.lat()-msw.lat());
                _mdc.map.setCenter(new GLatLng(homeBounds.centerLat(skew),homeBounds.centerLng()));
           
		var curZoom = 15;
		_mdc.zoomMap(curZoom); 
//                var curZoom = _mdc.map.getZoom();
                for(var i=0; i <curZoom; i++)
                {
                    var mapBounds = _mdc.map.getBounds();
                    var msw = mapBounds.getSouthWest();
                    var xdelta = homeBounds.minLat-msw.lat();
                    var ydelta = homeBounds.minLng-msw.lng();
                    if (ydelta >0 && xdelta > 0)
                    {
                        break;
                    }
                    curZoom--;
                    _mdc.zoomMap(curZoom);
                }
            }
        }
        // restore the default to honor the map boundries.
//        this.no_geo = false;
        

        //show house from single search
        if($('home_num').value.length > 0 || _mdc.mapHomesCount == 1)
        {
            var home = _mdc.mapHomes[0];
		if (home)
            home.centerMap(home.marker);
        }
        
        $('loading_div').style.display = "none";
                $('resultsListContent').style.display = 'block';
            setTimeout("$('resultsListContent').style.display = 'block';", 100);    
        if(this.show_summary == true)
        {
            toggleResultsSummary(true);
        }
        
        if(this.last_home_detail != null)
        {
            var home = _mdc.findHomeById(this.last_home_detail);
            if(home != null)
            {
                home.doMouseClick(home.marker);
            }
        }
        
        this.last_home_detail = null;
    },
    getHomeDetails:function(id)
    {
        var home = _mdc.findHomeById(id);
        if(home != null)
        {
            if(home.info.year != 0)
            {
                _mdc.currentHome = home;
            
                //show the details
                switchTab("info", null);
                return;
            }
        
            GDownloadUrl("/ajax/home_details.php?id=" + id + "&table_prefix=" + this.table_prefix , this.parseDetailsResponse.bind(this));
        }
    },
    getHomeDetails2:function(id)
    {
        var home = _mdc.findHomeById(id);
        if(home != null)
        {
            if(home.info.year != 0)
            {
                _mdc.currentHome = home;
            
                //show the details
                switchTab("info", null);
                return;
            }
        
            GDownloadUrl("/ajax/home_details2.php?id=" + id + "&table_prefix=" + this.table_prefix , this.parseDetailsResponse.bind(this));
        }
    },
    parseDetailsResponse:function(data, responseCode)
    {
        var xml = GXml.parse(data);
        var home = xml.documentElement;
        if(home.childNodes.length == 0)
        {
            return false;
        }
        else if(home.nodeName == "parsererror")
        {
            alert(home.firstChild.nodeValue);
            return false;
        }
    
        var home_id = home.getAttribute("id");
        var ohome = _mdc.findHomeById(parseInt(home_id, 10));
        ohome.info.acres = home.getAttribute("acres");
        ohome.info.year = parseInt(home.getAttribute("year"), 10);
        var home_type = home.getElementsByTagName("home_type")[0];
        var list_class = home.getElementsByTagName("list_class")[0];
        var district = home.getElementsByTagName("district")[0];
        var home_style = home.getElementsByTagName("home_style")[0];
        var home_address = home.getElementsByTagName("home_address")[0];
        var photos = home.getElementsByTagName("photo");
        var open_house = home.getElementsByTagName("open_house")[0];
        var feature = home.getElementsByTagName("feature")[0];
        if (feature != null)
        {
                    ohome.info.feature = feature.firstChild.nodeValue;
        }
        

        
        var photo_array = new Array();
        for(var i = 0;i < photos.length; i++)
        {
            var photo = photos[i];
            var type = photo.getAttribute("type");
            if(type != "virtual")
            {
                var img = document.createElement("img");//new Image();
                img.src = photo.firstChild.nodeValue;
                img.setAttribute("type", type);
                photo_array[i] = img;
            }
            else
            {
                photo_array[i] = photo.firstChild.nodeValue;
            }
        }
        
        ohome.info.addPhotos(photo_array);
        
        if(home_style.firstChild != null)
        {
            ohome.info.home_style = home_style.firstChild.nodeValue;
        }
        
        if(home_type.firstChild != null)
        {
            ohome.info.home_type = home_type.firstChild.nodeValue;
        }

        if(list_class.firstChild != null)
        {
            ohome.info.list_class = list_class.firstChild.nodeValue;
        }

        if(district != null && district.firstChild != null)
        {
            ohome.info.district = district.firstChild.nodeValue;
        }

        if (open_house != null && open_house.firstChild != null)
        {
            ohome.info.open_house_start = open_house.getAttribute("start");
            ohome.info.open_house_end = open_house.getAttribute("end");
            ohome.info.open_house_comment = open_house.firstChild.nodeValue;
        }

        if (home_address.firstChild != null) 
        {
            ohome.info.home_address = home_address.firstChild.nodeValue;
        }
        
        _mdc.currentHome = ohome;
        
        //show the details    
        switchTab("info", "comps");
        
        GDownloadUrl("/ajax/comp_results.php?home_id=" + _mdc.currentHome.id +"&table_prefix=" + this.table_prefix , this.parseCompsResponse.bind(this));
    },
    parseCompsResponse:function(data, responseCode)
    {
        var xml = GXml.parse(data);
        var elHomes = xml.documentElement;
        
        var comps = new Array();
        
        var homes = elHomes.getElementsByTagName("home");
        for (var i = 0; i < homes.length; i++) 
        {
            var home = homes[i];
            var address = home.getElementsByTagName("address")[0];
    
            var street = null;
            if(address.firstChild != null)
            {
                street = address.firstChild.nodeValue;
            }
            
            var photo_url = null;
            var photo = home.getElementsByTagName("photo");
            if(photo != null && photo.length > 0)
            {
                if(photo[0].firstChild != null)
                {
                    photo_url = photo[0].firstChild.nodeValue;
                }
            }
            
            var agentInfo = null;
            var listing_agent = home.getElementsByTagName("listing_agent");
            if(listing_agent != null && listing_agent.length > 0 && listing_agent[0].firstChild != null)
            {
                var la = "";
                var lo = "";
                
                la = listing_agent[0].firstChild.nodeValue;
                var listing_office = home.getElementsByTagName("listing_office");
                if(listing_office != null && listing_office.length > 0)
                {
                    if(listing_office[0].firstChild != null)
                    {
                        lo = listing_office[0].firstChild.nodeValue
                    }
                }
                
                agentInfo = new Agent(0, la, lo);
            }
            
            var addr = new Address(street, address.getAttribute("city"), address.getAttribute("state"), address.getAttribute("zip"), address.getAttribute("lat"), address.getAttribute("lng"));
            var info = new HomeInfo(home.getAttribute("listing_type"), home.getAttribute("mls"), home.getAttribute("price"), home.getAttribute("realtor"), 
                                    home.getAttribute("beds"), home.getAttribute("baths"), home.getAttribute("sq_feet"), home.getAttribute("sold"));
            
            if(photo_url != null)
            {
                info.photos[0] = new Image();
                info.photos[0].src = photo_url;
            }
            
            var marker = new Home(home.getAttribute("id"), info, addr, agentInfo);
            
            //marker.createCompMarker(_mdc.currentHome.id, compHomes.length);
            comps[comps.length] = marker;
            //compHomes[compHomes.length] = marker;
        }
        
        _mdc.currentHome.info.addComps(comps);
    },
    doMapIt:function(id, prefix)
    {
        this.setTablePrefix(prefix);
        _mdc.hideDisclaimers();
        $(this.table_prefix).style.display = "block";
        
        this.home_query = "home_id="+id+"&table_prefix="+this.table_prefix;
        GDownloadUrl("/ajax/search_stats.php?" + this.home_query, this.processMapItStatsResponse.bind(this));
    },
    processMapItStatsResponse:function(data, responseCode)
    {
        var xml = GXml.parse(data);
        var total = this.parseStatsResponse(xml.documentElement);
        if(total > 0)
        {
            GDownloadUrl("/ajax/search_results.php?" + this.home_query, this.parseMapItSearchResponse.bind(this));
        }
    },
    parseMapItSearchResponse:function(data, responseCode)
    {
        this.parseSearchResponse(data, responseCode);
        
        var home = _mdc.findFirstHome();
        if(home != null)
        {
            home.centerMap(home.marker);
            home.doMouseClick(home.marker);
        }
        
    },

    /* helper functions */
    getTablePrefix:function()
    {
        if(this.listing_count == 1)
        {
            if(document.search.table_prefix.disabled == false)
            {
                return document.search.table_prefix.value;
            }
            else
            {
                return null;
            }
        }
        else
        {
            var cbs = document.search.table_prefix;
            for(var i = 0; i < cbs.length; i++)
            {
                var cb = cbs[i];
                if(cb.checked == true)
                {
                    var prefix_args = cb.value.split(' ');
                    if (prefix_args.length > 1 && prefix_args[1] == "featured")
                    {
                        this.only_featured = true;
                    }
                    else
                    {
                        this.only_featured = false;
                    }
                    return prefix_args[0];
                }
            }
        }
        
        return null;
    },
    setTablePrefix:function(prefix)
    {
        this.table_prefix = prefix;
        if(this.listing_count == 1)
        {
            if(document.search.table_prefix.disabled == false)
            {
                document.search.table_prefix.checked = true;
            }
        }
        else
        {
            var cbs = document.search.table_prefix;
            for(var i = 0; i < cbs.length; i++)
            {
                var cb = cbs[i];
                if(cb.disabled == false && (prefix == null || cb.value == prefix))
                {
                    cb.checked = true;
                    if(prefix == null) break;
                }
                else
                {
                    cb.checked = false;
                }
            }
        }
    },
    updateHomeType:function()
    {
        var i = 0;
        var type = this.getTablePrefix();
        var list = null;
        for(i = 0; i < this.home_types.length; i++)
        {
            if(this.home_types[i][0] == type)
            {
                list = this.home_types[i];
                break;
            }
        }
        
        var select = $('home_type');
        if(list != null)
        {
            for(i = 1; i < list.length; i++)
            {
                select.options[i] = list[i];
            }
            
            //remove any extras
            var size = select.options.length;
            if(size > list.length)
            {
                var index = i;
                for(; i < size; i++)
                {
                    select.options[index] = null;
                }
            }
        }
    },
    updateListClass:function()
    {
        var i = 0;
        var type = this.getTablePrefix();
        var list = null;
        for(i = 0; i < this.list_classes.length; i++)
        {
            if(this.list_classes[i][0] == type)
            {
                list = this.list_classes[i];
                break;
            }
        }
        
        var select = $('list_class');
        if (select == null || select.type != 'select-one')
            return;	
        if(list != null)
        {
            for(i = 1; i < list.length; i++)
            {
                select.options[i] = list[i];
            }
            
            //remove any extras
            var size = select.options.length;
            if(size > list.length)
            {
                var index = i;
                for(; i < size; i++)
                {
                    select.options[index] = null;
                }
            }
        }
    },
    updateSchoolDist:function()
    {
        var i = 0;
        var type = this.getTablePrefix();
        var list = null;
        for(i = 0; i < this.districts.length; i++)
        {
            if(this.districts[i][0] == type)
            {
                list = this.districts[i];
                break;
            }
        }
        
        var select = $('district');
        if(list != null)
        {
            for(i = 1; i < list.length; i++)
            {
                select.options[i] = list[i];
            }
            
            //remove any extras
            var size = select.options.length;
            if(size > list.length)
            {
                var index = i;
                for(; i < size; i++)
                {
                    select.options[index] = null;
                }
            }
        }
    },
    updateTablePrefix: function()
    {
        this.updateHomeType();
        this.updateListClass();
        if ($('district') != null)
        {
            this.updateSchoolDist();
        }
        if ($('show_fsbo_agent') != null)
        {
            this.updateShowFsboAgent();
        }
    },
    updateShowFsboAgent: function()
    {
        $('show_fsbo_agent').disabled = (this.getTablePrefix() != 'fsbo');
    },
    initListings:function()
    {
        var rb = null;
        if(this.listing_count == 1)
        {
            rb = document.search.table_prefix;
            if(rb.disabled == false)
            {
                rb.checked = true;
            }
        }
        else
        {
            var tp = document.search.table_prefix;
            for(var i = 0; i < tp.length; i++)
            {
                rb = tp[i];
                if(rb.disabled == false)
                {
                    rb.checked = true;
                    break;
                }
            }
        }
        
        if(rb != null)
        {
            this.updateHomeType(rb);
            this.updateListClass(rb);
            if ($('district') != null)
            {
                this.updateSchoolDist(rb);
            }
        }
    }
}//end search

function ShowBigImage(img_el)
{
    if ($('big_image') != null)
    {
        var src = img_el.src;
        $('big_image').src= src;
        if (img_el.naturalWidth != null)
        {
            $('big_image_div').style.width = (img_el.naturalWidth + 14)+"px";
            $('big_image_div').style.height = (img_el.naturalHeight + 14)+"px";
        }
/*        else if (img_el.style.width != null)
	{
            $('big_image_div').style.width = (img_el.style.width + 14)+"px";
            $('big_image_div').style.height = (img_el.style.height + 14)+"px";
        }
        else
        {
            $('big_image_div').style.width = "500px";
            $('big_image_div').style.height = "500px";
        }
*/
        $('big_image_div').style.display='block';
    }
}

function RemoveBigImage(img_el)
{
    if ($('big_image') != null)
    {
        $('big_image').src='/shared/images/spacer.gif';
        $('big_image_div').style.display='none';
    }
}


var SearchResults = Class.create();

SearchResults.prototype=
{
    initialize: function() 
    {
        this.chooseListViewBody();
    },

    chooseListViewBody: function()
    {
        this.add_office = false;
        this.add_metrolist = false;
        this.add_minimal = false;
        this.add_wpmls = false;
        this.add_santafe = false;
        if ($('list_view_body') != null)
        {
            this.list_view = $('list_view_body');
        }
        else if ($('list_view_include_office') != null)
        {
            this.list_view = $('list_view_include_office');
            this.add_office = true;
        }
        else if ($('list_view_metrolist') != null)
        {
            this.list_view =$('list_view_metrolist');
            this.add_metrolist = true;
        }
        else if ($('list_view_minimal') != null)
        {
            this.list_view = $('list_view_minimal');
            this.add_minimal = true;
        }
        else if ($('list_view_wpmls') != null)
        {
            this.list_view = $('list_view_wpmls');
            this.add_wpmls = true;
        }
        else if ($('list_view_santafe') != null)
        {
            this.list_view = $('list_view_santafe');
            this.add_santafe = true;
        }
    },

    setDividerFunc: function(func)
    {
        this.divider_func = func;
    },

    clearResultsList:function()
    {
        this.chooseListViewBody();
        var rows = this.list_view.rows;
        
        while(rows.length != 0)
        {
            this.list_view.deleteRow(0);
        }
    },
    addListViewRow:function(markerHome)
    {
        if (typeof custom_summary_info != "undefined" && custom_summary_info == 'y')
        {
            var success = customSummaryInfo(this, markerHome);
            if (success)
            {
                return;
            }           
        }

        var tr = document.createElement("tr");//tbody.insertRow(tbody.rows.length);
        this.list_view.appendChild(tr);

        var cell0 = tr.insertCell(0);
        cell0.style.padding = "5px";
        cell0.width = "90px";

        if(markerHome.info.photos.length > 0)
        {
            if ($('big_image') != null)
            {
                cell0.innerHTML = "<a href=\"#\" onclick=\"_search.search_results.clickListView("+markerHome.id+"); return false;\"><img id=\"list_img"+markerHome.id+"\" onmouseover=\"ShowBigImage(this);\" onmouseout=\"RemoveBigImage(this);\" alt=\"\" src=\""+markerHome.info.photos[0].src+"\" width=\"77px\" height=\"56px\" border=\"0\" /></a>";
            }
            else
            {
                cell0.innerHTML = "<a href=\"#\" onclick=\"_search.search_results.clickListView("+markerHome.id+"); return false;\"><img alt=\"\" src=\""+markerHome.info.photos[0].src+"\" width=\"77px\" height=\"56px\" border=\"0\" /></a>";
            }
        }
        else
        {
            cell0.innerHTML = "<a href=\"#\" onclick=\"_search.search_results.clickListView("+markerHome.id+"); return false;\"><img alt=\"\" src=\"/shared/images/nophoto.gif\" width=\"77px\" height=\"56px\" border=\"0\" /></a>";
        }

        var format = new NumberFormat(markerHome.info.price);
        format.setCurrency(true);

        var cell1 = tr.insertCell(1);
        var format = new NumberFormat(markerHome.info.price);
        format.setCurrency(true);
        var relevence = '';
        var price_colspan = 2;
        if (markerHome.info.relevence != null && markerHome.info.relevence != '')
        {
            relevence = Math.round(markerHome.info.relevence * 100);
            relevence = "<td class=\"body_contrast_color\" style=\"font-size: 12px; padding-bottom: 2px; padding-left: 10px;\">Match: "+relevence+"%</td>";
            price_colspan =1;
        }
        var summaryInfo = "<table cellpadding=\"0\" cellspacing=\"0\">"+
                            "<tr><td colspan='"+price_colspan+"' class=\"corange\" style=\"font-size: 12px; padding-bottom: 2px;\">"+format.toFormatted()+"</td>"+relevence+"</tr>"+
                            "<tr><td  class=\"body_contrast_color\" style=\"font-size: 12px; padding-bottom: 2px;\">Beds: "+markerHome.info.bedrooms+"</td>"+
                            "<td  class=\"body_contrast_color\" style=\"font-size: 12px; padding-bottom: 2px; padding-left: 10px;\">Baths: "+markerHome.info.baths+"</td></tr>";
        format.setNumber(markerHome.info.sq_feet)
        format.setCurrency(false);
        if (!this.add_minimal)
        {
            summaryInfo += "<tr><td colspan=\"2\"><span class=\"body_contrast_color\" style=\"font-size: 12px;\">Total Sq Ft: "+format.toFormatted()+"</span></td></tr>";
        }
        summaryInfo += "</table>";
        cell1.innerHTML =  summaryInfo;

        if (this.divider_func != null)
        {
            this.divider_func();

        }
        else
        {
            cell1.style.borderBottom = "1px dotted #C1B9AC";
        }
            
        if (this.add_metrolist)
        {
            tr = document.createElement("tr");
            this.list_view.appendChild(tr);
            cell0 = tr.insertCell(0);
            cell0.colSpan = "2";
            cell0.className = "cdgrey";
            cell0.style.fontSize = "12px";
            cell0.innerHTML=markerHome.agent.office_name;
            cell0.style.paddingLeft = "5px";
            cell0.style.paddingRight = "5px";
            cell0.style.paddingBottom = "3px";

            tr = document.createElement("tr");
            this.list_view.appendChild(tr);
            cell0 = tr.insertCell(0);
            cell0.style.borderBottom = "1px dotted #C1B9AC";
            cell0.className = "cdgrey";
            cell0.style.fontSize = "12px";
            cell0.innerHTML="MLS #"+markerHome.info.mls_num;
            cell0.style.paddingLeft = "5px";
            cell0.style.paddingRight = "5px";
            cell0.style.paddingBottom = "5px";
            cell1 = tr.insertCell(1);
            cell1.style.borderBottom = "1px dotted #C1B9AC";
            cell1.className = "cdgrey";
            cell1.style.fontSize = "12px";
            cell1.align='right';
            cell1.innerHTML="<img src='/shared/images/idx_logo.gif' width='33px' height='17px'/>";
            cell1.style.paddingLeft = "5px";
            cell1.style.paddingRight = "5px";
            cell1.style.paddingBottom = "5px";
        }
    else if (markerHome.info.listing_type == '26') // pikes peak
        {
            tr = document.createElement("tr");
            this.list_view.appendChild(tr);
            cell1 = tr.insertCell(0);
            cell1.colSpan="2";
            cell1.className = "body_contrast_color";
            cell1.style.fontSize = "12px";
            cell1.align='center';
            cell1.style.paddingTop="5px";
            if (markerHome.agent.my_listing || markerHome.agent.my_brokerage)
            {
                cell1.innerHTML="<span class='body_contrast_color' style='font-size:12px'>" + markerHome.agent.office_name + "</span>";
            }
            else
            {
                cell1.innerHTML="<img src='/shared/images/ppmls_logo.jpg' class='jpg' width='114px' height='50px'/>";
            }
            cell1.style.paddingLeft = "5px";
            cell1.style.paddingRight = "5px";
            cell1.style.paddingBottom = "5px";

            if (this.divider_func != null)
            {
                this.divider_func();
            }
            else
            {
                cell0.style.borderBottom = "1px dotted #C1B9AC";
                cell1.style.borderBottom = "1px dotted #C1B9AC";

            }
        }
        else if (markerHome.info.listing_type == '14' || markerHome.info.listing_type == '68') // new metrolist
        {
            tr = document.createElement("tr");
            this.list_view.appendChild(tr);
            cell0 = tr.insertCell(0);
            cell0.colSpan = "2";
            cell0.className = "body_contrast_color";
            cell0.style.fontSize = "12px";

		//ben: quick temporary fix until Nile checks it out
            cell0.innerHTML=(markerHome && markerHome.agent ? markerHome.agent.office_name : "");

            cell0.style.paddingLeft = "5px";
            cell0.style.paddingRight = "5px";
            cell0.style.paddingBottom = "3px";
            cell0.style.paddingTop = "5px";

            tr = document.createElement("tr");
            this.list_view.appendChild(tr);
            cell0 = tr.insertCell(0);
            cell0.className = "body_contrast_color";
            cell0.style.fontSize = "12px";
            cell0.innerHTML="MLS #"+markerHome.info.mls_num;
            cell0.style.paddingLeft = "5px";
            cell0.style.paddingRight = "5px";
            cell0.style.paddingBottom = "5px";
            cell1 = tr.insertCell(1);
            cell1.className = "body_contrast_color";
            cell1.style.fontSize = "12px";
            cell1.align='right';
            cell1.style.paddingLeft = "5px";
            cell1.style.paddingRight = "5px";
            cell1.style.paddingBottom = "5px";

			if (markerHome.info.listing_type != 68 || markerHome.id.charAt(0) == '1')
			{
	            cell1.innerHTML="<img src='/shared/images/idx_logo.gif' width='33px' height='17px'/>";
			}
			else
			{
				cell1.innerHTML="&nbsp;";
			}
            if (this.divider_func != null)
            {
                this.divider_func();
            }
            else
            {
                cell0.style.borderBottom = "1px dotted #C1B9AC";
                cell1.style.borderBottom = "1px dotted #C1B9AC";

            }
//            eval ("this."+search_form_divider+"();");
//            this.list_view.innerHTML += search_form_divider;
        }
        else if (markerHome.info.listing_type == '21') // armls
        {
            tr = document.createElement("tr");
            this.list_view.appendChild(tr);
            cell1 = tr.insertCell(0);
            cell1.colSpan="2";
            cell1.className = "body_contrast_color";
            cell1.style.fontSize = "12px";
            cell1.align='center';
            cell1.style.paddingTop="5px";
            if (markerHome.agent.my_listing || markerHome.agent.my_brokerage)
            {
                cell1.innerHTML="<span class='body_contrast_color' style='font-size:12px'>" + markerHome.agent.office_name + "</span>";
            }
            else
            {
                cell1.innerHTML="<img src='/shared/images/armls_logo.png' class='png' width='76px' height='17px'/>";
            }
            cell1.style.paddingLeft = "5px";
            cell1.style.paddingRight = "5px";
            cell1.style.paddingBottom = "5px";

            if (this.divider_func != null)
            {
                this.divider_func();
            }
            else
            {
                cell0.style.borderBottom = "1px dotted #C1B9AC";
                cell1.style.borderBottom = "1px dotted #C1B9AC";

            }
        }
        else if (this.add_office)
        {
            tr = document.createElement("tr");
            this.list_view.appendChild(tr);
            cell0 = tr.insertCell(0);
            cell0.style.borderBottom = "1px dotted #C1B9AC";
            cell0.colSpan = "2";
            cell0.className = "cdgrey";
            cell0.style.fontSize = "12px";
            cell0.innerHTML="Listed with: "+markerHome.agent.office_name;
            cell0.style.paddingLeft = "5px";
            cell0.style.paddingRight = "5px";
            cell0.style.paddingBottom = "5px";
        }
        else if (markerHome.info.listing_type == '19' || markerHome.info.listing_type == '15')
        {
            tr = document.createElement("tr");
            this.list_view.appendChild(tr);
            cell1 = tr.insertCell(0);
            cell1.style.borderBottom = "1px dotted #C1B9AC";
            cell1.colSpan="2";
            cell1.className = "cdgrey";
            cell1.style.fontSize = "12px";
            cell1.align='center';
            if (markerHome.info.listing_type == '19' || markerHome.agent.my_listing || markerHome.agent.my_brokerage)
            {
                cell1.innerHTML="<span class='cgreen' style='font-size:12px;font-weight:bold;'>" + markerHome.agent.office_name + "</span>";
            }
            else
            {
                cell1.innerHTML="<img src='/shared/images/wpmls_logo.gif' width='54px' height='27px'/>";
            }
            cell1.style.paddingLeft = "5px";
            cell1.style.paddingRight = "5px";
            cell1.style.paddingBottom = "5px";
        }
        else if (this.add_santafe)
        {
            tr = document.createElement("tr");
            this.list_view.appendChild(tr);
            cell1 = tr.insertCell(0);
            cell1.style.borderBottom = "1px dotted #C1B9AC";
            cell1.colSpan="2";
            cell1.className = "cdgrey";
            cell1.style.fontSize = "12px";
            cell1.align='center';
            if ((markerHome.agent.office_name.search("Prudential Santa Fe") != -1) || (markerHome.agent.office_name == "Prudential North Valley"))
            {
                cell1.innerHTML="<span class='cdgrey' style='font-size:12px'>" + markerHome.agent.office_name + "</span>";
            }
            else
            {
                cell1.innerHTML="<img class='png' src='/shared/images/idx_logo_sf.png'/>";
            }
            cell1.style.paddingLeft = "5px";
            cell1.style.paddingRight = "5px";
            cell1.style.paddingBottom = "5px";
        }
        else if (markerHome.info.listing_type == '4') // nnrmls
        {
            tr = document.createElement("tr");
            this.list_view.appendChild(tr);
            cell1 = tr.insertCell(0);
            cell1.style.borderBottom = "1px dotted #C1B9AC";
            cell1.colSpan="2";
            cell1.className = " cdgrey";
            cell1.style.fontSize = "12px";
            cell1.align='center';
            cell1.innerHTML="<img src='/shared/images/nnrmls_reciprocity-thumb.gif'/>";
            cell1.style.paddingLeft = "5px";
            cell1.style.paddingRight = "5px";
            cell1.style.paddingBottom = "5px";
        }
        else
        {
            cell0.style.borderBottom = "1px dotted #C1B9AC";
        }
        if (markerHome.info.listing_type == '23') // Residential Developments
        {
            cell1.innerHTML = "<table cellpadding=\"0\" cellspacing=\"0\">"+
                              "<tr><td colspan=\"2\" style=\"font-size:12px;\">"+markerHome.info.title+"</td></tr>"+
                                "<tr><td colspan=\"2\" class=\"corange\" style=\"font-size: 12px; padding-bottom: 2px;\">"+markerHome.info.price_range+"</td></tr>";

        }

    },

    blueroof_divider_func: function()
    {
        var tr = document.createElement("tr");
        this.list_view.appendChild(tr);
        var cell = tr.insertCell(0);
        cell.colSpan = "2";
        var div = document.createElement("div");
        cell.appendChild(div);
        div.className="divider_blueroof";
        div.style.width="272px";
        var img = document.createElement("img");
        img.src= "/shared/images/spacer.gif";
        img.style.height="0px";
        img.height="0px";
        div.appendChild(img);
    },

    clickListView:function(id)
    {
        var home = _mdc.findHomeById(id);
        if(home != null)
        {
            home.doMouseClick(home.marker);
        }
    }
}
