// ####################################
// ########## Listing Search Object ##########
// ####################################

function ListingSearch() {
    this.MarketAreaId = null;
    this.InitBasicOptions();
    
    //#### Extended Search Properties
    this.ContactId = null;
    this.BuyerId = null;
}

function ValueChanged(value1, value2)
{
    if (value1 == null && value2 == null) {
        return false;
    }
    else if (value1 != null && value2 == null) {
        return true;    
    }
    else if (value1 == null && value2 != null) {
        return true;    
    }
    else {
        return value1 == value2 ? false : true;
    }
}

ListingSearch.prototype.SetListingTypeId = function(value) {
    if (ValueChanged(this.ListingTypeId, value)) {
        _advancedSearch.ClearAll();
        this.ListingTypeId = value;
        _advancedSearch.DisplayLeftSideLinksAndChangeSavedSearchCriteria();
    }
}

ListingSearch.prototype.SetPriceMin = function(minvalue) {  
    if (ValueChanged(this.PriceMin, minvalue)) {
	    this.PriceMin = minvalue;
	    $('minPrice').value="";
	    _advancedSearch.DisplayLeftSideLinksAndChangeSavedSearchCriteria();
    }
}

ListingSearch.prototype.SetPriceMax = function(maxvalue) {
    if (ValueChanged(this.PriceMax, maxvalue)) {
	    this.PriceMax = maxvalue;
	    $('maxPrice').value="";
	    _advancedSearch.DisplayLeftSideLinksAndChangeSavedSearchCriteria();
    }
}

ListingSearch.prototype.SetBedrooms = function(minvalue, maxvalue) {
    if (ValueChanged(this.BedMin, minvalue) || ValueChanged(this.BedMax, maxvalue)) {
	    this.BedMin = minvalue;
	    this.BedMax = maxvalue;
	    _advancedSearch.DisplayLeftSideLinksAndChangeSavedSearchCriteria();
    }
}

ListingSearch.prototype.SetBathrooms = function(minvalue, maxvalue) {
    if (ValueChanged(this.BathMin, minvalue) || ValueChanged(this.BathMax, maxvalue)) {
	    this.BathMin = minvalue;
	    this.BathMax = maxvalue;
	    _advancedSearch.DisplayLeftSideLinksAndChangeSavedSearchCriteria();
    }
}

ListingSearch.prototype.SetListingDays = function(value) {
    if (ValueChanged(this.ListingDays, value)) {
	    this.ListingDays = value;
	    _advancedSearch.DisplayLeftSideLinksAndChangeSavedSearchCriteria();
    }
}

ListingSearch.prototype.SetSqFtMin= function(minvalue) {
    if (ValueChanged(this.SqFtMin, minvalue)) {
	    this.SqFtMin = minvalue;
	    _advancedSearch.DisplayLeftSideLinksAndChangeSavedSearchCriteria();
    }
}

ListingSearch.prototype.SetSqFtMax= function(maxvalue) {
    if (ValueChanged(this.SqFtMax, maxvalue)) {
	    this.SqFtMax = maxvalue;
	    _advancedSearch.DisplayLeftSideLinksAndChangeSavedSearchCriteria();
    }
}

ListingSearch.prototype.SetYrBuiltMin= function(minvalue) {
    if (ValueChanged(this.YearBuiltMin, minvalue)) {
	    this.YearBuiltMin = minvalue;
	    _advancedSearch.DisplayLeftSideLinksAndChangeSavedSearchCriteria();
    }
}

ListingSearch.prototype.SetYrBuiltMax= function(maxvalue) {
    if (ValueChanged(this.YearBuiltMax, maxvalue)) {
	    this.YearBuiltMax = maxvalue;
	    _advancedSearch.DisplayLeftSideLinksAndChangeSavedSearchCriteria();
    }
}

ListingSearch.prototype.SetUnits = function(minvalue, maxvalue) {
    if (ValueChanged(this.UnitsMin, minvalue) || ValueChanged(this.UnitsMax, maxvalue)) {
	    this.UnitsMin = minvalue;
	    this.UnitsMax = maxvalue;
	    _advancedSearch.DisplayLeftSideLinksAndChangeSavedSearchCriteria();
    }
}

