<?php
/**********************************************************************
***********************************************************************/

$page_security = 'SA_PROJECTS';
$path_to_root = "../..";
include($path_to_root . "/includes/session.inc");
//include_once ($path_to_root . "/trace.php");
$js = "";
if ($SysPrefs->use_popup_windows)
	$js .= get_js_open_window(900, 500);
if (user_use_date_picker())
	$js .= get_js_date_picker();
	
page(_($help_context = "Projects"), @$_REQUEST['popup'], false, "", $js);

include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/includes/data_checks.inc");
include_once($path_to_root . "/inventory/includes/inventory_db.inc");
include_once($path_to_root . "/projects/includes/ui/projects_ui.inc");
include_once($path_to_root . "/projects/includes/db/projects_db.inc");
include_once($path_to_root . "/projects/includes/db/project_sites_db.inc");
include_once($path_to_root . "/projects/includes/db/project_approval_path_db.inc");
include_once($path_to_root . "/projects/includes/db/project_serial_categories_db.inc");
//include_once($path_to_root . "/projects/includes/db/tasks_db.inc");
include_once($path_to_root . "/includes/approval_functions.inc");
include_once($path_to_root . "/projects/includes/ui/approve_projects_ui.inc");


$user_comp = user_company();

$new_item = get_post('project_id')== '' || get_post('project_id')== -1 || get_post('cancel') || get_post('clone'); 


global $Ajax;
//------------------------------------------------------------------------------------

if (isset($_GET['project_id']))
{
	$_POST['project_id'] = $_GET['project_id'];
}

$project_id = get_post('project_id');

if (list_updated('project_id')) {
	$_POST['NewProjectID'] = $project_id = get_post('project_id');
    clear_project_data();
	$Ajax->activate('details');
	$Ajax->activate('controls');
}

if (get_post('cancel')) {
	$_POST['NewProjectID'] = $project_id = $_POST['project_id'] = '';
    clear_project_data();
	set_focus('project_id');
	$Ajax->activate('_page_body');
}
if (list_updated('category_id') || list_updated('mb_flag')) {
	$Ajax->activate('details');
}

if (isset($_GET['Approved_id'])) 
{
    $trans_no = $_GET['Approved_id'];
    
//    $project = get_project_record($trans_no);
    $message = get_session_variable('approve_project_messages');
    clear_session_variable('approve_project_messages');

    display_notification_centered($message);
    
    hyperlink_no_params("$path_to_root/projects/inquiry/unapproved_projects_inquiry.php", _("Approve another project."));

    display_footer_exit();
}


//------------------------------------------------------------------------------------


if (isset($_POST['addupdate'])) 
{ 
    save_project($new_item, $project_id);
    $Ajax->activate('_page_body');
    
}

if (get_post('clone')) {
	unset($_POST['project_id']);
	$project_id = '';
	unset($_POST['inactive']);
	set_focus('NewProjectID');
	$Ajax->activate('_page_body');
        //l('224 inside get_post ');
}

//------------------------------------------------------------------------------------

//function check_usage($project_id, $dispmsg=true)
//{
//	$msg = item_in_foreign_codes($project_id);
//
//	if ($msg != '')	{
//		if($dispmsg) display_error($msg);
//		return false;
//	}
//	return true;
//}
//
if (isset($_POST['delete']) && strlen($_POST['delete']) > 1) 
{
    remove_project($_POST['project_id']);
    $Ajax->activate('_page_body');
}


if(isset($_POST['Release_Project_For_Department_Users'])){
    $rpfa_msg = release_project_for_department_users($project_id);
    if($rpfa_msg['success'] !== true){
        display_error($rpfa_msg['message']);
    }
    else{
        set_session_variable('approve_project_messages',$rpfa_msg['message']);
        meta_forward($_SERVER['PHP_SELF'], "Approved_id=$project_id");
    }
    
}
//-------------------------------------------------------------------------------------------- 

start_form(true);

if (db_has_projects()) 
{
	start_table(TABLESTYLE_NOBORDER);
	start_row();
        project_list_cells(_("Select a project:"), 'project_id', null,
	  _('New project'), true, check_value('show_inactive'));
    
	$new_item = get_post('project_id')=='';
        
        if(get_post('project_id') == -1 || get_post('project_id') == ''){
            $new_item = TRUE;
        }


//	check_cells(_("Show inactive:"), 'show_inactive', null, true);
	end_row();
	end_table();

	if (get_post('_show_inactive_update')) {
		$Ajax->activate('project_id');
		set_focus('project_id');
	}
}
else
{
	hidden('project_id', get_post('project_id'));
}



div_start('details');

$project_id = get_post('project_id');


if ($project_id == -1){
    $project_id = '';
}

if (!$project_id){
	unset($_POST['_tabs_sel']); // force settings tab for new customer
}

tabbed_content_start('tabs', array(
		'settings' => array(_('&General settings'), $project_id),
//		'tasks' => array(_('&Tasks'), $project_id),
//        'sites' => array(_('&Sites'), $project_id),
        'approval_Path' => array(_('&Approval Paths'), $project_id),
//        'invoicing_items' => array(_('&Invoicing Items'), $project_id),
//        'billing_schedule' => array(_('&Project Billing Schedule'), $project_id),
//        'users' => array(_('&Users'), $project_id),
	));
	
	switch (get_post('_tabs_sel')) 
        {
		default:
		case 'settings':
                        project_settings($project_id); 
			break;
//		case 'tasks':
//			$_GET['project_id'] = $project_id;
//			$_GET['popup'] = 1;
//			include_once($path_to_root."/projects/manage/tasks.php");
//			break;
//        case 'sites':
//                $_GET['project_id'] = $project_id;
//                $_GET['popup'] = 1;
//                include_once($path_to_root."/projects/manage/project_sites.php");
//                break;
//        case 'invoicing_items':
//                $_GET['project_id'] = $project_id;
//                $_GET['popup'] = 1;
//                include_once($path_to_root."/projects/manage/project_invoicing_items.php");
//                break;
        case 'approval_Path':
                $_GET['project_id'] = $project_id;
                $_GET['popup'] = 1;
                include_once($path_to_root."/projects/manage/project_approval_path.php");
                break;    
//        case 'billing_schedule':
//			$_GET['project_id'] = $project_id;
//			$_GET['popup'] = 1;
//			include_once($path_to_root."/projects/manage/projects_billing_schedule.php");
//			break;
                    
//        case 'users':
//			$_GET['project_id'] = $project_id;
//			$_GET['popup'] = 1;
//			include_once($path_to_root."/projects/manage/users.php");
//			break;
	};
br();
tabbed_content_end();

div_end();


hidden('popup', @$_REQUEST['popup']);

end_form();

//------------------------------------------------------------------------------------

end_page(@$_REQUEST['popup']);


?>
