<?php
if ( !defined( 'SMARTY_DIR' ) ) {

     include_once( '../init.php' );

}

include( 'sessioninc.php' );

$data = array();

$sectionid = $_REQUEST[ 'sectionid' ];

if ($_REQUEST['search_new'] == 1 or (!isset($sectionid) && !isset($_REQUEST['advsearch']) && !isset($_SESSION['advsearch']) ) ) {

    $sectionid = 0;

    $_SESSION['advsearch'] = array();

    $_SESSION['advsearch']['srchlookagestart'] = $config['end_year']*-1;

    $_SESSION['advsearch']['srchlookageend'] = $config['start_year']*-1;

}

/*  Query to reterive records from osdate_questions table
 sorted descending on mandatory -  mandatory fields should be displayed first
*/

if ((!isset($get_search) || $get_search == '') || isset($_REQUEST['advsearch'])) {

    if ($_REQUEST['cursectionid'] == '0' ) {
        /* Save data from section 0 - signup data */

        $_SESSION['advsearch']['srchusername'] = $_REQUEST['srchusername'];


        if (isset($_REQUEST['srchgender']) ) {

            $_SESSION['advsearch']['srchgender'] = $_REQUEST['srchgender'];

        } else {
			unset($_SESSION['advsearch']['srchgender']);
		}

        if (isset($_REQUEST['srchlookgender']) ) {

            $_SESSION['advsearch']['srchlookgender'] = $_REQUEST['srchlookgender'];

        } else {
		    unset($_SESSION['advsearch']['srchlookgender']);
		}

        $_SESSION['advsearch']['srchlookagestart'] = $_REQUEST['srchlookagestart'];

        $_SESSION['advsearch']['srchlookageend'] = $_REQUEST['srchlookageend'];

        $_SESSION['advsearch']['srchradius'] = $_REQUEST['lookradius'];

        $_SESSION['advsearch']['radiustype'] = $_REQUEST['radiustype'];

		$_SESSION['advsearch']['with_photo'] = $_REQUEST['with_photo'];

		$_SESSION['advsearch']['with_video'] = $_REQUEST['with_video'];
		$_SESSION['advsearch']['who_is_online'] = $_REQUEST['who_is_online'];

        $_SESSION['advsearch']['srchlookcountry'] = $srchlookcountry = $_REQUEST['srchlookcountry'];

        $_SESSION['advsearch']['srchlookcounty'] = $srchlookcounty = $_REQUEST['srchlookcounty'];

        $_SESSION['advsearch']['srchlookstate_province'] = $srchlookstate_province = $_REQUEST['srchlookstate_province'];

        $_SESSION['advsearch']['srchlookcity'] = $srchlookcity = $_REQUEST['srchlookcity'];

        $_SESSION['advsearch']['srchlookzip'] = $srchlookzip = strtoupper($_REQUEST['srchlookzip']);

    } elseif ($_REQUEST['cursectionid'] > 0 && ( isset($_REQUEST['question']) || isset($_REQUEST['selected_questions']) ) ) {
	/* Check already selected options and if they are unchecked, remove session settings */
		if (isset($_REQUEST['selected_questions'])) {
			foreach ($_REQUEST['selected_questions'] as $k => $q) {
				$_SESSION['advsearch']['question'][$q]=null;
			}
		}

		if (isset($_REQUEST['question']) ) {
	        foreach ($_REQUEST['question'] as $mkey => $val) {
	 			$_SESSION['advsearch']['question'][$mkey] = $val;
			}
        }

    }
}
// edit by Adam
// make dropdowns propogate across sections
// pulled this code out of the above cursectionid==0 if block
// and moved down here so assignments were made and relations were kept outside of that section.

/* Added to avoid issue with !, ? and & as part of the field */
foreach ($_SESSION['advsearch'] as $fld => $fldval) {
	$_SESSION['advsearch'][$fld] = str_replace(array("?","!","&"),array("\?","\!","\&"), $fldval);
}

$srchlookcountry = $_SESSION['advsearch']['srchlookcountry'] ;

$srchlookcounty = $_SESSION['advsearch']['srchlookcounty'];

$srchlookstate_province = $_SESSION['advsearch']['srchlookstate_province'];

$srchlookcity = $_SESSION['advsearch']['srchlookcity'];

$srchlookzip = $_SESSION['advsearch']['srchlookzip'];

$srchradius = $_SESSION['advsearch']['srchradius'];

