<?php

	include "library.php";

	$cxn = start_code();

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

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

	
//	page_header( "", $cxn );



		/* check parameters */

	crash_if_missing( $newlisting, "newlisting missing in save listing", $cxn );
	crash_if_missing( $account_id, "account id missing in save listing", $cxn );
	
	if ($newlisting != "true")
		crash_if_missing( $listing_id, "listing id missing in save listing", $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");
	$todays_date = date("Y-m-d H:i:s");


	$ok = validate_form( $listing_id, $newlisting, "abfs_listing", $screen_data, $screen_data_errors, $validation_errors, $cxn );		


	if ($validation_errors != "")
	{	
				/* must happen before print output */
		jump_to_page( "edit_listing.php", "newlisting", $newlisting, "listing_id", $listing_id );
			
		exit();
	}


	page_header( "", $cxn );



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



		/* save the record to the database */

	if ($newlisting == "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 );
		                                           

		$screen_data['account_id'] = val( $_SESSION, 'account_id' );
		$screen_data['date_created'] = $date_created;
		$screen_data['date_modified'] = $date_modified;
		$screen_data['ip_country'] = $ip_country;
//		$screen_data['system_user'] = $system_user;
		$screen_data['source'] = $source;
		$screen_data['last_login'] = $todays_date;
		$screen_data['locked'] = "no";
		$screen_data['deleted'] = "no";
		$screen_data['login_attempts'] = "0";
		$screen_data['number_of_logins'] = "0";		
		$screen_data['email_address_valid'] = "no";
		$screen_data['new_photo_number'] = "0";
		$screen_data['email_verification_code'] = $email_verification_code;

//		$country = $screen_data['country']; 
		
		
		save_form_data_to_database( $listing_id, $newlisting, "abfs_listing", "listing", $screen_data, $cxn );
		
		
//		$_SESSION['id'] = "";


			/* send notification email */

//		if ($_SESSION['database'] == "academi1_academicdating")


		send_email( "Australian Businesses for Sale Platform", "noreply@academicdating.net", "mark.mcilroy@outlook.com", "Australian Businesses for Sale Platform", "noreply@academicdating.net", "Australian Businesses for Sale Platform, new listing", "", "", $cxn, "live" );


//		$query = "INSERT INTO account_activity2 (member_id, event, event_datetime) VALUES (".$new_id.", 'account 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 ($newlisting == "false")
	{
		$new_id = $listing_id;



		$screen_data['date_modified'] = $date_modified;
//		$screen_data['last_login'] = $todays_date;



			// note: don't update the email verification code or there will be mismatches when the member edits their account and saves it

		save_form_data_to_database( $listing_id, $newlisting, "abfs_listing", "listing", $screen_data, $cxn );


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


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

	}
	else
		crash( "Internal Error: invalid newlisting flag in save_listing", $cxn );


		
	if ($newlisting == "true")
        {

		echo "<br>";

		echo "Listing saved successfully.<br><br><br>";


	    echo show_home_link();



			// conversions tracking code for new adwords account admin@academicdating.net
		
		// <!-- Google Code for Academic Dating Conversion Page -->

//		echo '<script type="text/javascript">';
//		
//		echo '/* <![CDATA[ */';
//		
//		echo 'var google_conversion_id = 851976106;';
//		echo 'var google_conversion_language = "en";';
//		echo 'var google_conversion_format = "3";';
//		echo 'var google_conversion_color = "ffffff";';
//		echo 'var google_conversion_label = "JKL_COXqoHEQqr-glgM";';
//		echo 'var google_remarketing_only = false;';
//		
//		echo '/* ]]> */';
//		
//		echo '</script>';
//		echo '<script type="text/javascript" src="//www.googleadservices.com/pagead/conversion.js">';
//		echo '</script>';
//		echo '<noscript>';
//		echo '<div style="display:inline;">';
//		echo '<img height="1" width="1" style="border-style:none;" alt="" src="//www.googleadservices.com/pagead/conversion/851976106/?label=JKL_COXqoHEQqr-glgM&amp;guid=ON&amp;script=0"/>';
//		echo '</div>';
//		echo '</noscript>';



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

		// adsense ads are not permitted on control-flow, non-content pages
//            show_ads( $cxn, true, true );
        }
        else
        {
            echo "<br>";

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

	    echo show_home_link();
        }

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