<?php

	include "library.php";

	$cxn = start_code();
	
    $cxn_common = db_common_login();
	

	
			/* get variables */

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

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

		
//	page_header( "", $cxn );

		/* check parameters */

	crash_if_missing( $newtask, "newtask missing in save task", $cxn );

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

	
			/* define timestamps */

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


			/* get the data from the form */

	$date_received = val( $_POST, "date_received" );
	$date_quote_accepted = val( $_POST, "date_quote_accepted" );
	$date_requested_by = val( $_POST, "date_requested_by" );
	$date_completed = val( $_POST, "date_completed" );
	$client_names = val( $_POST, "client_names" );
	$paraplanner = val( $_POST, "paraplanner" );
	$checked_by = val( $_POST, "checked_by" );
	$client_type = val( $_POST, "client_type" );
	$task_type = val( $_POST, "task_type" );
	$status = val( $_POST, "status" );
	$number_of_strategies = val( $_POST, "number_of_strategies" );
	$estimated_hours = val( $_POST, "estimated_hours" );
	$quoted_price = val( $_POST, "quoted_price" );
	$completed_hours = val( $_POST, "completed_hours" );
	$checking_minutes = val( $_POST, "checking_minutes" );
	$rework_hours = val( $_POST, "rework_hours" );
	$practice = val( $_POST, "practice" );
	$adviser1 = val( $_POST, "adviser1" );
	$adviser2 = val( $_POST, "adviser2" );
	$adviser3 = val( $_POST, "adviser3" );
	$invoice_number = val( $_POST, "invoice_number" );
	$notes = val( $_POST, "notes" );
//	$date_created = val( $_POST, "date_created" );
//	$date_modified = val( $_POST, "date_modified" );
			
			
//	$handle = 		trim( val( $_POST, "handle" ) );


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

	$_SESSION['date_received'] = $date_received;
	$_SESSION['date_quote_accepted'] = $date_quote_accepted;
	$_SESSION['date_requested_by'] = $date_requested_by;
	$_SESSION['date_completed'] = $date_completed;
	$_SESSION['client_names'] = $client_names;
	$_SESSION['paraplanner'] = $paraplanner;
	$_SESSION['checked_by'] = $checked_by;
	$_SESSION['client_type'] = $client_type;
	$_SESSION['task_type'] = $task_type;
	$_SESSION['status'] = $status;
	$_SESSION['number_of_strategies'] = $number_of_strategies;
	$_SESSION['estimated_hours'] = $estimated_hours;
	$_SESSION['quoted_price'] = $quoted_price;
	$_SESSION['completed_hours'] = $completed_hours;
	$_SESSION['checking_minutes'] = $checking_minutes;
	$_SESSION['rework_hours'] = $rework_hours;
	$_SESSION['practice'] = $practice;
	$_SESSION['adviser1'] = $adviser1;
	$_SESSION['adviser2'] = $adviser2;
	$_SESSION['adviser3'] = $adviser3;
	$_SESSION['invoice_number'] = $invoice_number;
	$_SESSION['notes'] = $notes;
//	$_SESSION['date_created'] = $date_created;
//	$_SESSION['date_modified'] = $date_modified;

			
//	$_SESSION['handle'] = $handle;


		/* check validation functions */

	$validation_errors = "";
	
	$date_received_error = "false";
	$date_quote_accepted_error = "false";
	$date_requested_by_error = "false";
	$date_completed_error = "false";
	$client_names_error = "false";
	$paraplanner_error = "false";
	$checked_by_error = "false";
	$client_type_error = "false";
	$task_type_error = "false";
	$status_error = "false";
	$number_of_strategies_error = "false";
	$estimated_hours_error = "false";
	$quoted_price_error = "false";
	$completed_hours_error = "false";
	$checking_minutes_error = "false";
	$rework_hours_error = "false";
	$practice_error = "false";
	$adviser1_error = "false";
	$adviser2_error = "false";
	$adviser3_error = "false";
	$invoice_number_error = "false";
	$notes_error = "false";
