<?php

	include "library.php";

	$cxn = start_code();


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

			/* get variables */

	$show_id = val( $_GET, "showid" );

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

	$external = val( $_GET, "external" );

//	$sample = val( $_GET, "sample" );

//	$conversation_id = val( $_GET, "conversationid" );


	if ($external == "true")
	{
		$id = "";

		session_destroy();		/* kill any session hanging around from the URL SID etc */

    	session_start();

		$_SESSION['session_active'] = "yes";
	}
	else
		$listing_id = $show_id;


	$_SESSION['returnpoint'] = $returnpoint;
	$_SESSION['startpos'] = $startpos;


		/* start the page */

	page_header( "Listing", $cxn, true );


		/* check parameters */

	crash_if_missing( $show_id, "show_id missing in display_profile", $cxn );

	crash_if_missing( $returnpoint, "return point missing in display_profile", $cxn );


	$query = "SELECT * FROM listing WHERE id = ".$show_id;

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

	if (query_num_rows($result) == 0)
	{
		crash( "Internal error: id not found", $cxn );
	}
	else
	{
		$data = get_row( $result );

		if ($data['deleted'] == 'yes')
		{
			echo black( "This listing has been deleted from the system." );
			exit();
		}


        $currency_code = $data['currency_code'];
        $price = $data['price'];
        $show_price_on_site = $data['show_price_on_site'];
        $headline_title = $data['headline_title'];
        $turnover = $data['turnover'];
        $net_profit = $data['net_profit'];
        $short_description = $data['short_description'];
        $long_description = $data['long_description'];
        $industry = $data['industry'];
        $suburb = $data['suburb'];
        $city = $data['city'];
        $state = $data['state'];
        $country = $data['country'];
        $date_modified = $data['date_modified'];
        


					/* display the photos */


		$query = "SELECT * FROM photos WHERE listing_id = ".$show_id." ORDER BY primary_photo desc, id";


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

		echo "<table width='100%'><tr>";

		if (query_num_rows($result) == 0)
		{
			echo "<td width='75%'>";

			show_photo( $show_id, 120, -1, false, false, $cxn );

			echo "</td>";
		}

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

			echo "<td width='75%'>";

			show_photo( $show_id, 120, $data['photo_number'], true, false, $cxn );

			echo "</td>";

			if (($i+1) % 4 == 0)
				echo "</tr><tr>";
		}



		$query = "SELECT * FROM account INNER JOIN listing ON listing.account_id = account.id WHERE listing.id = ".$show_id;

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

		$data = get_row( $result );

		$sendtoid = $data['account_id'];


		if (! low_res_screen())
			$button_width = 195;
		else
			$button_width = 120;


        echo "</td><td width='25%' align='center'>";

		echo "<br>";

        if (! low_res_screen())
    		echo "<br><br><br>";

        
		show_green_button( "Show Contact Details", $button_width, "show_contact_details.php", "listing_id", $show_id );

			
		echo "<br><br>";
		
		show_green_button( "Remember", $button_width, "add_to_remembered.php", "rememberedid", $show_id, "returnpoint", "main", "startpos", $startpos );

		echo "<br><br>";

        echo "</td>";
        
        
        

		echo "</tr></table>";


			/* show text details */


		echo "<br>";

//		echo "<table width='100%' cellspacing='4'>";		
//		echo "<table width='100%'>";

		echo "<table width='100%' style='width: 100%;'><tr><td width=10%></td><td width=58%></td></tr>";
		
		echo "<br>";
		
		echo "<tr><td></td><td>";
		
//		if ($sample == "true")
//			echo "<tr><td>".red( "Example Profile" )."</td></tr>";

		echo "<tr><td width='20%'><span class='dprompt'>&nbsp;</span></td><td width='50%'><span class='dtext'>&nbsp;</span></td><td width='30%' rowspan='8' align='center' valign='top'>";

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


		echo "<table width='100%'>";
		
		
		echo "<tr><td width='10%' valign='top'><span class='dprompt'>Listing ID</span></td><td width='75%' align='left'><span class='gwatermark''>".convert_to_html( $listing_id )."</span></td></tr>";
		
//		show_field( "Currency Code", $currency_code );

		if ($show_price_on_site == "on")
			show_field( "Price", "$".number_format( floatval( $price )));
		else
			show_field( "Price", "Contact Vendor" );
			
		show_field( "Headline", $headline_title );

		show_field( "Listed/updated", date( 'j F Y', strtotime( $date_modified ) ) );
		
		show_field( "Turnover", $turnover );
		
		show_field( "Net Profit", $net_profit );
				
		show_field( "Short Description", $short_description );

		show_field( "Long Description", $long_description );

		show_field( "Industry", $industry );

		show_field( "Suburb", $suburb );

		show_field( "City", name_case( $city ));

		show_field( "State", $state );
		
//		show_field( "Country", $country );


		echo "</table><br>";

		echo "<br><br><br>";
		
        if (! low_res_screen())
		    show_ads( $cxn );
		else
            show_ads_large_square();

	}
?>