<?php
if(!isset($jobs_css_file_loaded)) {
	?>
	<link rel="stylesheet" href="<?php echo $path?>modules/jobs_ext.css">
	<?php
}

function namesToCapital($str) {
	$ret = array();
	$str = explode(" ", $str);
	if(is_array($str) && count($str)>0) {
		foreach ($str as $i=>$s) {
			if(trim($s) != "") {
				$ret [] = ucfirst(trim(strtolower($s)));
			}
		}
	}
	
	if(count($ret) > 0) return implode(" ", $ret);
	return $str;
}

$have_filters = false;

if(isset($_REQUEST['jobs']) && isset($_REQUEST['jobs']['search']) && $_REQUEST['jobs']['search'] == "yes") {
	$have_filters = true;
	$jobs = $_REQUEST['jobs'];
	$job_cat_id = (isset($jobs['job_cat_id']) && intval($jobs['job_cat_id'])>0) ? $jobs['job_cat_id'] : 0;
	$Location = (isset($jobs['Location']) && trim($jobs['Location']) != "") ? trim($jobs['Location']) : "";
	$Conditions = (isset($jobs['Conditions']) && trim($jobs['Conditions']) != "") ? trim($jobs['Conditions']) : "";
	$posted = ($jobs['posted'] != "all") ? intval($jobs['posted']) : "all";
	$limit = intval($jobs['limit']);
	$from = $_REQUEST['from'];
	if(isset($_POST['search_resumes'])) {
		$from = 0;
	}
}
elseif (isset($_REQUEST['cv_search']) && trim($_REQUEST['cv_search']) != "") {
	$cv_filter = explode(",", trim($_REQUEST['cv_search']));
	$cv = array();
	$jobs["job_cat_id"]	= $cv_filter[0];
	$jobs["Location"]	= $cv_filter[1];
	$jobs["Conditions"]	= $cv_filter[2];
	$jobs["posted"]		= $cv_filter[3];
	$jobs["limit"]		= $cv_filter[4];
	$jobs["from"]			= $cv_filter[5];
	if(is_array($jobs)) {
		$have_filters = true;
		$job_cat_id = (isset($jobs['job_cat_id']) && intval($jobs['job_cat_id'])>0) ? $jobs['job_cat_id'] : 0;
		$Location = (isset($jobs['Location']) && trim($jobs['Location']) != "") ? trim($jobs['Location']) : "";
		$Conditions = (isset($jobs['Conditions']) && trim($jobs['Conditions']) != "") ? trim($jobs['Conditions']) : "";
		$posted = ($jobs['posted'] != "all") ? intval($jobs['posted']) : "all";
		$limit = intval($jobs['limit']);
		$from = $jobs['from'];
	}
}