ListingSearch.prototype.SetAcres= function(minvalue, maxvalue) {
    if (ValueChanged(this.AcresMin, minvalue) || ValueChanged(this.AcresMax, maxvalue)) {
	    this.AcresMin = minvalue;
	    this.AcresMax = maxvalue;
	    _advancedSearch.DisplayLeftSideLinksAndChangeSavedSearchCriteria();
    }
}
ListingSearch.prototype.SetForeclosures= function(value) {
    if (ValueChanged(this.Foreclosures, value)) {
        this.Foreclosures = value;
	    _advancedSearch.DisplayLeftSideLinksAndChangeSavedSearchCriteria();
    }
}
ListingSearch.prototype.SetOpenHouse= function(value) {
    if (ValueChanged(this.OpenHouse, value)) {
        this.OpenHouse = value;
	    _advancedSearch.DisplayLeftSideLinksAndChangeSavedSearchCriteria();
    }
}

ListingSearch.prototype.ShowSearchCriteria = function() {	
    if (_search.Criteria == "Favorites" || _search.Criteria == "Discards") {        
        var searchOptionsTitle;
        if (_search.Criteria == "Favorites") {
            searchOptionsTitle = "Favorites";
        }
        else if (_search.Criteria == "Discards") {
            searchOptionsTitle = "Discarded";
        }

        $('spanSearchOptionsTitle').innerHTML = searchOptionsTitle;
        $('spanSearchingFor').style.display='none';
        $('divGoBackToSearch').style.display='block';
        $('divRunSearch').style.display='none';
	    $('divSaveSearch').style.display='none';
	    $('divSearchResults').style.display='none';
    	$('divSearchOptions').style.display='none';
    	$('divNotebookDashboard').style.display='block';
    }
    else {
        this.CheckMinMaxPrice();
	    var searchResults = (!(IsNullOrEmpty(this.ListingTypeId)))?"<a style=\"text-decoration:none;color:#000000;\" href=\"javascript:_search.SetListingTypeId(null);removeOption('Prop');GetListings();\"><img src=\"../App_Themes/Default/Images/Map/icon_tiny_x.png\" border=\"0\">&nbsp;&nbsp;"+this.GetListingTypeName(this.ListingTypeId)+"</a><br>":"";
	    searchResults += (this.PriceMin!=null)?"<a style=\"text-decoration:none;color:#000000;\" href=\"javascript:_search.SetPriceMin(null);removeOption('PriceMin');GetListings();\"><img src=\"../App_Themes/Default/Images/Map/icon_tiny_x.png\" border=\"0\">&nbsp;&nbsp;Min Price: $"+addCommas(this.PriceMin)+"</a><br>":"";
	    searchResults += (this.PriceMax!=null)?"<a style=\"text-decoration:none;color:#000000;\" href=\"javascript:_search.SetPriceMax(null);removeOption('PriceMax');GetListings();\"><img src=\"../App_Themes/Default/Images/Map/icon_tiny_x.png\" border=\"0\">&nbsp;&nbsp;Max Price: $"+addCommas(this.PriceMax)+"</a><br>":"";
	    searchResults += (this.BedMin!=null)?"<a style=\"text-decoration:none;color:#000000;\" href=\"javascript:_search.SetBedrooms(null,null);removeOption('Beds');GetListings();\"><img src=\"../App_Themes/Default/Images/Map/icon_tiny_x.png\" border=\"0\">&nbsp;&nbsp;"+((this.BedMax!='999' && this.BedMax != null )?this.BedMin+" - "+this.BedMax:this.BedMin)+" or more Beds</a><br>":"";
	    searchResults += (this.BathMin != null) ? "<a style=\"text-decoration:none;color:#000000;\" href=\"javascript:_search.SetBathrooms(null,null);removeOption('Baths');GetListings();\"><img src=\"../App_Themes/Default/Images/Map/icon_tiny_x.png\" border=\"0\">&nbsp;&nbsp;" + ((this.BathMax != '999' && this.BathMax != null) ? this.BathMin + " - " + this.BathMax : this.BathMin) + " or more Baths</a><br>" : "";
	    searchResults += (this.SqFtMin!=null)?"<a style=\"text-decoration:none;color:#000000;\" href=\"javascript:_search.SetSqFtMin(null);removeOption('SqFtMin');GetListings();\"><img src=\"../App_Themes/Default/Images/Map/icon_tiny_x.png\" border=\"0\">&nbsp;&nbsp;Min Sq. Feet:&nbsp;"+this.SqFtMin+"</a><br>":"";
	    searchResults += (this.SqFtMax!=null)?"<a style=\"text-decoration:none;color:#000000;\" href=\"javascript:_search.SetSqFtMax(null);removeOption('SqFtMax');GetListings();\"><img src=\"../App_Themes/Default/Images/Map/icon_tiny_x.png\" border=\"0\">&nbsp;&nbsp;Max Sq. Feet:&nbsp;"+this.SqFtMax+"</a><br>":"";
	    searchResults += (this.YearBuiltMin!=null)?"<a style=\"text-decoration:none;color:#000000;\" href=\"javascript:_search.SetYrBuiltMin(null);removeOption('YrBuiltMin');GetListings();\"><img src=\"../App_Themes/Default/Images/Map/icon_tiny_x.png\" border=\"0\">&nbsp;&nbsp;Built After:&nbsp;"+this.YearBuiltMin+"</a><br>":"";
	    searchResults += (this.YearBuiltMax!=null)?"<a style=\"text-decoration:none;color:#000000;\" href=\"javascript:_search.SetYrBuiltMax(null);removeOption('YrBuiltMax');GetListings();\"><img src=\"../App_Themes/Default/Images/Map/icon_tiny_x.png\" border=\"0\">&nbsp;&nbsp;Built Before:&nbsp;"+this.YearBuiltMax+"</a><br>":"";
	    searchResults += (this.UnitsMin != null) ? "<a style=\"text-decoration:none;color:#000000;\" href=\"javascript:_search.SetUnits(null,null);removeOption('Units');GetListings();\"><img src=\"../App_Themes/Default/Images/Map/icon_tiny_x.png\" border=\"0\">&nbsp;&nbsp;" + ((this.UnitsMax != '999' && this.UnitsMax != null) ? this.UnitsMin + " - " + this.UnitsMax : this.UnitsMin) + " or more Units</a><br>" : "";
	    searchResults += (this.AcresMin != null) ? "<a style=\"text-decoration:none;color:#000000;\" href=\"javascript:_search.SetAcres(null,null);removeOption('Acres');GetListings();\"><img src=\"../App_Themes/Default/Images/Map/icon_tiny_x.png\" border=\"0\">&nbsp;&nbsp;" + ((this.AcresMax != '999' && this.AcresMax != null) ? this.AcresMin + " - " + this.AcresMax : this.AcresMin) + " or more Acres</a><br>" : "";
        searchResults += (!(IsNullOrEmpty(this.ListingDays)))?"<a style=\"text-decoration:none;color:#000000;\" href=\"javascript:_search.SetListingDays(null);removeOption('Days');GetListings();\"><img src=\"../App_Themes/Default/Images/Map/icon_tiny_x.png\" border=\"0\">&nbsp;&nbsp;"+this.GetDaysOnMarket(this.ListingDays)+"</a><br>":"";
        searchResults += (!(IsNullOrEmpty(this.Foreclosures)))?"<a style=\"text-decoration:none;color:#000000;\" href=\"javascript:_search.SetForeclosures(null);removeOption('Foreclosures');GetListings();\"><img src=\"../App_Themes/Default/Images/Map/icon_tiny_x.png\" border=\"0\">&nbsp;&nbsp;"+this.GetForeclosures(this.Foreclosures)+"</a><br>":"";
        searchResults += (!(IsNullOrEmpty(this.OpenHouse)))?"<a style=\"text-decoration:none;color:#000000;\" href=\"javascript:_search.SetOpenHouse(null);removeOption('OpenHouse');GetListings();\"><img src=\"../App_Themes/Default/Images/Map/icon_tiny_x.png\" border=\"0\">&nbsp;&nbsp;Show Open Houses Only</a><br>":"";
        
        $('spanSearchOptionsTitle').innerHTML = "Your Search";
        $('divSearchCriteria').style.display='block';
        $('spanSearchingFor').style.display='block';
        $('divGoBackToSearch').style.display='none';
        $('divRunSearch').style.display='block';
	    $('divSaveSearch').style.display='block';
	    $('divSearchResults').innerHTML = searchResults;
	    $('divSearchResults').style.display='block';
    	$('divSearchOptions').style.display='block';
    	$('divNotebookDashboard').style.display='none';
    }

	$('spanStartHere').style.display='none';
	$('spanInstructions').style.display='none';
		
	_paging.pageNumber=1; //reset paging
}

