﻿function SearchCriteria(wsUrl){
    this.cityIds = null;
    this.countyIds = null;
    this.zips = null;
    this.neighborhoods = null;
    this.priceTo = null;
    this.priceFrom = null;
    this.propertyTypeIds = null;
    this.beds = null;
    this.baths = null;
    this.newCon = false;
    this.adultCom = false;
    this.weichertOnly = false;
    this.newToMakert = false;
    this.bounds = null;
    this.zoom = null;
    this.page = null;
    this.view = null;
    this.mls = null;
    this.exc = null;
    this.stypeid = null;
    this.dir = null;
    this.lotsize = null;
    this.sqft = null;
    this.days = null;
    this.built = null;
    this.fees = null;
    this.orig = null;
    this.sortby = null;
    this.type = null;
    this.districtIds = null;
    this.features = null;
        
    this.http = new WDCHttp(wsUrl);
    
    this.init();
}

SearchCriteria.prototype.init = function(){
};

SearchCriteria.prototype.toString = function(){
    var str = '';
    if(this.cityIds !== null && this.cityIds != ''){str += 'cityid=' + this.cityIds + '&';}
    if(this.countyIds !== null && this.countyIds != ''){str += 'countyid=' + this.countyIds + '&';}
    if(this.zips !== null && this.zips != ''){str += 'zip=' + this.zips + '&';}
    if(this.neighborhoods !== null && this.neighborhoods != ''){str += 'hood=' + this.neighborhoods + '&';}
    if(this.priceTo !== null && this.priceTo != ''){str += 'minpr=' + this.priceTo + '&';}
    if(this.priceFrom !== null && this.priceFrom != ''){str += 'maxpr=' + this.priceFrom + '&';}
    if(this.propertyTypeIds !== null && this.propertyTypeIds != ''){str += 'ptypeid=' + this.propertyTypeIds + '&';}
    if(this.beds !== null && this.beds != ''){str += 'minbr=' + this.beds + '&';}
    if(this.baths !== null && this.baths != ''){str += 'minba=' + this.baths + '&';}
    if(this.newCon == true){str += 'type=ncon&';}
    if(this.adultCom == true){str += 'type=adc&';}
    if(this.weichertOnly == true){str += 'type=wei&';}
    if(this.newToMakert == true){str += 'type=new&';}
    if(this.bounds !== null && this.bounds != ''){str += 'bounds=' + this.bounds + '&';}
    if(this.zoom !== null && this.zoom != ''){str += 'zm=' + this.zoom + '&';}
    if(this.page !== null && this.page != ''){str += 'pg=' + this.page + '&';}
    if(this.view !== null && this.view != ''){str += 'view=' + this.view + '&';}
    if(this.mls !== null && this.mls != ''){str += 'mls=' + this.mls + '&';}  
    if(this.exc !== null && this.exc != ''){str += 'exc=' + this.exc + '&';}  
    if(this.stypeid !== null && this.stypeid != ''){str += 'stypeid=' + this.stypeid + '&';}  
    if(this.dir !== null && this.dir != ''){str += 'dir=' + this.dir+ '&';}  
    if(this.lotsize !== null && this.lotsize != ''){str += 'lotsize=' + this.lotsize+ '&';}  
    if(this.sqft !== null && this.sqft != ''){str += 'sqft=' + this.sqft+ '&';}  
    if(this.days !== null && this.days != ''){str += 'days=' + this.days+ '&';}  
    if(this.built !== null && this.built != ''){str += 'built=' + this.built+ '&';}  
    if(this.fees !== null && this.fees != ''){str += 'fees=' + this.fees+ '&';}  
    if(this.orig !== null && this.orig != ''){str += 'orig=' + this.orig+ '&';}  
    if(this.sortby !== null && this.sortby != ''){str += 'sortby=' + this.sortby+ '&';}  
    if(this.type !== null && this.type != ''){str += 'type=' + this.type+ '&';}  
    if(this.districtIds !== null && this.districtIds != ''){str += 'districtId=' + this.districtIds + '&';}
    if(this.features !== null && this.features != ''){str += 'features=' + this.features + '&';}

    if(str.length > 0){
        str = str.substring(0,str.length-1);
    }
    return str;
};

SearchCriteria.prototype.loadFromQueryString = function(queryString){
    var qs = new Querystring(queryString);
    this.cityIds = qs.get('cityid');
    this.countyIds = qs.get('countyid');
    this.zips = qs.get('zip');
    this.neighborhoods = qs.get('hood');
    this.priceTo = qs.get('minpr');
    this.priceFrom = qs.get('maxpr');
    this.propertyTypeIds = qs.get('ptypeid');
    this.beds = qs.get('minbr');
    this.baths = qs.get('minba');
    this.bounds = qs.get('bounds');
    this.zoom = qs.get('zm');
    this.page = qs.get('pg');
    this.view = qs.get('view');
    this.mls = qs.get('mls');
    this.exc = qs.get('exc');
    this.stypeid = qs.get('stypeid');
    this.dir = qs.get('dir'); 
    this.lotsize = qs.get('lotsize'); 
    this.sqft = qs.get('sqft');  
    this.days = qs.get('days');  
    this.built = qs.get('built'); 
    this.fees = qs.get('fees');  
    this.orig = qs.get('orig');
    this.sortby = qs.get('sortby');
    var type = qs.get('type'); 
    if(type!=null){
        var types = type.split(',');
        var temp = [];
        for(var i=0;i<types.length;i++){
            switch(types[i]){
                case 'ncon':
                    this.newCon = true;
                    break;
                case 'adc':
                    this.adultCom = true;
                    break;
                case 'wei':
                    this.weichertOnly = true;
                    break;
                case 'new':
                    this.newToMakert = true;
                  break;
                default:
                    temp.push(types[i]);
            }    
        }
        if(temp.length>0){
            this.types = temp.join(',');
        }        
    }
    this.districtIds = qs.get('districtId');
    this.features = qs.get('features');

};

SearchCriteria.prototype.execute = function( responseCallback, errorCallback ){
    this.http.get(this, this.toString(), function(context,result){
        if(result.error){
            if(errorCallback){
                errorCallback(context,result);
            }
        } else if(responseCallback) {
            responseCallback(context,result);
        }
        //google analytics tracking
        if(typeof( pageTracker ) != 'undefined'){
            pageTracker._trackPageview(context.http.url);
        }
    });
};