if($have_filters == true) {
	/*
	$cv_search_filter = array(
		"job_cat_id"	=> $job_cat_id,
		"Location"	=> $Location,
		"Conditions"	=> $Conditions,
		"posted"		=> $posted,
		"limit"			=> $limit,
		"from"			=> $from
	);
	*/
	
	$cv_search_filter = array(
		"c1"	=> $job_cat_id,
		"c2"	=> $Location,
		"c3"	=> $Conditions,
		"c4"		=> $posted,
		"c5"			=> $limit,
		"c6"			=> $from
	);
}
else {
	$jobs = array();
	$jobs["job_cat_id"]	= 0;
	$jobs["Location"]	= "";
	$jobs["Conditions"]	= "";
	$jobs["posted"]		= "all";
	$jobs["limit"]		= 20;
	$jobs["from"]			= 0;
	
	if(is_array($jobs)) {
		$have_filters = true;
		$job_cat_id = (isset($jobs['job_cat_id']) && intval($jobs['job_cat_id'])>0) ? $jobs['job_cat_id'] : 0;
		$Location = (isset($jobs['Location']) && trim($jobs['Location']) != "") ? trim($jobs['Location']) : "";
		$Conditions = (isset($jobs['Conditions']) && trim($jobs['Conditions']) != "") ? trim($jobs['Conditions']) : "";
		$posted = ($jobs['posted'] != "all") ? intval($jobs['posted']) : "all";
		$limit = intval($jobs['limit']);
		$from = $jobs['from'];
	}
	
	$cv_search_filter = array(
		"c1"	=> 0,
		"c2"	=> "",
		"c3"	=> "",
		"c4"		=> "all",
		"c5"			=> 20,
		"c6"			=> 0
	);
}
?>
<table width="98%" border="0" cellspacing="0" cellpadding="0" align="center">
	<tr>
		<td>
			<table width="100%" border="0" cellspacing="0" cellpadding="0">
				<tr>
					<td>
						<table border="0" cellspacing="0" cellpadding="0">
							<tr>
								<td><img src="<?=$path;?>images/navigation/featured_box/featured_left.gif" width="14" height="29"></td>
								<td class="featuredHeader">Search Jobs </td>
								<td><img src="<?=$path;?>images/navigation/featured_box/featured_cor.gif" width="29" height="29"></td>
							</tr>
						</table>
					</td>
					<td align="right">&nbsp;</td>
				</tr>
			</table>
		</td>
	</tr>
	<tr>
	    <td>
	    	<table align=center border=0 cellpadding=0 cellspacing=0 width="100%">
				<tr>
					<td height=10 width=10><img height=10 src="<?=$path;?>images/navigation/featured_box/box_1.gif" width=10></td>
					<td background="<?=$path;?>images/navigation/featured_box/box_top.gif" class="darkBlueBold"><img src="<?=$path;?>images/navigation/spacer.gif" width="1" height="1"></td>
					<td width=10><img height=10 src="<?=$path;?>images/navigation/featured_box/box_2.gif" width=12></td>
				</tr>
				<tr>
					<td background="<?=$path;?>images/navigation/featured_box/box_left.gif"></td>
					<td>
					<?php
					if(!isset($_SESSION['user_id']) && $ultra_domainname == "www.cyprusjobs.com") {
						?>
						<div class="gads" style="text-align: center;">
							<script type="text/javascript"><!--
									google_ad_client = "ca-pub-3156050866912357";
									/* BLUE_Banner_468x60 */
									google_ad_slot = "7335816081";
									google_ad_width = 468;
									google_ad_height = 60;
									//-->
							</script>
							<script type="text/javascript"
									src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
							</script>
						</div>
						<?php
					}
					?>
					
					<form method="post" action="my_jobs/jobs_job_list.html">
						<div class="inline_container">
						<table cellpadding="0" cellspacing="0" border="0">
							<tr>
								<td colspan="4" class="itdtitle" align="left">
									Search Jobs Postings By:
								</td>
							</tr>
							<tr>
								<td align="right" class="itd">Job Category</td>
								<td align="left" class="itd">
									<select name="jobs[job_cat_id]" class="setwidth">
										<option value=""><?php echo 'Select Category';?></option>
										<?php
										$qexl = "select * from job_cat where active='yes' order by order_seq asc;";
										$rexl = @mysql_query($qexl);
										if($rexl && mysql_num_rows($rexl) > 0) {
											while ($rowexl = mysql_fetch_assoc($rexl)){
												$selected = '';
												if($jobs['job_cat_id'] == $rowexl['job_cat_id']) $selected = ' selected="selected"';
												echo '<option value="',trim($rowexl['job_cat_id']),'" ',$selected,'>', stripslashes($rowexl['title_en']), '</option>';
											}
										}
										?>
									</select>
								</td>
								<td align="right" class="itd_lb">Posted</td>
								<td align="left" class="itd">
									<select name="jobs[posted]" class="setwidth">
										<option value="all" selected="selected">Any Time</option>
										<?php
										$posted_vals = array(
											'0' => 'Today',
											'1' => 'Yesterday',
											'7'	=> 'Last 7 Days',
											'30'=> 'Last 30 Days'
											
										);
										foreach ($posted_vals as $pv=>$pv_title) {
											$selected = '';
											if($jobs['posted'] != "all" && $jobs['posted'] == $pv) $selected = 'selected="selected"';
											echo '<option value="',strval($pv),'" ',$selected,'>', $pv_title, '</option>';
										}
										?>
									</select>
								</td>
							</tr>
							<tr>
								<td align="left" class="itd">Location</td>
								<td align="left" class="itd">
									<select name="jobs[Location]" class="setwidth">
										<option value=""><?php echo 'Any'?></option>
										<?php
										$locations = array("Nicosia", "Limassol", "Larnaca", "Paphos", "Famagusta");
										foreach ($locations as $loc) {
											$selected = '';
											if($jobs['Location'] == $loc) $selected = 'selected="selected"';
											echo '<option value="',$loc,'" ',$selected,'>', $loc, '</option>';
										}
										?>
									</select>
								</td>
								<td align="left" class="itd_lb">Job Type</td>
								<td align="left" class="itd">
									<?php
									$all_commitment = array(
										"full-time" 	=> "Full Time",
										"part-time"		=> "Part Time",
										"contract"		=> "Contract",
										"temp"			=> "Temporary",
										"internship"	=> "Internship",
										"other"			=> "Other"
									);
									?>
									<select name="jobs[Conditions]" class="setwidth">
										<option value=""><?php echo 'Any';?></option>
										<?php
										foreach ($all_commitment as $ctype=>$ctype_title) {
											$selected = '';
											if($jobs['Conditions'] == $ctype) $selected = 'selected="selected"';
											echo '<option value="',$ctype,'" ',$selected,'>', $ctype_title, '</option>';
										}
										?>
									</select>
								</td>
							</tr>
							<tr>
								<td colspan="2" align="left" class="footer">
									<?php
									echo 'Show', ' ';
									echo '<select name="jobs[limit]" style="vertical-align:middle;">';
									$limit_ar = array('10', '15', '20', '25', '30', '50');
									foreach ($limit_ar as $lt) {
										$selected = '';
										if(strval($jobs['limit']) == $lt) $selected = 'selected="selected"';
										echo '<option value="',$lt,'" ',$selected,'>',$lt,'</option>';
									}
									echo '</select> ', 'results per page';
									?>
								</td>
								<td colspan="2" align="right" class="footer">
									<input type="submit" name="search_jobs" value="Search" class="submit_button">
								</td>
							</tr>
						</table>
						<input type="hidden" name="from" id="from" value="0">
						<input type="hidden" name="module_page" value="my_jobs">
						<input type="hidden" name="jobs_step" value="jobs_job_list"/>
						<input type="hidden" name="jobs[search]" value="yes"/>
						</div>
					</form>
					<?php
					if($have_filters) {
						//$cv = $_REQUEST['cv'];
						//$job_cat_id = (isset($jobs['job_cat_id']) && intval($jobs['job_cat_id'])>0) ? $jobs['job_cat_id'] : 0;
						//$Location = (isset($jobs['Location']) && trim($jobs['Location']) != "") ? trim($jobs['Location']) : "";
						//$posted = ($jobs['posted'] != "all") ? $jobs['posted'] : "all";
						//$limit = intval($jobs['limit']);
						
						$q = "SELECT * FROM job_jobs WHERE active='yes' ";
						
						if($job_cat_id >0) {
							$q .= " AND job_cat_id='$job_cat_id' ";
						}
						
						if($Location != "") {
							$q .= " AND Location like '%$Location%' ";
						}
						
						if($Conditions != "") {
							$q .= " AND Conditions='$Conditions' ";
						}
						
						switch (strval($posted)) {
							case "all":
								$last_six_months = date("Y-m-d", strtotime("-12 months", time()));
								$q .= " AND added_on>'$last_six_months' ";
								break;
							case "0":
								$today = date("Y-m-d");
								$q .= " AND added_on='$today' ";
								break;
							case "1":
								$q .= " AND DATE(added_on)=DATE_SUB(CURDATE(),INTERVAL 1 DAY) ";
								break;
							case "7":
								$q .= " AND DATE_SUB(CURDATE(),INTERVAL 7 DAY) <= DATE(added_on) ";
								break;
							case "30":
								$q .= " AND DATE_SUB(CURDATE(),INTERVAL 30 DAY) <= DATE(added_on) ";
								break;	
						}
						
						$q .= " ORDER BY added_on DESC, added_on_time DESC ";
						
						//echo $q;
						$total_records = 0;
						$r = @mysql_query($q);
						if($r && mysql_num_rows($r)>0){
							$total_records = mysql_num_rows($r);
						}
						
						$q .= "	LIMIT $from,$limit;";
						
						echo "<br/>".$q;
						
						if($total_records > 0) {
							$r = mysql_query($q) or die("<strong>$q</strong>".mysql_error());
							if($r && mysql_num_rows($r)>0) {
								?>
								<div class="list_pager">
									<table cellpadding="0" cellspacing="0" border="0" width="100%" style="*width:97%; margin-right: -1px;">
										<tr>
											<td class="pager_totals"><strong><?php echo $total_records;?></strong> record(s) found | Showing <?php echo $limit; ?> per page</td>
										</tr>	
										<?php
										if($limit != "all") {
											?>
											<tr>
												<td align="left" nowrap  class="pager_buttons"><strong>Go to page &raquo;</strong>
													<?php
													
													
													$rcount = $total_records;
													$per_page = $limit;
													$pages = floor($rcount / $per_page);
													if( ($rcount / $per_page) - $pages > 0 ) $pages += 1;
													$class = "";
													if($from == 0) $class = "current";
													
													$filter = $cv_search_filter;
													$filter['c6'] = 0;
													$first_filter = implode(",", $filter);
													
													echo '<a href="my_jobs/jobs_job_list.html?cv_search=',$first_filter,'" rel="0" class="page '.$class.'">First</a>';
													if(10*$per_page < $from) echo '...';
													for($i=0; $i<$pages; $i++) {
														$new_from = $i * $per_page;
														
														$filter = $cv_search_filter;
														$filter['c6'] = $new_from;
														$first_filter = implode(",", $filter);
														
														$class = "";
														if($new_from == $from) $class = "current";
														if($from - 10 * $per_page <= $new_from && $from + 10 * $per_page >= $new_from) {
															$link_text = ($i+1 < 10) ? "&nbsp;".($i+1) : ($i+1);
															echo '<a href="my_jobs/jobs_job_list.html?cv_search=',$first_filter,'" rel="'.$new_from.'" class="page '.$class.'">'.$link_text.'</a>';
														}
														
													}
													if((($pages-1)* $per_page) > $from + 10 * $per_page) echo '...';
													$filter = $cv_search_filter;
													$filter['c6'] = (($pages-1)* $per_page);
													$first_filter = implode(",", $filter);
													echo '<a href="my_jobs/jobs_job_list.html?cv_search=',$first_filter,'" rel="'.(($pages-1)* $per_page).'" class="page '.$class.'">Last</a>';
													?>
												</td>
											</tr>
											<?php
										}
										?>
										
									</table>
								</div>
								<div class="inline_container">
									<table cellpadding="0" cellspacing="0" border="0" width="100%">
										<tr>
											<td align="center" class="itdtitle">#</td>
											<td align="left" class="itdtitle">Job Title</td>
											<td align="left" class="itdtitle">Location(s)</td>
											<td align="left" nowrap class="itdtitle">Company</td>
										</tr>
										<?php
										$cnt = 1 + $from;
										while ($row = mysql_fetch_assoc($r)) {
											//$name = (($row['Genre'] == "m") ? "Mr." : "Ms.") . " " . stripslashes(trim($row['FirstName'])) . " " . stripslashes(trim($row['LastName']));
											$job_title = namesToCapital(trim(stripslashes($row['JobTitle'])));
											
											if(mb_strlen($job_title) > 60) {
												$job_title = wordwrap($job_title, 60, "<br />\n", true);
											}
											
											$locations = str_replace(";", ", ", trim($row['Location']));
											//$coo = trim(stripslashes($row['CountryOfOrigin']));
											$company_name = trim(stripslashes($row['CompanyName']));
											
											if(mb_strlen($company_name) > 50) {
												$company_name = wordwrap($company_name, 50, "<br />\n", true);
											}
											
											$job_jobs_id = intval($row['job_jobs_id']);
											$added_on = date("m/d/Y", strtotime($row['added_on']));
											
											?>
											<tr class="itr">
												<td class="itdh" align="center"><?php echo $cnt;?></td>
												<td class="itd_lb" width="33%" nowrap align="left">
													<?php 
													//echo '<a href="index.php?module_page=my_jobs&jobs_step=jobs_details&job_jobs_id=', $job_jobs_id, '" target="_blank">' , ($job_title), '</a>';
													echo '<a href="jobdetails/jobs_details.html?job_jobs_id=', $job_jobs_id, '" target="_blank">' , ($job_title), '</a>';
													echo '<br/><span style="font-size: 10px; color: #8F8F8F;">Last Updated: ', $added_on, '</span>';
													?>
												</td>
												<td class="itd_lb" width="30%" align="left"><?php echo $locations;?></td>
												<td class="itd_lb" width="33%" align="left"><strong><?php echo $company_name;?></strong></td>
											</tr>
											<?php
											$cnt++;
										}
										?>
									</table>	
								</div>
								<div class="list_pager">
									<table cellpadding="0" cellspacing="0" border="0" width="100%" style="*width:97%; margin-right: -1px;">
										<tr>
											<td class="pager_totals"><strong><?php echo $total_records;?></strong> record(s) found | Showing <?php echo $limit; ?> per page</td>
										</tr>	
										<?php
										if($limit != "all") {
											?>
											<tr>
												<td align="left" nowrap  class="pager_buttons"><strong>Go to page &raquo;</strong>
													<?php
													
													
													$rcount = $total_records;
													$per_page = $limit;
													$pages = floor($rcount / $per_page);
													if( ($rcount / $per_page) - $pages > 0 ) $pages += 1;
													$class = "";
													if($from == 0) $class = "current";
													
													$filter = $cv_search_filter;
													$filter['c6'] = 0;
													$first_filter = implode(",", $filter);
													
													echo '<a href="my_jobs/jobs_job_list.html?cv_search=',$first_filter,'" rel="0" class="page '.$class.'">First</a>';
													if(10*$per_page < $from) echo '...';
													for($i=0; $i<$pages; $i++) {
														$new_from = $i * $per_page;
														
														$filter = $cv_search_filter;
														$filter['c6'] = $new_from;
														$first_filter = implode(",", $filter);
														
														$class = "";
														if($new_from == $from) $class = "current";
														if($from - 10 * $per_page <= $new_from && $from + 10 * $per_page >= $new_from) {
															$link_text = ($i+1 < 10) ? "&nbsp;".($i+1) : ($i+1);
															echo '<a href="my_jobs/jobs_job_list.html?cv_search=',$first_filter,'" rel="'.$new_from.'" class="page '.$class.'">'.$link_text.'</a>';
														}
														
													}
													if((($pages-1)* $per_page) > $from + 10 * $per_page) echo '...';
													$filter = $cv_search_filter;
													$filter['c6'] = (($pages-1)* $per_page);
													$first_filter = implode(",", $filter);
													echo '<a href="my_jobs/jobs_job_list.html?cv_search=',$first_filter,'" rel="'.(($pages-1)* $per_page).'" class="page '.$class.'">Last</a>';
													?>
												</td>
											</tr>
											<?php
										}
										?>
										
									</table>
								</div>
								<?php
							}
						}
						else {
							echo '<div class="note"><span class="red"><strong>Sorry:</strong></span><br/>There are no records matching your criteria.</div>';
						}
					}
					?>
					</td>
					<td background="<?=$path;?>images/navigation/featured_box/box_right.gif"></td>
				</tr>
				<tr>
					<td height=12 width=10><img height=12 src="<?=$path;?>images/navigation/featured_box/box_3.gif" width=10></td>
					<td background="<?=$path;?>images/navigation/featured_box/box_bot.gif"><img src="<?=$path;?>images/navigation/spacer.gif" width="1" height="1"></td>
					<td><img height=12 src="<?=$path;?>images/navigation/featured_box/box_4.gif" width=12></td>
				</tr>
			</table>
		</td>
	</tr>
</table>
<?php
?>