ListingSearch.prototype.CheckMinMaxPrice = function() {
    if(!(IsNullOrEmpty($('minPrice').value))){
        this.PriceMin = $('minPrice').value.replace(/,/g,'');
        removeOption('PriceMin');
	}	
	if(!(IsNullOrEmpty($('maxPrice').value))){
        this.PriceMax = $('maxPrice').value.replace(/,/g,'');
        removeOption('PriceMax');
	}
}

ListingSearch.prototype.GetListingTypeName = function(value) { 
    switch(parseInt(value))
	    {
	    case 8:
	        return "House";
	      break    
	    case 4:
	        return "Condo";
	      break
	    case 2:
	        return "Multi";
	      break
	    case 1:
	        return "Land";
	      break
	    }
}

ListingSearch.prototype.GetDaysOnMarket = function(value) {
    switch(parseInt(value))
	    {
		case 1:
			return "Last 24 Hours";
			break
		case 7:
			return "Last Week";
			break
		case 14:
			return "Last Two Weeks";
			break
        case 30:
		case 31:
			return "Last Month";
			break
        case 60:
        case 61: 
	    case 62:
			return "Last Two Months";
		break
		case 99:
			return "All";
		break
		}
}

ListingSearch.prototype.GetForeclosures = function(value) {
    switch(parseInt(value))
	    {
		case 0:
			return "Exclude Foreclosures";
			break
		case 1:
			return "Show Only Foreclosures";
			break
		}
}

