<?php

	include "library.php";

	$cxn = start_code();

	
			/* get variables */

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

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

	
//	page_header( "", $cxn );


		/* emergency if the session's crashed save what the user typed in */
		/* so we can try and recover it */

	if ($newprofile == "" || ($newprofile != "true" && $id ==""))
	{
		$text = "handle='".trim( val( $_POST, "handle" ) )."', \n";
		$text .= "short_intro='".val( $_POST, "short_intro" )."', \n";
		$text .= "interests='".val( $_POST, "interests" )."', \n";
		$text .= "general_comments='".val( $_POST, "general_comments" )."', \n";
		$text .= "looking_for='".val( $_POST, "looking_for" )."', \n";
		$text .= "city='".val( $_POST, "city" )."', \n";
		$text .= "country='".val( $_POST, "country" )."', \n";
		$text .= "email='".val( $_POST, "email" )."', \n";
		$text .= "password='".val( $_POST, "password" )."', \n";
		$text .= "date_of_birth='".val( $_POST, "date_of_birth" )."', \n";
		$text .= "sex='".val( $_POST, "sex" )."', \n";
		$text .= "sex_wanted='".val( $_POST, "sex_wanted" )."', \n";
		$text .= "has_children='".val( $_POST, "has_children" )."', \n";
		$text .= "wants_children='".val( $_POST, "wants_children" )."', \n";
		$text .= "wants_relationship='".val( $_POST, "wants_relationship" )."', \n";
		$text .= "wants_friendships='".val( $_POST, "wants_friendships" )."', \n";
		$text .= "first_name='".val( $_POST, "first_name" )."', \n";
		$text .= "surname='".val( $_POST, "surname" )."', \n";
		$text .= "source_referral='".val( $_POST, "source_referral" )."', \n";
		$text .= "color_scheme='".val( $_POST, "color_scheme" )."', \n";
		$text .= "phone_number='".val( $_POST, "phone_number" )."', \n";
		$text .= "date_format='".val( $_POST, "date_format" )."', \n";
		$text .= "visibility='".val( $_POST, "visibility" )."', \n";
		$text .= "reveal_first_name='".val( $_POST, "reveal_first_name" )."', \n";
		$text .= "state='".val( $_POST, "state" )."', \n";
		$text .= "postal_code='".val( $_POST, "postal_code" )."', \n";
		$text .= "religion='".val( $_POST, "religion" )."', \n";
		$text .= "ethnic_race='".val( $_POST, "ethnic_race" )."', \n";
		$text .= "work_type='".val( $_POST, "work_type" )."', \n";

		$text .= "accept_conditions='".val( $_POST, "accept_conditions" )."', \n";

		$text .= "confirm_email='".val( $_POST, "confirm_email" )."' \n";
		$text .= "confirm_password='".val( $_POST, "confirm_password" )."' \n";

 		$query = "INSERT INTO sys_log (event, event_datetime, text) VALUES ('crash_recovery', '".date("Y-m-d H:i:s")."', '".prep_for_sql( $text )."' )";

		$result = mysql_query($query, $cxn);
	}

		/* check parameters */

	crash_if_missing( $newprofile, "newprofile missing in save profile", $cxn );

	if ($newprofile != "true")
		crash_if_missing( $id, "id missing in save profile", $cxn );

	
			/* define timestamps */

	$date_created = date("Y-m-d H:i:s");
	$date_modified = date("Y-m-d H:i:s");
	$date_assigned = date("Y-m-d H:i:s");
	$date_entered = date("Y-m-d H:i:s");
	$effective_date = date("Y-m-d H:i:s");


			/* get the data from the form */

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


	$accept_conditions = val( $_POST, "accept_conditions" );

	$confirm_email = val( $_POST, "confirm_email" );
	$confirm_password = val( $_POST, "confirm_password" );


			/* put the form data into the session table in case the form needs to be modified */

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

	$_SESSION['accept_conditions'] = $accept_conditions;

	$_SESSION['confirm_email'] = $confirm_email;
	$_SESSION['confirm_password'] = $confirm_password;


		/* check for a duplicate profile */