$with_photo = $_SESSION['advsearch']['with_photo'];

$with_video = $_SESSION['advsearch']['with_video'];
$who_is_online = $_SESSION['advsearch']['who_is_online'];

if ($srchlookcountry != '' && $srchlookcountry != 'AA') {

	$lang['lookstates'] = getStates($srchlookcountry,'Y');

	$zipsavailable = $osDB->getOne('select count(*) from ! where countrycode = ?', array(ZIPCODES_TABLE, $srchlookcountry) );

	$t->assign('zipsavailable', $zipsavailable);

	if (count($lang['lookstates']) == 1) {
		foreach ($lang['lookstates'] as $key => $val) {
			$_SESSION['advsearch']['srchlookstate_province'] = $srchlookstate_province = $key;
		}
	}

	if ($srchlookstate_province != '' && $srchlookstate_province != 'AA') {

		$lang['lookcounties'] = getCounties($srchlookcountry, $srchlookstate_province, 'Y');

		if (count($lang['lookcounties']) == 1) {
			foreach ($lang['lookcounties'] as $key => $val) {
				$_SESSION['advsearch']['srchlookcounty'] = $srchlookcounty = $key;
			}
		}

		if ($srchlookcounty != '' && $srchlookcounty != 'AA') {

			$lang['lookcities'] = getCities($srchlookcountry, $srchlookstate_province, $srchlookcounty, 'Y');

			if (count($lang['lookcities']) == 1) {
				foreach($lang['lookcities'] as $key => $val) {
					$_SESSION['advsearch']['srchlookcity'] = $srchlookcity = $key;
				}
			}

			if ($srchlookcity != '' && $srchlookcity != 'AA') {

				$lang['lookzipcodes'] = getZipcodes($srchlookcountry, $srchlookstate_province, $srchlookcounty, $srchlookcity, 'Y');
			}
		}
	}
}

//
// end of adam edits
//

$psize = getPageSize();

$t->assign ( 'psize',  $psize );


