<?php

	include "library.php";

	$cxn = start_code();

	
			/* get variables */

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

	$newprofile = val( $_GET, "newprofile" );
	$_SESSION['newprofile'] = $newprofile;


		/* check parameters */

	crash_if_missing( $newprofile, "newprofile missing in edit_profile", $cxn );

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

	if ($newprofile == "false" && $id == "")
	{
		$_SESSION['nextpage'] = url_encode( "edit_profile.php", "newprofile", $newprofile );

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

		exit();
	}

		/* this form edit window operates in three modes - a blank screen for a new
		profile, editing retrieved data for editing a profile, and editting the previously
		entered data after a validation error
		*/

		/* set the validation error flag  */

	if (val( $_SESSION, 'validation_error' ) == "true")
		$validation_error = "true";
	else
		$validation_error = "false";	/* blank or set to false */

	$_SESSION['validation_error'] = "false";


		/* start the page */

	page_header( "Your Profile", $cxn );


		/* re-entry to the screen following failed for validation */

	if ($validation_error == "true")
	{
		$handle = val( $_SESSION, "handle" );
		$short_intro = val( $_SESSION, "short_intro" );
		$interests = val( $_SESSION, "interests" );
		$general_comments = val( $_SESSION, "general_comments" );
		$looking_for = val( $_SESSION, "looking_for" );
		$city = val( $_SESSION, "city" );
		$country = val( $_SESSION, "country" );
		$email = val( $_SESSION, "email" );
		$password = val( $_SESSION, "password" );
		$date_of_birth = val( $_SESSION, "date_of_birth" );
		$sex = val( $_SESSION, "sex" );
		$sex_wanted = val( $_SESSION, "sex_wanted" );
		$membership_type = val( $_SESSION, "membership_type" );
		$has_children = val( $_SESSION, "has_children" );
		$wants_children = val( $_SESSION, "wants_children" );
		$wants_relationship = val( $_SESSION, "wants_relationship" );
		$wants_friendships = val( $_SESSION, "wants_friendships" );
		$first_name = val( $_SESSION, "first_name" );
		$surname = val( $_SESSION, "surname" );
		$source_referral = val( $_SESSION, "source_referral" );
		$color_scheme = val( $_SESSION, "color_scheme" );
		$phone_number = val( $_SESSION, "phone_number" );
		$date_format = val( $_SESSION, "date_format" );
		$visibility = val( $_SESSION, "visibility" );
		$reveal_first_name = val( $_SESSION, "reveal_first_name" );
		$state = val( $_SESSION, "state" );
		$postal_code = val( $_SESSION, "postal_code" );
		$accept_conditions = val( $_SESSION, "accept_conditions" );
		$religion = val( $_SESSION, "religion" );
		$ethnic_race = val( $_SESSION, "ethnic_race" );
		$work_type = val( $_SESSION, "work_type" );

		$confirm_email = val( $_SESSION, "confirm_email" );
		$confirm_password = val( $_SESSION, "confirm_password" );
	}
	else
	if ($newprofile == "true")		/* the first entry to the screen for a new profile */
	{
		$handle = "";
		$short_intro = "";
		$interests = "";
		$general_comments = "";
		$looking_for = "";
		$city = "";
		$country = "";
		$email = "";
		$password = "";
		$date_of_birth = "";
		$sex = "";
		$sex_wanted = "";
		$membership_type = "";
		$has_children = "";
		$wants_children = "";
		$wants_relationship = "";
		$wants_friendships = "";
		$first_name = "";
		$surname = "";
		$source_referral = "";
		$color_scheme = "";
		$phone_number = "";
		$date_format = "";
		$visibility = "";
		$reveal_first_name = "";
		$state = "";
		$postal_code = "";
		$accept_conditions = "";
		$religion = "";
		$ethnic_race = "";
		$work_type = "";

		$confirm_email = "";
		$confirm_password = "";
	}
	else	/* first entry to the screen for to modify an existing profile */
	{
		$query = "SELECT * from profile WHERE id = ".$id;

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

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

			$handle = $data['handle'];
			$short_intro = $data['short_intro'];
			$interests = $data['interests'];
			$general_comments = $data['general_comments'];
			$looking_for = $data['looking_for'];
			$city = $data['city'];
			$country = $data['country'];
			$email = $data['email'];
			$password = $data['password'];
			$date_of_birth = format_date_of_birth( $data['date_of_birth'], $data['date_format'] );
			$sex = $data['sex'];
			$sex_wanted = $data['sex_wanted'];
			$membership_type = $data['membership_type'];
			$has_children = $data['has_children'];
			$wants_children = $data['wants_children'];
			$wants_relationship = $data['wants_relationship'];
			$wants_friendships = $data['wants_friendships'];
			$first_name = $data['first_name'];
			$surname = $data['surname'];
			$source_referral = $data['source_referral'];
			$color_scheme = $data['color_scheme'];
			$phone_number = $data['phone_number'];
			$date_format = $data['date_format'];
			$visibility = $data['visibility'];
			$reveal_first_name = $data['reveal_first_name'];
			$state = $data['state'];
			$postal_code = $data['postal_code'];
			$religion = $data['religion'];
			$ethnic_race = $data['ethnic_race'];
			$work_type = $data['work_type'];

			$confirm_email = $email;
			$confirm_password = $password;

			$accept_conditions = "on";
		}
	}

	if (val( $_SESSION, "validation_errors" ) != "")
		echo red( "<b>Errors<br>".$_SESSION['validation_errors']."</b><br><br>" );

	$countries = build_country_list( $cxn );


	echo blue( "Click on the 'Save' button below to save your changes<br><br>" );

	echo blue( "Fields with an " ).watermark( "*" ).blue( " are compulsory, other fields are optional<br><br>" );

	echo blue( "Please note: contact details such as email addresses, telephone numbers and messanger ID's are not allowed in profiles<br><br><br>" );
	
	form_start( "save_profile_go.php" );

	form_textx( "*", "Handle Name", "handle", 20, $handle );

	form_radiox( "", "Profile Visibility", "visibility", "all_members", "All members", $visibility );

	form_radiox( "", "", "visibility", "only_with_contact", "Only members that I have contact with", $visibility );

	form_radiox( "", "", "visibility", "hidden", "Hidden", $visibility );

	form_textareax( "*", "Short Introduction", "short_intro" , 2, 100, $short_intro );

