<?php

	include "library.php";

	$cxn = start_code();

    exit_if_search_bot();

    exit_if_session_expired( $cxn );


//      Monitor execution time and memory usage
//	include "/home/academicdating/public_html/ubench/Ubench.php";
//	$bench = new Ubench;
//  $bench->start();


	$today_date = date_create( date( "Y-m-d H:i:s" ) );


	$today_minus_2_hours = clone $today_date;

	date_sub( $today_minus_2_hours, date_interval_create_from_date_string( '2 hours' ));
	
    $today_minus_2_hours_str = $today_minus_2_hours->format( "Y-m-d H:i:s" );
	
	$date_today = date( "Y-m-d H:i:s" );

	
//	$today_minus_30_days = clone $today_date;
//
//	date_sub( $today_minus_30_days, date_interval_create_from_date_string( '30 days' ));
//	
//    $today_minus_30_days_str = $today_minus_30_days->format( "Y-m-d" );		

	
	$today_minus_365_days = clone $today_date;

	date_sub( $today_minus_365_days, date_interval_create_from_date_string( '365 days' ));
	
    $today_minus_365_days_str = $today_minus_365_days->format( "Y-m-d 00:00:00" );		


		/* determine the search criteria */


    $type = str_replace( "'", "''", val( $_SESSION, "stype" ) );
	$city = str_replace( "'", "''", val( $_SESSION, "scity" ) );
	$postcode = str_replace( "'", "''", val( $_SESSION, "spostcode" ) );
	$search_for_word = str_replace( "'", "''", val( $_SESSION, "ssearch_for_word" ) );
	

	$start_pos = val( $_GET, "startpos" );

    crash_if_missing( $start_pos, "start pos missing in view_jobs", $cxn );

//    log_hits( "search", $cxn );


	$num_per_page = 25;

    $upper_limit = $start_pos + $num_per_page + 1;
    
	$query = "SELECT * ";

	$query .= " FROM job ";

	$query .= "WHERE status = 'active' ";

	if ($type != "")
		$query = $query." AND type = '".$type."'";

	if ($postcode != "")
		$query = $query." AND postcode = '".$postcode."'";

	if ($city != "")
		$query = $query." AND city = '".$city."'";

	if ($search_for_word != "")
		$query = $query." AND (short_description like '%".$search_for_word."' OR full_description like '%".$search_for_word."%' )";


	$query .= " ORDER BY id DESC LIMIT ".$start_pos.", ".$upper_limit;



		/* run the query */

	$result = run_query( $query, $cxn );

	$found_one = "false";

	$color_scheme = val( $_SESSION, 'color_scheme' );

	$num_rows = 0;

	for ($i=0; $i < query_num_rows($result); $i++)
	{
		$this_row = get_row( $result );

		$data[$num_rows] = $this_row;

		$num_rows++;
	}


// on the last page, re-run the search with wider criteria

    if ($start_pos >= $num_rows && val( $_SESSION, 'search_attempt' ) < 5)
        widen_search( $city, $postcode, $search_for_word, $cxn );


	
		/* page output starts here */

	page_header( "View Jobs", $cxn, true );

    echo "<br><br>";
    

	if (val( $_SESSION, 'search_attempt' ) >= "2")
		echo "<p align='center'>".red( "Your search returned a small number of results. This is a wider search." )."</p>";


		/* profile numbers in the list start at 0 */
		/* move down the list to the starting position */

	if ($start_pos + $num_per_page <= $num_rows)
		$num_this_page = $num_per_page;
	else
		$num_this_page = $num_rows - $start_pos;


	$end_pos = $start_pos + $num_this_page - 1;
	$start = $start_pos + 1;
	$end = $end_pos + 1;

//    echo watermark( "Profiles ".$start." to ".$end."<br><br>" );

    if (! low_res_screen())
    {
        if ($color_scheme == 2)
            echo "<table width='100%'><tr><td width='5%'>&nbsp;</td><td style='background: #000000;'>";
        else
            echo "<table width='100%'><tr><td width='5%'>&nbsp;</td><td style='background: #ffffff;'>";
    
        echo "<br>";
    }
    
    
//    if ($num_this_page > 0)
//		echo "<hr>";