if (isset($_REQUEST['advsearch'])  ){
/* Search is requested. Now let us select data and display. Output in sqlselect. */

    if (!isset($_REQUEST['sort_by'])) {
    /* First time search actiated.. Prepare query */

 	       /* if not a blog search, do it like this */
 	       $sort_by = $config['search_sort_by'];

 	       $sort_order = ' asc ';

		   if ($sort_by == 'logintime') $sort_order = 'desc';

 	       if ($_SESSION['advsearch']['srchradius'] != '') {
 		       /* zipcode proximity search */
 	           /* First get the latitude and longitude of the zip code entered */
 	           $cntrycode=($_SESSION['advsearch']['srchlookcountry']!='AA')?$_SESSION['advsearch']['srchlookcountry']:$config['default_country'];

 	           $srchzip = $_SESSION['advsearch']['srchlookzip'];

 	           if ($cntrycode == 'GB') {
 	               $ukzip = explode(' ',$_SESSION['advsearch']['srchlookzip']);
 	               $srchzip = $ukzip[0];
 	           }

 	           $row =& $osDB->getRow('select * from ! where code=?  and countrycode=?',array(ZIPCODES_TABLE, $srchzip, $cntrycode ) );

 	           $lat = $row['latitude'];
 	           $lng = $row['longitude'];
			   unset($row);
 	           $zipcodes_in = "";

 	           if ($lng!='' && $lat!='') {

 	               $radius = $_SESSION['advsearch']['srchradius'];
 	               $radiustype = $_SESSION['advsearch']['radiustype'];

 	               if ($radiustype == 'kms') {
                    /* Kilometers calculation */
 	                   $_SESSION['advsearch']['zipcodes_in'] = " ( sqrt(power(69.1*(user.zip_latitude - $lat),2)+power(69.1*(user.zip_longitude-$lng)*cos(user.zip_latitude/57.3),2)) < " . $radius ." ) ";
 	               } else {
                    /* Miles  */
 	                   $_SESSION['advsearch']['zipcodes_in'] = " (  (3958* 3.1415926 * sqrt((user.zip_latitude - $lat) * (user.zip_latitude- $lat) + cos(user.zip_latitude / 57.29578) * cos($lat/57.29578)*(user.zip_longitude - $lng) * (user.zip_longitude - $lng))/180) < " . $radius ." ) ";
 	               }
				}
			}

	        $prefsel = "";

	        $questionmatch='';

	        $questionusers = array();

	        $_SESSION['advsearch']['questionusers']=array();
	        $_SESSION['advsearch']['questionmatch']='';
	        if (count($_SESSION['advsearch']['question']) > 0){

				$match_needed = 0;

		        /* Let us make the question query */

	            foreach($_SESSION['advsearch']['question'] as $questionid => $options) {

	                $opts = '';
					if (count($options) <= 0) {
						$_SESSION['advsearch']['question'][$questionid] = '';
					} else {
						if ($questionid != 5 && $questionid != 27) {
			                foreach ($options as $k => $val) {

			                    if ($val != '') {

			                        if ($opts != '' ) $opts.=', ';

			                        $opts .= "'".$val."'";
			                    }
							}
		                } else {
							if (count($options) > 0) {
								if ($options[0] != '' && $options[1] != '') {
									$opts = ' between '.$options[0].' and '.$options[1];
								}
							}
						}
					}
	                if ($opts != '') {
						$match_needed++;

	                    if ($prefsel != '') { $prefsel .= ' or '; }

						if ($questionid != 5 && $questionid != 27) {
		                    $prefsel .= " ( pref.questionid = ".$questionid." and pref.answer in ( ".$opts." ) ) ";
						} else {
		                    $prefsel .= " ( pref.questionid = ".$questionid." and pref.answer ".$opts." ) ";

						}
	                }
	            }

	            if ($prefsel != '') {

	                $questionusers =& $osDB->getAll("select distinct userid, count(questionid) as match_cnt from ! as pref where 1 and ".$prefsel." group by  userid",array(USER_PREFERENCE_TABLE) );

	                if (count($questionusers) > 0) {
						$met_condition = 0;
	                    foreach ($questionusers as $qst) {

							if ($qst['match_cnt'] == $match_needed) {
								$met_condition=1;
		                        if ($questionmatch != '') $questionmatch .= ", ";
		                        $questionmatch.= "'".$qst['userid']."'";
							}
	                    }


						if ($met_condition > 0 ) {
		                    $questionmatch = ' user.id in ('.$questionmatch.') ';
						} else {
							$questionmatch =" user.id in ('9999999999') ";
						}

	                    $_SESSION['advsearch']['questionusers']=$questionusers;
	                    $_SESSION['advsearch']['questionmatch']=$questionmatch;
						unset($questionusers);
	                } else {
						$questionmatch =" user.id in ('9999999999') ";
	                    $_SESSION['advsearch']['questionmatch']=$questionmatch;
					}
	            } else {
                    $_SESSION['advsearch']['questionusers']=array();
                    $_SESSION['advsearch']['questionmatch']='';
				}
				unset($questionusers, $questionmatch);
	        }

	     /* Make a banned users list */
	        $bannedlist = '';
	        $bannedusers = $osDB->getAll('select usr.id as ref_userid from ! as bdy, ! as usr where bdy.act=? and ((usr.username = bdy.username and bdy.ref_username = ?) or (usr.username = bdy.ref_username and bdy.username = ? ) )', array(BUDDY_BAN_TABLE, USER_TABLE, 'B', $_SESSION['UserName'], $_SESSION['UserName']) );
	        if (count($bannedusers) > 0) {
	            $bannedlist=' and user.id not in (';
	            $bdylst = '';
	            foreach ($bannedusers as $busr) {
	                if ($bdylst != '') $bdylst .= ',';
	                $bdylst .= "'".$busr['ref_userid']."'";
	            }
	            $bannedlist .=$bdylst.') ';
	            unset($bannedusers);
	        }

	        $actflag = get_lang('active');

			if ($with_photo) {
				if ($with_video) {
					if ($who_is_online) {
				        $sqlselect = "SELECT DISTINCT user.id, user.username, user.gender, user.lastvisit, user.country, user.state_province, user.county, user.about_me,  user.city, user.lookgender, user.zip, onl.is_online, floor((to_days(curdate())-to_days(birth_date))/365.25)  as age FROM ! mem, ! user, ! as onl, ".USER_SNAP_TABLE." as ph, ".USER_VIDEOS_TABLE." as vd where ph.userid=user.id and vd.userid = user.id and onl.userid=user.id and user.level=mem.roleid and mem.includeinsearch='1' and user.active=1 and user.status in ('active', '".$actflag."')  ";
					} else {
				        $sqlselect = "SELECT DISTINCT user.id, user.username, user.gender, user.lastvisit, user.country, user.state_province, user.county, user.about_me, user.city, user.lookgender, user.zip, onl.is_online, floor((to_days(curdate())-to_days(birth_date))/365.25)  as age FROM ! mem,".USER_SNAP_TABLE." as ph, ". USER_VIDEOS_TABLE." as vd, ! user left join ! as onl on onl.userid=user.id where ph.userid=user.id and vd.userid = user.id and user.level=mem.roleid and mem.includeinsearch='1' and user.active=1 and user.status in ('active', '".$actflag."')  ";
					}
				} else {
					if ($who_is_online) {
				        $sqlselect = "SELECT DISTINCT user.id, user.username, user.gender, user.lastvisit, user.country, user.state_province, user.county, user.about_me, user.city, user.lookgender, user.zip, onl.is_online, floor((to_days(curdate())-to_days(birth_date))/365.25)  as age FROM ! mem, ! user, ! as onl, ".USER_SNAP_TABLE." as ph where  ph.userid=user.id and onl.userid=user.id and user.level=mem.roleid and mem.includeinsearch='1' and user.active=1 and user.status in ('active', '".$actflag."')  ";
					} else {
				        $sqlselect = "SELECT DISTINCT user.id, user.username, user.gender, user.lastvisit, user.country, user.state_province, user.county, user.about_me, user.city, user.lookgender, user.zip, onl.is_online, floor((to_days(curdate())-to_days(birth_date))/365.25)  as age FROM ! mem,".USER_SNAP_TABLE." as ph, ! user left join ! as onl on onl.userid=user.id where  ph.userid=user.id and user.level=mem.roleid and mem.includeinsearch='1' and user.active=1 and user.status in ('active', '".$actflag."')  ";
					}
				}
			} else {
				if ($with_video) {
					if ($who_is_online) {
						$sqlselect = "SELECT DISTINCT user.id, user.username, user.gender, user.lastvisit, user.country, user.state_province, user.county, user.about_me, user.city, user.lookgender, user.zip, onl.is_online, floor((to_days(curdate())-to_days(birth_date))/365.25)  as age FROM ! mem, ! user, ! as onl, ".USER_VIDEOS_TABLE." as vd where vd.userid = user.id and onl.userid=user.id and user.level=mem.roleid and mem.includeinsearch='1' and user.active=1 and user.status in ('active', '".$actflag."')  ";
					} else {
						$sqlselect = "SELECT DISTINCT user.id, user.username, user.gender, user.lastvisit, user.country, user.state_province, user.county,user.about_me,  user.city, user.lookgender, user.zip, onl.is_online, floor((to_days(curdate())-to_days(birth_date))/365.25)  as age FROM ".USER_VIDEOS_TABLE." as vd, ! mem, ! user left join ! as onl on onl.userid=user.id where vd.userid = user.id and user.level=mem.roleid and mem.includeinsearch='1' and user.active=1 and user.status in ('active', '".$actflag."')  ";
					}
				} else {
					if ($who_is_online) {
						$sqlselect = "SELECT DISTINCT user.id, user.username, user.gender, user.lastvisit, user.country, user.state_province, user.county, user.about_me, user.city, user.lookgender, user.zip, onl.is_online, floor((to_days(curdate())-to_days(birth_date))/365.25)  as age FROM ! mem, ! user, ! as onl where onl.userid=user.id and user.level=mem.roleid and mem.includeinsearch='1' and user.active=1 and user.status in ('active', '".$actflag."')  ";
					} else {
						$sqlselect = "SELECT DISTINCT user.id, user.username, user.gender, user.lastvisit, user.country, user.state_province, user.county,user.about_me,  user.city, user.lookgender, user.zip, onl.is_online, floor((to_days(curdate())-to_days(birth_date))/365.25)  as age FROM ! mem, ! user left join ! as onl on onl.userid=user.id where user.level=mem.roleid and mem.includeinsearch='1' and user.active=1 and user.status in ('active', '".$actflag."')  ";
					}
				}
			}

	        if ($_SESSION['advsearch']['questionmatch'] != '') {
	            $sqlselect .= ' and '.$_SESSION['advsearch']['questionmatch'];
	        }

			if ($_SESSION['advsearch']['zipcodes_in']!='') {
				$sqlselect .= ' and '.$_SESSION['advsearch']['zipcodes_in'];
			}

			if ($_SESSION['advsearch']['srchusername'] != '') {
				$sqlselect .= "and upper(user.username) like upper('%".$_SESSION['advsearch']['srchusername']."%') ";
			}

			if (count($_SESSION['advsearch']['srchlookgender']) > 0) {
				$lookgender='';
				foreach($_SESSION['advsearch']['srchlookgender'] as $lg) {
					if ($lookgender != '') $lookgender.=", ";
					$lookgender .= "'".$lg."'";
				}
				$sqlselect .= " and user.gender in (".$lookgender.") ";
			}

			/* Bypass cross matching in search if set in global settings or the lookgender is not accepted */

			if ( ($config['bypass_search_lookgender'] == 'N' or $config['bypass_search_lookgender'] == '0' ) and ( $config['accept_lookgender'] == 'Y' or $config['accept_lookgender'] == '1') && $_SESSION['advsearch']['srchgender'] != '' ) {

				$txtgender_search = "and (user.lookgender = 'A' or (user.lookgender = 'B' and '".$_SESSION['advsearch']['srchgender']."' in ('M','F') ) or user.lookgender = '".$_SESSION['advsearch']['srchgender']."') ";

				$sqlselect .= $txtgender_search;
			}

			$yearend  = date("Y-m-d", mktime(0, 0, 0, date("m"),   date("d"),   date("Y") - $_SESSION['advsearch']['srchlookagestart']));
			$yearstart  = date("Y-m-d", mktime(0, 0, 0, date("m"),   date("d"),   date("Y") - $_SESSION['advsearch']['srchlookageend'])-1);

			$sqlselect .= " and ( birth_date  between '".$yearstart."' and '". $yearend."' ) ";

	        if ($_SESSION['advsearch']['srchlookcountry']!='' and $_SESSION['advsearch']['srchlookcountry']!= 'AA' and $_SESSION['advsearch']['srchlookcountry']!= '-1') {
            $sqlselect .= " and user.country = '".$_SESSION['advsearch']['srchlookcountry']."' ";
	        }
	        if ($_SESSION['advsearch']['srchlookcounty']!='' and $_SESSION['advsearch']['srchlookcounty']!= 'AA' and $_SESSION['advsearch']['srchlookcounty']!= '-1') {
            $sqlselect .= " and user.county = '".$_SESSION['advsearch']['srchlookcounty']."' ";
	        }
	        if ($_SESSION['advsearch']['srchlookstate_province']!='' and $_SESSION['advsearch']['srchlookstate_province']!= 'AA' and $_SESSION['advsearch']['srchlookstate_province']!= '-1') {
	            $sqlselect .= " and user.state_province = '".$_SESSION['advsearch']['srchlookstate_province']."' ";
	        }
	        if ($_SESSION['advsearch']['srchlookcity']!='' and $_SESSION['advsearch']['srchlookcity']!= 'AA' and $_SESSION['advsearch']['srchlookcity']!= '-1') {
	            $sqlselect .= " and user.city = '".$_SESSION['advsearch']['srchlookcity']."' ";
	        }
	        if (($_SESSION['advsearch']['srchlookzip']!='' and $_SESSION['advsearch']['srchlookzip']!= 'AA' and $_SESSION['advsearch']['srchlookzip']!= '-1') and $_SESSION['advsearch']['srchradius']=='') {
	            $sqlselect .= " and user.zip = '".$_SESSION['advsearch']['srchlookzip']."' ";
	        }

	        $sqlselect .= $bannedlist;

	        $_SESSION['advsearch']['sql'] = $sqlselect;

	        $sqlselect .= ' '.sort_by_validate($sort_by, $sort_order);

	} elseif ($_REQUEST['sort_by'] != '' or $_REQUEST['page'] != '') {

		if ($_REQUEST['page'] != '' && $_REQUEST['sort_by'] == '') { $_REQUEST['sort_by'] = $_SESSION['sort_by']; }

		$_SESSION['sort_by'] = $_REQUEST['sort_by'];

		if ($_REQUEST['sort_by'] == '') {

			$sort_by='username';

		} else {

			$sort_by=$_REQUEST['sort_by'];
		}

		if ($_REQUEST['sort_order'] == '') {

			if ($_SESSION['sort_order'] == '') {
				$sort_order='asc';
			} else {
				$sort_order=$_SESSION['sort_order'];
			}

		} else {

			$sort_order=$_REQUEST['sort_order'];

		}

		$_SESSION['sort_order'] = $sort_order;

		$sqlselect = $_SESSION['advsearch']['sql']." ".sort_by_validate($sort_by, $sort_order);
	}

    /* If  not blog search, do it this way.  We already have the results for the blog
      search */
    if ( $_REQUEST['cursectionid'] != 99 ) {

	    $t->assign('sort_by',$sort_by);

	    $t->assign('sort_order',$sort_order);

            /* Actually perform the search query to see what info we will get */
	    $rs =& $osDB->getAll($sqlselect,array(MEMBERSHIP_TABLE, USER_TABLE, ONLINE_USERS_TABLE ));

		unset($sqlselect);

	    $cpage = $_REQUEST['page'];

	    $lang['sort_types'] = get_lang_values('sort_types');

	    $lang['search_results_per_page'] = get_lang_values('search_results_per_page');

	    if( $cpage == '' ) $cpage = 1;

		$rcount = count($rs);

		if( $rcount > 0 ) {

			$t->assign( 'totalrecs', $rcount );

			$pages = ceil( $rcount / $psize );

			$start = ( $cpage - 1 ) * $psize;

			$t->assign ( 'start', $start );

			if( $pages > 1 ) {

				if ( $cpage > 1 ) {

					$prev = $cpage - 1;

					$t->assign( 'prev', $prev );

				}

				$t->assign ( 'cpage', $cpage );

				$t->assign ( 'pages', $pages );

				if ( $cpage < $pages ) {

					$next = $cpage + 1;

					$t->assign ( 'next', $next );

				}
				$rs = array_slice($rs, $start, $psize);

			}
			$data = array();

			foreach( $rs as $row) {

				$row['countryname'] = getCountryName($row['country'] );

				$row['statename'] = getStateName(  $row['country'], $row['state_province'] );

				if (count($_SESSION['advsearch']['questionusers']) > 0 ) {
					foreach ($_SESSION['advsearch']['questionusers'] as $usr) {
						if ($usr['userid'] == $row['id']) {
							$row['matchcnt'] = $usr['match_cnt'];
						}
					}
				}

						  /* Save the search results into data */
				$data[] = $row;
			}
		} else {
			$t->assign( 'error', 1);
		}

	}

    $lang['sort_types'] = get_lang_values('sort_types');

	$t->assign ( 'data', $data );

	unset ($data, $rs);

	$t->assign ( 'lang', $lang );

	$t->assign('rendered_page', $t->fetch('admin/advmatch.tpl') );

    $t->display ( 'admin/index.tpl' );

    exit;

}

