//
// Filter Criteria values and methods
//

function Filter () {
    this.tbPriceF = MINPRICE_DEFAULT;
    this.tbPriceT = MAXPRICE_DEFAULT;
    this.category = "";
    this.loadCriteria();
}

Filter.prototype.getPriceMin = function() {
    return this.tbPriceF;
}

Filter.prototype.getPriceMax = function() {
    return this.tbPriceT;
}

Filter.prototype.filterOnCategory = function(p_cat) {
    this.category = p_cat;
}

Filter.prototype.sliderCB = function(min, max) {
    this.tbPriceF = min;
    this.tbPriceT = max;
}

function setSelectWidget( id, val )
{
    var elm = document.getElementById( id );
    var opts = elm.options;
    for ( i=0; i<opts.length; i++ )
    {
        if ( opts[i].value == val )
        {
            elm.selectedIndex = i;
            break;
        }
    }
}

function setCheckboxWidget( id, bval )
{
    var elm = document.getElementById( id );
    elm.checked = bval;
}

function setCurrencyWidget( id, val )
{
    var elm = document.getElementById( id );
    elm.value = formatPrice( val + '' );
}

/**
 * Unloads the Filter criteria from the page form
 */
Filter.prototype.unloadCriteria = function() {
    this.tbPriceMin = document.getElementById("txt_price_from").value.replace("$","").replace(/,/g,"");
    this.tbPriceMax = document.getElementById("txt_price_to").value.replace("$","").replace(/,/g,"");
	this.tbBeds = document.getElementById("ddlBedrooms").value;
	this.tbBaths = document.getElementById("ddlBathrooms").value;
	this.tbSqFeet = document.getElementById("ddlSquareFeet").value;
	this.tbAcreage = document.getElementById("ddlAcreage").value;
	this.tbYearsOld = document.getElementById("ddlYearBuilt").value;
	this.types1 = document.getElementById("cblTypes_0").checked;
	this.types2 = document.getElementById("cblTypes_1").checked;
	this.types3 = document.getElementById("cblTypes_2").checked;
	this.types4 = document.getElementById("cblTypes_3").checked;
	//
	// Cookies
	//
    SetCookie( "PriceMin",     this.tbPriceMin );
    SetCookie( "PriceMax",     this.tbPriceMax );
	SetCookie( "Bedrooms",     this.tbBeds );
	SetCookie( "Bathrooms",    this.tbBaths );
	SetCookie( "SquareFeet",   this.tbSqFeet );
	SetCookie( "Acreage",      this.tbAcreage );
	SetCookie( "YearsOld",     this.tbYearsOld );
	SetCookie( "SingleFamily", this.types1+'' );
	SetCookie( "Condo",        this.types2+'');
	SetCookie( "Land",         this.types3+'' );
	SetCookie( "MultiFamily",  this.types4+'' );
}

/**
 * Unloads the Filter criteria from the page form
 */
Filter.prototype.loadCriteria = function() {
	var boolVal;
	this.tbPriceMin = GetCookie("PriceMin");      if ( this.tbPriceMin == null ) this.tbPriceMin = MINPRICE_DEFAULT;
	this.tbPriceMax = GetCookie("PriceMax");      if ( this.tbPriceMax == null ) this.tbPriceMax = MAXPRICE_DEFAULT;
	this.tbBeds     = GetCookie("Bedrooms");      if ( this.tbBeds == null )     this.tbBeds = "0";
	this.tbBaths    = GetCookie("Bathrooms");     if ( this.tbBaths == null )    this.tbBaths = "0";
	this.tbSqFeet   = GetCookie("SquareFeet");    if ( this.tbSqFeet == null )   this.tbSqFeet = "0";
	this.tbAcreage  = GetCookie("Acreage");       if ( this.tbAcreage == null )  this.tbAcreage = "0";
	this.tbYearsOld = GetCookie("YearsOld");      if ( this.tbYearsOld == null ) this.tbYearsOld = "0";
	boolVal = GetCookie("SingleFamily"); if ( boolVal == null ) this.types1 = true; else this.types1 = ( boolVal == "true" ) ? true : false;
	boolVal = GetCookie("Condo");        if ( boolVal == null ) this.types2 = false; else this.types2 = ( boolVal == "true" ) ? true : false;
	boolVal = GetCookie("Land");         if ( boolVal == null ) this.types3 = false; else this.types3 = ( boolVal == "true" ) ? true : false;
	boolVal = GetCookie("MultiFamily");  if ( boolVal == null ) this.types4 = false; else this.types4 = ( boolVal == "true" ) ? true : false;
	this.tbPriceF   = this.tbPriceMin;
	this.tbPriceT   = this.tbPriceMax;
	//
	// Cookies
	//
	setCurrencyWidget( "txt_price_from", this.tbPriceMin );
	setCurrencyWidget( "txt_price_to", this.tbPriceMax );
	setSelectWidget( "ddlBedrooms", this.tbBeds );
	setSelectWidget( "ddlBathrooms", this.tbBaths );
	setSelectWidget( "ddlSquareFeet", this.tbSqFeet );
	setSelectWidget( "ddlAcreage", this.tbAcreage );
	setSelectWidget( "ddlYearBuilt", this.tbYearsOld );
	setCheckboxWidget( "cblTypes_0", this.types1 );
	setCheckboxWidget( "cblTypes_1", this.types2 );
	setCheckboxWidget( "cblTypes_2", this.types3 );
	setCheckboxWidget( "cblTypes_3", this.types4 );
}