//	if ($newprofile == "true")
//	{
//		$first_name = str_replace( "'", "''", val( $_POST, "first_name" ) );
//		$surname = str_replace( "'", "''", val( $_POST, "surname" ) );
//		$date_of_birth = str_replace( "'", "''", val( $_POST, "date_of_birth" ) );
//		$date_format = str_replace( "'", "''", val( $_POST, "date_format" ) );
//
//		if ($first_name != "" && $surname != "" && $date_of_birth != "" && $date_format != "")
//		{
//			$sql_date_of_birth = convert_date_of_birth( $date_of_birth, $date_format, $cxn );
//
//			$query = "SELECT handle FROM profile WHERE first_name = '".$first_name."' AND surname = '".$surname."' AND date_of_birth = '".$sql_date_of_birth."'";
//
//			if (query_has_rows( $query, $cxn ))
//			{
//				$result = run_query( $query, $cxn );
//				$data = get_row( $result );
//
//				echo black( "You already have a profile set up on the system<br><br>" );
//				echo black( "Your handle name is ".$data['handle']."<br><br>" );
//				echo black( "Please use your existing handle to log on to the system<br><br>" );
//				echo show_home_link();
//
//				exit();
//			}
//		}
//	}

		/* check validation functions */

	$validation_errors = "";	


		/* check text */

	$profile_text = $handle." ".$short_intro." ".$interests." ".$general_comments." ".$looking_for;

	if ($profile_text != "")
	{
		if (strpos( strtolower( $profile_text ), "fuck" ) != false )
		{
			$validation_errors = $validation_errors."Please remove the word 'fuck' from your profile<br>";
//			send_email( "System", "markmcilroy@ozemail.com.au", "markmcilroy@ozemail.com.au", "System", "markmcilroy@ozemail.com.au", "Academic Dating: Profile text rejected", $profile_text, $profile_text );
		}

		$error_str = check_banned_words( $profile_text, $cxn );

		if ($error_str != "")
		{
			$validation_errors = $validation_errors."Please remove the word(s) ".$error_str." from your profile<br>";
//			send_email( "System", "markmcilroy@ozemail.com.au", "markmcilroy@ozemail.com.au", "System", "markmcilroy@ozemail.com.au", "Academic Dating: Profile text rejected", $profile_text, $profile_text );
		}


			/* check for email addresses */

		if (check_for_email( $profile_text, $cxn ))
		{
			$validation_errors = $validation_errors."Your profile text must not contain contact details such as email address<br>";
//			send_email( "System", "markmcilroy@ozemail.com.au", "markmcilroy@ozemail.com.au", "System", "markmcilroy@ozemail.com.au", "Academic Dating: Profile text rejected", $profile_text, $profile_text );
		}


			/* check for phone numbers */

		if (check_for_phone_number( $profile_text, $cxn ))
		{
			$validation_errors = $validation_errors."Your profile text must not contain a number such as a telephone number<br>";
//			send_email( "System", "markmcilroy@ozemail.com.au", "markmcilroy@ozemail.com.au", "System", "markmcilroy@ozemail.com.au", "Academic Dating: Profile text rejected", $profile_text, $profile_text );
		}


			/* check for messanger id's */

		if (strpos( strtolower( $profile_text ), "messenger id" ) != false )
			$validation_errors = $validation_errors."Your profile text must not contain contact details such as a messenger id<br>";

		if (strpos( strtolower( $profile_text ), "yahoo" ) != false )
			$validation_errors = $validation_errors."Your profile text must not contain contact details such as a messenger id<br>";

		if (strpos( strtolower( $profile_text ), "hotmail" ) != false )
			$validation_errors = $validation_errors."Your profile text must not contain contact details such as a messenger id<br>";
	}


		/* validations */

	if (! check_handle_chars( $handle ))
		$validation_errors = $validation_errors."Handle names can only contain letters and digits<br>";

	if (! check_handle( $newprofile, $handle, $id, $cxn ))
		$validation_errors = $validation_errors."This handle has already been used by an existing member<br>";

	if (strlen( $handle ) < 4 || strlen( $handle ) > 18)
		$validation_errors = $validation_errors."Your handle name must be between 4 and 18 characters long<br>";

	if ($visibility == "")
		$validation_errors = $validation_errors."You have not selected whether you want your profile to be visible or hidden<br>";

