﻿var parcelQuery;

var saleDateFieldName = "sale_date";
var salePriceFieldName = "sale_price";
var acreageFieldname = "Total_Acres"
var twnshipFieldName = "Township_Name";
var TotValueFieldName = "Total_Value";
var streetFieldName = "Street_Name"
var feattypeFieldName = "FeatType";

dojo.declare("custom.parcels.parcelQuery", null, {
    ownerName: "",
    pin: "",
    deedbk: "",
    deedpg: "",

    streetname: "",

    toMonth: "",
    fromMonth: "",
    toYear: "",
    fromYear: "",
    minAcreage: "",
    maxAcreage: "",

    minValue: "",
    maxValue: "",
    minSalePrice: "",
    maxSalePrice: "",

    township: "",

    useComm: true,
    useRes: true,
    wildcard: "",

    clearQuery: function() {

        this.wildcard = "";

        this.ownerName = "";
        this.pin = "";
        this.deedbk = "";
        this.deedpg = "";

        this.streetname = "";

        this.toMonth = "";
        this.fromMonth = "";
        this.toYear = "";
        this.fromYear = "";
        this.minAcreage = "";
        this.maxAcreage = "";

        this.minValue = "";
        this.maxValue = "";
        this.minSalePrice = "";
        this.maxSalePrice = "";

        this.township = "";

        this.useCOMM = true;
        this.useRes = true;
    },
    getWhereClause: function() {
        var sWhere = "";

        if (this.township != "")
            sWhere = custom.parcels.fields.township + " = '" + this.township.toUpperCase() + "'";

        if ((this.toYear != "") && (this.toMonth != "") && (this.toYear != "year") && (this.toMonth != "month")) {
            // date '01/01/2000'
            var toDate = this.toMonth + "/28/" + this.toYear;
            if (sWhere != "")
                sWhere = sWhere + " and " + saleDateFieldName + " <= date '" + toDate + "'";
            else
                sWhere = saleDateFieldName + " <= date '" + toDate + "'";
        }
        if ((this.fromYear != "") && (this.fromMonth != "") && (this.fromYear != "year") && (this.fromMonth != "month")) {
            // date '01/01/2000'
            var fromDate = this.fromMonth + "/01/" + this.fromYear;
            if (sWhere != "")
                sWhere = sWhere + " and " + saleDateFieldName + " >= date '" + fromDate + "'";
            else
                sWhere = saleDateFieldName + " >= date '" + fromDate + "'";
        }



        // ################################################
        // get the Acreage Values


        // acreage
        if (this.minAcreage != "") {
            // make sure it is numeric...
            if (sWhere != "")
                sWhere = sWhere + " and ";

            sWhere = sWhere + acreageFieldname + " >= " + this.minAcreage;
        }

        // acreage
        if (this.maxAcreage != "") {
            // make sure it is numeric...
            if (sWhere != "")
                sWhere = sWhere + " AND ";

            sWhere = sWhere + acreageFieldname + " <= " + this.maxAcreage;
        }


        // Total Value
        if (this.minValue != "") {
            if (sWhere != "")
                sWhere = sWhere + " AND ";

            sWhere = sWhere + "(" + TotValueFieldName + ">=" + this.minValue + ")";

        }

        if (this.maxValue != "") {
            if (sWhere != "")
                sWhere = sWhere + " AND ";

            sWhere = sWhere + "(" + TotValueFieldName + "<=" + this.maxValue + ")";

        }


        // Sale price Value
        if (this.minSalePrice != "") {
            if (sWhere != "")
                sWhere = sWhere + " AND ";

            sWhere = sWhere + "(" + salePriceFieldName + ">=" + this.minSalePrice + ")";

        }

        if (this.maxSalePrice != "") {
            if (sWhere != "")
                sWhere = sWhere + " AND ";

            sWhere = sWhere + "(" + salePriceFieldName + "<=" + this.maxSalePrice + ")";

        }



        // ########################################################
        // Owner Name

        if (this.ownerName != "") {
            if (sWhere != "")
                sWhere = sWhere + " and ";

            if (this.ownerName.indexOf("%") > 0 || this.ownerName.indexOf("*") > 0) {
                this.ownerName.replace("*", "%");
                sWhere = sWhere + "(OWNER1 like '" + this.ownerName.toUpperCase() + "')";
            }
            else {
                sWhere = sWhere + "(OWNER1 like '" + this.ownerName.toUpperCase() + "%')";
            }
        }

        // ########################################################
        // Deed Book & Page
        if (this.deedbk != "") {
            if (sWhere != "")
                sWhere = sWhere + " and ";
            sWhere = sWhere + "(Book ='" + this.deedbk.toUpperCase() + "')";

        }

        if (this.deedpg != "") {
            if (sWhere != "")
                sWhere = sWhere + " and ";
            sWhere = sWhere + "(Page ='" + this.deedpg.toUpperCase() + "')";

        }

        // ########################################################
        // PIN
        if (this.pin != "") {
            if (sWhere != "")
                sWhere = sWhere + " AND ";

            sWhere = sWhere + "(Parcel_ID = '" + this.pin + "' OR PIN = '" + this.pin + "' OR Parcel_ID like '" + this.pin + "%' OR Parcel_ID like '" + this.pin + "%')";
        }


        if (this.streetname != "") {
            if (sWhere != "")
                sWhere = sWhere + " AND ";


            if (this.streetname.indexOf("%") > 0 || this.streetname.indexOf("*") > 0) {
                streetname.replace("*", "%");
                sWhere = sWhere + "(" + streetFieldName + " like '" + this.streetname.toUpperCase() + "')";
            }
            else {
                sWhere = sWhere + "(" + streetFieldName + " like '" + this.streetname.toUpperCase() + "%')";
            }
        }



        if ((this.useRes) && !(this.useCOMM)) {
            if (sWhere != "")
                sWhere = sWhere + " AND ";

            sWhere = sWhere + "(" + feattypeFieldName + " = 'RESIDENTIAL')";

        } else if ((this.useCOMM) & !(this.useRes)) {
            if (sWhere != "")
                sWhere = sWhere + " AND ";

            sWhere = sWhere + "(" + feattypeFieldName + " = 'COMMERCIAL')";

        }

        return sWhere;

    }

});