//	$date_created_error = "false";
//	$date_modified_error = "false";
	

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

	if ($date_received != "")
	{
		$valid_date = check_date_valid( convert_date_of_birth(prep_for_sql( $date_received ), "dd/mm/yyyy", $cxn ), $cxn);
	
		if ($valid_date == "false")
		{
			$validation_errors = $validation_errors."The date received is not in a valid format. Please enter it in the format dd/mm/yyyy<br>";
			$date_received_error = "true";
		}
	}

	if ($date_quote_accepted != "")
	{
		$valid_date = check_date_valid( convert_date_of_birth(prep_for_sql( $date_quote_accepted ), "dd/mm/yyyy", $cxn ), $cxn);
	
		if ($valid_date == "false")
		{
			$validation_errors = $validation_errors."The date quote accepted is not in a valid format. Please enter it in the format dd/mm/yyyy<br>";
			$date_quote_accepted_error = "true";
		}
	}

	if ($date_requested_by != "")
	{
		$valid_date = check_date_valid( convert_date_of_birth(prep_for_sql( $date_requested_by ), "dd/mm/yyyy", $cxn ), $cxn);
	
		if ($valid_date == "false")
		{
			$validation_errors = $validation_errors."The date requested by is not in a valid format. Please enter it in the format dd/mm/yyyy<br>";
			$date_requested_by_error = "true";
		}
	}

	if ($date_completed != "")
	{
		$valid_date = check_date_valid( convert_date_of_birth(prep_for_sql( $date_completed ), "dd/mm/yyyy", $cxn ), $cxn);
	
		if ($valid_date == "false")
		{
			$validation_errors = $validation_errors."The date completed is not in a valid format. Please enter it in the format dd/mm/yyyy<br>";
			$date_completed_error = "true";
		}
	}


	if ($practice == "")
	{
		$validation_errors = $validation_errors."You have not which practice the request has come from<br>";
		$practice_error = "true";
	}

	if ($client_type == "")
	{
		$validation_errors = $validation_errors."You have not selected whether the client is a couple or single<br>";
		$client_type_error = "true";
	}

	if ($task_type == "")
	{
		$validation_errors = $validation_errors."You have not selected the task type, e.g. SOA<br>";
		$task_type_error = "true";
	}
			
	if ($number_of_strategies != "" && (! check_number_chars( $number_of_strategies )))
	{
		$validation_errors = $validation_errors."Number of strategies is in an invalid format. The field can only contain digits and a decimal point.<br>";
		$number_of_strategies_error = "true";
	}

	if ($estimated_hours != "" && (! check_number_chars( $estimated_hours )))
	{
		$validation_errors = $validation_errors."Estimated hours is in an invalid format. The field can only contain digits and a decimal point.<br>";
		$estimated_hours_error = "true";
	}

	if ($quoted_price != "" && (! check_number_chars( $quoted_price )))
	{
		$validation_errors = $validation_errors."Quoted price is in an invalid format. The field can only contain digits and a decimal point.<br>";
		$quoted_price_error = "true";
	}

	if ($completed_hours != "" && (! check_number_chars( $completed_hours )))
	{
		$validation_errors = $validation_errors."Completed hours is in an invalid format. The field can only contain digits and a decimal point.<br>";
		$completed_hours_error = "true";
	}

	if ($checking_minutes != "" && (! check_number_chars( $checking_minutes )))
	{
		$validation_errors = $validation_errors."Checking minutes is in an invalid format. The field can only contain digits and a decimal point.<br>";
		$checking_minutes_error = "true";
	}
	
	if ($rework_hours != "" && (! check_number_chars( $rework_hours )))
	{
		$validation_errors = $validation_errors."Rework hours is in an invalid format. The field can only contain digits and a decimal point.<br>";
		$rework_hours_error = "true";
	}
	
	
	$_SESSION['validation_errors'] = $validation_errors;
	
	$_SESSION['date_received_error'] = $date_received_error;
	$_SESSION['date_quote_accepted_error'] = $date_quote_accepted_error;
	$_SESSION['date_requested_by_error'] = $date_requested_by_error;
	$_SESSION['date_completed_error'] = $date_completed_error;
	$_SESSION['client_names_error'] = $client_names_error;
	$_SESSION['paraplanner_error'] = $paraplanner_error;
	$_SESSION['checked_by_error'] = $checked_by_error;
	$_SESSION['client_type_error'] = $client_type_error;
	$_SESSION['task_type_error'] = $task_type_error;
	$_SESSION['status_error'] = $status_error;
	$_SESSION['number_of_strategies_error'] = $number_of_strategies_error;
	$_SESSION['estimated_hours_error'] = $estimated_hours_error;
	$_SESSION['quoted_price_error'] = $quoted_price_error;
	$_SESSION['completed_hours_error'] = $completed_hours_error;
	$_SESSION['checking_minutes_error'] = $checking_minutes_error;
	$_SESSION['rework_hours_error'] = $rework_hours_error;
	$_SESSION['practice_error'] = $practice_error;
	$_SESSION['adviser1_error'] = $adviser1_error;
	$_SESSION['adviser2_error'] = $adviser2_error;
	$_SESSION['adviser3_error'] = $adviser3_error;
	$_SESSION['invoice_number_error'] = $invoice_number_error;
	$_SESSION['notes_error'] = $notes_error;