//	if ($short_intro == "")
//		$validation_errors = $validation_errors."Your short introduction cannot be blank<br>";

	if (strlen( $short_intro ) > 200)
		$validation_errors = $validation_errors."Your short introduction must be less than 200 characters long<br>";

	if ($city == "")
		$validation_errors = $validation_errors."City must not be blank<br>";
	
	if ($country == "")
		$validation_errors = $validation_errors."Country must not be blank<br>";

	if ($first_name == "")
		$validation_errors = $validation_errors."First name must not be blank<br>";

	if ($surname == "")
		$validation_errors = $validation_errors."Surname must not be blank<br>";

//	if ($source_referral == "")
//		$validation_errors = $validation_errors."You have not selected where you heard about Academic Dating from<br>";

//	if ($phone_number == "")
//		$validation_errors = $validation_errors."You must supply a telephone number. Please include the area code<br>";

	if ($date_of_birth == "")
		$validation_errors = $validation_errors."Date of birth must not be blank<br>";

	if ($date_format == "")
		$validation_errors = $validation_errors."Date format must not be blank<br>";

	if ($email == "")
		$validation_errors = $validation_errors."You must fill in an email address<br>";

	if (strpos( $email, "@" ) === false || strpos( $email, "." ) === false)
		$validation_errors = $validation_errors."The email address that you have provided is not a valid email address<br>";

	if ($sex != "Male" && $sex != "Female")
		$validation_errors = $validation_errors."You have not selected your gender<br>";

	if ($sex_wanted != "Men" && $sex_wanted != "Women")
		$validation_errors = $validation_errors."You have not selected the gender you are looking for<br>";

