/// <summary>
        /// Searches for properties based on features, location and keywords that are given via post parameters.
        /// </summary>
        /// <returns></returns>
        public ActionResult Search()
        {
            //   var estateTypes = GetEstateTypes();
              //  var contracts = GetContracts();

            SearchQuery searchQuery = new SearchQuery(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
            Preferences pref = new Preferences();

            var user = GetCurrentUser();
            if(user != null)
                pref.ClientID = user.DBID;

            // Getting ze post data ja

            System.Collections.Specialized.NameValueCollection postedValues = Request.Form;

            if (postedValues["SearchStr"] != null && postedValues["SearchStr"].Length > 0)
            {
                searchQuery.SetKeywords(postedValues["searchStr"]);
                ViewBag.SearchStr = postedValues["searchStr"];

            }
            if (postedValues["province"] != null && postedValues["province"] != "" && postedValues["province"] != "Any")
            {
                searchQuery.ProvinceId = (int.Parse(postedValues["province"]));
            }
            if (postedValues["city"] != null && postedValues["city"] != "" && postedValues["city"] != "Any")
            {
                searchQuery.City_Id = (int.Parse(postedValues["city"]));
            }
            if (postedValues["area"] != null && postedValues["area"] != "" && postedValues["area"] != "Any")
            {
                searchQuery.Area_Id = (int.Parse(postedValues["area"]));
            }
            if (postedValues["area"] != null && postedValues["area"] != "" && postedValues["area"] != "Any")
            {
                searchQuery.Area_Id = (int.Parse(postedValues["area"]));
                pref.Area_ID = searchQuery.Area_Id;
            }
            if(postedValues["noBedrooms"] != null)
            {
                searchQuery.BedroomsMin = int.Parse(postedValues["noBedrooms"]);
                pref.Area_ID = searchQuery.BedroomsMin;
            }
            if (postedValues["noBathrooms"] != null)
            {
                searchQuery.BathroomsMin = int.Parse(postedValues["noBathrooms"]);
                pref.MinBathrooms = searchQuery.BathroomsMin;
            }
            if (postedValues["noGarages"] != null)
            {
                searchQuery.GaragesMin = int.Parse(postedValues["noGarages"]);
                pref.MinGarages = searchQuery.GaragesMin;
            }
            if(postedValues["priceRange"] != null && postedValues["priceRange"] != "Any" && postedValues["priceRange"] != "-1")
            {
                pref.MaxPrice = int.Parse(postedValues["priceRange"]);
                searchQuery.PriceMax = pref.MaxPrice;
            }

            var res = searchQuery.GenerateResults();

            MySqlDataReader reader = res;

            List<Dictionary<string, string>> props = new List<Dictionary<string, string>>();
            while (reader.Read())
            {
                int id = reader.GetInt32("List_ID");
              //  string name = reader.GetString(1);
                string streetname = reader.GetString("Address_Streetname");
                int streetno = reader.GetInt32("Address_Streetno");
                int no_bathrooms = reader.GetInt32("Property_Bathroom_Count");
                int no_bedrooms = reader.GetInt32("Property_Bedroom_Count");
                int area = reader.GetInt32("Property_Plot_Size");
               // int city_id = reader.GetInt32("city_id");
                string area_name = reader.GetString("Area_Name");
                string city = reader.GetString("City_Name");
                string province = reader.GetString("Province_Name");// reader.GetString("province");
                //int estateTypeId = reader.GetInt32("type_id");
               // int contractId = reader.GetInt32("contract_id");
                string image = reader.GetString("Image_URL");
                float price = reader.GetFloat("List_Price");

                // composite fields
              //  string type = estateTypes[estateTypeId];
                string link = Url.Action("Residence", "Estates") + "?id=" + id;
                //string imagePath = "/Assets/Images/Homes/" + image;
                string imagePath = image;
                string locationStr = string.Format("{0}, {1}", city, province);
              //  string contract = contracts[contractId];

                var nfi = (NumberFormatInfo)CultureInfo.InvariantCulture.NumberFormat.Clone();
                nfi.NumberGroupSeparator = " ";
                string formatted = price.ToString("#,#", nfi); // "1 234 897.11"

                string priceStr = string.Format("R {0}", formatted);
                string location = streetname + " " + streetno.ToString();

                Dictionary<string, string> propDic = new Dictionary<string, string>();

                propDic["image"] = imagePath;
                propDic["link"] = link;
                propDic["street"] = location;
                propDic["city"] = locationStr;
                propDic["price"] = priceStr;
                propDic["area"] = area.ToString();
                propDic["bedrooms"] = no_bedrooms.ToString();
                propDic["bathrooms"] = no_bathrooms.ToString();

                props.Add(propDic);
            }

            string json = JsonConvert.SerializeObject(props.ToArray());

            Response.Clear();
            Response.ContentType = "application/json; charset=utf-8";
            Response.Write(json);

            Response.End();

            reader.Close();
            searchQuery.Close();

            // Log query to preferences
            if(pref.IsSomethingSet()&&user != null)
                pref.Insert(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);

            return View();
        }