//	$_SESSION['date_created_error'] = $date_created_error;
//	$_SESSION['date_modified_error'] = $date_modified_error;
	
	
//	$_SESSION['handle_error'] = $handle_error;

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

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


	page_header( "", $cxn, $cxn_common );

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

	$date_received = prep_for_sql( $date_received );
	$date_quote_accepted = prep_for_sql( $date_quote_accepted );
	$date_requested_by = prep_for_sql( $date_requested_by );
	$date_completed = prep_for_sql( $date_completed );
	$client_names = prep_for_sql( $client_names );
	$paraplanner = prep_for_sql( $paraplanner );
	$checked_by = prep_for_sql( $checked_by );
	$client_type = prep_for_sql( $client_type );
	$task_type = prep_for_sql( $task_type );
	$status = prep_for_sql( $status );
	$number_of_strategies = prep_for_sql( $number_of_strategies );
	$estimated_hours = prep_for_sql( $estimated_hours );
	$quoted_price = prep_for_sql( $quoted_price );
	$completed_hours = prep_for_sql( $completed_hours );
	$checking_minutes = prep_for_sql( $checking_minutes );
	$rework_hours = prep_for_sql( $rework_hours );
	$practice = prep_for_sql( $practice );
	$adviser1 = prep_for_sql( $adviser1 );
	$adviser2 = prep_for_sql( $adviser2 );
	$adviser3 = prep_for_sql( $adviser3 );
	$invoice_number = prep_for_sql( $invoice_number );
	$notes = prep_for_sql( $notes );
//	$date_created = prep_for_sql( $date_created );
//	$date_modified = prep_for_sql( $date_modified );

		
		
//	$handle = prep_for_sql( $_POST['handle'] );

	if ($date_received == "")
		$sql_date_received = "";
	else
		$sql_date_received = convert_date_of_birth( $date_received, "dd/mm/yyyy", $cxn );
		
	if ($date_quote_accepted == "")
		$sql_date_quote_accepted = "";
	else
		$sql_date_quote_accepted = convert_date_of_birth( $date_quote_accepted, "dd/mm/yyyy", $cxn );
		
	if ($date_requested_by == "")
		$sql_date_requested_by = "";
	else
		$sql_date_requested_by = convert_date_of_birth( $date_requested_by, "dd/mm/yyyy", $cxn );
		
	if ($date_completed == "")
		$sql_date_completed = "";
	else
		$sql_date_completed = convert_date_of_birth( $date_completed, "dd/mm/yyyy", $cxn );


	//$email_verification_code = substr(md5(time()), 0, 8);