if ($sectionid > 0 ) {
	$currdisplayorder = $osDB->getOne('select displayorder from ! where id=?', array(SECTIONS_TABLE, $sectionid) );

	$nextsectionid = $osDB->getOne('select id from ! where displayorder > ? and enabled = ? order by displayorder asc',array(SECTIONS_TABLE, $currdisplayorder, 'Y') );

	if (!isset($nextsectionid)) $nextsectionid = 0;

    /* reterive record from osdate_questions and osdate_questionoptions table   */

    $temp =& $osDB->getAll( 'select id, question, mandatory, description, guideline, maxlength, control_type, extsearchhead from ! where enabled = ? and section = ? and question <> ? and extsearchable = ?  order by mandatory desc, displayorder', array( QUESTIONS_TABLE, 'Y', $sectionid , '', 'Y') );

    $data = array();

    foreach( $temp as $index => $row ) {
        if (($config['use_extsearchhead'] == '1' or $config['use_extsearchhead'] == 'Y') && $row['extsearchhead'] != '') {
            $row['question'] = $row['extsearchhead'];
        }

		/* THis is made to adjust for multi-language */
		if ($_SESSION['opt_lang'] != 'english') {
	        if (($config['use_extsearchhead'] == '1' or $config['use_extsearchhead'] == 'Y') && $row['extsearchhead'] != '') {
	            $lang_question = $_SESSION['profile_questions'][$row['id']]['extsearchhead'];
	        } else {
				$lang_question = $_SESSION['profile_questions'][$row['id']]['question'];
			}
			$lang_descr = 	$_SESSION['profile_questions'][$row['id']]['description'];
			$lang_guide = 	$_SESSION['profile_questions'][$row['id']]['guideline'];
			if ($lang_question != '') {
				$row['question'] = $lang_question;
			}
			if ($lang_descr != '') {
				$row['description'] = $lang_descr;
			}
			if ($lang_guide != '') {
				$row['guideline'] = $lang_guide;
			}
		}

        $options =& $osDB->getAll( 'select * from ! where enabled = ? and questionid = ? order by displayorder', array( OPTIONS_TABLE, 'Y', $row['id'] ) ) ;

		/* THis is made to adjust for multi-language */
		$optsrs = array();
		if ($_SESSION['opt_lang'] != 'english') {
			foreach($options as $kx => $opt) {
				$lang_ansopt = $_SESSION['profile_questions'][$row['id']][$opt['id']];
				if ($lang_ansopt != '') {
					$opt['answer'] = $lang_ansopt;
				}
				$optsrs[] = $opt;
			}
		} else {$optsrs = $options; }

		unset($options);

        $row['options'] = makeOptions ( $optsrs);

        $endoptions = makeOptions ($optsrs);

		unset($optsrs);

        krsort($endoptions);

        reset($endoptions);

        $row['endoptions'] = $endoptions;

        $data [] = $row;
    }
	unset($row, $temp);
        /* Get a default date for the blog search */
	if ( $_SESSION['advsearch']['date_posted'] == '' ) {

		$_SESSION['advsearch']['date_posted'] = date('Y-m-d');
	}
} else {

	$nextsectionid = $osDB->getOne('select id from ! where enabled = ? order by displayorder asc',array(SECTIONS_TABLE,'Y') );
}