//	form_textareax( "", "Interests", "interests", 5, 100, $interests );

	form_textareax( "", "Comments", "general_comments", 20, 100, $general_comments );

//	form_textareax( "", "What are you looking for", "looking_for", 15, 100, $looking_for );

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

//	form_radiox( "", "Membership type", "membership_type", "basic", "Basic - To send emails, purchase 'credits'", $membership_type );

///	if (has_paypal( $cxn ))
//		form_radiox( "", "", "membership_type", "subscription", "Subscription - US $25 per month for unlimited use of the site", $membership_type );

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

	echo "<tr></tr><tr><td>".blue( "<b>Your Details</b>" )."</td></tr>";

	form_radiox( "", "Your Gender", "sex", "", "Male", $sex );

	form_radiox( "", "", "sex", "", "Female", $sex );

	form_checkboxx( "", "Interested in a relationship", "wants_relationship", $wants_relationship );

	form_checkboxx( "", "Interested in making friends", "wants_friendships", $wants_friendships );

//	form_dropdownx( "", "Do you have children", "has_children", "<option></option><option>Yes</option><option>No</option>", $has_children );

//	form_dropdownx( "", "Do you want more children", "wants_children", "<option></option><option>Yes</option><option>No</option><option>Undecided</option>", $wants_children );

	form_dropdownx( "", "Religion", "religion", "<option></option><option>Bahai</option><option>Buddist</option><option>Caodaist</option><option>Chinese Folk</option><option>Chondist</option><option>Christian</option><option>Confucist</option><option>Hindu</option><option>Muslim</option><option>Native</option><option>Jainist</option><option>Jewish</option><option>Shinto</option><option>Sikh</option><option>Taoist</option><option>Yiguanist</option>", $religion );

	form_textx( "", "Ethnicity", "ethnic_race", 25, $ethnic_race );

	form_textx( "", "Work type / Industry", "work_type", 50, $work_type );

	form_textx( "*", "City", "city", 25, $city );

	form_textx( "", "State", "state", 25, $state );

	form_dropdownx( "*", "Country", "country", $countries, $country );

	form_textx( "", "Postal/Zip code", "postal_code", 15, $postal_code );

	form_checkboxx( "", "Reveal first name", "reveal_first_name", $reveal_first_name );

	echo "<tr></tr><tr><td>".sp(1)."</td></tr>";

	form_radiox( "", "You are looking for", "sex_wanted", "Men", "Men", $sex_wanted );

	form_radiox( "", "", "sex_wanted", "Women", "Women", $sex_wanted );
	
	echo "<tr></tr><tr><td>".blue( "<b><br>Hidden Information</b>" )."</td></tr>";

	form_textx( "*", "First Name", "first_name", 25, $first_name );

	form_textx( "*", "Surname", "surname", 25, $surname );

	form_textx( "*", "Date of Birth", "date_of_birth", 12, $date_of_birth );

	form_dropdownx( "*", "Date format", "date_format", "<option></option><option>mm/dd/yyyy</option><option>dd/mm/yyyy</option><option>yyyy/mm/dd</option>", $date_format );

	echo "<tr><td>".fprompt( "Telephone number" )."</td><td>".watermark( "*" )."</td><td><input type='text' class='finput' name='"."phone_number"."' size='"."25"."' value='".$phone_number."'>".sp(2).blue( "Include the area code" )."</td></tr>";

	echo "<tr><td></td><td></td><td>".blue( "Please be careful to enter your email address correctly" )."</td></tr>";

	form_textx( "*", "Email", "email", 50, $email );

	form_textx( "*", "Confirm Email", "confirm_email", 50, $confirm_email );