/**
 * remember the sub totals
 */
Filter.prototype.setSubTotals = function( t1, t2, t3, t4 ) {
    this.tot_1 = t1;
    this.tot_2 = t2;
    this.tot_3 = t3;
    this.tot_4 = t4;
}

/**
 * calculates total from what's checked only
 */
Filter.prototype.totalChecked = function() {
    var tot = 0;
    if ( this.types1 == true ) tot = tot + this.tot_1;
    if ( this.types2 == true ) tot = tot + this.tot_2;
    if ( this.types3 == true ) tot = tot + this.tot_3;
    if ( this.types4 == true ) tot = tot + this.tot_4;
    return tot;
}

/**
 * Resets the Filter criteria to default values
 *
 * (Note: this does not update or manage the DOM elements.)
 */
Filter.prototype.resetCriteria = function() {
	this.tbBeds = 0;
	this.tbBaths = 0;
	this.tbSqFeet = 0;
	this.tbAcreage = 0;
	// this.tbGarage = 0;
	// this.tbTimeOnMarket = 0;
	this.tbYearsOld = 0;
	this.types1 = true;
	this.types2 = true;
	this.types3 = false;
	this.types4 = false;
	// this.types5 = false;
	// this.types6 = false;
	// this.types7 = false;
	// this.features1 = false;
	// this.features2 = false;
    // var _tbPriceF = document.getElementById("ddlPriceFrom");
	// var _tbPriceT = document.getElementById("ddlPriceTo");
	var _tbBeds = document.getElementById("ddlBedrooms");
	var _tbBaths = document.getElementById("ddlBathrooms");
	var _tbSqFeet = document.getElementById("ddlSquareFeet");
	var _tbAcreage = document.getElementById("ddlAcreage");
	// var _tbGarage = document.getElementById("ddlGarage");
	// var _tbTimeOnMarket = document.getElementById("ddlTimeOnMarket");
	var _tbYearsOld = document.getElementById("ddlYearBuilt");
	var _types1 = document.getElementById("cblTypes_0");
	var _types2 = document.getElementById("cblTypes_1");
	var _types3 = document.getElementById("cblTypes_2");
	var _types4 = document.getElementById("cblTypes_3");
	// var _types5 = document.getElementById("cblTypes_4");
	// var _types6 = document.getElementById("cblTypes_5");
	// var _types7 = document.getElementById("cblTypes_6");
	// var _features1 = document.getElementById("cblFeatures_0");
	// var _features2 = document.getElementById("cblFeatures_1");
    this.unloadCriteria();
}

/**
 * Determines whether or not a Property meets this Filter criteria
 * @return {Boolean} Pass/Fail
 */
Filter.prototype.meetsCriteria = function(p) {

    var d = new Date();
    var year = d.getFullYear();
    
    if ( this.category != "" ) {
        return p.p_cat.indexOf(this.category) >= 0;
    }
    if (
         (this.types1 && p.ptype == "SF") ||
         (this.types2 && p.ptype == "CC") ||
         (this.types3 && p.ptype == "LD") ||
         (this.types4 && p.ptype == "MF")
        )
    {
        //
        // All must pass the price compare
        //
        if ( (Number(this.tbPriceF) != Number(MINPRICE_DEFAULT)) && (Number(this.tbPriceF) > Number(p.lprice)) )
            return false;
        if ( (Number(this.tbPriceT) != Number(MAXPRICE_DEFAULT)) && (Number(this.tbPriceT) < Number(p.lprice)) )
            return false;
        
        if ( (Number(this.tbAcreage) > 0) && (Number(this.tbAcreage) > Number(p.acre)) )
            return false;
            
        //
        // If this is Land, we're done.
        //
        if (p.ptype == "LD")
            return true;
        
        //
        // The rest must pass beds, baths and sqft
        //
        if ( (Number(this.tbBeds) > 0) && (Number(this.tbBeds) > Number(p.bedrooms)) )
            return false;
        if ( (Number(this.tbBaths) > 0) && (Number(this.tbBaths) > Number(p.baths)) )
            return false;
        if ( (Number(this.tbSqFeet) > 0) && (Number(this.tbSqFeet) > Number(p.sqft)) )
            return false;
       
        //
        // Year Built
        //
        var yearsOld = year - Number(p.byear);
        var yearsLim = Number(this.tbYearsOld);
        if ( ( yearsLim > 0 ) && (  yearsOld > yearsLim ) )
            return false;
        if ( ( yearsLim < 0 ) && ( -yearsLim < yearsOld ) )
            return false;

        return true;
    }
    return false;
}