$t->assign('nextsectionid',$nextsectionid);

if ( isset( $_GET['errid'] ) ) {

    $t->assign( 'mandatory_question_error', get_lang('errormsgs',$_GET['errid']) );

}

if (($_SESSION['search_save_type'] == 'N' or $_SESSION['search_save_type'] == 'R') && (!isset($get_search) or $get_search == '')  && $_REQUEST['search_new'] != '1' ) {

    /* Save this search */
    if ($_SESSION['search_save_type'] == 'N') {
        $srch_name = $_SESSION['search_new_name'];
    } else {
        $srch_name = $_SESSION['search_name'];
    }

    $qry_txt = serialize($_SESSION['advsearch']);

    if ($srch_name != '') {

		save_search($qry_txt, $srch_name);

    }

	$_SESSION['search_name'] = $srch_name;

	unset($qry_txt, $srch_name);
	$_SESSION['search_new_name'] = '';

	$_SESSION['save_type'] = 'R';

}

$t->assign('user_searches', $osDB->getAll('select search_name from ! where userid = ? order by search_name', array(USER_SEARCH_TABLE, $_SESSION['UserId']) ) );

$t->assign( 'data', $data );

$t->assign( 'head', $sections[ $sectionid ] );

unset($data, $sections);

$t->assign('lang', $lang);