//############## Extended Listing Search ########################
//###############################################################
ListingSearch.prototype.ExecuteSavedSearch = function(searchId, searchName) { //runs a pre-defined, saved search
    toggleView('List');
    _paging.pageNumber = 1; //reset paging
    _search.InitSavedOptions(searchId, searchName);
    _advancedSearch.ClearAll();
    loadSavedSearch();
}

ListingSearch.prototype.ExecuteFavoritesSearch = function() {
    toggleView('List');
    _paging.pageNumber=1; //reset paging
    this.Criteria = "Favorites";
    this.ShowSearchCriteria();
    GetListings();
}
ListingSearch.prototype.ExecuteDiscardsSearch = function() {
    toggleView('List');
    _paging.pageNumber=1; //reset paging
    this.Criteria = "Discards";
    this.ShowSearchCriteria();
    GetListings();
}
ListingSearch.prototype.ExecuteMLSListingIDSearch = function(mlsListingId) {

    if (mlsListingId == "Enter any MLS #" || IsNullOrEmpty(mlsListingId)) {
        return;
    }
    mlsListingId = StringFunctions.trim(mlsListingId);

    this.MLSListingID = mlsListingId;
    _paging.pageNumber = 1; //reset paging
    var prevCriteria = this.Criteria;
    this.Criteria = "MLSListingID";
    GetListings();
    this.MLSListingID = null;
    this.Criteria = prevCriteria;
}

ListingSearch.prototype.ExecuteAddressSearch = function(address) {

    if (address == "House and Street #" || IsNullOrEmpty(address)) {
        return;
    }
    address = StringFunctions.trim(address);

    var queryString = _advancedSearch.GetAdvancedSearchSelections();

    var confirmSearch = true;
    if (_search.ABasicOptionIsSet() || !IsNullOrEmpty(queryString)) {
        confirmSearch = confirm("All other search criteria will be removed.\nClick OK to continue.");
        if (confirmSearch == true) {
            // Wipe out basic options
            _search.InitBasicOptions();
            $('minPrice').value = "";
            $('maxPrice').value = ""
            _advancedSearch.ClearAll();

            this.Criteria = "Address";

            _search.ShowSearchCriteria();

        }
        else {
            $("addr_id_search_txt").value = "House and Street #";
        }
    }
    if (confirmSearch) {

        if (    this.Criteria != "Address" || ( !IsNullOrEmpty(this.Address) && this.Address != address ) ) {
            _paging.pageNumber = 1; //reset paging
        }

        this.Address = address;
        GetListings();
    }
    
}   
                    
ListingSearch.prototype.ExecuteREOSearch = function(entityID, criteriaMatch) {
    this.EntityID = entityID;
    _paging.pageNumber=1; //reset paging
    if(criteriaMatch) { 
        this.Criteria = "REO";
        GetListings(); }
    else {
        GetListingsREO_All();}
}

ListingSearch.prototype.ExecuteFeaturedAgentSearch = function(marketAreaId, listAgentID) {
    toggleView('List');
    this.MarketAreaId = marketAreaId;
    this.ListAgentID = listAgentID;
    _paging.pageNumber=1; //reset paging
    this.Criteria = "FeaturedAgent";
    GetListings();
}

