<?php

	include "library.php";

	$cxn = start_code();


	
			/* get variables */

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

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

	$listing_id = val( $_GET, "listing_id" );
	$_SESSION['listing_id'] = $listing_id;
	
			
		/* check parameters */

	crash_if_missing( $newlisting, "newlisting missing in edit_listing", $cxn );


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

	if ($account_id == "")
	{
		$_SESSION['nextpage'] = url_encode( "edit_listing.php", "newlisting", $newlisting );

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

		exit();
	}


	$query = "SELECT email_address_verified FROM account WHERE id = ".$account_id;

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

	if (query_num_rows($result) > 0)
	{
		$data = get_row( $result );
		
		if ($data['email_address_verified'] == "no")
		{
			echo "You must click on the link in the verification email before you can post listings<br><br>";
		
			echo show_home_link();
			
			exit();
		}
	}


		/* this form edit window operates in three modes - a blank screen for a new
		account, editing retrieved data for editing a account, 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 */


	if ($newlisting == "true")
	    page_header( "Create Listing", $cxn );
    else
	    page_header( "Edit Listing", $cxn );


	initialise_screen_data( $listing_id, $newlisting, "pbfs_listing", "listing", $validation_error,  $screen_data, $screen_data_error, $cxn );


	if (val( $_SESSION, "validation_errors" ) != "")
		echo red( "<b>Please correct the errors below</b><br><br>".$_SESSION['validation_errors']."<br><br>" );



	form_start( "save_listing_go.php" );
	
//	echo "<form action='".url_encode( "save_all_data_go.php", "formname", $form_name, "tabletitle", $table_title, "newrecord", $newrecord  )."' method='POST'><table>\n";

	write_yellow_input_boxes();
	

	generate_screen_fields_one_pane( $newlisting, "pbfs_listing", "false", $screen_data, $screen_data_error, $cxn );
			

	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>";
	
	show_green_button_submit( "Save", 80 );
	
	echo "</td></tr>";	

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


//	if ($newlisting == "false")
//		show_green_button( "Delete account", 160, "delete_account.php" );		


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

	form_end();

?>