$t->assign( 'sectionid', $sectionid );

$t->assign('frmname', 'frm' . $sectionid );

$t->assign('rendered_page', $t->fetch('advsearch.tpl') );

$t->display('admin/index.tpl');

function save_search ($qry_txt, $srch_name){
	global $osDB;

	$rec_available = $osDB->getOne('select 2 from ! where userid=? and search_name=?', array(USER_SEARCH_TABLE, $_SESSION['UserId'], trim($srch_name)) );

	if (isset($rec_available) && $rec_available==2) {
		$osDB->query('update ! set query=? where userid=? and search_name=?', array(USER_SEARCH_TABLE, $qry_txt, $_SESSION['UserId'], trim($srch_name)) );
	} else {
		$osDB->query('insert into ! (userid, search_name, query) values (?, ?, ?)', array(USER_SEARCH_TABLE, $_SESSION['UserId'], trim($srch_name), $qry_txt) );
	}

	$_SESSION['search_name'] = $srch_name;

	$_SESSION['search_new_name'] = '';

	$_SESSION['save_type'] = 'R';
}
function sort_by_validate($sort_by, $sort_order=' asc '){

	$sortme = ' order by ';
	if ($sort_by == 'username') {

		$sortme .= 'user.username ';

	} elseif ( $sort_by == 'age' ) {

		$sortme .= ' age ';

	} elseif ( $sort_by == 'level' ) {

		$sortme .= ' user.level ';

	} elseif ( $sort_by == 'logintime' ) {

		$sortme .= 'user.lastvisit ';

		$sort_order = ' desc ';

	} elseif ( $sort_by == 'online' ) {

		$sortme .= ' onl.is_online '.$sort_order.', user.username ';
	}

	return $sortme.' '.$sort_order;

}

?>