ListingSearch.prototype.ExecuteFeaturedBrokerSearch = function(marketAreaId, listBrokerID) {
    toggleView('List');
    this.MarketAreaId = marketAreaId;
    this.ListBrokerID = listBrokerID;
    _paging.pageNumber=1; //reset paging
    this.Criteria = "FeaturedBroker";
    GetListings(true, true);
}

//##### This is used only when buyer wants to view "Blue Label" REOs. #####
//##### For this search we disregard the existing search criteria     #####
ListingSearch.prototype.ExecuteBlueLabelSearch = function(marketAreaID, blueLabelID) {
    var confirmSearch = false;
    if (
        ((typeof(_search) != 'undefined') && _search.ABasicOptionIsSet()) ||
        ((typeof(queryString) !== 'undefined') && (queryString !== null) && (queryString !== ''))
    ) {
        if ((typeof (_blueLabelSearchWarning) !== 'undefined') && (_blueLabelSearchWarning === true)) {
            confirmSearch = true;
        } else {
            confirmSearch = confirm("All other search criteria will be removed.\nClick OK to continue.");
			confirmSearch = true;
        }

        if (confirmSearch == true) {
            // set the blueLabelSearchWarning to true
            _blueLabelSearchWarning = true;
            // Wipe out basic options
            _search.InitBasicOptions();
            $('minPrice').value = "";
            $('maxPrice').value = ""
            _advancedSearch.ClearAll();
            _search.ShowSearchCriteria();
        }
    }

    toggleView('Map');
    _paging.pageNumber = 1; //reset paging
    this.EntityID = blueLabelID;
    this.MarketAreaId = marketAreaID;
    this.Criteria = "BlueLabel";

    if ((_map !== undefined) && (_search !== undefined)) {
        _search.LatitudeMin = _map.GetMapView().BottomRightLatLong.Latitude;
        _search.LatitudeMax = _map.GetMapView().TopLeftLatLong.Latitude;
        _search.LongitudeMin = _map.GetMapView().TopLeftLatLong.Longitude;
        _search.LongitudeMax = _map.GetMapView().BottomRightLatLong.Longitude;
    }
    GetListings(true, true);
}

ListingSearch.prototype.InitBasicOptions = function() {
    this.ListingTypeId = null;
    this.PriceMin = null;
    this.PriceMax = null;
    this.BedMin = null;
    this.BedMax = null;
    this.BathMin = null;
    this.BathMax = null;
    this.AcresMin = null;
    this.AcresMax = null;
    this.UnitsMin = null;
    this.UnitsMax = null;
    this.SqFtMin = null;
    this.SqFtMax = null;
    this.YearBuiltMin = null;
    this.YearBuiltMax = null;
    this.ListingDays = null;
    this.Foreclosures = null;
    this.OpenHouse = null;
    this.LatitudeMin = null;
    this.LatitudeMax = null;
    this.LongitudeMin = null;
    this.LongitudeMax = null;
    this.SearchID = null;
    this.SearchName = null;
    this.MLSListingID = null;
    this.Address = null;
    this.EntityID = null; // (will be 0 if not an REO property)
    //this.CustomFeature = null;
    //this.CustomFeatureRange = null;
    this.Criteria = "Basic";
    this.SavedSearchCriteriaChanged = false;
}

ListingSearch.prototype.ABasicOptionIsSet = function() {
    return ( 
    this.ListingTypeId != null ||
    this.PriceMin != null ||
    this.PriceMax != null ||
    this.BedMin != null ||
    this.BedMax != null ||
    this.BathMin != null ||
    this.BathMax != null ||
    this.AcresMin != null ||
    this.AcresMax != null ||
    this.UnitsMin != null ||
    this.UnitsMax != null ||
    this.SqFtMin != null ||
    this.SqFtMax != null ||
    this.YearBuiltMin != null ||
    this.YearBuiltMax != null ||
    this.ListingDays != null ||
    this.Foreclosures != null ||
    this.OpenHouse != null ||
    this.SearchID != null ||
    this.SearchName != null ||
    this.EntityID != null );
}


ListingSearch.prototype.InitSavedOptions = function(searchID, searchName) {
    this.InitBasicOptions();
    this.SearchName = unescape(searchName);
    this.SearchID = searchID;
    this.Criteria = "Saved";
    this.SavedSearchCriteriaChanged = false;
}
