// ###############################################
//  Parcel Query code
// ###############################################



function parcelQuery() {
    this.Owner = "";
    this.PID = "";
    this.HouseNum = "";
    this.StreetName = "";
    this.StreetDir = "";
    this.StreetType = "";

    // advanced search
    this.township = "";
    this.city = "";
    this.subgroup = "";
    this.minvalue = 0;
    this.maxvalue = 0;
    this.toYear = "";
    this.fromYear = "";
    this.toMonth = "";
    this.fromMonth = "";
    this.minacreage = 0;
    this.maxacreage = 0;
    this.impval = "";
    this.landuse = "";
    this.book = "";
    this.page = "";

    // buffer
    this.x = 0;
    this.y = 0;
    this.selectedPID = "";
    this.radius = 0;
    
    // where clause
    this.where = "";


    this.clearQuery = function() {

        this.where = "";

        this.Owner = "";
        this.PID = "";
        this.HouseNum = "";
        this.StreetName = "";
        this.StreetDir = "";
        this.StreetType = "";

        // buffer
        this.x = 0;
        this.y = 0;
        this.selectedPID = "";
        this.radius = 0;

        // advanced search
        this.township = "";
        this.city = "";
        this.subgroup = "";
        this.minvalue = 0;
        this.maxvalue = 0;
        this.toYear = "";
        this.fromYear = "";
        this.toMonth = "";
        this.fromMonth = "";
        this.minacreage = 0;
        this.maxacreage = 0;
        this.impval = "";
        this.landuse = "";
        this.book = "";
        this.page = "";

    }

    this.getOwnerWhereClause = function() {
        var wherePart = [];

        if (this.Owner != "") {
            wherePart.push(parcelFields.OWNER + " LIKE '" + this.Owner.toUpperCase() + "%'");
        }
        this.where = wherePart.join("");
        
        return wherePart.join("");
    }

    this.getAdvancedWhereClause = function() {

        var sWhere = [];

        // Township
        if (this.township != "")
            sWhere.push(parcelFields.TOWNSHIP + " like '" + this.township + "%'");

        // City
        if (this.city != "")
            sWhere.push(parcelFields.CITY + " = '" + this.city.toUpperCase() + "'");


        if (this.subgroup != "")
            sWhere.push(parcelFields.SUBDIVISION + " = '" + this.subgroup.toUpperCase() + "'");


        // Last Sale Date
        if ((this.toYear != "") && (this.toMonth != "") && (this.toYear != "year") && (this.toMonth != "month")) {
            // date '01/01/2000'
            var lastDay = "30";
            switch (this.toMonth) {
                case "1", "3", "5", "7", "9", "10", "12":
                    lastDay = "31";
                    break;
                case "2":
                    if ((this.toYear % 4) == 0)
                        lastDay = "29";
                    else
                        lastDay = "28";
                    break;
                default:
                    lastDay = "30";
                    break;
            }

            var toDate = this.toMonth + "/" + lastDay + "/" + this.toYear;
            sWhere.push(parcelFields.SALEDATE + " <= '" + toDate + "'");
        }

        if ((this.fromYear != "") && (this.fromMonth != "") && (this.fromYear != "year") && (this.fromMonth != "month")) {
            // date '01/01/2000'
            var fromDate = this.fromMonth + "/01/" + this.fromYear;
            sWhere.push(parcelFields.SALEDATE + " >= '" + fromDate + "'");
        }

        // acreage
        if (this.minacreage > 0) 
            sWhere.push(parcelFields.ACREAGE + " >=  " + this.minacreage);
            
        if (this.maxacreage > 0)
            sWhere.push(parcelFields.ACREAGE + "<=" + this.maxacreage);


        // total value
        if (this.minvalue > 0)
            sWhere.push(parcelFields.TOTALVAL + " >= " + this.minvalue);
        if (this.maxvalue > 0)
            sWhere.push(parcelFields.TOTALVAL + " <= " + this.maxvalue);
        

        // improvement code
        if (this.impval != "") {
            if (this.impval == "improved")
                sWhere.push("(" + parcelFields.IMPROVEMENTVAL + " > 0)");
            else
                sWhere.push("(" + parcelFields.IMPROVEMENTVAL + " = 0)");
        }


        // Land use code
        if (this.landuse != "")
            sWhere.push("(" + parcelFields.LANDUSE + " = '" + this.landuse + "')");

        //Deed Book1
        if (this.book != "")
            sWhere.push("(" + parcelFields.BOOK + " = '" + this.book + "')");

        // Deed Page
        if (this.page != "")
            sWhere.push("(" + parcelFields.PAGE + " = '" + this.page + "')");

        this.where = sWhere.join(" AND ");

        return sWhere.join(" AND ");


    }

    this.getAddressWhereClause = function() {

        var swhere = "(" + parcelFields.STREETNAME + " like '" + this.StreetName + "%' OR " + parcelFields.STREETNAME + " = '" + this.StreetName + "')";

        if (this.HouseNum != "")
            swhere += " AND (" + parcelFields.HOUSENUM + " like '%" + this.HouseNum + "%' OR " + parcelFields.HOUSENUM + " = '" + this.HouseNum + "')";

        if (this.StreetDir != "")
            swhere += " AND " + parcelFields.STREETDIR + " = '" + this.StreetDir + "'";

        if (this.StreetType != "")
            swhere += " AND " + parcelFields.STREETTYPE + " = '" + this.StreetType + "'";

        this.where = swhere;

        return swhere;
    }

    this.getPIDWhereClause = function() {

        var swhere = parcelFields.PID + " like '" + this.PID + "%'";
        this.where = swhere;
        
        return swhere;
    }

}


