<?php

 include_once(LoadPHPFile('/RealConnect/Model/shared_model.php'));

 include_once(LoadPHPFile('/RealConnect/Model/properties_model.php'));

 include_once(LoadPHPFile('/RealConnect/Model/flexproperties_model.php'));

 include_once(LoadPHPFile('/RealConnect/Model/custom_mls_listing_model.php'));

?>



<?php



function base_url(){

	return get_site_url();

	//return "http://www.craig-temp.net/";

}

function properties($map = '', $special = '', $sort_term = '',

$rc_listing_view_template_file_path =''){



	global $disableUnusedCode;

		//$site_url = "http://www.craig-temp.net";

		$site_url = get_site_url();

		$model_Obj = new \stdClass();

		$model_Obj->shared_model = new Shared_model();

		$model_Obj->properties_model = new Properties_model();

		$model_Obj->flexproperties_model = new Flexproperties_model();

		$model_Obj->custom_mls_listing_model = new custom_mls_listing_model();



		$client_info = $model_Obj->shared_model->get_client_info($site_url);

		$mls_type = 'paragon';

		$office_id = '';

		if (count($client_info) > 0) {

			$mls_type = $client_info['mls_feed'];

			$office_id = $client_info['office_id'];

		}



		//set post variables

		$property_type = '';

		

		/*if(isset($_POST['property_type'])){

			$property_type = $_POST['property_type'];

		}

		*/



		$property_type_input = $property_type;

		$main_search = '';

		/*if(isset($_POST['main_search'])){

		$main_search = $_POST['main_search'];

	    }*/

		$main_search_box = $main_search;



		if ($map == 'map') {

			$searchScope = 'map';

		} else {

			$searchScope = '';

			if(isset($_POST['searchScope'])){

				$searchScope = $_POST['searchScope'];

			}



		}

		$ne = '';

		if(isset($_POST['ne'])){

			$ne = $_POST['ne'];

		}



		$nw = '';

		if(isset($_POST['nw'])){

			$nw = $_POST['nw'];

		}



		$se = '';

		if(isset($_POST['se'])){

			$se = $_POST['se'];

		}



		$sw = '';

		if(isset($_POST['sw'])){

			$ne = $_POST['sw'];

		}



		//initialize the arrays for data that will be sent to the views

		$data = array();

		$data['office'] = $office_id;



		//get WP page information

		$data['wp_content'] = '';

		$pageurl = '';

		global $postid;



		$postid = '';

		$prop_type_meta_value = '';

		$area_meta_value = '';

		$mlsID_meta_value = '';

		$office_meta_value = '';

		$agent_meta_value = '';

		$subdivision_meta_value = '';

		$price_range_meta_value = '';

		$short_sale_meta_value = '';

		$foreclosure_meta_value = '';

		$listing_date_meta_value = '';

		$farm_features_meta_value = '';

		$finished_sqft_meta_value = '';

		$acreage_meta_value = '';

		$search_term_meta_value = '';

		$searchbox_toggle_meta_value = '';

		$map_toggle_meta_value = '';

		$max_year_built_meta_value = '';

		$pagetitle = '';

		$builder_meta_value='';

		$subcategory_meta_value='';

		$level_meta_value='';

		$school_elementary_meta_value='';

		$school_middle_meta_value='';

		$school_high_meta_value='';

		$year_built_meta_value='';

		$max_listing_meta_value='';



		global $wpdb;

		if ($_SESSION['referer']) {

			if ($searchScope == 'new') {

				$_SESSION['selectednew']= $searchScope;

				unset($_SESSION['main_search']);

				unset($_SESSION['property_type']);

				unset($_SESSION['referer']);

			}

			if ($searchScope != 'new' || (!$_SESSION['selectednew'] && $searchScope != 'refine')) {

				

			

				$pageurl = $_SESSION['referer'];

			

				$pos = strpos($pageurl,'listings');

				if ($pos !== false) {

					$page_by_path = get_page_by_path($pageurl);

					$postid = $page_by_path->ID;





					//$content = $wpdb->get_col("SELECT post_content FROM $wpdb->posts WHERE ID=".$postid.";");

					//$pagetitle = $wpdb->get_col("SELECT post_title FROM $wpdb->posts WHERE ID=".$postid.";");

					/* converted above 2 sql's to 1 by vikas */

					$post_data=$wpdb->get_row("SELECT post_title, post_content FROM $wpdb->posts WHERE id=".$postid); 

					

					if(!empty($post_data))

					{

						$content=[$post_data->post_content];

						$pagetitle=[$post_data->post_title];

					}

					

					if ($content) {

						$data['wp_content'] = $content[0];

					}



					

				   // checking for shortcode options set in wp-admin shortcode on page for filtering records

					if ($postid != '' && $postid != 0) {



						$priceRange=$layerslider_id=$contactSeller=$virtualTourURL=$downloadFloorplanURL=$location=$otherLinks=$similarModels=$RCsectionTitle='';

						// following sql is added as replacement to 9-10 different sqls



						$metadata=$wpdb->get_results("SELECT meta_key, meta_value FROM $wpdb->postmeta 

						WHERE post_id=".$postid); 

						

						

						foreach($metadata as $obj){

							//$obj->meta_key;

								switch ($obj->meta_key)

								{

									case 'rc_virtual_tours_only':

										$virtual_tours_only_meta_value=$obj->meta_value[0];

									

										break;

									

									case 'rc_farms_only':

										$farms_only_meta_value=$obj->meta_value[0];

										break;	

										

									case 'rc_prop_type':

										$prop_type_meta_value=[$obj->meta_value];

										break;

									case 'rc_builder':

										$builder_meta_value =[$obj->meta_value];

										break;

									case 'rc_subcategory':

										$subcategory_meta_value =[$obj->meta_value];

										break;

									case 'rc_year_built':

										$year_built_meta_value =[$obj->meta_value];

										if($year_built_meta_value)

										{

											$year_built_meta_value[0]=str_replace("_", "--",$year_built_meta_value[0]);

										}

										break;

									case 'rc_level':

										$level_meta_value =[$obj->meta_value];

										break;	

									case 'rc_school_elementary':

										$school_elementary_meta_value =[$obj->meta_value];

										break;	

									case 'rc_school_middle':

										$school_middle_meta_value =[$obj->meta_value];

										break;

									case 'rc_school_high':

										$school_high_meta_value =[$obj->meta_value];

										break;

									case 'rc_area':

										$area_meta_value =[$obj->meta_value];

										break;

									case 'rc_mlsID':

										$mlsID_meta_value =[$obj->meta_value];

										break;

									case 'rc_office':

										$office_meta_value =[$obj->meta_value];

										break;

									case 'rc_agent':

										$agent_meta_value =[$obj->meta_value];

										break;

									case 'rc_subdivision':

										$subdivision_meta_value =[$obj->meta_value];

										break;

									case 'rc_price_range':

										$price_range_meta_value =[$obj->meta_value];

										break;

									case 'rc_short_sale':

										$short_sale_meta_value =[$obj->meta_value];

										break;

									case 'rc_foreclosure':

										$foreclosure_meta_value =[$obj->meta_value];

										break;

									case 'rc_listing_date':

										$listing_date_meta_value =[$obj->meta_value];

										break;

									case 'rc_farm_feature':

										$farm_features_meta_value =[$obj->meta_value];

										break;

									case 'rc_finished_sqft':

										$finished_sqft_meta_value =[$obj->meta_value];

										break;

									case 'rc_acres':

										$acreage_meta_value =[$obj->meta_value];

										break;

									case 'rc_search_term':

										$search_term_meta_value =[$obj->meta_value];

										break;

									case 'rc_map_toggle':

										$map_toggle_meta_value =[$obj->meta_value];

										break;

									case 'rc_max_year_built':

										$max_year_built_meta_value =[$obj->meta_value];

										break;		

									case 'rc_max_listing':

										$max_listing_meta_value =[$obj->meta_value];

										break;	

									case 'Price Range':

										$priceRange =$obj->meta_value;

										break;

									case 'layerslider_id':

										$layerslider_id =$obj->meta_value;

										break;

									case 'Contact Seller':

										$contactSeller =$obj->meta_value;

										break;

									case 'Virtual Tour URL':

										$virtualTourURL =$obj->meta_value;

										break;

									case 'Download Floorplan URL':

										$downloadFloorplanURL =$obj->meta_value;

										break;

									case 'Location':

										$location =$obj->meta_value;

										break;	

									case 'Other Links':

										$otherLinks =$obj->meta_value;

										break;			

									case 'Similar Models IDs':

										$similarModels =$obj->meta_value;

										break;			

									case 'RC Section Title':

										$RCsectionTitle =$obj->meta_value;

										break;	



								

								}

						}

						



					/* I think below fields are not needed/used on listings pages, 

					so these can be removed from sql to improve sql speed above */



						$data['priceRange'] = $priceRange;

						$data['layerslider_id'] =$layerslider_id ;

						

						//right col

						$data['contactSeller'] = $contactSeller;

						$data['virtualTourURL'] = $virtualTourURL;

						$data['downloadFloorplanURL'] = $downloadFloorplanURL;

						$data['location'] = $location;

						$data['otherLinks'] = $otherLinks;

						$data['similarModels']  =$similarModels ;

						$data['RCsectionTitle'] =$RCsectionTitle;



						





						// Custom Fields start

 						$post_categories = get_the_category( $postid );

						$showAssociatedModels = false;

						$showCommunityMap = false;



					

						if (is_array($post_categories) && count($post_categories)>0){

							foreach ($post_categories as $cat){

								if ($cat->name == "Community"){

									$showOtherCommunityLink=true;

									$showAssociatedModels =  true;

									$showCommunityMap = true;

									break;

								}

							}

						}

						

						// vikas

						$data['showAssociatedModels'] = $showAssociatedModels;

						$data['showCommunityMap'] = $showCommunityMap;





						if ($showAssociatedModels){

							$associatedModels = get_post_meta( $postid, "Associated Models",true);

							$atts = array('ids'=>$associatedModels);

						}



						$data['postid'] =  $postid;

						if ($pagetitle != '') {

   								$data['pagetitle'] = $pagetitle[0];

						}





			





						// start get models



						if (isset($atts) && is_array($atts) && array_key_exists("ids",$atts)){

						$valid_ids = array();

						$cat = get_cat_ID( 'Models' );

						if (trim($atts['ids'])=="all"){

							$query = array (

								'cat' => $cat

							);

						}else{

							$ids = explode(",", $atts['ids']);

							if (is_array($ids) && count($ids)>0){

								foreach ($ids as $id){

									$id = trim($id);

									if (is_numeric($id) && $id>0){

										$valid_ids[] = $id;

									}else{

										$id = explode("-",$id);

										if (is_array($id) && count($id)==2){

											$start	= $id[0];

											$end	= $id[1];

											if (is_numeric($start) && is_numeric($end) && $start>0 && $start<$end){

												for ($i=$start;$i<=$end;$i++){

													$valid_ids[] = $i;

												}

											}

										}

									}

								}

							}

							if (is_array($valid_ids) && count($valid_ids)>0){

								$queryObject=$wpdb->get_results("Select * from wp_posts where id In (".implode(",",$valid_ids).")");



							}else{

								$queryObject=array();

							}



						}

						$data['queryObject']  = $queryObject;

						if(isset($showOtherCommunityLink) && $showOtherCommunityLink)

							$data["showOtherCommunityLink"]=$showOtherCommunityLink;

 						}



					//end get models





					// custom fields start





					}

				}

			}

		}

		//page title



		

		if ($pagetitle != '') {

			if ($pagetitle) {

				$content['page_title'] = $pagetitle[0];

			} else {

				$content['page_title'] = "Properties Listing";

			}

		} else {

			$content['page_title'] = "Properties Listing";

		}



		//extended search form

		

		

		/* I believe all below $_POST conditions are not required now, as now we are not using website search*/



		$selected_property_type = '';

		if(isset($_POST['property_type_select'])){

			$selected_property_type = $_POST['property_type_select'];

		}



		$selected_subcategory_residential = '';

		if(isset($_POST['subcategory_residential'])){

			$selected_subcategory_residential = $_POST['subcategory_residential'];

		}



		$selected_subcategory_commercial_sale = '';

		if(isset($_POST['subcategory_commercial_sale'])){

			$selected_subcategory_commercial_sale = $_POST['subcategory_commercial_sale'];

		}



		$selected_area = '';

		if(isset($_POST['area'])){

			$selected_area = $_POST['area'];

		}



		$selected_mlsID = '';

		if(isset($_POST['mlsID'])){

			$selected_mlsID = $_POST['mlsID'];

		}



		$selected_builder = '';

		if(isset($_POST['builder'])){

			$selected_builder = $_POST['builder'];

		}



		$selected_subcategory='';

		if(isset($_POST['subcategory'])){

			$selected_subcategory = $_POST['subcategory'];

		}



		$selected_level = '';

		if(isset($_POST['level'])){

			$selected_level = $_POST['level'];

		}



		$selected_school_high = '';

		if(isset($_POST['school_high'])){

			$selected_school_high = $_POST['school_high'];

		}



		$selected_school_elementary = '';

		if(isset($_POST['school_elementary'])){

			$selected_school_elementary = $_POST['school_elementary'];

		}



		$selected_school_middle = '';

		if(isset($_POST['school_middle'])){

			$selected_school_middle = $_POST['school_middle'];

		}



		$selected_subdivision = '';

		if(isset($_POST['subdivision'])){

			$selected_subdivision = $_POST['subdivision'];

		}



		$selected_land_description = '';

		if(isset($_POST['land_description'])){

			$selected_land_description = $_POST['land_description'];

		}



		$selected_price_min = '';

		if(isset($_POST['price_min'])){

			$selected_price_min = $_POST['price_min'];

		}



		$selected_price_min = str_replace(',','',$selected_price_min);



		$selected_price_max = '';

		if(isset($_POST['price_max'])){

			$selected_price_max = $_POST['price_max'];

		}



		$selected_price_max = str_replace(',','',$selected_price_max);

		$selected_bedrooms = '';

		if(isset($_POST['bedrooms_select'])){

			$selected_bedrooms = $_POST['bedrooms_select'];

		}



		$selected_bathrooms = '';

		if(isset($_POST['baths_select'])){

			$selected_bathrooms = $_POST['baths_select'];

		}



		$selected_square_feet_min = '';

		if(isset($_POST['square_feet_min'])){

			$selected_square_feet_min = $_POST['square_feet_min'];

		}



		$selected_square_feet_max = '';

		if(isset($_POST['square_feet_max'])){

			$selected_square_feet_max = $_POST['square_feet_max'];

		}



		$selected_year_built_min = '';

		if(isset($_POST['year_built_min'])){

			$selected_year_built_min = $_POST['year_built_min'];

		}



		$selected_year_built_max = '';

		if(isset($_POST['year_built_max'])){

			$selected_year_built_max = $_POST['year_built_max'];

		}



		$selected_acreage_min = '';

		if(isset($_POST['acreage_min'])){

			$selected_acreage_min = $_POST['acreage_min'];

		}



		$selected_acreage_max = '';

		if(isset($_POST['acreage_max'])){

			$selected_acreage_max = $_POST['acreage_max'];

		}



		$selected_elem = '';

		if(isset($_POST['elementary'])){

			$selected_elem = $_POST['elementary'];

		}



		$selected_middle = '';

		if(isset($_POST['middle'])){

			$selected_middle = $_POST['middle'];

		}



		$selected_high = '';

		if(isset($_POST['high'])){

			$selected_high = $_POST['high'];

		}



		$selected_open_house_only = '';

		if (isset($_POST['open_house_only']) && $_POST['open_house_only'] == 'yes') {

			$selected_open_house_only = 'yes';

		}

		$selected_price_reduced_only = '';

		if (isset($_POST['price_reduced_only']) && $_POST['price_reduced_only'] == 'yes') {

			$selected_price_reduced_only = 'yes';

		}

		$selected_new_listings_only = '';

		if (isset($_POST['new_listing_only']) && $_POST['new_listing_only'] == 'yes') {

			$selected_new_listings_only = 'yes';

		}









		/* There is no sorting now, so i think we can remove sorting related code too below */

		//sorting

		$price_sort = '';

		$date_sort = '';

		if ($sort_term != '' && $sort_term != 'nosort') {

			$sort_arr = explode("_", $sort_term);

			$sort_field = $sort_arr[0];

			$sort_description = $sort_arr[1];

			if ($sort_description == 'high') {

				$sort_description = 'desc';

			} elseif ($sort_description == 'low') {

				$sort_description = 'asc';

			}

			if ($sort_field == 'price') {

				//price_sort

				if (isset($_SESSION['price_sort'])) {

					$price_sort_session = $_SESSION['price_sort'];

					if ($price_sort_session != $sort_description) {

						unset($_SESSION['price_sort']);

						$_SESSION['price_sort'] =  $sort_description;

						$price_sort = $sort_description;

					} else {

						$price_sort = $sort_description;

					}

				} else {

					$_SESSION['price_sort'] =  $sort_description;

					$price_sort = $sort_description;

				}

				//date_sort

				if ($_SESSION['date_sort']) {

					$date_sort = $_SESSION['date_sort'];

				} else {

					$_SESSION['date_sort'] = 'desc';

					$date_sort = $_SESSION['date_sort'];

				}

			} elseif ($sort_field == 'date') {

				//date_sort

				if (isset($_SESSION['date_sort'])) {

					$date_sort_session = $_SESSION['date_sort'];

					if ($date_sort_session != $sort_description) {

						unset($_SESSION['date_sort']);

						$_SESSION['date_sort'] =  $sort_description;

						$date_sort = $sort_description;

					} else {

						$date_sort = $sort_description;

					}

				} else {

					$_SESSION['date_sort'] =  $sort_description;

					$date_sort = $sort_description;

				}

				//price_sort

				if (isset($_SESSION['price_sort'])) {

					$price_sort = $_SESSION['price_sort'];

				} else {

					$_SESSION['price_sort'] =  'asc';

					$price_sort = $_SESSION['price_sort'];

				}

			} else {

				$_SESSION['price_sort'] = 'asc';

				$price_sort = 'asc';

				$_SESSION['date_sort'] =  'desc';

				$date_sort = 'desc';

			}

		} else {

			if (isset($_SESSION['price_sort'])) {

				$_SESSION['price_sort'] =  $_SESSION['price_sort'];

				$price_sort =  $_SESSION['price_sort'];

			} else {

				$_SESSION['price_sort'] =  'asc';

				$price_sort = 'asc';

			}

			if (isset($_SESSION['date_sort'])) {

				$_SESSION['date_sort'] =  $_SESSION['date_sort'];

				$date_sort = $_SESSION['date_sort'];

			} else {

				$_SESSION['date_sort'] = 'desc';

				$date_sort = 'desc';

			}

		}

		$data['price_sort'] = $price_sort;

		$data['date_sort'] = $date_sort;

		//*****set property_type session*****





		$default = 0;

		if ($prop_type_meta_value && $prop_type_meta_value != '' && !empty($prop_type_meta_value)) {

			if ($searchScope == 'new') {

				if ($selected_property_type && $selected_property_type != '') {

					$property_type = $selected_property_type;

					unset($_SESSION['property_type']);

					$_SESSION['property_type'] = $property_type;

				} else {

					if ($_SESSION['property_type']) {

						$property_type = 'residential';

						$default = 1;

					} else {

						if ($property_type) {

							$_SESSION['property_type'] = $property_type;

						} else {

							$property_type = 'residential';

							$default = 1;

						}

					}

				}

			} else {

				$temp_property_type = $prop_type_meta_value[0];

				$property_type_arr = explode(',', $temp_property_type);

				$ftr_count = 0;

				foreach ($property_type_arr as $ftr) {

					if ($ftr_count == 0) {

						$property_type_ftr = $ftr;

					} else {

						$property_type_ftr .= '~'.$ftr;

					}

					$ftr_count++;

				}

				$_SESSION['property_type'] = $property_type_ftr;

				$property_type = $property_type_ftr;

			}

		} else {

			if ($selected_property_type && $selected_property_type != '') {

				$property_type = $selected_property_type;

				unset($_SESSION['property_type']);

				$_SESSION['property_type'] =  $property_type;

			} else {

				if (isset($_SESSION['property_type'])) {

					if ($searchScope == 'new') {

						$property_type = 'residential';

						$default = 1;

					} else {

						$property_type = $_SESSION['property_type'];

					}

				} else {

					if ($property_type) {

						$_SESSION['property_type'] =  $property_type;

					} else {

						$property_type = 'residential';

						$default = 1;

					}

				}

			}

		}

		//set property_type input decision

		if ($property_type_input) {

			$_SESSION['property_type_input'] =  $property_type_input;

		} else {

			unset($_SESSION['property_type_input']);

		}



		//set variables to use in main_search

		//*****selected_subcategory_residential*****

		$subcategory_residential = '';

		$residential_count = 0;

		if ($selected_subcategory_residential) {

			if ($selected_subcategory_residential != '') {

				$subcategory_residential = $selected_subcategory_residential;

			}

		}

		//*****selected_subcategory_commercial_sale*****

		$subcategory_commercial = '';

		$commercial_count = 0;

		if ($selected_subcategory_commercial_sale) {

			if ($selected_subcategory_commercial_sale != '') {

				$subcategory_commercial = $selected_subcategory_commercial_sale;

			}

		}

		//*****selected_area*****

		//area_meta_value

		$area = '';

		if ($area_meta_value && $area_meta_value != '' && !empty($area_meta_value)) {

			if ($searchScope == 'new') {

				if ($selected_area) {

					if ($selected_area != '') {

						$area = $selected_area;

						$area = str_replace(",", "~", $area);

					}

				}

				if ($_SESSION['area_meta']) {

					unset($_SESSION['area_meta']);

				}

			} else {

				if ($_SESSION['area_meta']) {

					if ($searchScope == 'map') {

						unset($_SESSION['area_meta']);

					}

				} else {

					if ($searchScope != 'map') {

						$_SESSION['area_meta'] =  $area_meta_value[0];

					}

				}

				if ($searchScope != 'map') {

					$area = $area_meta_value[0];

					$area = str_replace(",", "~", $area);

				}

			}

		} else {

			if ($selected_area) {

				if ($selected_area != '') {

					if ($searchScope != 'map') {

						$area = $selected_area;

						$area = str_replace("~", ",", $area);

					}

				}

			}

			if (isset($_SESSION['area_meta'])) {

				unset($_SESSION['area_meta']);

			}

		}

		//*****selected_mlsID*****

		//mlsID_meta_value

		$mlsID = '';

		if ($mlsID_meta_value && $mlsID_meta_value != '' && !empty($mlsID_meta_value)) {

			if ($searchScope == 'new') {

				if ($selected_mlsID) {

					if ($selected_mlsID != '') {

						$mlsID = $selected_mlsID;

						$mlsID = str_replace(",", "~", $mlsID);

					}

				}

				if ($_SESSION['mlsID_meta']) {

					unset($_SESSION['mlsID_meta']);

				}

			} else {

				if ($_SESSION['mlsID_meta']) {

					if ($searchScope == 'map') {

						unset($_SESSION['mlsID_meta']);

					}

				} else {

					if ($searchScope != 'map') {

						$_SESSION['mlsID_meta'] =  $mlsID_meta_value[0];

					}

				}

				if ($searchScope != 'map') {

					$mlsID = $mlsID_meta_value[0];

					$mlsID = str_replace(",", "~", $mlsID);

				}

			}

		} else {

			if ($selected_mlsID) {

				if ($selected_mlsID != '') {

					if ($searchScope != 'map') {

						$mlsID = $selected_mlsID;

						$mlsID = str_replace("~", ",", $mlsID);

					}

				}

			}

			if (isset($_SESSION['mlsID_meta'])) {

				unset($_SESSION['mlsID_meta']);

			}

		}

		//*****selected_builder*****

		//builder_meta_value

		$builder = '';

		if ($builder_meta_value && $builder_meta_value != '' && !empty($builder_meta_value)) {

			if ($searchScope == 'new') {

				if ($selected_builder) {

					if ($selected_builder != '') {

						$builder = $selected_builder;

						$builder = str_replace(",", "~", $builder);

					}

				}

				if ($_SESSION['builder_meta']) {

					unset($_SESSION['builder_meta']);

				}

			} else {

				if ($_SESSION['builder_meta']) {

					if ($searchScope == 'map') {

						unset($_SESSION['builder_meta']);

					}

				} else {

					if ($searchScope != 'map') {

						$_SESSION['builder_meta'] =  $builder_meta_value[0];

					}

				}

				if ($searchScope != 'map') {

					$builder = $builder_meta_value[0];

					$builder = str_replace(",", "~", $builder);

				}

			}

		} else {

			if ($selected_builder) {

				if ($selected_builder != '') {

					if ($searchScope != 'map') {

						$builder = $selected_builder;

						$builder = str_replace("~", ",", $builder);

					}

				}

			}

			if (isset($_SESSION['builder_meta'])) {

				unset($_SESSION['builder_meta']);

			}

		}

		//*****selected_subcategory*****

		//subcategory_meta_value

		$subcategory = '';

		if ($subcategory_meta_value && $subcategory_meta_value != '' && !empty($subcategory_meta_value)) {

			if ($searchScope == 'new') {

				if ($selected_subcategory) {

					if ($selected_subcategory != '') {

						$subcategory = $selected_subcategory;

						$subcategory = str_replace(",", "~", $subcategory);

					}

				}

				if ($_SESSION['subcategory_meta']) {

					unset($_SESSION['subcategory_meta']);

				}

			} else {

				if ($_SESSION['subcategory_meta']) {

					if ($searchScope == 'map') {

						unset($_SESSION['subcategory_meta']);

					}

				} else {

					if ($searchScope != 'map') {

						$_SESSION['subcategory_meta'] =  $subcategory_meta_value[0];

					}

				}

				if ($searchScope != 'map') {

					$subcategory = $subcategory_meta_value[0];

					$subcategory = str_replace(",", "~", $subcategory);

				}

			}

		} else {

			if ($selected_subcategory) {

				if ($selected_subcategory != '') {

					if ($searchScope != 'map') {

						$subcategory = $selected_subcategory;

						$subcategory = str_replace("~", ",", $subcategory);

					}

				}

			}

			if (isset($_SESSION['subcategory_meta'])) {

				unset($_SESSION['subcategory_meta']);

			}

		}

		//*****selected_level*****

		//level_meta_value

		$level = '';

		if ($level_meta_value && $level_meta_value != '' && !empty($level_meta_value)) {

			if ($searchScope == 'new') {

				if ($selected_level) {

					if ($selected_level != '') {

						$level = $selected_level;

						$level = str_replace(",", "~", $level);

					}

				}

				if ($_SESSION['level_meta']) {

					unset($_SESSION['level_meta']);

				}

			} else {

				if ($_SESSION['level_meta']) {

					if ($searchScope == 'map') {

						unset($_SESSION['level_meta']);

					}

				} else {

					if ($searchScope != 'map') {

						$_SESSION['level_meta'] =  $level_meta_value[0];

					}

				}

				if ($searchScope != 'map') {

					$level = $level_meta_value[0];

					$level = str_replace(",", "~", $level);

				}

			}

		} else {

			if ($selected_level) {

				if ($selected_level != '') {

					if ($searchScope != 'map') {

						$level = $selected_level;

						$level = str_replace("~", ",", $level);

					}

				}

			}

			if (isset($_SESSION['level_meta'])) {

				unset($_SESSION['level_meta']);

			}

		}

		//*****selected_school_elementary*****

		//school_elementary_meta_value

		$school_elementary = '';

		if ($school_elementary_meta_value && $school_elementary_meta_value != '' && !empty($school_elementary_meta_value)) {

			if ($searchScope == 'new') {

				if ($selected_school_elementary) {

					if ($selected_school_elementary != '') {

						$school_elementary = $selected_school_elementary;

						$school_elementary = str_replace(",", "~", $school_elementary);

					}

				}

				if ($_SESSION['school_elementary_meta']) {

					unset($_SESSION['school_elementary_meta']);

				}

			} else {

				if ($_SESSION['school_elementary_meta']) {

					if ($searchScope == 'map') {

						unset($_SESSION['school_elementary_meta']);

					}

				} else {

					if ($searchScope != 'map') {

						$_SESSION['school_elementary_meta'] =  $school_elementary_meta_value[0];

					}

				}

				if ($searchScope != 'map') {

					$school_elementary = $school_elementary_meta_value[0];

					$school_elementary = str_replace(",", "~", $school_elementary);

				}

			}

		} else {

			if ($selected_school_elementary) {

				if ($selected_school_elementary != '') {

					if ($searchScope != 'map') {

						$school_elementary = $selected_school_elementary;

						$school_elementary = str_replace("~", ",", $school_elementary);

					}

				}

			}

			if (isset($_SESSION['school_elementary_meta'])) {

				unset($_SESSION['school_elementary_meta']);

			}

		}

		//*****selected_school_middle*****

		//school_middle_meta_value

		$school_middle = '';

		if ($school_middle_meta_value && $school_middle_meta_value != '' && !empty($school_middle_meta_value)) {

			if ($searchScope == 'new') {

				if ($selected_school_middle) {

					if ($selected_school_middle != '') {

						$school_middle = $selected_school_middle;

						$school_middle = str_replace(",", "~", $school_middle);

					}

				}

				if ($_SESSION['school_middle_meta']) {

					unset($_SESSION['school_middle_meta']);

				}

			} else {

				if ($_SESSION['school_middle_meta']) {

					if ($searchScope == 'map') {

						unset($_SESSION['school_middle_meta']);

					}

				} else {

					if ($searchScope != 'map') {

						$_SESSION['school_middle_meta'] = $school_middle_meta_value[0];

					}

				}

				if ($searchScope != 'map') {

					$school_middle = $school_middle_meta_value[0];

					$school_middle = str_replace(",", "~", $school_middle);

				}

			}

		} else {

			if ($selected_school_middle) {

				if ($selected_school_middle != '') {

					if ($searchScope != 'map') {

						$school_middle = $selected_school_middle;

						$school_middle = str_replace("~", ",", $school_middle);

					}

				}

			}

			if (isset($_SESSION['school_middle_meta'])) {

				unset($_SESSION['school_middle_meta']);

			}

		}

		//*****selected_school_high*****

		//school_high_meta_value

		$school_high = '';

		if ($school_high_meta_value && $school_high_meta_value != '' && !empty($school_high_meta_value)) {

			if ($searchScope == 'new') {

				if ($selected_school_high) {

					if ($selected_school_high != '') {

						$school_high = $selected_school_high;

						$school_high = str_replace(",", "~", $school_high);

					}

				}

				if ($_SESSION['school_high_meta']) {

					unset($_SESSION['school_high_meta']);

				}

			} else {

				if ($_SESSION['school_high_meta']) {

					if ($searchScope == 'map') {

						unset($_SESSION['school_high_meta']);

					}

				} else {

					if ($searchScope != 'map') {

						$_SESSION['school_high_meta' ] =  $school_high_meta_value[0];

					}

				}

				if ($searchScope != 'map') {

					$school_high = $school_high_meta_value[0];

					$school_high = str_replace(",", "~", $school_high);

				}

			}

		} else {

			if ($selected_school_high) {

				if ($selected_school_high != '') {

					if ($searchScope != 'map') {

						$school_high = $selected_school_high;

						$school_high = str_replace("~", ",", $school_high);

					}

				}

			}

			if (isset($_SESSION['school_high_meta'])) {

				unset($_SESSION['school_high_meta']);

			}

		}

		//*****selected_subdivision*****

		//subdivision_meta_value

		$subdivision = '';

		if ($subdivision_meta_value && $subdivision_meta_value != '' && !empty($subdivision_meta_value)) {

			if ($searchScope == 'new') {

				if ($selected_subdivision) {

					if ($selected_subdivision != '') {

						$subdivision = $selected_subdivision;

					}

				}

				if ($_SESSION['subdivision_meta']) {

					unset($_SESSION['subdivision_meta']);

				}

			} else {

				if ($_SESSION['subdivision_meta']) {

					if ($searchScope == 'map') {

						unset($_SESSION['subdivision_meta']);

					}

				} else {

					if ($searchScope != 'map') {

						$_SESSION['subdivision_meta'] =  $subdivision_meta_value[0];

					}

				}

				if ($searchScope != 'map') {

					$subdivision = $subdivision_meta_value[0];

				}

			}

		} else {

			if ($selected_subdivision) {

				if ($selected_subdivision != '') {

					if ($searchScope != 'map') {

						$subdivision = $selected_subdivision;

					}

				}

			}

			if (isset($_SESSION['subdivision_meta'])) {

				unset($_SESSION['subdivision_meta']);

			}

		}

		//*****selected_land_description*****

		$land_description = '';

		$land_count = 0;

		if ($selected_land_description) {

			if ($selected_land_description != '') {

				foreach ($selected_land_description as $selectedOption){

					if ($land_count == 0) {

						$land_description = $selectedOption;

					} else {

						$land_description .= '~'.$selectedOption;

					}

					$land_count++;

				}

			}

		}

		//*****price_range*****

		//price_range_meta_value

		//the price_range should always be new

		//and if both values are zero, we should set our variable to blank

		//to be used for decisions in the main_search session variable

		$price_range = '';

		if ($price_range_meta_value && $price_range_meta_value[0] != '' && !empty($price_range_meta_value)) {

			if ($searchScope == 'new') {

				if ($selected_price_min || $selected_price_max) {

					//force min and max to have zero if they happen to come in blank

					if ($selected_price_min == '') {

						$selected_price_min = 0;

					}

					if ($selected_price_max == '' || $selected_price_max == 0) {

						$selected_price_max = 1000000000;

					}

					$price_range = $selected_price_min.'--'.$selected_price_max;

				}

				if ($_SESSION['price_range_meta']) {

					unset($_SESSION['price_range_meta']);

				}

			} else {

				if (isset($_SESSION['price_range_meta'])) {

					//nothing

				} else {

					//$_SESSION['price_range_meta'] = $price_range_meta_value[0];

				}

				$_SESSION['price_range_meta'] = $price_range_meta_value[0];

				$temp_price_range = $price_range_meta_value[0];

				$price_range_arr = explode('_', $temp_price_range);

				$price_min = $price_range_arr[0];

				$price_max = $price_range_arr[1];

				$price_range = $price_min.'--'.$price_max;

			}

		} else {

			if ($selected_price_min || $selected_price_max) {

				//force min and max to have zero if they happen to come in blank

				if ($selected_price_min == '') {

					$selected_price_min = 0;

				}

				if ($selected_price_max == '' || $selected_price_max == 0) {

					$selected_price_max = 1000000000;

				}

				$price_range = $selected_price_min.'--'.$selected_price_max;

			}

			if (isset($_SESSION['price_range_meta'])) {

				unset($_SESSION['price_range_meta']);

			}

		}

		//*****selected_bedrooms*****

		//the selected_bedrooms should always be new

		//to be used for decisions in the main_search session variable

		$bedrooms = '';

		if ($selected_bedrooms) {

			if ($selected_bedrooms != '') {

				$bedrooms = $selected_bedrooms;

			}

		}

		//*****selected_bathrooms*****

		//the selected_bathrooms should always be new

		//to be used for decisions in the main_search session variable

		$bathrooms = '';

		if ($selected_bathrooms) {

			if ($selected_bathrooms != '') {

				$bathrooms = $selected_bathrooms;

			}

		}

		//*****acreage*****

		//the acreage should always be new

		//and if both values are zero, we should set our variable to blank

		//to be used for decisions in the main_search session variable

		$acreage = '';

		if ($selected_acreage_min || $selected_acreage_max) {

			//force min and max to have zero if they happen to come in blank

			if ($selected_acreage_min == '') {

				$selected_acreage_min = 0;

			}

			if ($selected_acreage_max == '') {

				$selected_acreage_max = 0;

			}

			$acreage = $selected_acreage_min.'--'.$selected_acreage_max;

		}

		//*****square_feet*****

		//finished_sqft_meta_value

		//the square_feet should always be new

		//and if both values are zero, we should set our variable to blank

		//to be used for decisions in the main_search session variable

		$square_feet = '';

		if ($finished_sqft_meta_value && $finished_sqft_meta_value != '' && !empty($finished_sqft_meta_value)) {

			if ($searchScope == 'new') {

				if ($selected_square_feet_min || $selected_square_feet_max) {

					//force min and max to have zero if they happen to come in blank

					if ($selected_square_feet_min == '') {

						$selected_square_feet_min = 0;

					}

					if ($selected_square_feet_max == '') {

						$selected_square_feet_max = 1000000000;

					}

					$square_feet = $selected_square_feet_min.'--'.$selected_square_feet_max;

				}

				if ($_SESSION['square_feet_meta']) {

					unset($_SESSION['square_feet_meta']);

				}

			} else {

				if ($_SESSION['square_feet_meta']) {

					//nothing

				} else {

					$_SESSION['square_feet_meta']= $finished_sqft_meta_value[0];

				}

				$temp_square_feet = $finished_sqft_meta_value[0];

				$square_feet_arr = explode('_', $temp_square_feet);

				$square_feet_min = $square_feet_arr[0];

				$square_feet_max = $square_feet_arr[1];

				$square_feet = $square_feet_min.'--'.$square_feet_max;

			}

		} else {

			if ($selected_square_feet_min || $selected_square_feet_max) {

				//force min and max to have zero if they happen to come in blank

				if ($selected_square_feet_min == '') {

					$selected_square_feet_min = 0;

				}

				if ($selected_square_feet_max == '') {

					$selected_square_feet_max = 1000000000;

				}

				$square_feet = $selected_square_feet_min.'--'.$selected_square_feet_max;

			}

			if (isset($_SESSION['square_feet_meta'])) {

				unset($_SESSION['square_feet_meta']);

			}

		}

		//*****year_built*****

		//year_built_meta_value

		$year_built = '';

		if ($year_built_meta_value && $year_built_meta_value != '' && !empty($year_built_meta_value)) {

			if ($searchScope == 'new') {

				if ($selected_year_built) {

					if ($selected_year_built != '') {

						$year_built = $selected_year_built;

					}

				}

				if ($_SESSION['year_built_meta']) {

					unset($_SESSION['year_built_meta']);

				}

			} else {

				if ($_SESSION['year_built_meta']) {

					//nothing

				} else {

					$_SESSION['year_built_meta'] =  $year_built_meta_value[0];

				}

				$year_built = $year_built_meta_value[0];

			}

		} else {

			if ($selected_year_built_min || $selected_year_built_max) {

				//force min and max to have zero if they happen to come in blank

				if ($selected_year_built_min == '') {

					$selected_year_built_min = 0;

				}

				if ($selected_year_built_max == '') {

					$selected_year_built_max = 0;

				}

				$year_built = $selected_year_built_min.'--'.$selected_year_built_max;

			}

		}

		/*//*****year_built*****

		//the year_built should always be new

		//and if both values are zero, we should set our variable to blank

		//to be used for decisions in the main_search session variable

		$year_built = '';

		if ($selected_year_built_min || $selected_year_built_max) {

			//force min and max to have zero if they happen to come in blank

			if ($selected_year_built_min == '') {

				$selected_year_built_min = 0;

			}

			if ($selected_year_built_max == '') {

				$selected_year_built_max = 0;

			}

			$year_built = $selected_year_built_min.'--'.$selected_year_built_max;

		}*/

		//*****selected_elem*****

		$elem = '';

		if ($selected_elem) {

			if ($selected_elem != '') {

				$elem = $selected_elem;

			}

		}

		//*****selected_middle*****

		$middle = '';

		if ($selected_middle) {

			if ($selected_middle != '') {

				$middle = $selected_middle;

			}

		}

		//*****selected_high*****

		$high = '';

		if ($selected_high) {

			if ($selected_high != '') {

				$high = $selected_high;

			}

		}

		//*****selected_open_house_only*****

		$open_house_selected = '';

		if ($selected_open_house_only) {

			if ($selected_open_house_only != '') {

				$open_house_selected = $selected_open_house_only;

			}

		}

		//*****selected_price_reduced_only*****

		$price_reduced_selected = '';

		if ($selected_price_reduced_only) {

			if ($selected_price_reduced_only != '') {

				$price_reduced_selected = $selected_price_reduced_only;

			}

		}

		//*****selected_new_listings_only*****

		$new_listing_selected = '';

		if ($selected_new_listings_only) {

			if ($selected_new_listings_only != '') {

				$new_listing_selected = $selected_new_listings_only;

			}

		}

		//office_meta_value

		$office = '';

		if ($office_meta_value && $office_meta_value != '' && !empty($office_meta_value)) {

			if ($searchScope == 'new') {

				$office = '';

			} else {

				$temp_office = $office_meta_value[0];

				$office_arr = explode(',', $temp_office);

				$ftr_count = 0;

				foreach ($office_arr as $ftr) {

					if ($ftr_count == 0) {

						$office = $ftr;

					} else {

						$office .= '~'.$ftr;

					}

					$ftr_count++;

				}

				//$office = $office_meta_value[0];

			}

		}

		//agent_meta_value

		$agent = '';

		if ($agent_meta_value && $agent_meta_value != '' && !empty($agent_meta_value)) {

			if ($searchScope == 'new') {

				$agent = '';

			} else {

				$agent = $agent_meta_value[0];

			}

		}

		//short_sale_meta_value

		$short_sale = '';

		if ($short_sale_meta_value && $short_sale_meta_value != '' && !empty($short_sale_meta_value)) {

			if ($searchScope == 'new') {

				$short_sale = '';

			} else {

				$short_sale = $short_sale_meta_value[0];

			}

		}

		//foreclosure_meta_value

		$foreclosure = '';

		if ($foreclosure_meta_value && $foreclosure_meta_value != '' && !empty($foreclosure_meta_value)) {

			if ($searchScope == 'new') {

				$foreclosure = '';

			} else {

				$foreclosure = $foreclosure_meta_value[0];

			}

		}



	

		//listing_date_meta_value

		$listing_date = '';

		if ($listing_date_meta_value && $listing_date_meta_value != '' && !empty($listing_date_meta_value)) {

			if ($searchScope == 'new') {

				$listing_date = '';

			} else {

				$temp_listing_date = $listing_date_meta_value[0];

				$listing_date_arr = explode('_', $temp_listing_date);

				$listing_date_min = $listing_date_arr[0];

				$listing_date_max = $listing_date_arr[1];

				$listing_date = $listing_date_min.'--'.$listing_date_max;

			}

		}

		//farm_features_meta_value

		$farm_features = '';

		//if ($property_type == 'residential') {

			if ($farm_features_meta_value && $farm_features_meta_value != '' && !empty($farm_features_meta_value)) {

				if ($searchScope == 'new') {

					$farm_features = '';

				} else {

					$temp_farm_features = $farm_features_meta_value[0];

					$farm_features_arr = explode(',', $temp_farm_features);

					$ftr_count = 0;

					foreach ($farm_features_arr as $ftr) {

						if ($ftr_count == 0) {

							$farm_features = $ftr;

						} else {

							$farm_features .= '~'.$ftr;

						}

						$ftr_count++;

					}

				}

			}

		//}

		//acres_meta_value

		$acreage = '';

		if ($acreage_meta_value && $acreage_meta_value != '' && !empty($acreage_meta_value)) {

			if ($searchScope == 'new') {

				$acreage = '';

			} else {

				$temp_acres = $acreage_meta_value[0];

				$acreage_arr = explode('_', $temp_acres);

				$acreage_min = $acreage_arr[0];

				$acreage_max = $acreage_arr[1];

				$acreage = $acreage_min.'--'.$acreage_max;

			}

		}

		//search_term_meta_value

		$search_term = '';

		if ($search_term_meta_value && $search_term_meta_value != '' && !empty($search_term_meta_value)) {

			if ($searchScope == 'new') {

				$search_term = '';

			} else {

				$temp_search_term = $search_term_meta_value[0];

				$search_term_arr = explode(',', $temp_search_term);

				$ftr_count = 0;

				foreach ($search_term_arr as $ftr) {

					if ($ftr_count == 0) {

						$search_term = $ftr;

					} else {

						$search_term .= '~'.$ftr;

					}

					$ftr_count++;

				}

			}

		}

		//searchbox_toggle_meta_value

		$searchbox_toggle = '';

		if ($searchbox_toggle_meta_value && $searchbox_toggle_meta_value != '' && !empty($searchbox_toggle_meta_value)) {

			if ($searchScope == 'new') {

				$searchbox_toggle = '';

			} else {

				$searchbox_toggle = $searchbox_toggle_meta_value[0];

			}

		}

		//max_year_built_meta_value

		$max_year_built = '';

		if ($max_year_built_meta_value && $max_year_built_meta_value != '' && !empty($max_year_built_meta_value)) {

			if ($searchScope == 'new') {

				$max_year_built = '';

			} else {

				$temp_max_year_built = $max_year_built_meta_value[0];

				$max_year_built_arr = explode(',', $temp_max_year_built);

				$ftr_count = 0;

				foreach ($max_year_built_arr as $ftr) {

					if ($ftr_count == 0) {

						$max_year_built = $ftr;

					} else {

						$max_year_built .= '~'.$ftr;

					}

					$ftr_count++;

				}

			}

		}

		//map_toggle_meta_value

		$map_toggle = '';

		if ($map_toggle_meta_value && $map_toggle_meta_value != '' && !empty($map_toggle_meta_value)) {

			$map_toggle = $map_toggle_meta_value[0];

		} else {

			$map_toggle = 'no';

		}

		//max_listing_meta_value

		$max_listing = '';

		if ($max_listing_meta_value && $max_listing_meta_value != '' && !empty($max_listing_meta_value)) {

				$max_listing = $max_listing_meta_value[0];

		}





		//virtual_tour_meta_value added by vikas..

		// setting new filters here sent to model as new paramter by vikas...

		$new_filters=array();

		$new_filters['virtual_tours_only'] = '0';

		if ($virtual_tours_only_meta_value && $virtual_tours_only_meta_value != '' && !empty($virtual_tours_only_meta_value)) {

			$new_filters['virtual_tours_only']=$virtual_tours_only_meta_value;

		}

		

		$new_filters['farms_only']='0';

		if ($farms_only_meta_value && $farms_only_meta_value != '' && !empty($farms_only_meta_value)) {

			$new_filters['farms_only']=$farms_only_meta_value;

		}







		//set main_search

		//*****map search*****

		if ($searchScope == 'map') {

			//do nothing

		//*****refine search*****

		} elseif ($searchScope == 'refine' || $searchScope == '') {

			//set main_search session value for the main_search input

			if(isset($_POST['main_search'])) {

				if ($_SESSION['main_search']) {

					$_SESSION['main_search'] =  $_SESSION['main_search'].','.$_POST['main_search'];

					$main_search = $_SESSION['main_search'];

				} else {

					$_SESSION['main_search']= $_POST['main_search'];

					$main_search = $_SESSION['main_search'];

				}

			//nothing from the input for main_search

			} else {

				if (isset($_SESSION['main_search'])) {

					$main_search = $_SESSION['main_search'];

				} else {

					$main_search = '';

				}

			}

		//*****new search*****

		} else {

			//we are not searching open houses or new listings at this point

			unset($_SESSION['special']);

			//clear the main_search session variable and reload it with the new search data

			if($_POST['main_search']) {

				if ($_SESSION['main_search']) {

					unset($_SESSION['main_search']);

					$_SESSION['main_search'] =  $_POST['main_search'];

					$main_search = $_SESSION['main_search'];

				} else {

					$_SESSION['main_search'] =  $_POST['main_search'];

					$main_search = $_SESSION['main_search'];

				}

			} else {

				if ($_SESSION['main_search']) {

					unset($_SESSION['main_search']);

					$main_search = '';

				}

			}

		}

		//*****extended search*****

		//********************************************************************************************************************************************************

		//selected_subcategory_residential

		//if we have a subcategory_residential, let's add it

		if ($subcategory_residential != '') {

			if ($_SESSION['main_search']) {

				$search_string = $_SESSION['main_search'];

				$find = 'selected_subcategory_residential/';

				$pos = strpos($search_string,$find);

				//if it's not already in the search, then let's add it

				if ($pos === false) {

					$_SESSION['main_search']=  $_SESSION['main_search'].',selected_subcategory_residential/'.$subcategory_residential;

				//if the string is in the search, let's replace it

				} else {

					$offset = $pos + strlen($find);

					$new_find = '/';

					$new_pos = strpos($search_string, $new_find, $offset);

					if ($new_pos === false) {

						$newnew_find = ',';

						$newnew_pos = strpos($search_string, $newnew_find, $offset);

						if ($newnew_pos === false) {

							if ($pos == 0) {

								$range_substring = substr($search_string, 0);

								$new_search_string = str_replace($range_substring, 'selected_subcategory_residential/'.$subcategory_residential, $search_string);

								$_SESSION['main_search'] =  $new_search_string;

							} else {

								$range_substring = substr($search_string, $pos);

								$new_search_string = str_replace($range_substring, 'selected_subcategory_residential/'.$subcategory_residential, $search_string);

								$_SESSION['main_search'] = $new_search_string;

							}

						} else {

							$range_substring = substr($search_string, $pos, ($newnew_pos - $pos));

							$new_search_string = str_replace($range_substring, 'selected_subcategory_residential/'.$subcategory_residential, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						}

						/*$range_substring = substr($search_string, 0);

						$new_search_string = str_replace($range_substring, 'selected_subcategory_residential/'.$subcategory_residential, $search_string);

						$this->session->set_userdata('main_search', $new_search_string);*/

					} else {

						$newnew_find = ',';

						$newnew_pos = strpos($search_string, $newnew_find, $offset);

						if ($newnew_pos === false) {

							$range_substring = substr($search_string, $pos, $new_pos);

							$new_search_string = str_replace($range_substring, 'selected_subcategory_residential/'.$subcategory_residential, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						} else {

							$range_substring = substr($search_string, $pos, ($newnew_pos - $pos));

							$new_search_string = str_replace($range_substring, 'selected_subcategory_residential/'.$subcategory_residential, $search_string);

							$_SESSION['main_search'] =  $new_search_string;

						}

					}

				}

			//the main_search session variable is not set, so let's set it

			} else {

				$_SESSION['main_search'] = 'selected_subcategory_residential/'.$subcategory_residential;

			}

		}

		//if we don't have a subcategory_residential then, either

		//	* the main_search session variable is set and it already includes a subcategory_residential - since we have a blank subcategory_residential, we wouldn't change it

		// 	* the main_search session variable is set and it does not include a subcategory_residential - since we have a blank subcategory_residential, we wouldn't add one

		// 	* the main_search session variable is not set - since we have a blank subcategory_residential, we do not need to create the main_search session variable

		//********************************************************************************************************************************************************

		//selected_subcategory_commercial_sale

		//if we have a subcategory_commercial, let's add it

		if ($subcategory_commercial != '') {

			if ($_SESSION['main_search']) {

				$search_string = $_SESSION['main_search'];

				$find = 'selected_subcategory_commercial_sale/';

				$pos = strpos($search_string,$find);

				//if it's not already in the search, then let's add it

				if ($pos === false) {

					$_SESSION['main_search'] = $_SESSION['main_search'].',selected_subcategory_commercial_sale/'.$subcategory_commercial;

				//if the string is in the search, let's replace it

				} else {

					$offset = $pos + strlen($find);

					$new_find = '/';

					$new_pos = strpos($search_string, $new_find, $offset);

					if ($new_pos === false) {

						$newnew_find = ',';

						$newnew_pos = strpos($search_string, $newnew_find, $offset);

						if ($newnew_pos === false) {

							if ($pos == 0) {

								$range_substring = substr($search_string, 0);

								$new_search_string = str_replace($range_substring, 'selected_subcategory_commercial_sale/'.$subcategory_commercial, $search_string);

								$_SESSION['main_search'] = $new_search_string;

							} else {

								$range_substring = substr($search_string, $pos);

								$new_search_string = str_replace($range_substring, 'selected_subcategory_commercial_sale/'.$subcategory_commercial, $search_string);

								$_SESSION['main_search'] =  $new_search_string;

							}

						} else {

							$range_substring = substr($search_string, $pos, ($newnew_pos - $pos));

							$new_search_string = str_replace($range_substring, 'selected_subcategory_commercial_sale/'.$subcategory_commercial, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						}

						/*$range_substring = substr($search_string, 0);

						$new_search_string = str_replace($range_substring, 'selected_subcategory_commercial_sale/'.$subcategory_commercial, $search_string);

						$this->session->set_userdata('main_search', $new_search_string);*/

					} else {

						$newnew_find = ',';

						$newnew_pos = strpos($search_string, $newnew_find, $offset);

						if ($newnew_pos === false) {

							$range_substring = substr($search_string, $pos, $new_pos);

							$new_search_string = str_replace($range_substring, 'selected_subcategory_commercial_sale/'.$subcategory_commercial, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						} else {

							$range_substring = substr($search_string, $pos, ($newnew_pos - $pos));

							$new_search_string = str_replace($range_substring, 'selected_subcategory_commercial_sale/'.$subcategory_commercial, $search_string);

							$_SESSION['main_search'] =  $new_search_string;

						}

					}

				}

			//the main_search session variable is not set, so let's set it

			} else {

				$_SESSION['main_search'] = 'selected_subcategory_commercial_sale/'.$subcategory_commercial;

			}

		}

		//if we don't have a subcategory_commercial then, either

		//	* the main_search session variable is set and it already includes a subcategory_commercial - since we have a blank subcategory_commercial, we wouldn't change it

		// 	* the main_search session variable is set and it does not include a subcategory_commercial - since we have a blank subcategory_commercial, we wouldn't add one

		// 	* the main_search session variable is not set - since we have a blank subcategory_commercial, we do not need to create the main_search session variable

		//********************************************************************************************************************************************************

		//selected_area

		//if we have an area, let's add it

		if ($area != '') {

			if ($_SESSION['main_search']) {

				$search_string = $_SESSION['main_search'];

				$find = 'selected_area/';

				$pos = strpos($search_string,$find);

				//if it's not already in the search, then let's add it

				if ($pos === false) {

					$_SESSION['main_search'] = $_SESSION['main_search'].',selected_area/'.$area;

				//if the string is in the search, let's replace it

				} else {

					$offset = $pos + strlen($find);

					$new_find = '/';

					$new_pos = strpos($search_string, $new_find, $offset);

					if ($new_pos === false) {

						$newnew_find = ',';

						$newnew_pos = strpos($search_string, $newnew_find, $offset);

						if ($newnew_pos === false) {

							if ($pos == 0) {

								$range_substring = substr($search_string, 0);

								$new_search_string = str_replace($range_substring, 'selected_area/'.$area, $search_string);

								$_SESSION['main_search'] = $new_search_string;

							} else {

								$range_substring = substr($search_string, $pos);

								$new_search_string = str_replace($range_substring, 'selected_area/'.$area, $search_string);

								$_SESSION['main_search'] = $new_search_string;

							}

						} else {

							$range_substring = substr($search_string, $pos, ($newnew_pos - $pos));

							$new_search_string = str_replace($range_substring, 'selected_area/'.$area, $search_string);

							$_SESSION['main_search'] =  $new_search_string;

						}

						/*$range_substring = substr($search_string, 0);

						$new_search_string = str_replace($range_substring, 'selected_area/'.$area, $search_string);

						$this->session->set_userdata('main_search', $new_search_string);*/

					} else {

						$newnew_find = ',';

						$newnew_pos = strpos($search_string, $newnew_find, $offset);

						if ($newnew_pos === false) {

							$range_substring = substr($search_string, $pos, $new_pos);

							$new_search_string = str_replace($range_substring, 'selected_area/'.$area, $search_string);

							$_SESSION['main_search'] =  $new_search_string;

						} else {

							$range_substring = substr($search_string, $pos, ($newnew_pos - $pos));

							$new_search_string = str_replace($range_substring, 'selected_area/'.$area, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						}

					}

				}

			//the main_search session variable is not set, so let's set it

			} else {

				$_SESSION['main_search'] = 'selected_area/'.$area;

			}

		}

		//if we don't have a subcategory_commercial then, either

		//	* the main_search session variable is set and it already includes a subcategory_commercial - since we have a blank subcategory_commercial, we wouldn't change it

		// 	* the main_search session variable is set and it does not include a subcategory_commercial - since we have a blank subcategory_commercial, we wouldn't add one

		// 	* the main_search session variable is not set - since we have a blank subcategory_commercial, we do not need to create the main_search session variable

		//********************************************************************************************************************************************************

		//selected_mlsID

		//if we have an mlsID, let's add it

		if ($mlsID != '') {

			if ($_SESSION['main_search']) {

				$search_string = $_SESSION['main_search'];

				$find = 'selected_mlsID/';

				$pos = strpos($search_string,$find);

				//if it's not already in the search, then let's add it

				if ($pos === false) {

					$_SESSION['main_search'] = $_SESSION['main_search'].',selected_mlsID/'.$mlsID;

				//if the string is in the search, let's replace it

				} else {

					$offset = $pos + strlen($find);

					$new_find = '/';

					$new_pos = strpos($search_string, $new_find, $offset);

					if ($new_pos === false) {

						$newnew_find = ',';

						$newnew_pos = strpos($search_string, $newnew_find, $offset);

						if ($newnew_pos === false) {

							if ($pos == 0) {

								$range_substring = substr($search_string, 0);

								$new_search_string = str_replace($range_substring, 'selected_mlsID/'.$mlsID, $search_string);

								$_SESSION['main_search'] = $new_search_string;

							} else {

								$range_substring = substr($search_string, $pos);

								$new_search_string = str_replace($range_substring, 'selected_mlsID/'.$mlsID, $search_string);

								$_SESSION['main_search'] = $new_search_string;

							}

						} else {

							$range_substring = substr($search_string, $pos, ($newnew_pos - $pos));

							$new_search_string = str_replace($range_substring, 'selected_mlsID/'.$mlsID, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						}

					} else {

						$newnew_find = ',';

						$newnew_pos = strpos($search_string, $newnew_find, $offset);

						if ($newnew_pos === false) {

							$range_substring = substr($search_string, $pos, $new_pos);

							$new_search_string = str_replace($range_substring, 'selected_mlsID/'.$mlsID, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						} else {

							$range_substring = substr($search_string, $pos, ($newnew_pos - $pos));

							$new_search_string = str_replace($range_substring, 'selected_mlsID/'.$mlsID, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						}

					}

				}

			//the main_search session variable is not set, so let's set it

			} else {

				$_SESSION['main_search'] = 'selected_mlsID/'.$mlsID;

			}

		}

		//if we don't have a subcategory_commercial then, either

		//	* the main_search session variable is set and it already includes a subcategory_commercial - since we have a blank subcategory_commercial, we wouldn't change it

		// 	* the main_search session variable is set and it does not include a subcategory_commercial - since we have a blank subcategory_commercial, we wouldn't add one

		// 	* the main_search session variable is not set - since we have a blank subcategory_commercial, we do not need to create the main_search session variable

		//********************************************************************************************************************************************************

		//selected_builder

		//if we have builder, let's add it

		if ($builder != '') {

			if ($_SESSION['main_search']) {

				$search_string = $_SESSION['main_search'];

				$find = 'selected_builder/';

				$pos = strpos($search_string,$find);

				//if it's not already in the search, then let's add it

				if ($pos === false) {

					$_SESSION['main_search'] =  $_SESSION['main_search'].',selected_builder/'.$builder;

				//if the string is in the search, let's replace it

				} else {

					$offset = $pos + strlen($find);

					$new_find = '/';

					$new_pos = strpos($search_string, $new_find, $offset);

					if ($new_pos === false) {

						$newnew_find = ',';

						$newnew_pos = strpos($search_string, $newnew_find, $offset);

						if ($newnew_pos === false) {

							if ($pos == 0) {

								$range_substring = substr($search_string, 0);

								$new_search_string = str_replace($range_substring, 'selected_builder/'.$builder, $search_string);

								$_SESSION['main_search'] = $new_search_string;

							} else {

								$range_substring = substr($search_string, $pos);

								$new_search_string = str_replace($range_substring, 'selected_builder/'.$builder, $search_string);

								$_SESSION['main_search'] = $new_search_string;

							}

						} else {

							$range_substring = substr($search_string, $pos, ($newnew_pos - $pos));

							$new_search_string = str_replace($range_substring, 'selected_builder/'.$builder, $search_string);

							$_SESSION['main_search'] =  $new_search_string;

						}

						/*$range_substring = substr($search_string, 0);

						$new_search_string = str_replace($range_substring, 'selected_builder/'.$builder, $search_string);

						$this->session->set_userdata('main_search', $new_search_string);*/

					} else {

						$newnew_find = ',';

						$newnew_pos = strpos($search_string, $newnew_find, $offset);

						if ($newnew_pos === false) {

							$range_substring = substr($search_string, $pos, $new_pos);

							$new_search_string = str_replace($range_substring, 'selected_builder/'.$builder, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						} else {

							$range_substring = substr($search_string, $pos, ($newnew_pos - $pos));

							$new_search_string = str_replace($range_substring, 'selected_builder/'.$builder, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						}

					}

				}

			//the main_search session variable is not set, so let's set it

			} else {

				$_SESSION['main_search'] = 'selected_builder/'.$builder;

			}

		}

		//if we don't have a subcategory_commercial then, either

		//	* the main_search session variable is set and it already includes a subcategory_commercial - since we have a blank subcategory_commercial, we wouldn't change it

		// 	* the main_search session variable is set and it does not include a subcategory_commercial - since we have a blank subcategory_commercial, we wouldn't add one

		// 	* the main_search session variable is not set - since we have a blank subcategory_commercial, we do not need to create the main_search session variable

		//********************************************************************************************************************************************************

		//selected_subcategory

		//if we have subcategory, let's add it

		if ($subcategory != '') {

			if ($_SESSION['main_search']) {

				$search_string = $_SESSION['main_search'];

				$find = 'selected_subcategory/';

				$pos = strpos($search_string,$find);

				//if it's not already in the search, then let's add it

				if ($pos === false) {

					$_SESSION['main_search'] =  $_SESSION['main_search'].',selected_subcategory/'.$subcategory;

				//if the string is in the search, let's replace it

				} else {

					$offset = $pos + strlen($find);

					$new_find = '/';

					$new_pos = strpos($search_string, $new_find, $offset);

					if ($new_pos === false) {

						$newnew_find = ',';

						$newnew_pos = strpos($search_string, $newnew_find, $offset);

						if ($newnew_pos === false) {

							if ($pos == 0) {

								$range_substring = substr($search_string, 0);

								$new_search_string = str_replace($range_substring, 'selected_subcategory/'.$subcategory, $search_string);

								$_SESSION['main_search'] = $new_search_string;

							} else {

								$range_substring = substr($search_string, $pos);

								$new_search_string = str_replace($range_substring, 'selected_subcategory/'.$subcategory, $search_string);

								$_SESSION['main_search'] = $new_search_string;

							}

						} else {

							$range_substring = substr($search_string, $pos, ($newnew_pos - $pos));

							$new_search_string = str_replace($range_substring, 'selected_subcategory/'.$subcategory, $search_string);

							$_SESSION['main_search'] =  $new_search_string;

						}

						/*$range_substring = substr($search_string, 0);

						$new_search_string = str_replace($range_substring, 'selected_subcategory/'.$subcategory, $search_string);

						$this->session->set_userdata('main_search', $new_search_string);*/

					} else {

						$newnew_find = ',';

						$newnew_pos = strpos($search_string, $newnew_find, $offset);

						if ($newnew_pos === false) {

							$range_substring = substr($search_string, $pos, $new_pos);

							$new_search_string = str_replace($range_substring, 'selected_subcategory/'.$subcategory, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						} else {

							$range_substring = substr($search_string, $pos, ($newnew_pos - $pos));

							$new_search_string = str_replace($range_substring, 'selected_subcategory/'.$subcategory, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						}

					}

				}

			//the main_search session variable is not set, so let's set it

			} else {

				$_SESSION['main_search'] = 'selected_subcategory/'.$subcategory;

			}

		}

		//if we don't have a subcategory_commercial then, either

		//	* the main_search session variable is set and it already includes a subcategory_commercial - since we have a blank subcategory_commercial, we wouldn't change it

		// 	* the main_search session variable is set and it does not include a subcategory_commercial - since we have a blank subcategory_commercial, we wouldn't add one

		// 	* the main_search session variable is not set - since we have a blank subcategory_commercial, we do not need to create the main_search session variable

		//********************************************************************************************************************************************************

		//selected_level

		//if we have level, let's add it

		if ($level != '') {

			if ($_SESSION['main_search']) {

				$search_string = $_SESSION['main_search'];

				$find = 'selected_level/';

				$pos = strpos($search_string,$find);

				//if it's not already in the search, then let's add it

				if ($pos === false) {

					$_SESSION['main_search'] = $_SESSION['main_search'].',selected_level/'.$level;

				//if the string is in the search, let's replace it

				} else {

					$offset = $pos + strlen($find);

					$new_find = '/';

					$new_pos = strpos($search_string, $new_find, $offset);

					if ($new_pos === false) {

						$newnew_find = ',';

						$newnew_pos = strpos($search_string, $newnew_find, $offset);

						if ($newnew_pos === false) {

							if ($pos == 0) {

								$range_substring = substr($search_string, 0);

								$new_search_string = str_replace($range_substring, 'selected_level/'.$level, $search_string);

								$_SESSION['main_search'] = $new_search_string;

							} else {

								$range_substring = substr($search_string, $pos);

								$new_search_string = str_replace($range_substring, 'selected_level/'.$level, $search_string);

								$_SESSION['main_search'] =  $new_search_string;

							}

						} else {

							$range_substring = substr($search_string, $pos, ($newnew_pos - $pos));

							$new_search_string = str_replace($range_substring, 'selected_level/'.$level, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						}

						/*$range_substring = substr($search_string, 0);

						$new_search_string = str_replace($range_substring, 'selected_level/'.$level, $search_string);

						$this->session->set_userdata('main_search', $new_search_string);*/

					} else {

						$newnew_find = ',';

						$newnew_pos = strpos($search_string, $newnew_find, $offset);

						if ($newnew_pos === false) {

							$range_substring = substr($search_string, $pos, $new_pos);

							$new_search_string = str_replace($range_substring, 'selected_level/'.$level, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						} else {

							$range_substring = substr($search_string, $pos, ($newnew_pos - $pos));

							$new_search_string = str_replace($range_substring, 'selected_level/'.$level, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						}

					}

				}

			//the main_search session variable is not set, so let's set it

			} else {

				$_SESSION['main_search'] = 'selected_level/'.$level;

			}

		}

		//if we don't have a subcategory_commercial then, either

		//	* the main_search session variable is set and it already includes a subcategory_commercial - since we have a blank subcategory_commercial, we wouldn't change it

		// 	* the main_search session variable is set and it does not include a subcategory_commercial - since we have a blank subcategory_commercial, we wouldn't add one

		// 	* the main_search session variable is not set - since we have a blank subcategory_commercial, we do not need to create the main_search session variable

		//********************************************************************************************************************************************************

		//selected_school_elementary

		//if we have school_elementary, let's add it

		if ($school_elementary != '') {

			if ($_SESSION['main_search']) {

				$search_string = $_SESSION['main_search'];

				$find = 'selected_school_elementary/';

				$pos = strpos($search_string,$find);

				//if it's not already in the search, then let's add it

				if ($pos === false) {

					$_SESSION['main_search'] = $_SESSION['main_search'].',selected_school_elementary/'.$school_elementary;

				//if the string is in the search, let's replace it

				} else {

					$offset = $pos + strlen($find);

					$new_find = '/';

					$new_pos = strpos($search_string, $new_find, $offset);

					if ($new_pos === false) {

						$newnew_find = ',';

						$newnew_pos = strpos($search_string, $newnew_find, $offset);

						if ($newnew_pos === false) {

							if ($pos == 0) {

								$range_substring = substr($search_string, 0);

								$new_search_string = str_replace($range_substring, 'selected_school_elementary/'.$school_elementary, $search_string);

								$_SESSION['main_search']= $new_search_string;

							} else {

								$range_substring = substr($search_string, $pos);

								$new_search_string = str_replace($range_substring, 'selected_school_elementary/'.$school_elementary, $search_string);

								$_SESSION['main_search'] = $new_search_string;

							}

						} else {

							$range_substring = substr($search_string, $pos, ($newnew_pos - $pos));

							$new_search_string = str_replace($range_substring, 'selected_school_elementary/'.$school_elementary, $search_string);

							$_SESSION['main_search']= $new_search_string;

						}

						/*$range_substring = substr($search_string, 0);

						$new_search_string = str_replace($range_substring, 'selected_school_elementary/'.$school_elementary, $search_string);

						$this->session->set_userdata('main_search', $new_search_string);*/

					} else {

						$newnew_find = ',';

						$newnew_pos = strpos($search_string, $newnew_find, $offset);

						if ($newnew_pos === false) {

							$range_substring = substr($search_string, $pos, $new_pos);

							$new_search_string = str_replace($range_substring, 'selected_school_elementary/'.$school_elementary, $search_string);

							$_SESSION['main_search']= $new_search_string;

						} else {

							$range_substring = substr($search_string, $pos, ($newnew_pos - $pos));

							$new_search_string = str_replace($range_substring, 'selected_school_elementary/'.$school_elementary, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						}

					}

				}

			//the main_search session variable is not set, so let's set it

			} else {

				$_SESSION['main_search'] = 'selected_school_elementary/'.$school_elementary;

			}

		}

		//if we don't have a subcategory_commercial then, either

		//	* the main_search session variable is set and it already includes a subcategory_commercial - since we have a blank subcategory_commercial, we wouldn't change it

		// 	* the main_search session variable is set and it does not include a subcategory_commercial - since we have a blank subcategory_commercial, we wouldn't add one

		// 	* the main_search session variable is not set - since we have a blank subcategory_commercial, we do not need to create the main_search session variable

		//********************************************************************************************************************************************************

		//selected_school_middle

		//if we have school_middle, let's add it

		if ($school_middle != '') {

			if ($_SESSION['main_search']) {

				$search_string = $_SESSION['main_search'];

				$find = 'selected_school_middle/';

				$pos = strpos($search_string,$find);

				//if it's not already in the search, then let's add it

				if ($pos === false) {

					$_SESSION['main_search'] = $_SESSION['main_search'].',selected_school_middle/'.$school_middle;

				//if the string is in the search, let's replace it

				} else {

					$offset = $pos + strlen($find);

					$new_find = '/';

					$new_pos = strpos($search_string, $new_find, $offset);

					if ($new_pos === false) {

						$newnew_find = ',';

						$newnew_pos = strpos($search_string, $newnew_find, $offset);

						if ($newnew_pos === false) {

							if ($pos == 0) {

								$range_substring = substr($search_string, 0);

								$new_search_string = str_replace($range_substring, 'selected_school_middle/'.$school_middle, $search_string);

								$_SESSION['main_search'] = $new_search_string;

							} else {

								$range_substring = substr($search_string, $pos);

								$new_search_string = str_replace($range_substring, 'selected_school_middle/'.$school_middle, $search_string);

								$_SESSION['main_search'] = $new_search_string;

							}

						} else {

							$range_substring = substr($search_string, $pos, ($newnew_pos - $pos));

							$new_search_string = str_replace($range_substring, 'selected_school_middle/'.$school_middle, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						}

						/*$range_substring = substr($search_string, 0);

						$new_search_string = str_replace($range_substring, 'selected_school_middle/'.$school_middle, $search_string);

						$this->session->set_userdata('main_search', $new_search_string);*/

					} else {

						$newnew_find = ',';

						$newnew_pos = strpos($search_string, $newnew_find, $offset);

						if ($newnew_pos === false) {

							$range_substring = substr($search_string, $pos, $new_pos);

							$new_search_string = str_replace($range_substring, 'selected_school_middle/'.$school_middle, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						} else {

							$range_substring = substr($search_string, $pos, ($newnew_pos - $pos));

							$new_search_string = str_replace($range_substring, 'selected_school_middle/'.$school_middle, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						}

					}

				}

			//the main_search session variable is not set, so let's set it

			} else {

				$_SESSION['main_search'] = 'selected_school_middle/'.$school_middle;

			}

		}

		//if we don't have a subcategory_commercial then, either

		//	* the main_search session variable is set and it already includes a subcategory_commercial - since we have a blank subcategory_commercial, we wouldn't change it

		// 	* the main_search session variable is set and it does not include a subcategory_commercial - since we have a blank subcategory_commercial, we wouldn't add one

		// 	* the main_search session variable is not set - since we have a blank subcategory_commercial, we do not need to create the main_search session variable

		//********************************************************************************************************************************************************

		//selected_school_high

		//if we have school_high, let's add it

		if ($school_high != '') {

			if ($_SESSION['main_search']) {

				$search_string = $_SESSION['main_search'];

				$find = 'selected_school_high/';

				$pos = strpos($search_string,$find);

				//if it's not already in the search, then let's add it

				if ($pos === false) {

					$_SESSION['main_search'] = $_SESSION['main_search'].',selected_school_high/'.$school_high;

				//if the string is in the search, let's replace it

				} else {

					$offset = $pos + strlen($find);

					$new_find = '/';

					$new_pos = strpos($search_string, $new_find, $offset);

					if ($new_pos === false) {

						$newnew_find = ',';

						$newnew_pos = strpos($search_string, $newnew_find, $offset);

						if ($newnew_pos === false) {

							if ($pos == 0) {

								$range_substring = substr($search_string, 0);

								$new_search_string = str_replace($range_substring, 'selected_school_high/'.$school_high, $search_string);

								$_SESSION['main_search'] = $new_search_string;

							} else {

								$range_substring = substr($search_string, $pos);

								$new_search_string = str_replace($range_substring, 'selected_school_high/'.$school_high, $search_string);

								$_SESSION['main_search'] =  $new_search_string;

							}

						} else {

							$range_substring = substr($search_string, $pos, ($newnew_pos - $pos));

							$new_search_string = str_replace($range_substring, 'selected_school_high/'.$school_high, $search_string);

							$_SESSION['main_search'] =  $new_search_string;

						}

						/*$range_substring = substr($search_string, 0);

						$new_search_string = str_replace($range_substring, 'selected_school_high/'.$school_high, $search_string);

						$this->session->set_userdata('main_search', $new_search_string);*/

					} else {

						$newnew_find = ',';

						$newnew_pos = strpos($search_string, $newnew_find, $offset);

						if ($newnew_pos === false) {

							$range_substring = substr($search_string, $pos, $new_pos);

							$new_search_string = str_replace($range_substring, 'selected_school_high/'.$school_high, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						} else {

							$range_substring = substr($search_string, $pos, ($newnew_pos - $pos));

							$new_search_string = str_replace($range_substring, 'selected_school_high/'.$school_high, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						}

					}

				}

			//the main_search session variable is not set, so let's set it

			} else {

				$_SESSION['main_search'] = 'selected_school_high/'.$school_high;

			}

		}

		//if we don't have a area then, either

		//	* the main_search session variable is set and it already includes a area - since we have a blank area, we wouldn't change it

		// 	* the main_search session variable is set and it does not include a area - since we have a blank area, we wouldn't add one

		// 	* the main_search session variable is not set - since we have a blank area, we do not need to create the main_search session variable

		//********************************************************************************************************************************************************

		//selected_subdivision

		//if we have a subdivision, let's add it

		if ($subdivision != '') {

			if ($_SESSION['main_search']) {

				$search_string = $_SESSION['main_search'];

				$find = 'selected_subdivision/';

				$pos = strpos($search_string,$find);

				//if it's not already in the search, then let's add it

				if ($pos === false) {

					$_SESSION['main_search'] = $_SESSION['main_search'].',selected_subdivision/'.$subdivision;

				//if the string is in the search, let's replace it

				} else {

					$offset = $pos + strlen($find);

					$new_find = '/';

					$new_pos = strpos($search_string, $new_find, $offset);

					if ($new_pos === false) {

						$newnew_find = ',';

						$newnew_pos = strpos($search_string, $newnew_find, $offset);

						if ($newnew_pos === false) {

							if ($pos == 0) {

								$range_substring = substr($search_string, 0);

								$new_search_string = str_replace($range_substring, 'selected_subdivision/'.$subdivision, $search_string);

								$_SESSION['main_search'] = $new_search_string;

							} else {

								$range_substring = substr($search_string, $pos);

								$new_search_string = str_replace($range_substring, 'selected_subdivision/'.$subdivision, $search_string);

								$_SESSION['main_search'] = $new_search_string;

							}

						} else {

							$range_substring = substr($search_string, $pos, ($newnew_pos - $pos));

							$new_search_string = str_replace($range_substring, 'selected_subdivision/'.$subdivision, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						}

						/*$range_substring = substr($search_string, 0);

						$new_search_string = str_replace($range_substring, 'selected_subdivision/'.$subdivision, $search_string);

						$this->session->set_userdata('main_search', $new_search_string);*/

					} else {

						$newnew_find = ',';

						$newnew_pos = strpos($search_string, $newnew_find, $offset);

						if ($newnew_pos === false) {

							$range_substring = substr($search_string, $pos, $new_pos);

							$new_search_string = str_replace($range_substring, 'selected_subdivision/'.$subdivision, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						} else {

							$range_substring = substr($search_string, $pos, ($newnew_pos - $pos));

							$new_search_string = str_replace($range_substring, 'selected_subdivision/'.$subdivision, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						}

					}

				}

			//the main_search session variable is not set, so let's set it

			} else {

				$_SESSION['main_search'] = 'selected_subdivision/'.$subdivision;

			}

		}

		//if we don't have a subdivision then, either

		//	* the main_search session variable is set and it already includes a subdivision - since we have a blank subdivision, we wouldn't change it

		// 	* the main_search session variable is set and it does not include a subdivision - since we have a blank subdivision, we wouldn't add one

		// 	* the main_search session variable is not set - since we have a blank subdivision, we do not need to create the main_search session variable

		//********************************************************************************************************************************************************

		//selected_land_description

		//if we have a land_description, let's add it

		if ($land_description != '') {

			if ($_SESSION['main_search']) {

				$search_string = $_SESSION['main_search'];

				$find = 'selected_land_description/';

				$pos = strpos($search_string,$find);

				//if it's not already in the search, then let's add it

				if ($pos === false) {

					$_SESSION['main_search'] = $_SESSION['main_search'].',selected_land_description/'.$land_description;

				//if the string is in the search, let's replace it

				} else {

					$offset = $pos + strlen($find);

					$new_find = '/';

					$new_pos = strpos($search_string, $new_find, $offset);

					if ($new_pos === false) {

						$newnew_find = ',';

						$newnew_pos = strpos($search_string, $newnew_find, $offset);

						if ($newnew_pos === false) {

							if ($pos == 0) {

								$range_substring = substr($search_string, 0);

								$new_search_string = str_replace($range_substring, 'selected_land_description/'.$land_description, $search_string);

								$_SESSION['main_search'] = $new_search_string;

							} else {

								$range_substring = substr($search_string, $pos);

								$new_search_string = str_replace($range_substring, 'selected_land_description/'.$land_description, $search_string);

								$_SESSION['main_search'] = $new_search_string;

							}

						} else {

							$range_substring = substr($search_string, $pos, ($newnew_pos - $pos));

							$new_search_string = str_replace($range_substring, 'selected_land_description/'.$land_description, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						}

						/*$range_substring = substr($search_string, 0);

						$new_search_string = str_replace($range_substring, 'selected_land_description/'.$land_description, $search_string);

						$this->session->set_userdata('main_search', $new_search_string);*/

					} else {

						$newnew_find = ',';

						$newnew_pos = strpos($search_string, $newnew_find, $offset);

						if ($newnew_pos === false) {

							$range_substring = substr($search_string, $pos, $new_pos);

							$new_search_string = str_replace($range_substring, 'selected_land_description/'.$land_description, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						} else {

							$range_substring = substr($search_string, $pos, ($newnew_pos - $pos));

							$new_search_string = str_replace($range_substring, 'selected_land_description/'.$land_description, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						}

					}

				}

			//the main_search session variable is not set, so let's set it

			} else {

				$_SESSION['main_search'] = 'selected_land_description/'.$land_description;

			}

		}

		//if we don't have a land_description then, either

		//	* the main_search session variable is set and it already includes a land_description - since we have a blank land_description, we wouldn't change it

		// 	* the main_search session variable is set and it does not include a land_description - since we have a blank land_description, we wouldn't add one

		// 	* the main_search session variable is not set - since we have a blank land_description, we do not need to create the main_search session variable

		//********************************************************************************************************************************************************

		//selected_price_min & selected_price_max

		//if we have a price_range, let's add it

		if ($price_range != '') {

			if ($_SESSION['main_search']) {

				$search_string = $_SESSION['main_search'];

				$find = 'price_range/';

				$pos = strpos($search_string,$find);

				//if it's not already in the search, then let's add it

				if ($pos === false) {

					$_SESSION['main_search'] =  $_SESSION['main_search'].',price_range/'.$price_range;

				//if the string is in the search, let's replace it

				} else {

					$offset = $pos + strlen($find);

					$new_find = '/';

					$new_pos = strpos($search_string, $new_find, $offset);

					if ($new_pos === false) {

						$newnew_find = ',';

						$newnew_pos = strpos($search_string, $newnew_find, $offset);

						if ($newnew_pos === false) {

							if ($pos == 0) {

								$range_substring = substr($search_string, 0);

								$new_search_string = str_replace($range_substring, 'price_range/'.$price_range, $search_string);

								$_SESSION['main_search'] = $new_search_string;

							} else {

								$range_substring = substr($search_string, $pos);

								$new_search_string = str_replace($range_substring, 'price_range/'.$price_range, $search_string);

								$_SESSION['main_search'] = $new_search_string;

							}

						} else {

							$range_substring = substr($search_string, $pos, ($newnew_pos - $pos));

							$new_search_string = str_replace($range_substring, 'price_range/'.$price_range, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						}

						/*$range_substring = substr($search_string, 0);

						$new_search_string = str_replace($range_substring, 'price_range/'.$price_range, $search_string);

						$this->session->set_userdata('main_search', $new_search_string);*/

					} else {

						$newnew_find = ',';

						$newnew_pos = strpos($search_string, $newnew_find, $offset);

						if ($newnew_pos === false) {

							$range_substring = substr($search_string, $pos, $new_pos);

							$new_search_string = str_replace($range_substring, 'price_range/'.$price_range, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						} else {

							$range_substring = substr($search_string, $pos, ($newnew_pos - $pos));

							$new_search_string = str_replace($range_substring, 'price_range/'.$price_range, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						}

						/*$range_substring = substr($search_string, $pos, $new_pos);

						$new_search_string = str_replace($range_substring, 'price_range/'.$price_range, $search_string);

						$this->session->set_userdata('main_search', $new_search_string);*/

					}

				}

			//the main_search session variable is not set, so let's set it

			} else {

				$_SESSION['main_search'] =  'price_range/'.$price_range;

			}

		}

		//if we don't have a price range then, either

		//	* the main_search session variable is set and it already includes a price_range - since we have a blank price_range, we wouldn't change it

		// 	* the main_search session variable is set and it does not include a price_range - since we have a blank price_range, we wouldn't add one

		// 	* the main_search session variable is not set - since we have a blank price_range, we do not need to create the main_search session variable

		//********************************************************************************************************************************************************

		//selected_bedrooms

		//if we have bedrooms, let's add it

		if ($bedrooms != '') {

			if ($_SESSION['main_search']) {

				$search_string = $_SESSION['main_search'];

				$find = 'selected_bedrooms/';

				$pos = strpos($search_string,$find);

				//if it's not already in the search, then let's add it

				if ($pos === false) {

					$_SESSION['main_search'] = $_SESSION['main_search'].',selected_bedrooms/'.$bedrooms;

				//if the string is in the search, let's replace it

				} else {

					$offset = $pos + strlen($find);

					$new_find = '/';

					$new_pos = strpos($search_string, $new_find, $offset);

					if ($new_pos === false) {

						$newnew_find = ',';

						$newnew_pos = strpos($search_string, $newnew_find, $offset);

						if ($newnew_pos === false) {

							if ($pos == 0) {

								$range_substring = substr($search_string, 0);

								$new_search_string = str_replace($range_substring, 'selected_bedrooms/'.$bedrooms, $search_string);

								$_SESSION['main_search'] = $new_search_string;

							} else {

								$range_substring = substr($search_string, $pos);

								$new_search_string = str_replace($range_substring, 'selected_bedrooms/'.$bedrooms, $search_string);

								$_SESSION['main_search'] = $new_search_string;

							}

						} else {

							$range_substring = substr($search_string, $pos, ($newnew_pos - $pos));

							$new_search_string = str_replace($range_substring, 'selected_bedrooms/'.$bedrooms, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						}

						/*$range_substring = substr($search_string, 0);

						$new_search_string = str_replace($range_substring, 'selected_bedrooms/'.$bedrooms, $search_string);

						$this->session->set_userdata('main_search', $new_search_string);*/

					} else {

						$newnew_find = ',';

						$newnew_pos = strpos($search_string, $newnew_find, $offset);

						if ($newnew_pos === false) {

							$range_substring = substr($search_string, $pos, $new_pos);

							$new_search_string = str_replace($range_substring, 'selected_bedrooms/'.$bedrooms, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						} else {

							$range_substring = substr($search_string, $pos, ($newnew_pos - $pos));

							$new_search_string = str_replace($range_substring, 'selected_bedrooms/'.$bedrooms, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						}

					}

				}

			//the main_search session variable is not set, so let's set it

			} else {

				$_SESSION['main_search'] = 'selected_bedrooms/'.$bedrooms;

			}

		}

		//if we don't have bedrooms then, either

		//	* the main_search session variable is set and it already includes bedrooms - since we have blank bedrooms, we wouldn't change it

		// 	* the main_search session variable is set and it does not include bedrooms - since we have blank bedrooms, we wouldn't add one

		// 	* the main_search session variable is not set - since we have blank bedrooms, we do not need to create the main_search session variable

		//********************************************************************************************************************************************************

		//selected_bathrooms

		//if we have bathrooms, let's add it

		if ($bathrooms != '') {

			if ($_SESSION['main_search']) {

				$search_string = $_SESSION['main_search'];

				$find = 'selected_bathrooms/';

				$pos = strpos($search_string,$find);

				//if it's not already in the search, then let's add it

				if ($pos === false) {

					$_SESSION['main_search'] =  $_SESSION['main_search'].',selected_bathrooms/'.$bathrooms;

				//if the string is in the search, let's replace it

				} else {

					$offset = $pos + strlen($find);

					$new_find = '/';

					$new_pos = strpos($search_string, $new_find, $offset);

					if ($new_pos === false) {

						$newnew_find = ',';

						$newnew_pos = strpos($search_string, $newnew_find, $offset);

						if ($newnew_pos === false) {

							if ($pos == 0) {

								$range_substring = substr($search_string, 0);

								$new_search_string = str_replace($range_substring, 'selected_bathrooms/'.$bathrooms, $search_string);

								$_SESSION['main_search'] = $new_search_string;

							} else {

								$range_substring = substr($search_string, $pos);

								$new_search_string = str_replace($range_substring, 'selected_bathrooms/'.$bathrooms, $search_string);

								$_SESSION['main_search'] =  $new_search_string;

							}

						} else {

							$range_substring = substr($search_string, $pos, ($newnew_pos - $pos));

							$new_search_string = str_replace($range_substring, 'selected_bathrooms/'.$bathrooms, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						}

						/*$range_substring = substr($search_string, 0);

						$new_search_string = str_replace($range_substring, 'selected_bathrooms/'.$bathrooms, $search_string);

						$this->session->set_userdata('main_search', $new_search_string);*/

					} else {

						$newnew_find = ',';

						$newnew_pos = strpos($search_string, $newnew_find, $offset);

						if ($newnew_pos === false) {

							$range_substring = substr($search_string, $pos, $new_pos);

							$new_search_string = str_replace($range_substring, 'selected_bathrooms/'.$bathrooms, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						} else {

							$range_substring = substr($search_string, $pos, ($newnew_pos - $pos));

							$new_search_string = str_replace($range_substring, 'selected_bathrooms/'.$bathrooms, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						}

					}

				}

			//the main_search session variable is not set, so let's set it

			} else {

				$_SESSION['main_search'] = 'selected_bathrooms/'.$bathrooms;

			}

		}

		//if we don't have bathrooms then, either

		//	* the main_search session variable is set and it already includes bathrooms - since we have blank bathrooms, we wouldn't change it

		// 	* the main_search session variable is set and it does not include bathrooms - since we have blank bathrooms, we wouldn't add one

		// 	* the main_search session variable is not set - since we have blank bathrooms, we do not need to create the main_search session variable

		//********************************************************************************************************************************************************

		//selected_square_feet_min & selected_square_feet_max

		//if we have a acreage, let's add it

		if ($acreage != '') {

			if ($_SESSION['main_search']) {

				$search_string = $_SESSION['main_search'];

				$find = 'acreage/';

				$pos = strpos($search_string,$find);

				//if it's not already in the search, then let's add it

				if ($pos === false) {

					$_SESSION['main_search'] = $_SESSION['main_search'].',acreage/'.$acreage;

				//if the string is in the search, let's replace it

				} else {

					$offset = $pos + strlen($find);

					$new_find = '/';

					$new_pos = strpos($search_string, $new_find, $offset);

					if ($new_pos === false) {

						$newnew_find = ',';

						$newnew_pos = strpos($search_string, $newnew_find, $offset);

						if ($newnew_pos === false) {

							if ($pos == 0) {

								$range_substring = substr($search_string, 0);

								$new_search_string = str_replace($range_substring, 'acreage/'.$acreage, $search_string);

								$_SESSION['main_search'] = $new_search_string;

							} else {

								$range_substring = substr($search_string, $pos);

								$new_search_string = str_replace($range_substring, 'acreage/'.$acreage, $search_string);

								$_SESSION['main_search'] = $new_search_string;

							}

						} else {

							$range_substring = substr($search_string, $pos, $newnew_pos);

							$new_search_string = str_replace($range_substring, 'acreage/'.$acreage, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						}

						/*$range_substring = substr($search_string, 0);

						$new_search_string = str_replace($range_substring, 'acreage/'.$acreage, $search_string);

						$this->session->set_userdata('main_search', $new_search_string);*/

					} else {

						$newnew_find = ',';

						$newnew_pos = strpos($search_string, $newnew_find, $offset);

						if ($newnew_pos === false) {

							$range_substring = substr($search_string, $pos, $new_pos);

							$new_search_string = str_replace($range_substring, 'acreage/'.$acreage, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						} else {

							$range_substring = substr($search_string, $pos, $newnew_pos);

							$new_search_string = str_replace($range_substring, 'acreage/'.$acreage, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						}

					}

				}

			//the main_search session variable is not set, so let's set it

			} else {

				$_SESSION['main_search'] = 'acreage/'.$acreage;

			}

		}



		//if we don't have acreage then, either

		//	* the main_search session variable is set and it already includes acreage - since we have blank acreage, we wouldn't change it

		// 	* the main_search session variable is set and it does not include acreage - since we have blank acreage, we wouldn't add one

		// 	* the main_search session variable is not set - since we have blank acreage, we do not need to create the main_search session variable

		//********************************************************************************************************************************************************

		//selected_square_feet_min & selected_square_feet_max

		//if we have a square_feet, let's add it

		if ($square_feet != '') {

			if ($_SESSION['main_search']) {

				$search_string = $_SESSION['main_search'];

				$find = 'square_feet/';

				$pos = strpos($search_string,$find);

				//if it's not already in the search, then let's add it

				if ($pos === false) {

					$_SESSION['main_search'] = $_SESSION['main_search'].',square_feet/'.$square_feet;

				//if the string is in the search, let's replace it

				} else {

					$offset = $pos + strlen($find);

					$new_find = '/';

					$new_pos = strpos($search_string, $new_find, $offset);

					if ($new_pos === false) {

						$newnew_find = ',';

						$newnew_pos = strpos($search_string, $newnew_find, $offset);

						if ($newnew_pos === false) {

							if ($pos == 0) {

								$range_substring = substr($search_string, 0);

								$new_search_string = str_replace($range_substring, 'square_feet/'.$square_feet, $search_string);

								$_SESSION['main_search'] =  $new_search_string;

							} else {

								$range_substring = substr($search_string, $pos);

								$new_search_string = str_replace($range_substring, 'square_feet/'.$square_feet, $search_string);

								$_SESSION['main_search']=  $new_search_string;

							}

						} else {

							$range_substring = substr($search_string, $pos, ($newnew_pos - $pos));

							$new_search_string = str_replace($range_substring, 'square_feet/'.$square_feet, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						}

						/*$range_substring = substr($search_string, 0);

						$new_search_string = str_replace($range_substring, 'square_feet/'.$square_feet, $search_string);

						$this->session->set_userdata('main_search', $new_search_string);*/

					} else {

						$newnew_find = ',';

						$newnew_pos = strpos($search_string, $newnew_find, $offset);

						if ($newnew_pos === false) {

							$range_substring = substr($search_string, $pos, $new_pos);

							$new_search_string = str_replace($range_substring, 'square_feet/'.$square_feet, $search_string);

							$_SESSION['main_search'] =  $new_search_string;

						} else {

							$range_substring = substr($search_string, $pos, ($newnew_pos - $pos));

							$new_search_string = str_replace($range_substring, 'square_feet/'.$square_feet, $search_string);

							$_SESSION['main_search'] =  $new_search_string;

						}

					}

				}

			//the main_search session variable is not set, so let's set it

			} else {

				$_SESSION['main_search'] = 'square_feet/'.$square_feet;

			}

		}

		//if we don't have a square feet then, either

		//	* the main_search session variable is set and it already includes a square_feet - since we have a blank square_feet, we wouldn't change it

		// 	* the main_search session variable is set and it does not include a square_feet - since we have a blank square_feet, we wouldn't add one

		// 	* the main_search session variable is not set - since we have a blank square_feet, we do not need to create the main_search session variable

		//********************************************************************************************************************************************************

		//selected_year_built_min & selected_year_built_max

		//if we have a year_built, let's add it

		if ($year_built != '') {

			if ($_SESSION['main_search']) {

				$search_string = $_SESSION['main_search'];

				$find = 'year_built/';

				$pos = strpos($search_string,$find);

				//if it's not already in the search, then let's add it

				if ($pos === false) {

					$_SESSION['main_search'] = $_SESSION['main_search'].',year_built/'.$year_built;

				//if the string is in the search, let's replace it

				} else {

					$offset = $pos + strlen($find);

					$new_find = '/';

					$new_pos = strpos($search_string, $new_find, $offset);

					if ($new_pos === false) {

						$newnew_find = ',';

						$newnew_pos = strpos($search_string, $newnew_find, $offset);

						if ($newnew_pos === false) {

							if ($pos == 0) {

								$range_substring = substr($search_string, 0);

								$new_search_string = str_replace($range_substring, 'year_built/'.$year_built, $search_string);

								$_SESSION['main_search'] = $new_search_string;

							} else {

								$range_substring = substr($search_string, $pos);

								$new_search_string = str_replace($range_substring, 'year_built/'.$year_built, $search_string);

								$_SESSION['main_search'] = $new_search_string;

							}

						} else {

							$range_substring = substr($search_string, $pos, ($newnew_pos - $pos));

							$new_search_string = str_replace($range_substring, 'year_built/'.$year_built, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						}

						/*$range_substring = substr($search_string, 0);

						$new_search_string = str_replace($range_substring, 'year_built/'.$year_built, $search_string);

						$this->session->set_userdata('main_search', $new_search_string);*/

					} else {

						$newnew_find = ',';

						$newnew_pos = strpos($search_string, $newnew_find, $offset);

						if ($newnew_pos === false) {

							$range_substring = substr($search_string, $pos, $new_pos);

							$new_search_string = str_replace($range_substring, 'year_built/'.$year_built, $search_string);

							$_SESSION['main_search']= $new_search_string;

						} else {

							$range_substring = substr($search_string, $pos, ($newnew_pos - $pos));

							$new_search_string = str_replace($range_substring, 'year_built/'.$year_built, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						}

					}

				}

			//the main_search session variable is not set, so let's set it

			} else {

				$_SESSION['main_search'] = 'year_built/'.$year_built;

			}

		}

		//if we don't have a year_built then, either

		//	* the main_search session variable is set and it already includes a year_built - since we have a blank year_built, we wouldn't change it

		// 	* the main_search session variable is set and it does not include a year_built - since we have a blank year_built, we wouldn't add one

		// 	* the main_search session variable is not set - since we have a blank year_built, we do not need to create the main_search session variable

		//********************************************************************************************************************************************************

		//selected_elem

		//if we have a elementary school, let's add it

		if ($elem != '') {

			if ($_SESSION['main_search']) {

				$search_string = $_SESSION['main_search'];

				$find = 'selected_elem/';

				$pos = strpos($search_string,$find);

				//if it's not already in the search, then let's add it

				if ($pos === false) {

					$_SESSION['main_search'] =  $_SESSION['main_search'].',selected_elem/'.$elem;

				//if the string is in the search, let's replace it

				} else {

					$offset = $pos + strlen($find);

					$new_find = '/';

					$new_pos = strpos($search_string, $new_find, $offset);

					if ($new_pos === false) {

						$newnew_find = ',';

						$newnew_pos = strpos($search_string, $newnew_find, $offset);

						if ($newnew_pos === false) {

							if ($pos == 0) {

								$range_substring = substr($search_string, 0);

								$new_search_string = str_replace($range_substring, 'selected_elem/'.$elem, $search_string);

								$_SESSION['main_search'] = $new_search_string;

							} else {

								$range_substring = substr($search_string, $pos);

								$new_search_string = str_replace($range_substring, 'selected_elem/'.$elem, $search_string);

								$_SESSION['main_search'] = $new_search_string;

							}

						} else {

							$range_substring = substr($search_string, $pos, ($newnew_pos - $pos));

							$new_search_string = str_replace($range_substring, 'selected_elem/'.$elem, $search_string);

							$_SESSION['main_search'] =  $new_search_string;

						}

						/*$range_substring = substr($search_string, 0);

						$new_search_string = str_replace($range_substring, 'selected_elem/'.$elem, $search_string);

						$this->session->set_userdata('main_search', $new_search_string);*/

					} else {

						$newnew_find = ',';

						$newnew_pos = strpos($search_string, $newnew_find, $offset);

						if ($newnew_pos === false) {

							$range_substring = substr($search_string, $pos, $new_pos);

							$new_search_string = str_replace($range_substring, 'selected_elem/'.$elem, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						} else {

							$range_substring = substr($search_string, $pos, ($newnew_pos - $pos));

							$new_search_string = str_replace($range_substring, 'selected_elem/'.$elem, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						}

					}

				}

			//the main_search session variable is not set, so let's set it

			} else {

				$_SESSION['main_search'] = 'selected_elem/'.$elem;

			}

		}

		//if we don't have a selected_elem then, either

		//	* the main_search session variable is set and it already includes a selected_elem - since we have a blank selected_elem, we wouldn't change it

		// 	* the main_search session variable is set and it does not include a selected_elem - since we have a blank selected_elem, we wouldn't add one

		// 	* the main_search session variable is not set - since we have a blank selected_elem, we do not need to create the main_search session variable

		//********************************************************************************************************************************************************

		//selected_middle

		//if we have a middle school, let's add it

		if ($middle != '') {

			if ($_SESSION['main_search']) {

				$search_string = $_SESSION['main_search'];

				$find = 'selected_middle/';

				$pos = strpos($search_string,$find);

				//if it's not already in the search, then let's add it

				if ($pos === false) {

					$_SESSION['main_search'] = $_SESSION['main_search'].',selected_middle/'.$middle;

				//if the string is in the search, let's replace it

				} else {

					$offset = $pos + strlen($find);

					$new_find = '/';

					$new_pos = strpos($search_string, $new_find, $offset);

					if ($new_pos === false) {

						$newnew_find = ',';

						$newnew_pos = strpos($search_string, $newnew_find, $offset);

						if ($newnew_pos === false) {

							if ($pos == 0) {

								$range_substring = substr($search_string, 0);

								$new_search_string = str_replace($range_substring, 'selected_middle/'.$middle, $search_string);

								$_SESSION['main_search'] = $new_search_string;

							} else {

								$range_substring = substr($search_string, $pos);

								$new_search_string = str_replace($range_substring, 'selected_middle/'.$middle, $search_string);

								$_SESSION['main_search'] = $new_search_string;

							}

						} else {

							$range_substring = substr($search_string, $pos, ($newnew_pos - $pos));

							$new_search_string = str_replace($range_substring, 'selected_middle/'.$middle, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						}

						/*$range_substring = substr($search_string, 0);

						$new_search_string = str_replace($range_substring, 'selected_middle/'.$middle, $search_string);

						$this->session->set_userdata('main_search', $new_search_string);*/

					} else {

						$newnew_find = ',';

						$newnew_pos = strpos($search_string, $newnew_find, $offset);

						if ($newnew_pos === false) {

							$range_substring = substr($search_string, $pos, $new_pos);

							$new_search_string = str_replace($range_substring, 'selected_middle/'.$middle, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						} else {

							$range_substring = substr($search_string, $pos, ($newnew_pos - $pos));

							$new_search_string = str_replace($range_substring, 'selected_middle/'.$middle, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						}

					}

				}

			//the main_search session variable is not set, so let's set it

			} else {

				$_SESSION['main_search'] = 'selected_middle/'.$middle;

			}

		}

		//if we don't have a selected_middle then, either

		//	* the main_search session variable is set and it already includes a selected_middle - since we have a blank selected_middle, we wouldn't change it

		// 	* the main_search session variable is set and it does not include a selected_middle - since we have a blank selected_middle, we wouldn't add one

		// 	* the main_search session variable is not set - since we have a blank selected_middle, we do not need to create the main_search session variable

		//********************************************************************************************************************************************************

		//selected_high

		//if we have a high school, let's add it

		if ($high != '') {

			if ($_SESSION['main_search']) {

				$search_string = $_SESSION['main_search'];

				$find = 'selected_high/';

				$pos = strpos($search_string,$find);

				//if it's not already in the search, then let's add it

				if ($pos === false) {

					$_SESSION['main_search'] = $_SESSION['main_search'].',selected_high/'.$high;

				//if the string is in the search, let's replace it

				} else {

					$offset = $pos + strlen($find);

					$new_find = '/';

					$new_pos = strpos($search_string, $new_find, $offset);

					if ($new_pos === false) {

						$newnew_find = ',';

						$newnew_pos = strpos($search_string, $newnew_find, $offset);

						if ($newnew_pos === false) {

							if ($pos == 0) {

								$range_substring = substr($search_string, 0);

								$new_search_string = str_replace($range_substring, 'selected_high/'.$high, $search_string);

								$_SESSION['main_search'] = $new_search_string;

							} else {

								$range_substring = substr($search_string, $pos);

								$new_search_string = str_replace($range_substring, 'selected_high/'.$high, $search_string);

								$_SESSION['main_search'] = $new_search_string;

							}

						} else {

							$range_substring = substr($search_string, $pos, ($newnew_pos - $pos));

							$new_search_string = str_replace($range_substring, 'selected_high/'.$high, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						}

						/*$range_substring = substr($search_string, 0);

						$new_search_string = str_replace($range_substring, 'selected_high/'.$high, $search_string);

						$this->session->set_userdata('main_search', $new_search_string);*/

					} else {

						$newnew_find = ',';

						$newnew_pos = strpos($search_string, $newnew_find, $offset);

						if ($newnew_pos === false) {

							$range_substring = substr($search_string, $pos, $new_pos);

							$new_search_string = str_replace($range_substring, 'selected_high/'.$high, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						} else {

							$range_substring = substr($search_string, $pos, ($newnew_pos - $pos));

							$new_search_string = str_replace($range_substring, 'selected_high/'.$high, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						}

					}

				}

			//the main_search session variable is not set, so let's set it

			} else {

				$_SESSION['main_search']= 'selected_high/'.$high;

			}

		}

		//if we don't have a selected_high then, either

		//	* the main_search session variable is set and it already includes a selected_high - since we have a blank selected_high, we wouldn't change it

		// 	* the main_search session variable is set and it does not include a selected_high - since we have a blank selected_high, we wouldn't add one

		// 	* the main_search session variable is not set - since we have a blank selected_high, we do not need to create the main_search session variable

		//********************************************************************************************************************************************************

		//selected_open_house_only

		//if we have open_house_selected, let's add it

		if ($open_house_selected != '') {

			if ($_SESSION['main_search']) {

				$search_string = $_SESSION['main_search'];

				$find = 'open_house_selected/';

				$pos = strpos($search_string,$find);

				//if it's not already in the search, then let's add it

				if ($pos === false) {

					$_SESSION['main_search'] = $_SESSION['main_search'].',open_house_selected/'.$open_house_selected;

				//if the string is in the search, let's replace it

				} else {

					$offset = $pos + strlen($find);

					$new_find = '/';

					$new_pos = strpos($search_string, $new_find, $offset);

					if ($new_pos === false) {

						$newnew_find = ',';

						$newnew_pos = strpos($search_string, $newnew_find, $offset);

						if ($newnew_pos === false) {

							if ($pos == 0) {

								$range_substring = substr($search_string, 0);

								$new_search_string = str_replace($range_substring, 'open_house_selected/'.$open_house_selected, $search_string);

								$_SESSION['main_search'] = $new_search_string;

							} else {

								$range_substring = substr($search_string, $pos);

								$new_search_string = str_replace($range_substring, 'open_house_selected/'.$open_house_selected, $search_string);

								$_SESSION['main_search'] = $new_search_string;

							}

						} else {

							$range_substring = substr($search_string, $pos, ($newnew_pos - $pos));

							$new_search_string = str_replace($range_substring, 'open_house_selected/'.$open_house_selected, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						}

						/*$range_substring = substr($search_string, 0);

						$new_search_string = str_replace($range_substring, 'open_house_selected/'.$open_house_selected, $search_string);

						$this->session->set_userdata('main_search', $new_search_string);*/

					} else {

						$newnew_find = ',';

						$newnew_pos = strpos($search_string, $newnew_find, $offset);

						if ($newnew_pos === false) {

							$range_substring = substr($search_string, $pos, $new_pos);

							$new_search_string = str_replace($range_substring, 'open_house_selected/'.$open_house_selected, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						} else {

							$range_substring = substr($search_string, $pos, ($newnew_pos - $pos));

							$new_search_string = str_replace($range_substring, 'open_house_selected/'.$open_house_selected, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						}

					}

				}

			//the main_search session variable is not set, so let's set it

			} else {

				$_SESSION['main_search'] = 'open_house_selected/'.$open_house_selected;

			}

		}

		//if we don't have a selected_high then, either

		//	* the main_search session variable is set and it already includes open_house_selected - since we have a blank open_house_selected, we wouldn't change it

		// 	* the main_search session variable is set and it does not include open_house_selected - since we have a blank open_house_selected, we wouldn't add one

		// 	* the main_search session variable is not set - since we have a blank open_house_selected, we do not need to create the main_search session variable

		//********************************************************************************************************************************************************

		//selected_price_reduced_only

		//if we have price_reduced_selected, let's add it

		if ($price_reduced_selected != '') {

			if ($_SESSION['main_search']) {

				$search_string = $_SESSION['main_search'];

				$find = 'price_reduced_selected/';

				$pos = strpos($search_string,$find);

				//if it's not already in the search, then let's add it

				if ($pos === false) {

					$_SESSION['main_search'] = $_SESSION['main_search'].',price_reduced_selected/'.$price_reduced_selected;

				//if the string is in the search, let's replace it

				} else {

					$offset = $pos + strlen($find);

					$new_find = '/';

					$new_pos = strpos($search_string, $new_find, $offset);

					if ($new_pos === false) {

						$newnew_find = ',';

						$newnew_pos = strpos($search_string, $newnew_find, $offset);

						if ($newnew_pos === false) {

							if ($pos == 0) {

								$range_substring = substr($search_string, 0);

								$new_search_string = str_replace($range_substring, 'price_reduced_selected/'.$price_reduced_selected, $search_string);

								$_SESSION['main_search'] = $new_search_string;

							} else {

								$range_substring = substr($search_string, $pos);

								$new_search_string = str_replace($range_substring, 'price_reduced_selected/'.$price_reduced_selected, $search_string);

								$_SESSION['main_search'] = $new_search_string;

							}

						} else {

							$range_substring = substr($search_string, $pos, ($newnew_pos - $pos));

							$new_search_string = str_replace($range_substring, 'price_reduced_selected/'.$price_reduced_selected, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						}

						/*$range_substring = substr($search_string, 0);

						$new_search_string = str_replace($range_substring, 'price_reduced_selected/'.$price_reduced_selected, $search_string);

						$this->session->set_userdata('main_search', $new_search_string);*/

					} else {

						$newnew_find = ',';

						$newnew_pos = strpos($search_string, $newnew_find, $offset);

						if ($newnew_pos === false) {

							$range_substring = substr($search_string, $pos, $new_pos);

							$new_search_string = str_replace($range_substring, 'price_reduced_selected/'.$price_reduced_selected, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						} else {

							$range_substring = substr($search_string, $pos, ($newnew_pos - $pos));

							$new_search_string = str_replace($range_substring, 'price_reduced_selected/'.$price_reduced_selected, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						}

					}

				}

			//the main_search session variable is not set, so let's set it

			} else {

				$_SESSION['main_search'] = 'price_reduced_selected/'.$price_reduced_selected;

			}

		}

		//if we don't have a selected_high then, either

		//	* the main_search session variable is set and it already includes price_reduced_selected - since we have a blank price_reduced_selected, we wouldn't change it

		// 	* the main_search session variable is set and it does not include price_reduced_selected - since we have a blank price_reduced_selected, we wouldn't add one

		// 	* the main_search session variable is not set - since we have a blank price_reduced_selected, we do not need to create the main_search session variable

		//********************************************************************************************************************************************************

		//selected_new_listing_only

		//if we have new_listing_selected, let's add it

		if ($new_listing_selected != '') {

			if ($_SESSION['main_search']) {

				$search_string = $_SESSION['main_search'];

				$find = 'new_listing_selected/';

				$pos = strpos($search_string,$find);

				//if it's not already in the search, then let's add it

				if ($pos === false) {

					$_SESSION['main_search'] = $_SESSION['main_search'].',new_listing_selected/'.$new_listing_selected;

				//if the string is in the search, let's replace it

				} else {

					$offset = $pos + strlen($find);

					$new_find = '/';

					$new_pos = strpos($search_string, $new_find, $offset);

					if ($new_pos === false) {

						$newnew_find = ',';

						$newnew_pos = strpos($search_string, $newnew_find, $offset);

						if ($newnew_pos === false) {

							if ($pos == 0) {

								$range_substring = substr($search_string, 0);

								$new_search_string = str_replace($range_substring, 'new_listing_selected/'.$new_listing_selected, $search_string);

								$_SESSION['main_search'] = $new_search_string;

							} else {

								$range_substring = substr($search_string, $pos);

								$new_search_string = str_replace($range_substring, 'new_listing_selected/'.$new_listing_selected, $search_string);

								$_SESSION['main_search'] = $new_search_string;

							}

						} else {

							$range_substring = substr($search_string, $pos, ($newnew_pos - $pos));

							$new_search_string = str_replace($range_substring, 'new_listing_selected/'.$new_listing_selected, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						}

						/*$range_substring = substr($search_string, 0);

						$new_search_string = str_replace($range_substring, 'new_listing_selected/'.$new_listing_selected, $search_string);

						$this->session->set_userdata('main_search', $new_search_string);*/

					} else {

						$newnew_find = ',';

						$newnew_pos = strpos($search_string, $newnew_find, $offset);

						if ($newnew_pos === false) {

							$range_substring = substr($search_string, $pos, $new_pos);

							$new_search_string = str_replace($range_substring, 'new_listing_selected/'.$new_listing_selected, $search_string);

							$_SESSION['main_search']= $new_search_string;

						} else {

							$range_substring = substr($search_string, $pos, ($newnew_pos - $pos));

							$new_search_string = str_replace($range_substring, 'new_listing_selected/'.$new_listing_selected, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						}

					}

				}

			//the main_search session variable is not set, so let's set it

			} else {

				$_SESSION['main_search'] = 'new_listing_selected/'.$new_listing_selected;

			}

		}



		//if we don't have a selected_high then, either

		//	* the main_search session variable is set and it already includes acres - since we have a blank acres, we wouldn't change it

		// 	* the main_search session variable is set and it does not include acres - since we have a blank acres, we wouldn't add one

		// 	* the main_search session variable is not set - since we have a blank acres, we do not need to create the main_search session variable

		//********************************************************************************************************************************************************

		//agent

		//if we have agent, let's add it

		if ($agent != '') {

			if ($_SESSION['main_search']) {

				$search_string = $_SESSION['main_search'];

				$find = 'agent_selected/';

				$pos = strpos($search_string,$find);

				//if it's not already in the search, then let's add it

				if ($pos === false) {

					$_SESSION['main_search'] =  $_SESSION['main_search'].',agent_selected/'.$agent;

				//if the string is in the search, let's replace it

				} else {

					$offset = $pos + strlen($find);

					$new_find = '/';

					$new_pos = strpos($search_string, $new_find, $offset);

					if ($new_pos === false) {

						$newnew_find = ',';

						$newnew_pos = strpos($search_string, $newnew_find, $offset);

						if ($newnew_pos === false) {

							if ($pos == 0) {

								$range_substring = substr($search_string, 0);

								$new_search_string = str_replace($range_substring, 'agent_selected/'.$agent, $search_string);

								$_SESSION['main_search'] = $new_search_string;

							} else {

								$range_substring = substr($search_string, $pos);

								$new_search_string = str_replace($range_substring, 'agent_selected/'.$agent, $search_string);

								$_SESSION['main_search'] = $new_search_string;

							}

						} else {

							$range_substring = substr($search_string, $pos, ($newnew_pos - $pos));

							$new_search_string = str_replace($range_substring, 'agent_selected/'.$agent, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						}

						/*$range_substring = substr($search_string, 0);

						$new_search_string = str_replace($range_substring, 'agent_selected/'.$agent, $search_string);

						$this->session->set_userdata('main_search', $new_search_string);*/

					} else {

						$newnew_find = ',';

						$newnew_pos = strpos($search_string, $newnew_find, $offset);

						if ($newnew_pos === false) {

							$range_substring = substr($search_string, $pos, $new_pos);

							$new_search_string = str_replace($range_substring, 'agent_selected/'.$agent, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						} else {

							$range_substring = substr($search_string, $pos, ($newnew_pos - $pos));

							$new_search_string = str_replace($range_substring, 'agent_selected/'.$agent, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						}

						/*

						$range_substring = substr($search_string, $pos, $new_pos);

						$new_search_string = str_replace($range_substring, 'agent_selected/'.$agent, $search_string);

						$this->session->set_userdata('main_search', $new_search_string);*/

					}

				}

			//the main_search session variable is not set, so let's set it

			} else {

				$_SESSION['main_search'] = 'agent_selected/'.$agent;

			}

		} else {

			$search_string = '';

			if(isset($_SESSION['main_search'])){

				$search_string = $_SESSION['main_search'];

		    }

			$find = 'agent_selected/';

			$pos = strpos($search_string,$find);

			//if it's in the search, remove it

			if ($pos !== false) {

				$offset = $pos + strlen($find);

				$new_find = ',';

				$new_pos = strpos($search_string, $new_find, $offset);

				if ($new_pos === false) {

					$range_substring = substr($search_string, 0);



					//search, replace, subject

					$new_search_string = str_replace($range_substring, '', $search_string);

					$_SESSION['main_search'] = $new_search_string;

				} else {



					//string, start, length

					$range_substring = substr($search_string, $pos, $new_pos + 1);

					$new_search_string = str_replace($range_substring, '', $search_string);

					$_SESSION['main_search'] = $new_search_string;

				}

			}

		}

		//if we don't have a selected_high then, either

		//	* the main_search session variable is set and it already includes agent - since we have a blank agent, we wouldn't change it

		// 	* the main_search session variable is set and it does not include agent - since we have a blank agent, we wouldn't add one

		// 	* the main_search session variable is not set - since we have a blank agent, we do not need to create the main_search session variable

		//********************************************************************************************************************************************************

		//office

		//if we have office, let's add it

		if ($office != '') {

			if (isset($_SESSION['main_search'])) {

				$search_string = $_SESSION['main_search'];

				$find = 'office_selected/';

				$pos = strpos($search_string,$find);

				//if it's not already in the search, then let's add it

				if ($pos === false) {

					$_SESSION['main_search']=  $_SESSION['main_search'].',office_selected/'.$office;

				//if the string is in the search, let's replace it

				} else {

					$offset = $pos + strlen($find);

					$new_find = '/';

					$new_pos = strpos($search_string, $new_find, $offset);

					if ($new_pos === false) {

						$newnew_find = ',';

						$newnew_pos = strpos($search_string, $newnew_find, $offset);

						if ($newnew_pos === false) {

							if ($pos == 0) {

								$range_substring = substr($search_string, 0);

								$new_search_string = str_replace($range_substring, 'office_selected/'.$office, $search_string);

								$_SESSION['main_search']= $new_search_string;

							} else {

								$range_substring = substr($search_string, $pos);

								$new_search_string = str_replace($range_substring, 'office_selected/'.$office, $search_string);

								$_SESSION['main_search'] = $new_search_string;

							}

						} else {

							$range_substring = substr($search_string, $pos, ($newnew_pos - $pos));

							$new_search_string = str_replace($range_substring, 'office_selected/'.$office, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						}

						/*$range_substring = substr($search_string, 0);

						$new_search_string = str_replace($range_substring, 'office_selected/'.$office, $search_string);

						$this->session->set_userdata('main_search', $new_search_string);*/

					} else {

						$newnew_find = ',';

						$newnew_pos = strpos($search_string, $newnew_find, $offset);

						if ($newnew_pos === false) {

							$range_substring = substr($search_string, $pos, $new_pos);

							$new_search_string = str_replace($range_substring, 'office_selected/'.$office, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						} else {

							$range_substring = substr($search_string, $pos, ($newnew_pos - $pos));

							$new_search_string = str_replace($range_substring, 'office_selected/'.$office, $search_string);

							$_SESSION['main_search']= $new_search_string;

						}

						/*$range_substring = substr($search_string, $pos, $new_pos);

						$new_search_string = str_replace($range_substring, 'office_selected/'.$office, $search_string);

						$this->session->set_userdata('main_search', $new_search_string);*/

					}

				}

			//the main_search session variable is not set, so let's set it

			} else {

				$_SESSION['main_search'] = 'office_selected/'.$office;

			}

		} else {

			if(isset($_SESSION['main_search'])){

			$search_string = $_SESSION['main_search'];

			}

			$find = 'office_selected/';

			$pos = strpos($search_string,$find);

			//if it's in the search, remove it

			if ($pos !== false) {

				$offset = $pos + strlen($find);

				$new_find = ',';

				$new_pos = strpos($search_string, $new_find, $offset);

				if ($new_pos === false) {

					$range_substring = substr($search_string, 0);



					//search, replace, subject

					$new_search_string = str_replace($range_substring, '', $search_string);

					$_SESSION['main_search'] = $new_search_string;

				} else {

					//string, start, length

					$range_substring = substr($search_string, $pos, $new_pos + 1);

					$new_search_string = str_replace($range_substring, '', $search_string);

					$_SESSION['main_search'] = $new_search_string;

				}

			}

		}

		//if we don't have a selected_high then, either

		//	* the main_search session variable is set and it already includes office - since we have a blank office, we wouldn't change it

		// 	* the main_search session variable is set and it does not include office - since we have a blank office, we wouldn't add one

		// 	* the main_search session variable is not set - since we have a blank office, we do not need to create the main_search session variable

		//********************************************************************************************************************************************************

		//short_sale

		//if we have short_sale, let's add it

		if ($short_sale != '') {

			if ($_SESSION['main_search']) {

				$search_string = $_SESSION['main_search'];

				$find = 'short_sale_selected/';

				$pos = strpos($search_string,$find);

				//if it's not already in the search, then let's add it

				if ($pos === false) {

					$_SESSION['main_search'] = $_SESSION['main_search'].',short_sale_selected/'.$short_sale;

				//if the string is in the search, let's replace it

				} else {

					$offset = $pos + strlen($find);

					$new_find = '/';

					$new_pos = strpos($search_string, $new_find, $offset);

					if ($new_pos === false) {

						$newnew_find = ',';

						$newnew_pos = strpos($search_string, $newnew_find, $offset);

						if ($newnew_pos === false) {

							if ($pos == 0) {

								$range_substring = substr($search_string, 0);

								$new_search_string = str_replace($range_substring, 'short_sale_selected/'.$short_sale, $search_string);

								$_SESSION['main_search'] = $new_search_string;

							} else {

								$range_substring = substr($search_string, $pos);

								$new_search_string = str_replace($range_substring, 'short_sale_selected/'.$short_sale, $search_string);

								$_SESSION['main_search'] =  $new_search_string;

							}

						} else {

							$range_substring = substr($search_string, $pos, ($newnew_pos - $pos));

							$new_search_string = str_replace($range_substring, 'short_sale_selected/'.$short_sale, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						}

						/*$range_substring = substr($search_string, 0);

						$new_search_string = str_replace($range_substring, 'short_sale_selected/'.$short_sale, $search_string);

						$this->session->set_userdata('main_search', $new_search_string);*/

					} else {

						$newnew_find = ',';

						$newnew_pos = strpos($search_string, $newnew_find, $offset);

						if ($newnew_pos === false) {

							$range_substring = substr($search_string, $pos, $new_pos);

							$new_search_string = str_replace($range_substring, 'short_sale_selected/'.$short_sale, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						} else {

							$range_substring = substr($search_string, $pos, ($newnew_pos - $pos));

							$new_search_string = str_replace($range_substring, 'short_sale_selected/'.$short_sale, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						}

						/*$range_substring = substr($search_string, $pos, $new_pos);

						$new_search_string = str_replace($range_substring, 'short_sale_selected/'.$short_sale, $search_string);

						$this->session->set_userdata('main_search', $new_search_string);*/

					}

				}

			//the main_search session variable is not set, so let's set it

			} else {

				$_SESSION['main_search'] = 'short_sale_selected/'.$short_sale;

			}

		} else {

			if(isset($_SESSION['main_search'])){



				$search_string = $_SESSION['main_search'];

			}

			$find = 'short_sale_selected/';

			$pos = strpos($search_string,$find);

			//if it's in the search, remove it

			if ($pos !== false) {

				$offset = $pos + strlen($find);

				$new_find = ',';

				$new_pos = strpos($search_string, $new_find, $offset);

				if ($new_pos === false) {

					$range_substring = substr($search_string, 0);



					//search, replace, subject

					$new_search_string = str_replace($range_substring, '', $search_string);

					$_SESSION['main_search'] = $new_search_string;

				} else {

					//string, start, length

					$range_substring = substr($search_string, $pos, $new_pos + 1);

					$new_search_string = str_replace($range_substring, '', $search_string);

					$_SESSION['main_search'] = $new_search_string;

				}

			}

		}

		//if we don't have a selected_high then, either

		//	* the main_search session variable is set and it already includes short_sale - since we have a blank short_sale, we wouldn't change it

		// 	* the main_search session variable is set and it does not include short_sale - since we have a blank short_sale, we wouldn't add one

		// 	* the main_search session variable is not set - since we have a blank short_sale, we do not need to create the main_search session variable

		//********************************************************************************************************************************************************

		//foreclosure

		//if we have foreclosure, let's add it

		if ($foreclosure != '') {

			if ($_SESSION['main_search']) {

				$search_string = $_SESSION['main_search'];

				$find = 'foreclosure_selected/';

				$pos = strpos($search_string,$find);

				//if it's not already in the search, then let's add it

				if ($pos === false) {

					$_SESSION['main_search'] =  $_SESSION['main_search'].',foreclosure_selected/'.$foreclosure;

				//if the string is in the search, let's replace it

				} else {

					$offset = $pos + strlen($find);

					$new_find = '/';

					$new_pos = strpos($search_string, $new_find, $offset);

					if ($new_pos === false) {

						$newnew_find = ',';

						$newnew_pos = strpos($search_string, $newnew_find, $offset);

						if ($newnew_pos === false) {

							if ($pos == 0) {

								$range_substring = substr($search_string, 0);

								$new_search_string = str_replace($range_substring, 'foreclosure_selected/'.$foreclosure, $search_string);

								$_SESSION['main_search'] =  $new_search_string;

							} else {

								$range_substring = substr($search_string, $pos);

								$new_search_string = str_replace($range_substring, 'foreclosure_selected/'.$foreclosure, $search_string);

								$_SESSION['main_search'] = $new_search_string;

							}

						} else {

							$range_substring = substr($search_string, $pos, ($newnew_pos - $pos));

							$new_search_string = str_replace($range_substring, 'foreclosure_selected/'.$foreclosure, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						}

						/*$range_substring = substr($search_string, 0);

						$new_search_string = str_replace($range_substring, 'foreclosure_selected/'.$foreclosure, $search_string);

						$this->session->set_userdata('main_search', $new_search_string);*/

					} else {

						$newnew_find = ',';

						$newnew_pos = strpos($search_string, $newnew_find, $offset);

						if ($newnew_pos === false) {

							$range_substring = substr($search_string, $pos, $new_pos);

							$new_search_string = str_replace($range_substring, 'foreclosure_selected/'.$foreclosure, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						} else {

							$range_substring = substr($search_string, $pos, ($newnew_pos - $pos));

							$new_search_string = str_replace($range_substring, 'foreclosure_selected/'.$foreclosure, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						}

						/*$range_substring = substr($search_string, $pos, $new_pos);

						$new_search_string = str_replace($range_substring, 'foreclosure_selected/'.$foreclosure, $search_string);

						$this->session->set_userdata('main_search', $new_search_string);*/

					}

				}

			//the main_search session variable is not set, so let's set it

			} else {

				$_SESSION['main_search'] = 'foreclosure_selected/'.$foreclosure;

			}

		} else {

			if(isset($_SESSION['main_search'])){

				$search_string = $_SESSION['main_search'];

			}

			$find = 'foreclosure_selected/';

			$pos = strpos($search_string,$find);

			//if it's in the search, remove it

			if ($pos !== false) {

				$offset = $pos + strlen($find);

				$new_find = ',';

				$new_pos = strpos($search_string, $new_find, $offset);

				if ($new_pos === false) {

					$range_substring = substr($search_string, 0);



					//search, replace, subject

					$new_search_string = str_replace($range_substring, '', $search_string);

					$_SESSION['main_search'] = $new_search_string;

				} else {

					//string, start, length

					$range_substring = substr($search_string, $pos, $new_pos + 1);

					$new_search_string = str_replace($range_substring, '', $search_string);

					$_SESSION['main_search'] = $new_search_string;

				}

			}

		}

		//if we don't have a selected_high then, either

		//	* the main_search session variable is set and it already includes foreclosure - since we have a blank foreclosure, we wouldn't change it

		// 	* the main_search session variable is set and it does not include foreclosure - since we have a blank foreclosure, we wouldn't add one

		// 	* the main_search session variable is not set - since we have a blank foreclosure, we do not need to create the main_search session variable

		//********************************************************************************************************************************************************

		//listing_date

		//if we have listing_date, let's add it

		if ($listing_date != '') {

			if ($_SESSION['main_search']) {

				$search_string = $_SESSION['main_search'];

				$find = 'listing_date_selected/';

				$pos = strpos($search_string,$find);

				//if it's not already in the search, then let's add it

				if ($pos === false) {

					$_SESSION['main_search'] = $_SESSION['main_search'].',listing_date_selected/'.$listing_date;

				//if the string is in the search, let's replace it

				} else {

					$offset = $pos + strlen($find);

					$new_find = '/';

					$new_pos = strpos($search_string, $new_find, $offset);

					if ($new_pos === false) {

						$newnew_find = ',';

						$newnew_pos = strpos($search_string, $newnew_find, $offset);

						if ($newnew_pos === false) {

							if ($pos == 0) {

								$range_substring = substr($search_string, 0);

								$new_search_string = str_replace($range_substring, 'listing_date_selected/'.$listing_date, $search_string);

								$_SESSION['main_search'] = $new_search_string;

							} else {

								$range_substring = substr($search_string, $pos);

								$new_search_string = str_replace($range_substring, 'listing_date_selected/'.$listing_date, $search_string);

								$_SESSION['main_search'] = $new_search_string;

							}

						} else {

							$range_substring = substr($search_string, $pos, ($newnew_pos - $pos));

							$new_search_string = str_replace($range_substring, 'listing_date_selected/'.$listing_date, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						}

						/*$range_substring = substr($search_string, 0);

						$new_search_string = str_replace($range_substring, 'listing_date_selected/'.$listing_date, $search_string);

						$this->session->set_userdata('main_search', $new_search_string);*/

					} else {

						$newnew_find = ',';

						$newnew_pos = strpos($search_string, $newnew_find, $offset);

						if ($newnew_pos === false) {

							$range_substring = substr($search_string, $pos, $new_pos);

							$new_search_string = str_replace($range_substring, 'listing_date_selected/'.$listing_date, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						} else {

							$range_substring = substr($search_string, $pos, ($newnew_pos - $pos));

							$new_search_string = str_replace($range_substring, 'listing_date_selected/'.$listing_date, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						}

						/*$range_substring = substr($search_string, $pos, $new_pos);

						$new_search_string = str_replace($range_substring, 'listing_date_selected/'.$listing_date, $search_string);

						$this->session->set_userdata('main_search', $new_search_string);*/

					}

				}

			//the main_search session variable is not set, so let's set it

			} else {

				$_SESSION['main_search'] = 'listing_date_selected/'.$listing_date;

			}

		} else {

			if(isset($_SESSION['main_search'])){

			 $search_string = $_SESSION['main_search'];

			}

			$find = 'listing_date_selected/';

			$pos = strpos($search_string,$find);

			//if it's in the search, remove it

			if ($pos !== false) {

				$offset = $pos + strlen($find);

				$new_find = ',';

				$new_pos = strpos($search_string, $new_find, $offset);

				if ($new_pos === false) {

					$range_substring = substr($search_string, 0);



					//search, replace, subject

					$new_search_string = str_replace($range_substring, '', $search_string);

					$_SESSION['main_search'] = $new_search_string;

				} else {

					//string, start, length

					$range_substring = substr($search_string, $pos, $new_pos + 1);

					$new_search_string = str_replace($range_substring, '', $search_string);

					$_SESSION['main_search'] = $new_search_string;

				}

			}

		}

		//if we don't have a listing_date then, either

		//	* the main_search session variable is set and it already includes listing_date - since we have a blank listing_date, we wouldn't change it

		// 	* the main_search session variable is set and it does not include listing_date - since we have a blank listing_date, we wouldn't add one

		// 	* the main_search session variable is not set - since we have a blank listing_date, we do not need to create the main_search session variable

		//********************************************************************************************************************************************************

		//farm_features

		//if we have farm_features, let's add it

		if ($farm_features != '') {

			if ($_SESSION['main_search']) {

				$search_string = $_SESSION['main_search'];

				$find = 'farm_features_selected/';

				$pos = strpos($search_string,$find);

				//if it's not already in the search, then let's add it

				if ($pos === false) {

					$_SESSION['main_search'] = $_SESSION['main_search'].',farm_features_selected/'.$farm_features;

				//if the string is in the search, let's replace it

				} else {

					$offset = $pos + strlen($find);

					$new_find = '/';

					$new_pos = strpos($search_string, $new_find, $offset);

					if ($new_pos === false) {

						$newnew_find = ',';

						$newnew_pos = strpos($search_string, $newnew_find, $offset);

						if ($newnew_pos === false) {

							if ($pos == 0) {

								$range_substring = substr($search_string, 0);

								$new_search_string = str_replace($range_substring, 'farm_features_selected/'.$farm_features, $search_string);

								$_SESSION['main_search'] =  $new_search_string;

							} else {

								$range_substring = substr($search_string, $pos);

								$new_search_string = str_replace($range_substring, 'farm_features_selected/'.$farm_features, $search_string);

								$_SESSION['main_search']= $new_search_string;

							}

						} else {

							$range_substring = substr($search_string, $pos, ($newnew_pos - $pos));

							$new_search_string = str_replace($range_substring, 'farm_features_selected/'.$farm_features, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						}

						/*$range_substring = substr($search_string, 0);

						$new_search_string = str_replace($range_substring, 'farm_features_selected/'.$farm_features, $search_string);

						$this->session->set_userdata('main_search', $new_search_string);*/

					} else {

						$newnew_find = ',';

						$newnew_pos = strpos($search_string, $newnew_find, $offset);

						if ($newnew_pos === false) {

							$range_substring = substr($search_string, $pos, $new_pos);

							$new_search_string = str_replace($range_substring, 'farm_features_selected/'.$farm_features, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						} else {

							$range_substring = substr($search_string, $pos, ($newnew_pos - $pos));

							$new_search_string = str_replace($range_substring, 'farm_features_selected/'.$farm_features, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						}

						/*$range_substring = substr($search_string, $pos, $new_pos);

						$new_search_string = str_replace($range_substring, 'farm_features_selected/'.$farm_features, $search_string);

						$this->session->set_userdata('main_search', $new_search_string);*/

					}

				}

			//the main_search session variable is not set, so let's set it

			} else {

				$_SESSION['main_search'] = 'farm_features_selected/'.$farm_features;

			}

		} else {

			if(isset($_SESSION['main_search'])){

				$search_string = $_SESSION['main_search'];

		    }

			$find = 'farm_features_selected/';

			$pos = strpos($search_string,$find);

			//if it's in the search, remove it

			if ($pos !== false) {

				$offset = $pos + strlen($find);

				$new_find = ',';

				$new_pos = strpos($search_string, $new_find, $offset);

				if ($new_pos === false) {

					$range_substring = substr($search_string, 0);



					//search, replace, subject

					$new_search_string = str_replace($range_substring, '', $search_string);

					$_SESSION['main_search'] = $new_search_string;

				} else {

					//string, start, length

					$range_substring = substr($search_string, $pos, $new_pos + 1);

					$new_search_string = str_replace($range_substring, '', $search_string);

					$_SESSION['main_search'] = $new_search_string;

				}

			}

		}

		//if we don't have a farm_features then, either

		//	* the main_search session variable is set and it already includes farm_features - since we have a blank farm_features, we wouldn't change it

		// 	* the main_search session variable is set and it does not include farm_features - since we have a blank farm_features, we wouldn't add one

		// 	* the main_search session variable is not set - since we have a blank farm_features, we do not need to create the main_search session variable

		//********************************************************************************************************************************************************

		//search_term

		//if we have search_term, let's add it

		if ($search_term != '') {

			if ($_SESSION['main_search']) {

				$search_string = $_SESSION['main_search'];

				$find = 'search_term_selected/';

				$pos = strpos($search_string,$find);

				//if it's not already in the search, then let's add it

				if ($pos === false) {

					$_SESSION['main_search'] = $_SESSION['main_search'].',search_term_selected/'.$search_term;

				//if the string is in the search, let's replace it

				} else {

					$offset = $pos + strlen($find);

					$new_find = '/';

					$new_pos = strpos($search_string, $new_find, $offset);

					if ($new_pos === false) {

						$newnew_find = ',';

						$newnew_pos = strpos($search_string, $newnew_find, $offset);

						if ($newnew_pos === false) {

							if ($pos == 0) {

								$range_substring = substr($search_string, 0);

								$new_search_string = str_replace($range_substring, 'search_term_selected/'.$search_term, $search_string);

								$_SESSION['main_search'] = $new_search_string;

							} else {

								$range_substring = substr($search_string, $pos);

								$new_search_string = str_replace($range_substring, 'search_term_selected/'.$search_term, $search_string);

								$_SESSION['main_search'] = $new_search_string;

							}

						} else {

							$range_substring = substr($search_string, $pos, ($newnew_pos - $pos));

							$new_search_string = str_replace($range_substring, 'search_term_selected/'.$search_term, $search_string);

							$_SESSION['main_search']= $new_search_string;

						}

						/*$range_substring = substr($search_string, 0);

						$new_search_string = str_replace($range_substring, 'search_term_selected/'.$search_term, $search_string);

						$this->session->set_userdata('main_search', $new_search_string);*/

					} else {

						$newnew_find = ',';

						$newnew_pos = strpos($search_string, $newnew_find, $offset);

						if ($newnew_pos === false) {

							$range_substring = substr($search_string, $pos, $new_pos);

							$new_search_string = str_replace($range_substring, 'search_term_selected/'.$search_term, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						} else {

							$range_substring = substr($search_string, $pos, $newnew_pos);

							$new_search_string = str_replace($range_substring, 'search_term_selected/'.$search_term, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						}

						/*$range_substring = substr($search_string, $pos, $new_pos);

						$new_search_string = str_replace($range_substring, 'search_term_selected/'.$search_term, $search_string);

						$this->session->set_userdata('main_search', $new_search_string);*/

					}

				}

			//the main_search session variable is not set, so let's set it

			} else {

				$_SESSION['main_search'] = 'search_term_selected/'.$search_term;

			}

		} else {

			if(isset($_SESSION['main_search'])){

			$search_string = $_SESSION['main_search'];

		    }

			$find = 'search_term_selected/';

			$pos = strpos($search_string,$find);

			//if it's in the search, remove it

			if ($pos !== false) {

				$offset = $pos + strlen($find);

				$new_find = ',';

				$new_pos = strpos($search_string, $new_find, $offset);

				if ($new_pos === false) {

					$range_substring = substr($search_string, 0);



					//search, replace, subject

					$new_search_string = str_replace($range_substring, '', $search_string);

					$_SESSION['main_search']= $new_search_string;

				} else {

					//string, start, length

					$range_substring = substr($search_string, $pos, $new_pos + 1);

					$new_search_string = str_replace($range_substring, '', $search_string);

					$_SESSION['main_search'] =  $new_search_string;

				}

			}

		}

		//if we don't have a search_term then, either

		//	* the main_search session variable is set and it already includes search_term - since we have a blank search_term, we wouldn't change it

		// 	* the main_search session variable is set and it does not include search_term - since we have a blank search_term, we wouldn't add one

		// 	* the main_search session variable is not set - since we have a blank search_term, we do not need to create the main_search session variable

		//********************************************************************************************************************************************************

		//max_year_built

		//if we have max_year_built, let's add it

		if ($max_year_built != '') {

			if ($_SESSION['main_search']) {

				$search_string = $_SESSION['main_search'];

				$find = 'max_year_built_selected/';

				$pos = strpos($search_string,$find);

				//if it's not already in the search, then let's add it

				if ($pos === false) {

					$_SESSION['main_search'] = $_SESSION['main_search'].',max_year_built_selected/'.$max_year_built;

				//if the string is in the search, let's replace it

				} else {

					$offset = $pos + strlen($find);

					$new_find = '/';

					$new_pos = strpos($search_string, $new_find, $offset);

					if ($new_pos === false) {

						$newnew_find = ',';

						$newnew_pos = strpos($search_string, $newnew_find, $offset);

						if ($newnew_pos === false) {

							if ($pos == 0) {

								$range_substring = substr($search_string, 0);

								$new_search_string = str_replace($range_substring, 'max_year_built_selected/'.$max_year_built, $search_string);

								$_SESSION['main_search']= $new_search_string;

							} else {

								$range_substring = substr($search_string, $pos);

								$new_search_string = str_replace($range_substring, 'max_year_built_selected/'.$max_year_built, $search_string);

								$_SESSION['main_search'] = $new_search_string;

							}

						} else {

							$range_substring = substr($search_string, $pos, ($newnew_pos - $pos));

							$new_search_string = str_replace($range_substring, 'max_year_built_selected/'.$max_year_built, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						}

						/*$range_substring = substr($search_string, 0);

						$new_search_string = str_replace($range_substring, 'max_year_built_selected/'.$max_year_built, $search_string);

						$this->session->set_userdata('main_search', $new_search_string);*/

					} else {

						$newnew_find = ',';

						$newnew_pos = strpos($search_string, $newnew_find, $offset);

						if ($newnew_pos === false) {

							$range_substring = substr($search_string, $pos, $new_pos);

							$new_search_string = str_replace($range_substring, 'max_year_built_selected/'.$max_year_built, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						} else {

							$range_substring = substr($search_string, $pos, $newnew_pos);

							$new_search_string = str_replace($range_substring, 'max_year_built_selected/'.$max_year_built, $search_string);

							$_SESSION['main_search'] = $new_search_string;

						}

						/*$range_substring = substr($search_string, $pos, $new_pos);

						$new_search_string = str_replace($range_substring, 'max_year_built_selected/'.$max_year_built, $search_string);

						$this->session->set_userdata('main_search', $new_search_string);*/

					}

				}

			//the main_search session variable is not set, so let's set it

			} else {

				$_SESSION['main_search'] = 'max_year_built_selected/'.$max_year_built;

			}

		} else {

			if(isset($_SESSION['main_search'])){

				$search_string = $_SESSION['main_search'];

			}

			$find = 'max_year_built_selected/';

			$pos = strpos($search_string,$find);

			//if it's in the search, remove it

			if ($pos !== false) {

				$offset = $pos + strlen($find);

				$new_find = ',';

				$new_pos = strpos($search_string, $new_find, $offset);

				if ($new_pos === false) {

					$range_substring = substr($search_string, 0);



					//search, replace, subject

					$new_search_string = str_replace($range_substring, '', $search_string);

					$_SESSION['main_search'] = $new_search_string;

				} else {

					//string, start, length

					$range_substring = substr($search_string, $pos, $new_pos + 1);

					$new_search_string = str_replace($range_substring, '', $search_string);

					$_SESSION['main_search'] = $new_search_string;

				}

			}

		}

		//if we don't have a max_year_built then, either

		//	* the main_search session variable is set and it already includes max_year_built - since we have a blank max_year_built, we wouldn't change it

		// 	* the main_search session variable is set and it does not include max_year_built - since we have a blank max_year_built, we wouldn't add one

		// 	* the main_search session variable is not set - since we have a blank max_year_built, we do not need to create the main_search session variable

		//********************************************************************************************************************************************************

		//make sure main_search variable is set

		



































		

		if (isset($_SESSION['main_search'])) {

			$main_search = $_SESSION['main_search'];

		} else {

			$main_search = '';

		}

		// Toggle if searchbox will display or not

		if ($searchbox_toggle != '') {

			$data['searchbox_toggle'] = $searchbox_toggle;

		} else {

			$data['searchbox_toggle'] = 'yes';

		}

		// Toggle if map will display or not

		if ($map_toggle != '') {

			$data['map_toggle'] = $map_toggle;

		} else {

			$data['map_toggle'] = 'yes';

		}

		/*****************************************************************************

		 DEBUG

		*****************************************************************************/

		//$property_type = 'residential';

		//echo 'searchScope: '.$searchScope;

		//echo 'price: '.$price_sort.' date: '.$date_sort;

		//echo 'variable date sort: '.$date_sort.' variable price sort: '.$price_sort.'<br>';

		//echo 'session date sort: '.$this->session->userdata('date_sort').' session price sort: '.$this->session->userdata('price_sort').'<br>';

		//$data['var_price_sort'] = $price_sort;

		//$data['var_date_sort'] = $date_sort;

		//$data['sess_price_sort'] = $this->session->userdata('price_sort');

		//$data['sess_date_sort'] = $this->session->userdata('date_sort');

		/*****************************************************************************

		 END DEBUG

		*****************************************************************************/

		//pagination

		$config = array();

		$config['base_url'] = base_url() . 'properties';

		$config['uri_segment'] = 2;

		if(isset($max_listing) &&  $max_listing != '') {

			$config['per_page'] = $max_listing;

		}else{

			$config['per_page'] = 25;

		}

		$page =  0; $page_no = 1;

		if(isset($_GET['page_no']) && !empty($_GET['page_no']) && is_numeric($_GET['page_no'])){

			$page  = ($_GET['page_no']-1)*$config['per_page'];

			$page_no = $_GET['page_no'];

		}







		//get the data for the search - search across all property tables

		$listings = array();

		$count_listings = 0;

		//Paragon

		

		// for virtual tours listings only...

		if ($mls_type == 'paragon') {



		

			//if we have set the default property type

			if ($default == 1) {

				$count_listings = $model_Obj->properties_model->count_perform_search($property_type, $main_search, $price_sort, $date_sort,$new_filters);

				$listings = $model_Obj->properties_model->perform_search($config['per_page'], $page, $property_type, $main_search, $price_sort, $date_sort,$new_filters);

					

				if ($count_listings <= 0) {

					$property_type = 'land';

					$_SESSION['property_type'];

					$_SESSION['property_type'] = $property_type;

					$count_listings = $model_Obj->properties_model->count_perform_search($property_type, $main_search, $price_sort, $date_sort,$new_filters);

					$listings = $model_Obj->properties_model->perform_search($config['per_page'], $page, $property_type, $main_search, $price_sort, $date_sort,$new_filters);

					if ($count_listings <= 0) {

						$property_type = 'land';

						unset($_SESSION['property_type']);

						$_SESSION['property_type'] = $property_type;

						$count_listings = $model_Obj->properties_model->count_perform_search($property_type, $main_search, $price_sort, $date_sort,$new_filters);

						$listings = $model_Obj->properties_model->perform_search($config['per_page'], $page, $property_type, $main_search, $price_sort, $date_sort,$new_filters);

						if ($count_listings <= 0) {

							$property_type = 'commercialLease';

							unset($_SESSION['property_type']);

							$_SESSION['property_type'] =  $property_type;

							$count_listings = $model_Obj->properties_model->count_perform_search($property_type, $main_search, $price_sort, $date_sort,$new_filters);

							$listings = $model_Obj->properties_model->perform_search($config['per_page'], $page, $property_type, $main_search, $price_sort, $date_sort,$new_filters);

							if ($count_listings <= 0) {

								$property_type = 'commercialSales';

								unset($_SESSION['property_type']);

								$_SESSION['property_type'] = $property_type;

								$count_listings = $model_Obj->properties_model->count_perform_search($property_type, $main_search, $price_sort, $date_sort,$new_filters);

								$listings = $model_Obj->properties_model->perform_search($config['per_page'], $page, $property_type, $main_search, $price_sort, $date_sort,$new_filters);

								if ($count_listings <= 0) {

									$property_type = 'multiFamily';

									unset($_SESSION['property_type']);

									$_SESSION['property_type'] = $property_type;

									$count_listings = $model_Obj->properties_model->count_perform_search($property_type, $main_search, $price_sort, $date_sort,$new_filters);

									$listings = $model_Obj->properties_model->perform_search($config['per_page'], $page, $property_type, $main_search, $price_sort, $date_sort,$new_filters);

									if ($count_listings <= 0) {

										$property_type = 'rental';

										unset($_SESSION['property_type']);

										$_SESSION['property_type'] =  $property_type;

										$count_listings = $model_Obj->properties_model->count_perform_search($property_type, $main_search, $price_sort, $date_sort,$new_filters);

										$listings = $model_Obj->properties_model->perform_search($config['per_page'], $page, $property_type, $main_search, $price_sort, $date_sort,$new_filters);

									}

								}

							}

						}

					}

				}

			//multiple property types from filters plugin

			} elseif (strpos($property_type,'~') !== false) {

				$property_types_arr = explode("~", $property_type);

				$prop_type_count = 0;

				foreach ($property_types_arr as $prop_type) {

					$this_property_type = $prop_type;

					if ($prop_type_count == 0) {

						$count_listings = $model_Obj->properties_model->count_perform_search($this_property_type, $main_search, $price_sort, $date_sort,$new_filters);

						$listings = $model_Obj->properties_model->perform_search($config['per_page'], $page, $this_property_type, $main_search, $price_sort, $date_sort,$new_filters);

					} else {

						$count_listings = $count_listings + $model_Obj->properties_model->count_perform_search($this_property_type, $main_search, $price_sort, $date_sort,$new_filters);

						$thisreturneddata = $model_Obj->properties_model->perform_search($config['per_page'], $page, $this_property_type, $main_search, $price_sort, $date_sort,$new_filters);

						if (count($listings) > 0) {

							$listings = array_merge($listings,$thisreturneddata);

						}

					}

					$prop_type_count++;

				}

			//otherwise, a normal search

			} else {

			

				$count_listings = $model_Obj->properties_model->count_perform_search($property_type, $main_search, $price_sort, $date_sort,$new_filters);

				// count taking approx. 800ms

				$listings = $model_Obj->properties_model->perform_search($config['per_page'], $page, $property_type, $main_search, $price_sort, $date_sort,$new_filters);

				//above sql taking approx. 1.2 seconds

				//echo 122;

				//exit;



			}

		//FlexMLS

		} else {

			//if we have set the default property type

			if ($default == 1) {

				$count_listings = $model_Obj->flexproperties_model->count_perform_search($property_type, $main_search, $price_sort, $date_sort,$new_filters);

				$listings = $model_Obj->flexproperties_model->perform_search($config['per_page'], $page, $property_type, $main_search, $price_sort, $date_sort,$new_filters);

				if ($count_listings <= 0) {

					$property_type = 'land';

					unset($_SESSION['property_type']);

					$_SESSION['property_type'] = $property_type;

					$count_listings = $model_Obj->flexproperties_model->count_perform_search($property_type, $main_search, $price_sort, $date_sort,$new_filters);

					$listings = $model_Obj->flexproperties_model->perform_search($config['per_page'], $page, $property_type, $main_search, $price_sort, $date_sort,$new_filters);

					if ($count_listings <= 0) {

						$property_type = 'multiFamily';

						unset($_SESSION['property_type']);

						$_SESSION['property_type'] = $property_type;

						$count_listings = $model_Obj->flexproperties_model->count_perform_search($property_type, $main_search, $price_sort, $date_sort,$new_filters);

						$listings = $model_Obj->flexproperties_model->perform_search($config['per_page'], $page, $property_type, $main_search, $price_sort, $date_sort,$new_filters);

						if ($count_listings <= 0) {

							$property_type = 'commercial';

							unset($_SESSION['property_type']);

							$_SESSION['property_type'] = $property_type;

							$count_listings = $model_Obj->flexproperties_model->count_perform_search($property_type, $main_search, $price_sort, $date_sort,$new_filters);

							$listings = $model_Obj->flexproperties_model->perform_search($config['per_page'], $page, $property_type, $main_search, $price_sort, $date_sort,$new_filters);

						}

					}

				}

			//multiple property types from filters plugin

			} elseif (strpos($property_type,'~') !== false) {

				$property_types_arr = explode("~", $property_type);

				$prop_type_count = 0;

				foreach ($property_types_arr as $prop_type) {

					$this_property_type = $prop_type;

					if ($prop_type_count == 0) {

						$count_listings = $model_Obj->flexproperties_model->count_perform_search($this_property_type, $main_search, $price_sort, $date_sort,$new_filters);

						$listings = $model_Obj->flexproperties_model->perform_search($config['per_page'], $page, $this_property_type, $main_search, $price_sort, $date_sort,$new_filters);

					} else {

						$count_listings = $count_listings + $model_Obj->flexproperties_model->count_perform_search($this_property_type, $main_search, $price_sort, $date_sort,$new_filters);

						$thisreturneddata = $model_Obj->flexproperties_model->perform_search($config['per_page'], $page, $this_property_type, $main_search, $price_sort, $date_sort,$new_filters);

						if (count($listings) > 0) {

							$listings = array_merge($listings,$thisreturneddata);

						}

					}

					$prop_type_count++;

				}

			//otherwise, a normal search

			} else {

				

				$count_listings = $model_Obj->flexproperties_model->count_perform_search($property_type, $main_search, $price_sort, $date_sort,$new_filters);

				$listings = $model_Obj->flexproperties_model->perform_search($config['per_page'], $page, $property_type, $main_search, $price_sort, $date_sort,$new_filters);

			}

		}

		//more pagination

		$config['total_rows'] = $count_listings;



	    //Get  realconnect user information

		$data['rc_user_email'] = '';

		$data['rc_display_name'] = '';





		/* vk, I think this code (following foreach loop) is not needed on listings page */

		if(!$disableUnusedCode)

		{

		

			foreach($listings as &$listing)

			{ 

				$mls_id = $listing['L_ListingID'];

				$property_type = $listing['property_type'];

				$contact_info_belong_to_our_client = $model_Obj->shared_model->contact_info_belong_to_our_client($mls_id,$property_type,$office_id);

				

				if(count($contact_info_belong_to_our_client) && isset($contact_info_belong_to_our_client['U_Email']) && $contact_info_belong_to_our_client['U_Email'] != ''){

					$data['rc_user_email'] = $contact_info_belong_to_our_client['U_Email'];

					$data['rc_display_name'] = $contact_info_belong_to_our_client['U_UserFirstName'].' '.$contact_info_belong_to_our_client['U_UserLastName'];

					$data['rotating_agent_show_contact_info'] = false;

				}

				else if (isset($client_info['email']) && $client_info['email'] != '') {

					$listing['rc_user_email'] = $client_info['email'];

					$listing['rc_display_name'] = $client_info['first_name'].' '.$client_info['last_name'];



				}

			}

	}	





		if(post_type_exists('real_connect')){

			$rc_price_range_filter_min = '';

				$rc_price_range_filter_max = '';

				if(isset($_SESSION['price_range_meta'])){

				$rc_price_range_filter = explode("_",$_SESSION['price_range_meta']);

				$rc_price_range_filter_min = $rc_price_range_filter[0];

				$rc_price_range_filter_max = $rc_price_range_filter[1];



				}

			/* Get admin custom listing data */

			$rc_custom_listing_count = $model_Obj->custom_mls_listing_model->rc_custom_mls_listing_count($property_type,$rc_price_range_filter_min,$rc_price_range_filter_max);



      //$data['rc_filter_listings'] = $model_Obj->properties_model->perform_search(30,0,$property_type,'office_selected/165','DESC',$date_sort = null);



      //$count_listings += count($data['rc_filter_listings']);



			if(($count_listings-$config['per_page']) >= $page){

				unset($data['rc_custom_listing']);

			}else{

				$posts_per_page ='-1';

				$posts_offset = 0;

				if(count($listings)){

					$posts_offset = 0;

					$posts_per_page = $config['per_page']-count($listings);



				}else{



					$posts_offset = $page - $count_listings;

					//$posts_per_page = $rc_custom_listing_count-$posts_per_page;

					$posts_per_page = 25;

				}

				$data['rc_custom_listing'] = $model_Obj->custom_mls_listing_model->rc_custom_mls_listing_details($property_type,$posts_offset,$posts_per_page,$rc_price_range_filter_min,$rc_price_range_filter_max);



			}



			$count_listings += $rc_custom_listing_count;

		}



    // pagination

		$data['links'] = custom_pagination($count_listings,$page_no,$config['per_page']);



		//load arrays with data that will be sent to the views

		$data['listings'] = $listings;

		$data['property_type'] = $property_type;

		$data['main_search'] = $main_search;

		$data['count_listings'] = $count_listings;







		if ($mls_type == 'paragon') {



			//get queries to prefill area and subdivision

			$elem_autofill=$middle_autofill=$high_autofill=$areas_autofill=$subdivision_autofill=array();

			/* VK I think below is not needed on listings page now */

			if(!$disableUnusedCode)

			{

				$areas_autofill = $model_Obj->shared_model->get_areas();



				$subdivision_autofill = $model_Obj->shared_model->get_subdivisions();

				$data['areas_autofill'] = $areas_autofill;

				$data['subdivision_autofill'] = $subdivision_autofill;

				//get queries to prefill school

				$elem_autofill = $model_Obj->shared_model->get_elem_schools();

				$middle_autofill = $model_Obj->shared_model->get_middle_schools();

				$high_autofill = $model_Obj->shared_model->get_high_schools();

			}

			

			

			$data['elem_autofill'] = $elem_autofill;

			$data['middle_autofill'] = $middle_autofill;

			$data['high_autofill'] = $high_autofill;

			$data['feautred_listings'] = $model_Obj->properties_model->rc_get_featured_listings();

			//load views

		

			if ($map == 'map') {



				//$this->load->view('main/map_view', $data);

				$_SESSION['map_view_data'] = $data;

				$_template_file = LoadPHPFile('/RealConnect/views/main/map_view.php');

				load_template( $_template_file);

			} else {



				//$this->load->view('main/listings_view', $data);

				$_SESSION['listings_view_data'] = $data;

		

				if(empty($rc_listing_view_template_file_path)){

					$_template_file = LoadPHPFile('/RealConnect/views/main/listings_view.php');

			    }else{

					$_template_file = $rc_listing_view_template_file_path;

				}

				//echo $_template_file;

				load_template( $_template_file);

			}

		} else {

			$areas_autofill=$subdivision_autofill=$elem_autofill=$middle_autofill=$high_autofill=$residential_autofill=$commercial_autofill=$land_autofill=array();

			

			

			

			/* VK I think below is not needed on listings page now */

			if(!$disableUnusedCode)

			{

				//get queries to prefill area and subdivision

				$areas_autofill = $model_Obj->shared_model->get_flex_areas();

				$subdivision_autofill = $model_Obj->shared_model->get_flex_subdivisions();

				$data['areas_autofill'] = $areas_autofill;

				$data['subdivision_autofill'] = $subdivision_autofill;

				//get queries to prefill school

				$elem_autofill = $model_Obj->shared_model->get_flex_elem_schools();

				$middle_autofill = $model_Obj->shared_model->get_flex_middle_schools();

				$high_autofill = $model_Obj->shared_model->get_flex_high_schools();

				$data['elem_autofill'] = $elem_autofill;

				$data['middle_autofill'] = $middle_autofill;

				$data['high_autofill'] = $high_autofill;

				//get residential subcategry info

				$residential_autofill = $model_Obj->shared_model->get_flex_res_subcategory();

				$data['res_autofill'] = $residential_autofill;

				//get commercial subcategory info

				$commercial_autofill = $model_Obj->shared_model->get_flex_comm_subcategory();

				$data['comm_autofill'] = $commercial_autofill;

				//get land subcategory info

				$land_autofill = $model_Obj->shared_model->get_flex_land_subcategory();

				$data['land_autofill'] = $land_autofill;

			

			}	



		

			//load views

			if ($map == 'map') {

				//$this->load->view('main/map_view', $data);

				$_SESSION['map_view_data'] = $data;

				$_template_file = LoadPHPFile('/RealConnect/views/main/map_view.php');

				load_template( $_template_file);

			} else {

				//$this->load->view('main/flexlistings_view', $data);

				$_SESSION['flexlistings_view_data'] = $data;

				$_template_file = LoadPHPFile('/RealConnect/views/main/flexlistings_view.php');

				load_template( $_template_file);

			}

		}



}



function rc_filter_company_listing_func($atts = '')

{



  $atts_values = shortcode_atts( array(

        'office_id' => "office_selected/".$atts['office'],

        'property_type' => $atts['prop_type'],

        'sort_order' => $atts['sort_order'],

        'max_listings' => $atts['max_listings'],

    ), $atts );



    //print_r($atts_values);

    $data = array();

    $site_url = get_site_url();

    $model_Obj = new \stdClass();

    $model_Obj->shared_model = new Shared_model();

    $model_Obj->properties_model = new Properties_model();



    $client_info = $model_Obj->shared_model->get_client_info($site_url);

    $mls_type = 'paragon';

    $office_id = '';

    if (count($client_info) > 0) {

      $mls_type = $client_info['mls_feed'];

      $office_id = $client_info['office_id'];

    }



    //set post variables

		$property_type = '';

		if(isset($_POST['property_type'])){

		$property_type = $_POST['property_type'];

	    }

    $start = 0;

		$property_type_input = $property_type;



    //$data = $model_Obj->properties_model->rc_filter_company_listing_model($atts_values['office_id'], $atts_values['property_type'],  $atts_values['sort_order'], $atts_values['max_listings']);

    $data['rc_filter_listings'] = $model_Obj->properties_model->perform_search($atts_values['max_listings'],$start,$atts_values['property_type'], $atts_values['office_id'], $atts_values['sort_order'],$date_sort = null);



    // echo '<pre>';

    // print_r($data);

    $_SESSION['office_view_data'] = $data;

    $_template_file = LoadPHPFile('/RealConnect/views/main/grid_listings_view_filter.php');

    load_template( $_template_file);

}

add_shortcode( 'rc_filter', 'rc_filter_company_listing_func' );





function propertyListingWithOfficeId($atts = ''){

	$sort_term = $_GET['price_sort'];

	if(!empty($sort_term)){

		$sort_arr = explode("_", $sort_term);

		//print_r($sort_arr);

		$sort_description = $sort_arr[1];

		if ($sort_description == 'high') {

			$price_sort = 'desc';

		}elseif ($sort_description == 'low') {

			$price_sort = 'asc';

		}

	}

	 $attsValues = shortcode_atts( array(

        'office_id' => $atts['office'],

        'property_type' => $atts['prop_type'],

        'max_listings' => $atts['max_listings'],

        'date_sort' => $atts['date_sort'],

        'area' => $atts['area'],

        'price' => $atts['price'],

    ), $atts );

    $area = $attsValues['area'];

	$site_url = get_site_url();

	$model_Obj = new \stdClass();

	$model_Obj->shared_model = new Shared_model();

	$model_Obj->properties_model = new Properties_model();

	$model_Obj->flexproperties_model = new Flexproperties_model();

	$client_info = $model_Obj->shared_model->get_client_info($site_url);

	$mls_type = 'paragon';

	if (count($client_info) > 0) {

		$mls_type = $client_info['mls_feed'];

		$officeId = $client_info['office_id'];

		if(!empty($attsValues['office_id'])){

			$office_id = $attsValues['office_id'];

		}else{

			$office_id = $officeId;

		}

	}

	if ($mls_type == 'paragon') {

		$template = show_template();

		$data['rc_filter_listings'] = $model_Obj->properties_model->getProperties($attsValues['max_listings'],$office_id,$attsValues['property_type'],$attsValues['date_sort'],$price_sort);

		 $total_company_listing = count($data['rc_filter_listings']);

		 $total_listing = $attsValues['max_listings'];

		 $mls_listing_limit = $total_listing - $total_company_listing;

		 if($total_listing > $total_company_listing){

			$data['rc_filter_mls_listings'] = $model_Obj->properties_model->getPropertiesWithoutOfficeId($mls_listing_limit,$office_id,$attsValues['property_type'],$price_sort,$area,$attsValues['price']);

		 }

		 if(!empty($data['rc_filter_mls_listings']) && !empty($data['rc_filter_listings'])){

			$listingResults['rc_filter_listings'] = array_merge($data['rc_filter_listings'],$data['rc_filter_mls_listings']);

		 }elseif(!empty($data['rc_filter_mls_listings'])){

			$listingResults['rc_filter_listings'] = $data['rc_filter_mls_listings'];

		 }else{

			$listingResults['rc_filter_listings'] = $data['rc_filter_listings'];

		 }

	     $_SESSION['office_view_data'] = $listingResults;

	     $_SESSION['office_id'] = $office_id;

	     if($template=='properties-grid-layout-template.php'){

			$_template_file = LoadPHPFile('/RealConnect/views/main/grid_listings_view_filter.php');

		 }elseif($template=='properties-template.php'){

			$_template_file = LoadPHPFile('/RealConnect/views/main/listings_view_filter.php');

		 }

		 load_template( $_template_file);

	}

	else{

		$data['rc_filter_listings'] = $model_Obj->flexproperties_model->getFlexProperties($attsValues['max_listings'],$office_id,$attsValues['property_type'],$attsValues['date_sort'],$price_sort);

		$total_company_listing = count($data['rc_filter_listings']);

		$total_listing = $attsValues['max_listings'];

		$flex_listing_limit = $total_listing - $total_company_listing;

		if($total_listing > $total_company_listing){

			$data['rc_filter_flex_listings'] = $model_Obj->flexproperties_model->getFlexPropertiesWithoutOfficeId($flex_listing_limit,$office_id,$attsValues['property_type'],$price_sort,$area,$attsValues['price']);

		}

		if(!empty($data['rc_filter_flex_listings']) && !empty($data['rc_filter_listings'])){

			$listingResults['listings'] = array_merge($data['rc_filter_listings'],$data['rc_filter_flex_listings']);

		 }elseif(!empty($data['rc_filter_flex_listings'])){

			$listingResults['listings'] = $data['rc_filter_flex_listings'];

		 }else{

			$listingResults['listings'] = $data['rc_filter_listings'];

		 }

		$_SESSION['office_view_data'] = $listingResults;

	    $_SESSION['office_id'] = $office_id;

		$_template_file = LoadPHPFile('/RealConnect/views/main/flexlistings_view_filter.php');

		load_template( $_template_file);

	}

}

add_shortcode( 'rc_property_listing_filter', 'propertyListingWithOfficeId' );



function show_template() {

    global $template;

    return basename($template);

}