<?php

	include "library.php";

	$cxn = start_code();
	

			/* 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
		$id = val( $_SESSION, "id" );


		/* display the sample profile */

	if ($sample == "true")
	{
		$query = "SELECT id FROM profile WHERE handle = 'chinagirl'";

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

		if (mysql_num_rows($result) == 0)
			crash( "Sample profile missing", $cxn );

		$data = get_row( $result );

		$show_id = $data['id'];
	}


		/* if the user is not logged in direct them to the login page then back here */

// use this line instead to allow visitrs to view profiles		
//	if ($external == "true" && $id == "" && $sample != "true")

	if ($id == "" && $sample != "true")
	{
		$_SESSION['nextpage'] = url_encode( "display_profile.php", "showid", $show_id, "returnpoint", $returnpoint, "startpos", $startpos );

		jump_to_page( "login.php" );		/* must happen before print output */

		exit();
	}

		/* view own profile  */

	if ($show_id == -1)
		$show_id = $id;

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


		/* start the page */

	page_header( "Profile", $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 *, year(date_of_birth) as dob_year, dayofyear(date_of_birth) as dob_dayofyear, datediff(current_date(), last_login) as days_since_login FROM profile WHERE id = ".$show_id;

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

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

		if ($data['visibility'] == 'hidden' &&
			$show_id != $id &&
			$sample != "true" &&
			! is_system_user( $id, $cxn ) &&
			val( $_SESSION, "debug" ) != true)
		{
			echo black( "This member's profile is currently hidden." );
			exit();
		}

		if ($data['visibility'] == 'only_with_contact' && $show_id != $id)
		{
			if ($id == "")
			{
				echo black( "Please log in to view this member's profile." );
				exit();
			}

			$query = "SELECT id FROM message WHERE (sender_id = ".$id." AND receiver_id = ".$show_id." ) OR ( sender_id = ".$show_id." AND receiver_id = ".$id." )";

			if (! query_has_rows( $query, $cxn ) &&
				! is_system_user( $id, $cxn ) &&
				val( $_SESSION, "debug" ) != true)

			{
				echo black( "This member's profile is currently hidden." );
				exit();
			}
		}

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

		if ($data['locked'] == 'yes')
		{
			echo black( "This member's account is currently locked." );
			exit();
		}

		$show_handle = $data['handle'];
		$short_intro = $data['short_intro'];
		$interests = $data['interests'];
		$general_comments = $data['general_comments'];
		$looking_for = $data['looking_for'];
		$first_name = $data['first_name'];
		$city = $data['city'];
		$state = $data['state'];
		$country = $data['country'];
		$date_of_birth = format_date_of_birth( $data['date_of_birth'], $data['date_format'] );
		$sex = $data['sex'];
		$sex_wanted = $data['sex_wanted'];
		$has_children = $data['has_children'];
		$wants_children = $data['wants_children'];
		$wants_relationship = $data['wants_relationship'];
		$wants_friendships = $data['wants_friendships'];
		$reveal_first_name = $data['reveal_first_name'];
		$days_since_login = $data['days_since_login'];
		$religion = $data['religion'];
		$ethnic_race = $data['ethnic_race'];
		$work_type = $data['work_type'];
		$ip_country = $data['ip_country'];


		if ((date("z")+1) > $data['dob_dayofyear'])
			$age = date( "Y" ) - $data['dob_year'];
		else
			$age = date( "Y" ) - $data['dob_year'];

			/* determine whether to show the 'reply to kiss' button */
   			/* note that $id does NOT have to have a value as public can browse without being logged in */

		$show_reply_to_kiss = "false";

		$id = val( $_SESSION, "id" );

		if ($id != "")
		{
			$query = "SELECT id FROM message WHERE sender_id = ".$show_id." AND receiver_id = ".$id." AND type = 'kiss' AND (has_been_replied_to is NULL or has_been_replied_to = 'no')";

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

			if (mysql_num_rows($result) > 0)
				$show_reply_to_kiss = "true";
		}

					/* display the photos */

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

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

		echo "<table><tr>";

		if (mysql_num_rows($result) == 0)
		{
			echo "<td>";

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

			echo "</td>";
		}

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

			echo "<td>";

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

			echo "</td>";

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

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


			/* show text details */

		if ($days_since_login != "" and $days_since_login > 90)
			echo red( "<br>Please Note: This member has not logged on for more than 3 months<br><br>" );

		echo "<br>";

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

		if ($sample == "true")
			echo "<tr><td>".red( "Example Profile" )."</td></tr>";

		echo "<tr><td width='20%'><span class='dprompt'>Handle Name</span></td><td width='60%'><span class='dtext'>".$show_handle."</span></td><td width='20%' rowspan='12' align='CENTER' valign='TOP'>";

			/* buttons */

		if ($sample == "true")
			echo "<img style='filter:alpha(opacity=35);-moz-opacity:.35;opacity:.35' src='/buttons/".button_filename( "send_kiss" )."'>";
		else
			show_button_h( "send_kiss", "send_message.php", "sendtype", "kiss", "sendtoid", $show_id );

		echo "<br><br>";

		if ($sample == "true")
			echo "<img style='filter:alpha(opacity=35);-moz-opacity:.35;opacity:.35' src='/buttons/".button_filename( "send_email" )."'>";
		else
			show_button_h( "send_email", "send_message.php", "sendtype", "email", "sendtoid", $show_id );

		echo "<br><br>";

		if ($sample == "true")
			echo "<img style='filter:alpha(opacity=35);-moz-opacity:.35;opacity:.35' src='/buttons/".button_filename( "remember" )."'>";
		else
			show_button_h( "remember", "add_to_remembered.php", "rememberedid", $show_id );

		echo "<br><br>";

		if ($sample != "true" && $id != "")
		{
			if (($conversation_id != "" || query_has_rows( "SELECT id FROM message WHERE (sender_id = ".$id." AND receiver_id = ".$show_id.") OR (sender_id = ".$show_id." AND receiver_id = ".$id.")", $cxn )) && $id != $show_id)
			{
				show_button_h( "block", "block.php", "blockid", $show_id );
				echo "<br><br>";
			}
		}

			/* chat request button */
			
		if ($sample == "true")
			echo "<img style='filter:alpha(opacity=35);-moz-opacity:.35;opacity:.35' src='/buttons/".button_filename( "request_chat" )."'>";
		else
		{
			if ($conversation_id == "" && query_has_rows( "SELECT id FROM online_status WHERE member_id = ".$show_id, $cxn ) && $id != "" && $id != $show_id)
			{
				echo "<a target='_blank' href='".url_encode( "chat_request_check.php", "id1", $id, "id2", $show_id  )."'><img src='/buttons/".button_filename( "request_chat" )."' border='0'></a>";

				echo "<br><br>";
			}
//			else
//			if (! query_has_rows( "SELECT id FROM online_status WHERE member_id = ".$show_id, $cxn ))
//				echo watermark( "not online" );
		}

			/* accept chat request button */

		if ($conversation_id != "" && $id != "")
		{
			show_button_h( "accept_chat", "chat.php", "id1", $id, "id2", $show_id, "conversationid", $conversation_id, "newchat", "false" );

			echo "<br><br>";
		}

		if ($show_reply_to_kiss == "true")
		{
			show_button_h( "reply_to_kiss", "reply_to_kiss.php", "replytoid", $show_id );

			echo "<br><br>";
		}

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

		if ($reveal_first_name == "on")
			show_field( "Name", $first_name );

		show_field( "Short Introduction", $short_intro );

//		show_field( "Interests", $interests );

		show_field( "Comments", $general_comments );

//		show_field( "Looking for", $looking_for );

		show_field( "Sex", $sex );

		show_field( "Age", $age );

		show_field( "Looking for", $sex_wanted );

		if ($wants_relationship == "on")
			show_field( "Interested in a relationship", "Yes" );
		else
			show_field( "Interested in a relationship", "No" );

		if ($wants_friendships == "on")
			show_field( "Interested in friendships", "Yes" );
		else
			show_field( "Interested in friendships", "No" );

		show_field( "Has children", $has_children );

		show_field( "Wants more children", $wants_children );

		show_field( "Religion", $religion );

		show_field( "Ethnicity", name_case( $ethnic_race ) );

		show_field( "Work type / industry", sentence_case( $work_type ) );

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

		show_field( "State", name_case( $state ) );

		show_field( "Country", $country );

		if ($ip_country != "" && trim($ip_country) != "-" )
			show_field( "<i>Writing from</i>", name_case( $ip_country ) );
//			echo "<tr><td valign='top'><span class='dprompt'><i>Writing from</i></span></td><td align='left'><span class='dtext'><i>".name_case( $ip_country )."</i></span></td></tr>";


		echo "</table><br>";

		echo "<br><br>";

		show_ads( $cxn );

	}
?>