//	if ($membership_type == "")
//		$validation_errors = $validation_errors."You have not selected which type of membership you would like to have<br>";

	if ($password == "")
		$validation_errors = $validation_errors."You must provide a password<br>";

	if (strlen( $password ) < 4)
		$validation_errors = $validation_errors."The password must be at least 4 characters long<br>";

	if ($email != $confirm_email)
		$validation_errors = $validation_errors."The email address and confirm-email-address do not match<br>";

	if ($password != $confirm_password)
		$validation_errors = $validation_errors."The password and confirm-password do not match<br>";

	if ($accept_conditions != "on")
		$validation_errors = $validation_errors."You must tick the checkbox signifying that you accept the conditions of use of this website.<br>";

	if ($date_format != "")
	{
		$age = check_date( convert_date_of_birth(prep_for_sql( $date_of_birth ), str_replace( "'", "''", $date_format), $cxn ), $cxn);

		if ($age == 0)
			$validation_errors = $validation_errors."The date of birth is not in a valid format. Please enter it in the format dd/mm/yyyy, mm/dd/yyyy or yyyy/mm/dd and select the format that you have used from the 'date format' list<br>";
		else
		if ($age < 18)
			$validation_errors = $validation_errors."You must be at least 18 years of age to join this site<br>";
	}

	$_SESSION['validation_errors'] = $validation_errors;

	if ($validation_errors != "")
	{	
		$_SESSION['validation_error'] = "true";

				/* must happen before print output */
		jump_to_page( "edit_profile.php", "newprofile", $newprofile );
			
		exit();
	}
	else
		$_SESSION['validation_error'] = "false";


	page_header( "", $cxn );

		/* prepare the data for the SQL query by replacing quote characters with two quotes */

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


	$sql_date_of_birth = convert_date_of_birth( $date_of_birth, $date_format, $cxn );


	$email_verification_code = md5(time());


	if ($color_scheme == "")
		$color_scheme = "0";		/* default to standard if not selected */


		/* save the record to the database */

	if ($newprofile == "true")
	{
		$prev_membership_type = "";

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

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

		if ($source == "")
			$source = ".blank";

		$ip_country = get_ip_country( $cxn );

		$ip_country = str_replace( "'", "''", $ip_country );
		                                           

			/* determine free credits */
			/* look for previous/duplicated profiles */

//		if (query_has_rows( "SELECT id FROM profile WHERE email = '".$email."'", $cxn ))
//			$free_credits = 0;
//		else
//			$free_credits = 5;

			$free_credits = 0;

			/* create profile  */

		$query = "INSERT INTO profile (handle, short_intro, interests, general_comments, looking_for, city, country, email, password, date_of_birth, sex, sex_wanted, has_children, wants_children, wants_relationship, wants_friendships, first_name, surname, color_scheme, date_format, date_created, date_modified, visibility, reveal_first_name, state, postal_code, locked, deleted, email_address_valid, phone_number, accepts_conditions, last_login, number_of_logins, email_verification_code, religion, ethnic_race, work_type, returned_mail, credits_balance, source, ip_country, new_photo_number, membership_type, subscription_valid, system_user, source_referral) values ('$handle', '$short_intro', '$interests', '$general_comments', '$looking_for', '$city', '$country', '$email', '$password', '$sql_date_of_birth', '$sex', '$sex_wanted', '$has_children', '$wants_children', '$wants_relationship', '$wants_friendships', '$first_name', '$surname', '$color_scheme', '$date_format', '$date_created', '$date_modified', '$visibility', '$reveal_first_name', '$state', '$postal_code', 'no', 'no', 'no', '$phone_number', '$accept_conditions', '$last_login', 0, '".$email_verification_code."', '$religion', '$ethnic_race', '$work_type', 'no', ".$free_credits.", '$source', '$ip_country', 0, '$membership_type', 'no', 'no', '$source_referral' )";

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

		echo black( "A verificaion email should arrive in your email inbox shortly.<br><br> You will need to click on the link in the verification email before you can send messages to other members<br><br>" );

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

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

		$data = get_row( $result );

//		send_welcome_letter( $data['id'], $cxn );
		
		send_verification_email( $data['id'], $email_verification_code, $cxn );

			/* update handle history */

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

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

		$data = get_row( $result );

		$new_id = $data['id'];


			/* save the handle history */

		$query = "INSERT INTO handle_history (member_id, handle, date_assigned) VALUES (".$new_id.", '".$handle."', '".$date_assigned."' )";

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


			/* save the email history */

		$query = "INSERT INTO email_history (member_id, email, date_entered) VALUES (".$new_id.", '".$email."', '".$date_entered."' )";

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


			/* save the membership type history */

		$query = "INSERT INTO membership_type_history (member_id, type, effective) VALUES (".$new_id.", '".$membership_type."', '".$effective_date."')";

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


			/* save the free credits transaction */

		if ($free_credits != 0)
		{
			$transaction_date = date("Y-m-d H:i:s");

			$query = "INSERT INTO credits (member_id, event, number, event_datetime) VALUES (".$new_id.", 'free credits on joining', ".$free_credits.", '".$transaction_date."' )";
			$result = run_query( $query, $cxn );
		}

			/* log any current user out to avoid confusion about who's account you're in */

		$_SESSION['id'] = "";


			/* send notification email */

//		if ($_SESSION['database'] == "academi1_academicdating")
//			send_email( "System", "markmcilroy@ozemail.com.au", "markmcilroy@ozemail.com.au", "System", "markmcilroy@ozemail.com.au", "A profile has been added to Academic Dating", "", "" );

		$query = "INSERT INTO account_activity (member_id, event, event_datetime) VALUES (".$new_id.", 'profile created', '".date("Y-m-d H:i:s")."')";
		$result = run_query( $query, $cxn );


			/* log the source of the new member */

		log_conversion( $cxn );

		echo black( "Successfully saved<br><br>" );

	}
	else
	if ($newprofile == "false")
	{
		$new_id = $id;


			/* update handle history  */

		$query = "SELECT handle, email, email_address_valid, returned_mail, membership_type FROM profile WHERE id = ".$id;

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

		$data = get_row( $result );

		$prev_email = $data['email'];
		$prev_membership_type = $data['membership_type'];
		$email_address_valid = $data['email_address_valid'];
		$returned_mail = $data['returned_mail'];

		if ($data['handle'] != $handle)
		{
			$query = "INSERT INTO handle_history (member_id, handle, date_assigned) VALUES (".$id.", '".$handle."', '".$date_assigned."')";

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

		if ($prev_membership_type != $membership_type)
		{
			$query = "INSERT INTO membership_type_history (member_id, type, effective) VALUES (".$id.", '".$membership_type."', '".$effective_date."')";

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

			/* update profile */

		if ($prev_email != $email)
		{
			$email_address_valid = "no";
			$returned_mail = "no";

				/* save the email history */

			$query = "INSERT INTO email_history (member_id, email, date_entered) VALUES (".$id.", '".$email."', '".$date_entered."' )";

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

		$query = "UPDATE profile SET handle = '".$handle."', short_intro = '".$short_intro."', interests = '".$interests."', general_comments = '".$general_comments."', looking_for = '".$looking_for."', city = '".$city."', country = '".$country."', email = '".$email."', password = '".$password."', date_of_birth = '".$sql_date_of_birth."', sex = '".$sex."', sex_wanted = '".$sex_wanted."', has_children = '".$has_children."', wants_children = '".$wants_children."', wants_relationship = '".$wants_relationship."', wants_friendships = '".$wants_friendships."', first_name = '".$first_name."', surname = '".$surname."', color_scheme = '".$color_scheme."', date_format = '".$date_format."', date_modified = '".$date_modified."', visibility = '".$visibility."', reveal_first_name = '".$reveal_first_name."', state = '".$state."', postal_code = '".$postal_code."', phone_number = '".$phone_number."', accepts_conditions = '".$accept_conditions."', email_verification_code = '".$email_verification_code."', email_address_valid = '".$email_address_valid."', religion = '".$religion."', ethnic_race = '".$ethnic_race."', work_type = '".$work_type."', returned_mail = '".$returned_mail."', membership_type = '".$membership_type."', source_referral = '".$source_referral."' WHERE id = ".$id;

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

		$_SESSION['color_scheme'] = $color_scheme;


			/* send a message so we get a bounce back if its a bad email address */
		if ($prev_email != $email)
		{
			echo black( "You have changed your email address.<br><br>A verificaion email should arrive in your email inbox shortly.<br><br> You will need to click on the link in the verification email before you can send messages to other members<br><br>" );

			send_verification_email( $id, $email_verification_code, $cxn );
		}

		echo black( "Successfully saved<br><br>" );

//			send_confirmation_email( $id, $cxn );
	}
	else
		crash( "Internal Error: invalid newprofile flag in save_profile", $cxn );

		/* clean out the session array or it will be used as */
		/* the default for other forms */

	$_SESSION['handle'] = "";
	$_SESSION['short_intro'] = "";
	$_SESSION['interests'] = "";
	$_SESSION['general_comments'] = "";
	$_SESSION['looking_for'] = "";
	$_SESSION['city'] = "";
	$_SESSION['country'] = "";
	$_SESSION['email'] = "";
	$_SESSION['password'] = "";
	$_SESSION['date_of_birth'] = "";
	$_SESSION['sex'] = "";
	$_SESSION['sex_wanted'] = "";
	$_SESSION['has_children'] = "";
	$_SESSION['wants_children'] = "";
	$_SESSION['wants_relationship'] = "";
	$_SESSION['wants_friendships'] = "";
	$_SESSION['first_name'] = "";
	$_SESSION['surname'] = "";
	$_SESSION['source_referral'] = "";
	$_SESSION['$color_scheme'] = "";
	$_SESSION['phone_number'] = "";
	$_SESSION['date_format'] = "";
	$_SESSION['visibility'] = "";
	$_SESSION['reveal_first_name'] = "";
	$_SESSION['state'] = "";
	$_SESSION['postal_code'] = "";
	$_SESSION['confirm_email'] = "";
	$_SESSION['confirm_password'] = "";
	$_SESSION['accept_conditions'] = "";
	$_SESSION['religion'] = "";
//	$_SESSION['religious_beliefs'] = "";
	$_SESSION['ethnic_race'] = "";
	$_SESSION['work_type'] = "";
//	$_SESSION['in_relationship'] = "";


		/* move to a subscription */

	if ($membership_type == "subscription" && $prev_membership_type != "subscription")
	{
		if ($newprofile == "true")
			echo black( "You have selected a subscription membership.<br><br>" );
		else
			echo black( "You have changed to a subscription membership.<br><br>" );

		if (has_paypal( $cxn ))
		{
			echo black( "You will need to set up your details on the PayPal site to activate your membership.<br><br>" );
			echo black( "Please click on the button below and then enter your payment details into the PayPal site.<br><br>" );
			echo "<br><br>";

			$credits_code = md5(time());
			$magic = md5( $new_id."|".$credits_code );
			$custom_subs = "subscription,".$new_id.",".$credits_code.",".$magic;

			$query = "UPDATE profile SET credits_code = '".$credits_code."' WHERE id = ".$new_id;
			$result = run_query( $query, $cxn );

			generate_subscription_button( $custom_subs, button_filename( "activate_membership" ) );
		}
		else
		{
			echo black( "The only payment option for your country is Western Union.<br><br>" );
			echo black( "You will need to make 2 months payment at a total of US $50 dollars before your membership will be active.<br><br>" );
			echo black( "For details see " ).show_link( "western&nbsp;union", "western_union.php" );
		}

		exit();
	}

		/* move away from a subscription */

	if ($prev_membership_type == "subscription" && 	$membership_type != "subscription")
	{
		echo black( "You have ended a subscription membership.<br><br>" );

		if (has_paypal( $cxn ))
		{
			echo black( "You will need to end the subscription in the PayPal site to stop payments coming out of your account<br><br>." );
			echo black( "Please refer to " )."<a href='http://www.paypal.com'>www.paypal.com</a><br><br>";
		}

		exit();
	}

	echo show_home_link();


			/* check to see whether a new user has selected an existing handle */

	function check_handle($newprofile, $handle, $id, $cxn)
	{
		$handle = str_replace( "'", "''", $handle);

		if ($newprofile == "true")
			$query = "SELECT handle FROM handle_history WHERE handle = '".$handle."'";
		else
			$query = "SELECT handle FROM handle_history WHERE handle = '".$handle."' AND member_id <> ".$id;

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

		if (mysql_num_rows($result) == 0)
			return true;
		else
			return false;
	}


		/* note this function is designed for dates of birth so it adds 19 to
		all 2-digit years, this wouldn't work for recent dates
		*/

	function convert_date_of_birth( $date_of_birth, $date_format, $cxn )
	{
		$dob2 = $date_of_birth;

		$j = 0;

		$parts[0] = "";
		$parts[1] = "";
		$parts[2] = "";
		$parts[3] = "";

		for ($i=0; $i < strlen($dob2); $i++)
		{
			if (is_digit( substr( $dob2, $i, 1 )))
				$parts[$j] = $parts[$j].substr( $dob2, $i, 1 );
			else
			{
				if ($j < 3)
					$j = $j + 1;
			}
		}

		if ($date_format == "dd/mm/yyyy")
		{
			$day = $parts[0];
			$month = $parts[1];
			$year = $parts[2];
		}
		else
		if ($date_format == "mm/dd/yyyy")
		{
			$month = $parts[0];
			$day = $parts[1];
			$year = $parts[2];
		}
		else
		if ($date_format == "yyyy/mm/dd")
		{
			$year = $parts[0];
			$month = $parts[1];
			$day = $parts[2];
		}
		else
			crash( "Internal Error: invalid date format in convert date of birth", $cxn );

		if (strlen( $day ) == 1)
			$day = "0".$day;

		if (strlen( $month ) == 1)
			$month = "0".$month;

		if ($year < 100)
			$year = $year + 1900;

		return ($year."-".$month."-".$day);
	}


	function is_digit( $ch )
	{
		if ($ch >= "0" && $ch <= "9")
			return true;
		else
			return false;
	}


		/* a handle can only contain alphanumeric characters */

	function check_handle_chars( $handle )
	{
		$ok = true;

		for ($i=0; $i < strlen( $handle ); $i++)
		{
			$ch = substr( $handle, $i, 1 );

			if (! (($ch >= "a" && $ch <="z") ||
					($ch >= "A" && $ch <= "Z") ||
					($ch >= "0" && $ch <= "9")))
				$ok = false;
		}

		return ($ok);
	}


		/* returns true if the text contains a phone number */

	function check_for_phone_number( $text )
	{
		$has_number = false;
		$points = 0;

		for ($i=0; $i < strlen( $text ); $i++)
		{
			$ch = substr( $text, $i, 1 );

			if ($ch >= "0" && $ch <="9")
				$points += 5;
			else
				$points--;

			if ($points < 0)
				$points = 0;

			if ($points >= 30)
				$has_number = true;
		}

		return ($has_number);
	}
	
?>
