<?php

	include "library.php";

	$cxn = start_code();
	
	$cxn_common = db_common_login();
	
	
			/* get variables */

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

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

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

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

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

	

	if (val( $_GET, "screen_width" ) == "" && val( $_SESSION, "screen_width" ) == "")
	{
		$parameters = "showid=".$show_id."&returnpoint=".$returnpoint."&startpos=".$startpos."&external=".$external."&stats_screen=".$stats_screen."&sample=".$sample."&conversationid=".$conversation_id;

		echo "<script language='JavaScript'> document.location=\"display_profile.php?screen_width=\" + screen.width + \"&screen_height=\" + screen.height + \"&".$parameters."\"; </script>";
	}


	if (val( $_GET, "screen_width" ) != "" && val( $_SESSION, "screen_width" ) == "")
	{
		log_screen_size( $_GET['screen_width'], $_GET['screen_height'], $cxn );

		$_SESSION['screen_width'] = $_GET['screen_width'];
		$_SESSION['screen_height'] = $_GET['screen_height'];		
	}
	

//	if ($external == "true")
//	{
//		$id = "";
//
//		$_SESSION['session_active'] = "yes";
//	}
//	else

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

    if ($id != "")
    {
	    $query = "SELECT handle FROM profile WHERE id = ".$id;

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

	    $data = get_row( $result );

	    $memb_handle = $data['handle'];
    }
        


		/* display the sample profile */

	if ($sample == "true")
	{
		$ip_country = get_ip_country( $cxn_common );

//		if ($ip_country == "United States")
//			$query = "SELECT id FROM profile WHERE handle = 'usaman'";


		$query = "SELECT id FROM profile WHERE handle = 'chinagirl'";


            // NOTE: other country sample profiles accidently deleted.
            
//		else		
//		if (sample_image_type( $cxn, $cxn_common ) == "african")
//			$query = "SELECT id FROM profile WHERE handle = 'africanman2'";
//
//		else
//			$query = "SELECT id FROM profile WHERE handle = 'usaman'";

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

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

		$data = get_row( $result );

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




	if ($external != "true" && $id == "" && $sample != "true" && $stats_screen != "true")

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

	    jump_to_page( val( $_SESSION, "login_page" ) );		/* 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, $cxn_common, "", 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 );

	if ($returnpoint == "")		
		$returnpoint = "main";


	$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 (query_num_rows($result) == 0)
	{
		crash( "Internal error: id not found", $cxn );
	}
	else
	{
		$data = get_row( $result );

		if ($data['visibility'] == 'hidden' && $stats_screen == "true")
		{
			echo black( "This member's profile is currently hidden." );
			exit();
		}

		if ($data['visibility'] == 'hidden' &&
			$show_id != $id &&
			$sample != "true" &&
			(! is_system_user( $id, $cxn )) &&
			(! is_system_user( $show_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 == "" && $stats_screen != "true")
			{
				echo black( "Please log in to view this member's profile." );
				exit();
			}

			if ($stats_screen != "true")
			{
				$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'];


        $marital_status = $data['marital_status'];
		$sex = $data['sex'];
		$sex_wanted = $data['sex_wanted'];
		$has_children = $data['has_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'];


		$age = calc_person_age( $data['dob_dayofyear'], $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 (query_num_rows($result) > 0)
//				$show_reply_to_kiss = "true";
//		}


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

		echo "<tr><td width='65%'>";


			// show the first line of the screen which has the photos and the skyscraper ads
			
		show_photos_and_skyscraper_ads( $id,  $show_id, $sample, $conversation_id, $stats_screen, $cxn );

		echo "<tr>";


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

		
		if ($color_scheme != 2)		/* black screen background */
		{
			if (! low_res_screen())		
				echo "<td width='65%' valign='top'>";
			else
				echo "<td width='95%' colspan=3>";
		}
		else
		{
			if (! low_res_screen())		
				echo "<td width='65%' valign='top'>";
			else
				echo "<td width='95%' colspan=3>";
		}


		if (low_res_screen())
		{
			echo "</table><table width='100%'>";
		}
		
		
		write_profile_items( $show_handle, $sample, $color_scheme, $reveal_first_name, $first_name, $short_intro, $interests, $general_comments, 
					$looking_for, $sex, $age, $wants_relationship, $wants_friendships, $sex_wanted, $marital_status, $has_children, 
					$religion, $ethnic_race, $work_type, $city, $state, $country, $ip_country, $cxn );

		
		echo "<br>";


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

		echo "</td>";


		echo "<td valign='top' align='center'>";
		
		
			// buttons
		
		if (! low_res_screen())
			write_buttons( $id, $show_id, $sample, $conversation_id, $stats_screen, $cxn );
		

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

//		if (! low_res_screen())
//		{
//			echo "<br><br>";
//			show_ads( $cxn );
//		}
//		else
		
		if (low_res_screen())		
		{
			echo "";
			
//			show_ads_narrow_banner();			
		
		    echo "<table width='100%'><tr><td align='center'>";
		    
			show_ads_large_square();
			
			echo "</td></td></table>";
		}
			
		echo "<br><br><br><br><br>";	// blank space at the bottom of the page so ad is not squashed against the bottom border

	}
	
	
	
		//*****************************************
		//
		// Start of functions
		//
		//*****************************************
		
		
	function write_buttons( $id, $show_id, $sample, $conversation_id, $stats_screen, $cxn )
	{
	
		echo "<br>";
		
		if (! low_res_screen())
			$button_width = 195;
		else
			$button_width = 100;
			
			
			// send contact request

		if ($sample == "true")
			show_green_button_opaque( "Send Contact Request", $button_width );			
		else
			show_green_button( "Send Contact Request", $button_width, "send_message.php", "sendtype", "contact_request", "sendtoid", $show_id );


		echo "<br><br>";


			// send email
			
		if ($sample == "true")
			show_green_button_opaque( "Send Email", $button_width );
		else
			show_green_button( "Send Email", $button_width, "send_message.php", "sendtype", "email", "sendtoid", $show_id );
			

		echo "<br><br>";


			// remember

		if ($sample == "true")
			show_green_button_opaque( "Remember", $button_width );			
		else
			show_green_button( "Remember", $button_width, "add_to_remembered.php", "rememberedid", $show_id );


		echo "<br><br>";


			// block
			
		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_green_button( "Block", $button_width, "block.php", "blockid", $show_id );
				
				echo "<br><br>";
			}
		}

			
			// chat request

	
		if ($conversation_id == "" && query_has_rows( "SELECT id FROM online_status WHERE member_id = ".$show_id, $cxn ) && $id != "" && $id != $show_id)
		{
			show_green_button( "Request Chat", $button_width, "chat_request_check.php", "id1", $id, "id2", $show_id );

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



			// accept chat

		if ($conversation_id != "" && $id != "")		
		{
			show_green_button( "Accept Chat", $button_width, "chat.php", "id1", $id, "id2", $show_id, "conversationid", $conversation_id, "newchat", "false" );

			echo "<br><br>";
		}


            // system edit
            
//		if ($stats_screen == "true")
//		{
//		    $_SESSION['id'] = $show_id;
//		    
//			show_green_button( "Edit", $button_width, "edit_profile.php", "newprofile", "false" );
//
//			echo "<br><br>";
//		}

	}
	
	
		// write the main profile data in the inner table
		
	function write_profile_items( $show_handle, $sample, $color_scheme, $reveal_first_name, $first_name, $short_intro, $interests, $general_comments, 
					$looking_for, $sex, $age, $wants_relationship, $wants_friendships, $sex_wanted, $marital_status, $has_children, 
					$religion, $ethnic_race, $work_type, $city, $state, $country, $ip_country, $cxn )
	{
	
		if (! low_res_screen())
			echo "<br>";
		
	
		if ($color_scheme != 2)		/* black screen background */
		{
			if (! low_res_screen())		//box-shadow: 10px 10px 5px #888888;
//				echo "<table cellspacing='4' style='border: 1px solid #bbbbbb; background: linear-gradient(#fcfcfc, #e1e1e1); '>";
				echo "<table cellspacing='4' style='border: 1px solid #bbbbbb; background: #fcfcfc; '>";
			else
//				echo "<table width='100%' cellspacing='4' style='border: 1px solid #bbbbbb; background: linear-gradient(#fcfcfc, #e1e1e1)'>";
				echo "<table width='100%' cellspacing='4' style='border: 1px solid #bbbbbb; background: #fcfcfc;'>";
		}
		else
		{
			if (! low_res_screen())		
				echo "<table cellspacing='4' style='border: 1px solid #bbbbbb;'>";
			else
				echo "<table width='100%' cellspacing='4' style='border: 1px solid #bbbbbb;'>";
		}


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


		if (! low_res_screen())
			echo "<tr><td width='10%'><span class='dprompt'>Handle Name</span></td><td width='40%'><span class='dtext'>".$show_handle."</span></td>";		
		else
			echo "<tr><td width='40%'><span class='dprompt'>Handle Name</span></td><td width='60%'><span class='dtext'>".$show_handle."</span></td>";

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

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

		show_field( "Brief Introduction", $short_intro );

		show_field( "Interests", $interests );

		show_field( "Gender", $sex );

		show_field( "Age", $age );

		show_field( "Country", $country );
		
//		if (get_ip_country_from_country( $country, $cxn ) != $ip_country && trim($ip_country) != "-")
//    		show_field( "Located in", $ip_country );		

		echo "</table>";
	}	
	
	
	
		// show the first line of the screen which has the photos and the skyscraper ads
		
	function show_photos_and_skyscraper_ads( $id,  $show_id, $sample, $conversation_id, $stats_screen, $cxn )
	{
			// display the photos

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

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

		
		if (query_num_rows($result) == 0)
		{
			echo "&nbsp;";

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


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

			echo "&nbsp;";
			
			show_photo( $show_id, 140, $data['photo_number'], true, false, $cxn );

			echo "&nbsp;";				

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

					
		echo "</td>";

		if (! low_res_screen())
		{
			echo "<br>";
			
			echo "</td><td width='20%'></td><td width='200px' align='center' rowspan=3>";
			
			show_ads_narrow_skyscraper();

//			show_ads_medium_skyscraper();

			echo "</td></tr>";		
		}
		else
		{
			echo "</td><td width='20%'></td><td width='200px' valign='top' align='center' rowspan=3>";
					
			write_buttons( $id, $show_id, $sample, $conversation_id, $stats_screen, $cxn );
			
			echo "</td></tr>";		
		}
	}	
	
	
?>