//      Include this line for compatability search where all records need to be read		
//	for ($i=$start_pos; $i < $start_pos + $num_this_page; $i++)
        
	for ($i=0; $i < $num_this_page; $i++)
	{
		$found_one = "true";

		echo "<a name='id".$data[$i]['id']."'>";		/* bookmark to go back to search screen */

        $amp = false;

        echo "<tr><td></td>";

        echo "<td>".name_case( $data[$i]['type'] )."</td>";

        echo "<td>".show_link( $data[$i]['short_description'], "display_job.php", "showid", $data[$i]['id'], "returnpoint", "search_jobs", "startpos", $start_pos )."</td>";

        echo "<td>City: ".$data[$i]['city']."</td>";

        echo "<td>Postcode: ".$data[$i]['postcode']."</td>";

        echo "</tr>";
        
        echo "<tr><td></td><td colspan='4'><hr></td></tr>";

		if (($i % 8 == 7) && ($i != $num_this_page-1))
//		if ($start_pos == 0 && ($i % 8 == 7) && ($i != $num_this_page-1))
		{
            echo "<br><br><br><br>";

            if (! low_res_screen())
            {
                echo "<table width='100%'><tr><td width='10%'>&nbsp;</td><td width='80%' align='left'>";
            
                echo show_ads_large_banner_arj();

                echo "</td></tr></table>";
            }
            else
            {
                echo "<table width='100%'><tr><td width='100%' align='center'>";

                echo show_ads_narrow_banner();
        
                echo "</td></tr></table>";
            }
            
            echo "<br><br><br><br><hr>";
		}

	}


    if (! low_res_screen())
        echo "</td><td width='5%'>&nbsp;</td><tr></table>";

//	if ($found_one == "false")
//	{
//		if ($logged_on == "on")

//		echo "<br><br><br><br><p align='center'>".black( "There are no jobs that match your search criteria" )."</p><br>";

//		else
//			echo "<br><br><br><br><p align='center'>".black( "There are no profiles matching your search criteria. We suggest that you try a wider search including removing the city and/or country restriction." )."</p><br>";			/
//	}
//	else

	{
			/* show the next-page and previous-page links */

		$prev_page = $start_pos - $num_per_page;
		$next_page = $start_pos + $num_per_page;
		$end_pos = $start_pos + $num_this_page - 1;

		echo "<p align='center'>";

		$start = $start_pos + 1;
		$end = $end_pos + 1;

		echo "<br><br><br><br>";

		echo black( "Jobs ".$start." to ".$end."<br><br><br>" );

		if ($start_pos > 1)
			echo show_link( "Previous Page", "view_jobs.php", "startpos", $prev_page );

//		if ($start_pos + $num_per_page < $num_rows)
		{
			if ($start_pos > 1)
				echo "&nbsp;&nbsp;&nbsp;&nbsp;";

			echo show_link( "Next Page", "view_jobs.php", "startpos", $next_page );
		}

		echo "</p>";
	}

	echo "<br>";

	
//	if ($num_rows > 0 && $num_rows < 20)
//	{
//		echo "<p align='center'>".black( "Your search returned a small number of profiles. We suggest that you try a wider search including removing the city and/or country restriction." )."</p><br>";
//	}
	
	
//	echo "<p align='center'>";


//	echo red( "To join the site click on " )."<a class='redlink2' href='".url_encode( "edit_profile.php", "newprofile", "true", "quick_signup", "false"  )."'>Create a Profile</a></p>";
    	


		/* a few blank lines so text is not crammed at the end of the screen */

	echo "<br>";


	if (! low_res_screen())
	{
		echo "<br><br><br>";
		
        echo "<table width='100%'><tr><td width='10%'>&nbsp;</td><td width='80%' align='left'>";

        echo show_ads_large_banner_arj();

        echo "</td></tr></table>";
	
	}
	else
	{
		echo "<br><br><br>";

		show_ads_large_square();
	}


	echo "<br><br><br><br><br><br><br>";
	
	
	
	
	
	
	echo "</body>";
	
	echo "</html>";


  
		/* widen the search parameters and re-run the search */
		
	function widen_search( $city, $postcode, $search_for_word, $cxn )
	{
		$_SESSION['search_attempt'] = val( $_SESSION, "search_attempt" ) + 1;

		if ($postcode != "")
		{
		    $_SESSION['spostcode'] = "";
		}
		else
		if ($search_for_word != "")
		{
			$_SESSION['ssearch_for_word'] = "";
        }
        else
		if ($city != "")
		{
		    $_SESSION['scity'] = "";
		}

			/* must happen before print output */
			
		jump_to_page( "view_jobs.php", "startpos", "0" );

    	exit();
	}


		/* sort an array of sort keys */

	function sort_array( $key_values, $num_rows )
	{
		arsort( $key_values );

		$i = 0;

		foreach ($key_values as $key => $val)
		{
			$key_data2[$i] = $key;
			$i++;
		}

		return ($key_data2);
	}
	

?>