//        $membership_type = "subscription";

		/* save the record to the database */


	if ($newtask == "true")
	{
//		$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 = 2;

			$free_credits = 0;

//		if ($email == "mark.mcilroy@outlook.com")
//			$system_user = "yes";
//		else
//			$system_user = "no";
			
			/* create profile  */

	
	$query = "INSERT INTO task (";
	$query .= "date_received, ";
	$query .= "date_quote_accepted, ";
	$query .= "date_requested_by, ";
	$query .= "date_completed, ";
	$query .= "client_names, ";
	$query .= "paraplanner, ";
	$query .= "checked_by, ";
	$query .= "client_type, ";
	$query .= "task_type, ";
	$query .= "status, ";
	$query .= "number_of_strategies, ";
	$query .= "estimated_hours, ";
	$query .= "quoted_price, ";
	$query .= "completed_hours, ";
	$query .= "checking_minutes, ";
	$query .= "rework_hours, ";
	$query .= "practice, ";
	$query .= "adviser1, ";
	$query .= "adviser2, ";
	$query .= "adviser3, ";
	$query .= "invoice_number, ";
	$query .= "notes, ";
	$query .= "date_created, ";
	$query .= "date_modified) VALUES (";

	$query .= "'".$sql_date_received."', ";
	$query .= "'".$sql_date_quote_accepted."', ";
	$query .= "'".$sql_date_requested_by."', ";
	$query .= "'".$sql_date_completed."', ";
	$query .= "'".$client_names."', ";
	$query .= "'".$paraplanner."', ";
	$query .= "'".$checked_by."', ";
	$query .= "'".$client_type."', ";
	$query .= "'".$task_type."', ";
	$query .= "'".$status."', ";
	$query .= "'".$number_of_strategies."', ";
	$query .= "'".$estimated_hours."', ";
	$query .= "'".$quoted_price."', ";
	$query .= "'".$completed_hours."', ";
	$query .= "'".$checking_minutes."', ";
	$query .= "'".$rework_hours."', ";
	$query .= "'".$practice."', ";
	$query .= "'".$adviser1."', ";
	$query .= "'".$adviser2."', ";
	$query .= "'".$adviser3."', ";
	$query .= "'".$invoice_number."', ";
	$query .= "'".$notes."', ";
	$query .= "'".$date_created."', ";
	$query .= "'".$date_modified."')";
	
	
//	$query = "INSERT INTO profile (handle, short_intro, interests, general_comments, looking_for, city, country, email, password, date_of_birth, marital_status, 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', '$marital_status ', //'$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', //'$system_user', '$source_referral' )";

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

//		$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 );

	
			/* 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( "Academic Dating", "noreply@academicdating.net", "mark.mcilroy@outlook.com", "Academic Dating", "noreply@academicdating.net", "Academic Dating new member, country=".$country, "", "" );

			/* log the source of the new member */

//		log_conversion( $cxn );

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

	}
	else
	if ($newtask == "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'];

			
//		$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."', marital_status = '".$marital_status."', 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_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;
		$query = "UPDATE task SET ";

		$query .= "date_received = '".$sql_date_received."', ";
		$query .= "date_quote_accepted = '".$sql_date_quote_accepted."', ";
		$query .= "date_requested_by = '".$sql_date_requested_by."', ";
		$query .= "date_completed = '".$sql_date_completed."', ";
		$query .= "client_names = '".$client_names."', ";
		$query .= "paraplanner = '".$paraplanner."', ";
		$query .= "checked_by = '".$checked_by."', ";
		$query .= "client_type = '".$client_type."', ";
		$query .= "task_type = '".$task_type."', ";
		$query .= "status = '".$status."', ";
		$query .= "number_of_strategies = '".$number_of_strategies."', ";
		$query .= "estimated_hours = '".$estimated_hours."', ";
		$query .= "quoted_price = '".$quoted_price."', ";
		$query .= "completed_hours = '".$completed_hours."', ";
		$query .= "checking_minutes = '".$checking_minutes."', ";
		$query .= "rework_hours = '".$rework_hours."', ";
		$query .= "practice = '".$practice."', ";
		$query .= "adviser1 = '".$adviser1."', ";
		$query .= "adviser2 = '".$adviser2."', ";
		$query .= "adviser3 = '".$adviser3."', ";
		$query .= "invoice_number = '".$invoice_number."', ";
		$query .= "notes = '".$notes."', ";
//		$query .= "date_created = '".$date_created."', ";
		$query .= "date_modified = '".$date_modified."'";

		$query .= " WHERE id = ".$id;
		
		$result = run_query( $query, $cxn );

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

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

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

	$_SESSION['date_received'] = "";
	$_SESSION['date_quote_accepted'] = "";
	$_SESSION['date_requested_by'] = "";
	$_SESSION['date_completed'] = "";
	$_SESSION['client_names'] = "";
	$_SESSION['paraplanner'] = "";
	$_SESSION['checked_by'] = "";
	$_SESSION['client_type'] = "";
	$_SESSION['task_type'] = "";
	$_SESSION['status'] = "";
	$_SESSION['number_of_strategies'] = "";
	$_SESSION['estimated_hours'] = "";
	$_SESSION['quoted_price'] = "";
	$_SESSION['completed_hours'] = "";
	$_SESSION['checking_minutes'] = "";
	$_SESSION['rework_hours'] = "";
	$_SESSION['practice'] = "";
	$_SESSION['adviser1'] = "";
	$_SESSION['adviser2'] = "";
	$_SESSION['adviser3'] = "";
	$_SESSION['invoice_number'] = "";
	$_SESSION['notes'] = "";
	$_SESSION['date_created'] = "";
	$_SESSION['date_modified'] = "";
		
		
//	$_SESSION['handle'] = "";
//	$_SESSION['in_relationship'] = "";




	if ($newtask == "true")
        {
//            $_SESSION['id'] = $new_id;


        echo "<br>";

        echo "Task saved successfully.<br><br>";
    
	    echo show_home_link();



//            generate_subscription_button( $custom_subs, button_filename( "activate_membership" ));

//            echo "<br>";
//            echo "<br>";
//            echo "<br>";

//            show_ads( $cxn, true, true );
        }
        else
        {
            echo "<br>";

//            echo "Profile saved successfully.<br><br>";

	    echo show_home_link();
        }

	
	function check_number_chars( $price )
	{
		$ok = true;

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

			if (! ($ch >= "0" && $ch <= "9") && $ch != ".")
				$ok = false;
		}

		return ($ok);
	}
	
?>