//	form_radiox( "", "Heard about ".$_SESSION['sitename'], "source_referral", "search", "From an internet search", $source_referral );

//	form_radiox( "", "", "source_referral", "friend", "From a friend", $source_referral );

//	form_radiox( "", "", "source_referral", "other", "Other", $source_referral );

	form_passwordx( "*", "Password", "password", 15, $password );

	form_passwordx( "*", "Confirm Password", "confirm_password", 15, $confirm_password );

	form_radiox( "", "Color scheme", "color_scheme", "0", "Standard", $color_scheme );

	form_radiox( "", "", "color_scheme", "1", "Pink italics", $color_scheme );

	form_radiox( "", "", "color_scheme", "2", "Black background with white text", $color_scheme );

	form_radiox( "", "", "color_scheme", "3", "Large font sizes", $color_scheme );

	echo "<tr></tr><tr><td>".sp(1)."</td></tr>";

	echo "<tr><td colspan=3>".black( "I accept the conditions of use of this website " );

	echo "<a target='_blank' href='".url_encode( "policies.php" )."'>read here</a>";

	show_link( "read here", "policies.php" )."</td></tr>";

	form_checkboxx( "", "Accept conditions", "accept_conditions", $accept_conditions );

	echo "</table><table width='100%'><td width='15%'></td><td width='20%'></td><td width='65%'></td></tr>";

	echo "<tr><td></td><td align='center'><br><br><input type='image' src='buttons/".button_filename( "save" )."' border='0'></td></tr>";

	echo "<tr><td></td><td align='center'><br><br>";

	if ($newprofile == "false")
		show_button_h( "delete_profile", "delete_profile.php" );

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

	form_end();

?>

