<?php

	include "library.php";

	$cxn = start_code( true );

	$cxn_common = db_common_login();
	
	
	$newrecord = val( $_GET, "newrecord" );

	$form_name = "financial_planning_practices";
		
	if ($newrecord == "false")
	{
//		if (val( $_POST, "id" ) != "")	
//			$id = val( $_POST, "id" );		
//		else

		if (val( $_GET, "id" ) != "")
			$id = val( $_GET, "id" );
		else

		$id = val( $_SESSION, "id" );
	
		if ($id == "")
			$id = 1;
	}

	else
		$id = "";
		
	$_SESSION['id'] = $id;
	$_SESSION['newrecord'] = $newrecord;

	
	$domain = $_SESSION['domain'];

	$ip_country = get_ip_country( $cxn );
	$ip_country = str_replace( "'", "", $ip_country );
	$country = rawurlencode( $ip_country );

	$query = "SELECT * FROM ".common_data_database_name.".screen_definition_main WHERE form_name = '".$form_name."'";
		
	$result = run_query( $query, $cxn );

	$data = get_row( $result );
	
	$page_heading = $data['page_heading'];
	
	$table_name = $data['table_name'];


	if ($newrecord == "false")
		page_header( "Edit ".$page_heading, $cxn, $cxn_common );			
	else
		page_header( "Create ".$page_heading, $cxn, $cxn_common );			
		

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

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


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


	initialise_screen_data( $id, $newrecord, $form_name, $table_name, $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>" );

	$query = "SELECT * FROM ".common_data_database_name.".screen_definition WHERE form_name = '".$form_name."' ORDER BY field_order";
	
	$result = run_query( $query, $cxn );

	$has_right_pane = "false";
	
	for ($i=0; $i < query_num_rows( $result ); $i++)
	{
		$data = get_row( $result );
		
		if ($data['right_side_pane'] == "yes")
			$has_right_pane = "true";
	}


	if ($has_right_pane == "true")
	{
		echo "<form id='form' name='form' action='".url_encode( "save_fp_practice_go.php", "formname", $form_name, "newrecord", $newrecord  )."' method='POST'><table width='100%'>\n";		

		write_yellow_input_boxes();

		echo "<tr><td></td><td></td><td></td><td rowspan=20>";
	
		echo "<table>";
	
		generate_screen_fields_one_pane( $newrecord, $form_name, "true", $screen_data, $screen_data_error, $cxn );

		echo "</table>";
		echo "</td><td width='20%'></td></tr>";
	
		generate_screen_fields_one_pane( $newrecord, $form_name, "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>";
	}
	else
	{
//		echo "<form id='form' name='form' action='".url_encode( "save_fp_practice_go.php", "formname", $form_name, "newrecord", $newrecord  )."' method='POST'><table>\n";
		form_start( "save_fp_practice_go.php", "formname", $form_name, "newrecord", $newrecord  );
	
		write_yellow_input_boxes();
	
		generate_screen_fields_one_pane( $newrecord, $form_name, "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 colspan='2' ><br>";


	echo "<script type='text/javascript'>";
	echo "";	
	echo "function set_date_to_today()";
	echo "{";
	echo "       var today = new Date();";
	echo "";
	echo "       var dd = today.getDate();";
	echo "       var mm = today.getMonth()+1;";
	echo "       var yyyy = today.getFullYear();";
	echo "";
	echo "       if (dd <= 9)";
	echo "           dd = '0' + dd;";
	echo "";
	echo "       if (mm <= 9)";
	echo "           mm = '0' + mm;";
	echo "";	
	echo "        var todays_date = dd + \"/\" + mm + \"/\" + yyyy;";
	echo "";		
	echo "        document.getElementById( 'last_contact' ).value = todays_date;";
	echo "}";
	echo "</script>";

//	echo " alert( 'x' );";		
	

	show_green_button_submit( "Save", 80 );
	
//	echo "</td></tr>";
//
//	echo "<tr><td>&nbsp;</td></tr>";
//	
//	echo "<tr><td></td><td align='center'>";

        echo "&nbsp;";
        
	$this_size = "100";
	$text = "Set to today";
	
	if (! low_res_screen())
		echo "<div onclick='set_date_to_today();' class='greenbuttondesktop' style='line-height: 25px; width:".$this_size.";' align='center'>".$text."</div> ";
	else
		echo "<div onclick='set_date_to_today();'  class='greenbuttonmobile' style='width:".$this_size.";' align='center'>".$text."</div> ";			


        echo "&nbsp;";
        
	show_green_button( "Previous", 100, "fp_practice_next_prev.php", "next", "false" );
        
        echo "&nbsp;";
        
	show_green_button( "Next", 100, "fp_practice_next_prev.php", "next", "true" );

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

	
	form_end();

		
	
		// search ID form

	echo "<table><tr><td>";
			
	echo "<form id='form2' name='form2' action='".url_encode( "fp_practice_search.php" )."' method='POST'><table>";

	form_text( "Search ID", "search_id", 10, "" );
	
	echo "<tr><td></td><td>";
			
	show_green_button_submit( "Search ID", 110, "form2" );
	
	echo "</td></tr>";

	form_end();

//        echo "<br><br><br>";
	
	echo "</td><td>";
	
	
		// search name form
		
	echo "<form id='form3' name='form2' action='".url_encode( "fp_practice_search_name.php" )."' method='POST'><table>";
        
	form_text( "Search name", "search_name", 20, "" );
	
	echo "<tr><td></td><td>";
			
	show_green_button_submit( "Search Name", 130, "form3" );
	
	echo "</td></tr>";

	form_end();

	echo "</td></tr></table>";
	
			
	
//	$s1 = "This online calculator is provided as a free service by Melbourne Paraplanning Services Pty Ltd (ABN 90 611 692 730).<br><br>";
	
//      $s1 .= "<br>";

//	echo blue( $s1 );

        echo "<br>";
        
//	echo black( "<i>To calculate the remaining loan term for higher repayments ".show_link( "click here", "remaining_term_inputs.php" )."</i><br>" );
	
        echo "<br><br><br><br>";

//        show_ads( $cxn, true, true );


?>
