<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_ATTENDANCE_ENTRY';
if (!@$_GET['popup'])
	$path_to_root = "..";
else	
	$path_to_root = "../..";

include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/includes/inventory.inc");
include_once($path_to_root . "/includes/data_checks.inc");
include_once($path_to_root . "/payroll/includes/payroll_db.inc");
include_once($path_to_root . "/payroll/includes/attendance_calculation.inc");
include_once($path_to_root . "/payroll/includes/leave_calculation.inc");
include_once($path_to_root . "/includes/db/module_settings_db.inc");
include_once($path_to_root . "/includes/db/module_settings_db.inc");


$week_start_date = "";
$week_end_date = "";
$step = 5;

$js = "";

if ($SysPrefs->use_popup_windows) {
	$js .= get_js_open_window(900, 500);
}

if (user_use_date_picker()) {
	$js .= get_js_date_picker();
}

if (!@$_GET['popup'])
    page(_($help_context = "Attendance Entry"), @$_GET['popup'], false, "", $js);

//----------------------------------------------------------------------------------------
simple_page_mode(true);
if (isset($_GET['employee_code']))
{
	$_POST['employee_code'] = $_GET['employee_code'];
}

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

$ot_rounddown_value = get_module_parameter_value("payroll", "ot_rounddown_value");
$deficit_roundup_value = get_module_parameter_value("payroll", "deficit_roundup_value");
$default_from_time1 = get_module_parameter_value("payroll", "from_time1");
$default_to_time1 = get_module_parameter_value("payroll", "to_time1");
$use_defailt_times = get_module_parameter_value("payroll", "use_defailt_times");

$payroll_module_parameters['ot_rounddown_value'] = $ot_rounddown_value;
$payroll_module_parameters['deficit_roundup_value'] = $deficit_roundup_value;
$payroll_module_parameters['default_from_time1'] = $default_from_time1;
$payroll_module_parameters['default_to_time1'] = $default_to_time1;
$payroll_module_parameters['use_defailt_times'] = $use_defailt_times;

if(!isset($_SESSION['attendance_search_employee_code'])){
    set_session_variable('attendance_search_employee_code', '');
}

if(isset($_POST['search_employee_code'])){
    set_session_variable('attendance_search_employee_code', $_POST['search_employee_code']);
}

if(!isset($_SESSION['attendance_search_period'])){
    set_session_variable('attendance_search_period', '');
}

if(isset($_POST['search_period'])){
    set_session_variable('attendance_search_period', $_POST['search_period']);
}

if(!isset($_SESSION['attendance_search_days'])){
    set_session_variable('attendance_search_days', '');
}

if(isset($_POST['search_days'])){
    set_session_variable('attendance_search_days', $_POST['search_days']);
}

if(!isset($_SESSION['attendance_entries_display_employee_position'])){
    $_SESSION['attendance_entries_display_employee_position'] = 0;
}

if(!isset($_SESSION['attendance_entries_employee_count'])){
    $_SESSION['attendance_entries_employee_count'] = 0;
}

if (isset($_POST['recalculate_selected_records']) || isset($_POST['update_selected_records']) || isset($_POST['release_for_approval']))
{
    $error_messages = "";
    
    get_filtering_dates();
    
    $emp_result = get_employees(get_session_variable('attendance_search_employee_code'));

    while ($emp_row = db_fetch($emp_result))
    {
        process_record(($_POST['week_start_date']), ($_POST['week_end_date']), $emp_row['employee_code'], ($_POST['search_days']));
    }
    $a=0;
}

function process_record($week_start_date, $week_end_date, $employee_code, $search_days){
    
    $error_messages = "";

    $result = get_period_attendance_records($week_start_date, $week_end_date, $employee_code, $search_days);

    while ($myrow = db_fetch($result))
    {
        $cid = $myrow["cid"]."_".$employee_code;
        $data = '';
        $data['single_record'] = '0';
        
        if((isset($_POST["selected_".$cid])) &&
                        ($_POST["selected_".$cid] == true)){

                $data['selected_id'] = $cid;
                $data['employee_code'] = $employee_code;
                $data['shift_id'] = $_POST['shift_id'.$cid];
                $data['calendar_work_time_code'] = $myrow['work_time_code'];
                
                if($myrow["id"]){
                    $data['aid'] = $myrow["id"];
                    $data['mode'] = "UPDATE_ITEM";
                    
                    $from_date1 = sql2date(substr($myrow["from_date_time1"],0,10));
                    $from_time1 = $_POST['from_date_time1'.$cid];
                    $to_date1 = sql2date(substr($myrow["to_date_time1"],0,10));
                    $to_time1 = $_POST['to_date_time1'.$cid];

//                    $from_date2 = sql2date(substr($myrow["from_date_time2"],0,10));
//                    $from_time2 = $_POST['from_date_time2'.$cid];
//                    $to_date2 = sql2date(substr($myrow["to_date_time2"],0,10));
//                    $to_time2 = $_POST['to_date_time2'.$cid];

                    $data['from_date1'] = $from_date1;
                    $data['from_time1'] = $from_time1;
                    $data['to_date1'] = $to_date1;
                    $data['to_time1'] = $to_time1;

//                    $data['from_date2'] = $from_date2;
//                    $data['from_time2'] = $from_time2;
//                    $data['to_date2'] = $to_date2;
//                    $data['to_time2'] = $to_time2;
                }
                else{
                    $data['mode'] = "ADD_ITEM";
                    
                    $data['from_date1'] = sql2date($myrow["date"]);
                    $data['from_time1'] = $_POST['from_date_time1'.$cid];
                    $data['to_date1'] = sql2date($myrow["date"]);
                    $data['to_time1'] = $_POST['to_date_time1'.$cid];

//                    $data['from_date2'] = sql2date($myrow["date"]);
//                    $data['from_time2'] = $_POST['from_date_time2'.$cid];
//                    $data['to_date2'] = sql2date($myrow["date"]);
//                    $data['to_time2'] = $_POST['to_date_time2'.$cid];
                }
                
                $data['task_id'] = $_POST['task_id'.$cid];
                $data['out_of_office_time1'] = $_POST['out_of_office_time1'.$cid];
                $data['shift_id'] = $_POST['shift_id'.$cid];
                $data['deducted_deficit_time'] = $_POST['deducted_deficit_time'.$cid];
                $data['granted_ot_time'] = $_POST['granted_ot_time'.$cid];
                $data['rate_table_id'] = $_POST['rate_table_id'.$cid];
                $data['comment'] = "";
                
                if(isset($_POST['leave_time_code'.$cid])){
                    $data['leave_time_code'] = $_POST['leave_time_code'.$cid];
                }
                else{
                    $data['leave_time_code'] = -1;
                }
                $data['leave_comment'] = "";
                
                $data['err_msg_prefix'] = 1;
                
                $sar_msg = save_attendance_record($data);  
                
                if($sar_msg['passed'] == false){
                    $error_messages .= $myrow["date"]." - ".$sar_msg['message'].PHP_EOL;
                }
                
//                unset($_POST['editing_employee_code']);
//                unset($_POST['editing_shift_id']);
//                unset($_POST['editing_from_date1']);
//                unset($_POST['editing_from_time1']);
//                unset($_POST['editing_to_date1']);
//                unset($_POST['editing_to_time1']);
//                unset($_POST['editing_from_date2']);
//                unset($_POST['editing_from_time2']);
//                unset($_POST['editing_to_date2']);
//                unset($_POST['editing_to_time2']);
//                unset($_POST['editing_granted_ot_time']);
//                unset($_POST['editing_deducted_deficit_time']);
//                unset($_POST['editing_shift_id']);
//                unset($_POST['editing_rate_table_id']); 
//                unset($_POST['editing_leave_time_code']);

            }
        
    }
    
    if($error_messages != ""){
        display_error($error_messages);
    }
    else{
        display_notification("Selected records have been updated");
    }
}

function add_or_update_item($selected_id,$Mode)
{

    if(isset($_POST["aid".$selected_id])){
        $data['aid'] = $_POST["aid".$selected_id];
    }
    else {
        $data['aid'] = 0;
    }
    $data['single_record'] = '1';
    $data['selected_id'] = $selected_id;
    $data['mode'] = $Mode;
    
    $data['task_id'] = $_POST['editing_task_id'];
        
    $data['employee_code'] = $_POST['editing_employee_code'];
    
    $data['from_date1'] = $_POST['editing_from_date1'];
    $data['from_time1'] = $_POST['editing_from_time1'];
    
    $data['to_date1'] = $_POST['editing_to_date1'];
    $data['to_time1'] =  $_POST['editing_to_time1'];

    $data['shift_id'] = $_POST['editing_shift_id'];
    $data['rate_table_id'] = $_POST['editing_rate_table_id'];
    $data['out_of_office_time1'] = $_POST['editing_out_of_office_time1'];
    $data['granted_ot_time'] = $_POST['editing_granted_ot_time'];
    $data['deducted_deficit_time'] = $_POST['editing_deducted_deficit_time'];
    $data['comment'] = $_POST['editing_comment'];

    $data['leave_time_code'] = $_POST['editing_leave_time_code'];
    $data['leave_comment'] = $_POST['editing_leave_comment'];
    
    $data['err_msg_prefix'] = 1;
    
    $result = get_period_attendance_records(date2sql($data['from_date1']), date2sql($data['to_date1']), $data['employee_code'], $_POST['search_days']);
    $row = db_fetch($result);
    if($row){
        $data['aid'] = $row['id'];
        $data['calendar_work_time_code'] = $row['work_time_code'];
    }
    
    return save_attendance_record ($data);
}

function save_attendance_record ($data)
{
    
    $wa_msg = write_attendance_record($data);
    
    return $wa_msg;

}


if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM')
{
    
    $add_or_update_item_msg = add_or_update_item($selected_id,$Mode);
    
    if ($Mode == 'ADD_ITEM') 
    {
        if($add_or_update_item_msg['passed'] == true)
            display_notification(_("Attendance record has been added."));
        else {
            display_error($add_or_update_item_msg['message']);
        }
    }

    if ($Mode == 'UPDATE_ITEM') 
    {
        if($add_or_update_item_msg['passed'] == true)
            display_notification(_("Attendance record has been updated."));
        else {
            display_error($add_or_update_item_msg['message']);
        }
    } 
    
    $Mode = 'RESET';

}

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


if($Mode == 'Delete')
{
    $rec = "";
    $aid = 0;
    
    if($selected_id > 0 && isset($_POST['aid'.$selected_id])){
        $aid = $_POST['aid'.$selected_id];
        $rec = get_attendance_record($aid);        
    }
    
    if($rec != ""){
        if(($rec['ot_status_code'] == 'N' || $rec['ot_status_code'] == 'U') &&
                ($rec['deficit_status_code'] == 'N' || $rec['deficit_status_code'] == 'U')){
            delete_attendance_record($aid);
        }
        else{
            display_error("Selected record is not editable and can not be deleted");
        }
    }
    else{
        display_error("Record id is not selected for record deletion");
    }

    
    $Mode = 'RESET';

}

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


function clear_data()
{
	unset($_POST['long_description']);
}




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


//function get_next_date($date,$days = 1)
//{
//    $date = strtotime("+".$days." days", strtotime($date));
//    return  date("m/d/Y", $date);
//}



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

function display_period_attendance_records($week_start_date, $week_end_date, $employee_code, $search_days)
{    
    global $ot_rounddown_value, $deficit_roundup_value, $payroll_module_parameters;
    
    $result = get_period_attendance_records($week_start_date, $week_end_date, $employee_code, $search_days);

    if (db_num_rows($result) == 0)
    {
        display_note(_("There are no attendance entries for the selected employee"));
    }
    else
    {
        start_table(TABLESTYLE, "width=98%");

            $th = array(_("Date"),_("Work Time"),
                        _("Task"),
                        _("In"), _("Out"), 
                        //_("Out Of Office Time"),
                        _("Entered Duration"),
                        _("Shift Name"), 
                        _("Counted Duration"), 
                        _("Calc. OT Time"),
                        _("Granted OT Time"), 
                        _("Payroll OT Time"),
                        _("Rate Table"),
                        _("Calc. Deficit Time"), _("Deducted Deficit Time"),
                        //_("Leave Time"), 
                        _("Apply Leave"),
                        "","","");

        table_header($th);
        
        $summary = "";
        $last_header_date ="";

        $k = $j = 0; //row colour counter
        
        $summary['rec_count'] = 0;

        $summary['total_out_of_office_time_s'] = 0;
        $summary['total_entered_duration_s'] = 0;
        $summary['total_counted_duration_s'] = 0;

        $summary['total_ot_time_calculated_s'] = 0;
        $summary['total_ot_time_granted_s'] = 0;
        $summary['total_payroll_ot_time_s'] = 0;

        $summary['total_deficit_time_calculated_s'] = 0;
        $summary['total_deficit_time_deducted_s'] = 0;  

        while ($myrow = db_fetch($result))
        {
            alt_table_row_color($k);

            $cid = $myrow["cid"]."_".$employee_code;
            
            if(isset($myrow["id"])){
                hidden("aid".$cid,$myrow["id"]);
            }

            //label_cell($cal['date']);
            $date_s = strtotime($myrow["date"]);
            $att_id = 0;
            if(isset($myrow['id'])){$att_id = $myrow['id'];}
            
            $link = $date_s."~".$att_id."~".$employee_code;
            
            label_cell(get_trans_view_str(ST_ATTENDANCE, $link, $myrow["date"].", ".date('l',$date_s)));
            //label_cell(get_trans_view_str(ST_ATTENDANCE, $cal['id']));
            //label_cell(get_trans_view_str(ST_ATTENDANCE, $cal['id']."~|member", "label"));

            display_record($cid, $employee_code, $myrow, $summary, $payroll_module_parameters);

            end_row();

            $date = $myrow["date"];
            $y = date("Y", strtotime($date));
            $m = date("m", strtotime($date));
            $d = date("d", strtotime($date));
            $day_name = strtolower(date("l", mktime(0,0,0,$m,$d,$y)));
        }
        
        display_summary($summary);
        
        end_table();
        
    }
}

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

if ($Mode == 'RESET')
{
	$selected_id = -1;
}

//if (list_updated('employee_code')) 
//	$Ajax->activate('Entitlements');
//--------------------------------------------------------------------------------------------------

if (!@$_GET['popup'])
	start_form();

get_filtering_dates();

br(2);
                        
date_cells("Select Week", 'selected_date', '', null, null, null, null, null, false);
three_value_list_cells('Period', 'search_period', null, 'Week', 'W', 'Month', 'M', 'Day', 'D');
three_value_list_cells('Days', 'search_days', null, 'All', 'A', 'Entered', 'E', 'Not Entered', 'N');
emoplyees_list_cells(_(" Employee:"), 'search_employee_code', null,'', false, false);
submit_cells('Search', _("Search"), '', '', 'default');

br(2);

submit("search_start", "First" );
submit("search_previous", "Previous" );
submit("search_next", "Next" );
submit("search_last", "Last" );
br(2);
$Ajax->activate('attendance');

div_start('attendance');



//$emp_result = get_employees();
if(isset($_POST['search_start'])){

    $position_start = $_SESSION['attendance_entries_display_employee_position'] = 0;
    
    $emp_result = get_employees_in_batches($position_start,$step, get_session_variable('attendance_search_employee_code'));
}
elseif(isset($_POST['search_previous'])){
    $position_now = $_SESSION['attendance_entries_display_employee_position'] - $step;
    if($position_now < 0){
        $position_now = 0;
    }
    
    $_SESSION['attendance_entries_display_employee_position'] = $position_now;

    $emp_result = get_employees_in_batches($position_now,$step, get_session_variable('attendance_search_employee_code'));
    
}
elseif(isset($_POST['search_next'])){
    $position_now = $_SESSION['attendance_entries_display_employee_position'] + $step;
        
    if($_SESSION['attendance_entries_employee_count'] < $position_now){
        $position_now -= $step;
    }
    
    $_SESSION['attendance_entries_display_employee_position'] = $position_now;

    $emp_result = get_employees_in_batches($position_now,$step, get_session_variable('attendance_search_employee_code'));
    
}
elseif(isset($_POST['search_last'])){
    
    
    $times = floor(($_SESSION['attendance_entries_employee_count'] / $step));
    $position_last = $times * $step;
    
    $_SESSION['attendance_entries_display_employee_position'] = $position_last;

    $emp_result = get_employees_in_batches($position_last,$step, get_session_variable('attendance_search_employee_code'));
    
}
else{
    if(isset($_POST['recalculate_selected_records']) || isset($_POST['update_selected_records']) || isset($_POST['release_for_approval'])){
        $position0 = $_SESSION['attendance_entries_display_employee_position'];
    }
    else{
        $position0 = $_SESSION['attendance_entries_display_employee_position'] = 0;
    }
    

    $row = get_employee_count(get_session_variable('attendance_search_employee_code'));

    if($row){
        $_SESSION['attendance_entries_employee_count'] = $row['no_of_employees'];
    }
    else{
        $_SESSION['attendance_entries_employee_count'] = 0;
    }
    
    $emp_result = get_employees_in_batches($position0,$step, get_session_variable('attendance_search_employee_code'));
}

while ($emp_row = db_fetch($emp_result))
{
    display_heading("[".$emp_row['employee_code']."] - ". $emp_row['initials']." ".$emp_row['last_name']." (". $emp_row['first_name'].")");

    display_period_attendance_records(($_POST['week_start_date']), ($_POST['week_end_date']), $emp_row['employee_code'], ($_POST['search_days']));

    br();
}

submit("recalculate_selected_records", "Recalculate Selected Records", true );

submit("update_selected_records", "Update Selected Records", true );
    
submit("release_for_approval", "Release Selected Records for approval" );

div_end();

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



hidden('selected_id', $selected_id);


//if($selected_id>0)
//{
//    $aid = $_POST['aid'.$selected_id];
//}

if(strlen($selected_id) > 3 && isset($_POST['aid'.$selected_id]))
{
    //$aid = $_POST[$selected_id];
    $underscore_pos = strpos($selected_id, "_");
    $aid = $_POST['aid'.$selected_id];
    
    $ar_row = get_attendance_record($aid);
    
    $_POST["editing_shift_id"] = $ar_row["shift_id"];
    
    $_POST["editing_employee_code"] = $ar_row["employee_code"];
    $_POST["editing_employee_name"] = $ar_row["last_name"];
    
    $from_date1 = sql2date($ar_row["from_date_time1"]);
    $from_time1 = sql2time($ar_row["from_date_time1"]);
        
    $_POST["editing_from_date1"] = $from_date1;
    $_POST["editing_from_time1"] = $from_time1;
    
    $to_date1 = sql2date($ar_row["to_date_time1"]);
    $to_time1 = sql2time($ar_row["to_date_time1"]);

    $_POST["editing_to_date1"] = $to_date1;
    $_POST["editing_to_time1"] = $to_time1;
    
    $_POST["editing_split_shift"] = $ar_row["split_shift"];
    
    $from_date2 = sql2date($ar_row["from_date_time2"]);
    $from_time2 = sql2time($ar_row["from_date_time2"]);
        
    $_POST["editing_from_date2"] = $from_date2;
    $_POST["editing_from_time2"] = $from_time2;
    
    $to_date2 = sql2date($ar_row["to_date_time2"]);
    $to_time2 = sql2time($ar_row["to_date_time2"]);

    $_POST["editing_to_date2"] = $to_date2;
    $_POST["editing_to_time2"] = $to_time2;
    
    $_POST["editing_out_of_office_time1"] = substr($ar_row["out_of_office_time1"], -8, 5);
    
    $_POST["editing_deducted_deficit_time"] = substr($ar_row["deducted_deficit_time"], -8, 5);
    
    $_POST["editing_granted_ot_time"] = substr($ar_row["granted_ot_time"], -8, 5);
    
    $_POST["editing_rate_table_id"] = $ar_row["rate_table_id"];
    
    $_POST["editing_comment"] = $ar_row["comment"];
    
    $_POST["editing_shift_id"] = $ar_row["shift_id"];
    
    $_POST["editing_task_id"] = $ar_row["task_id"];
    
    $_POST['editing_leave_time_code'] = "";
    $_POST["editing_leave_comment"] = "";
    
}
else
{
    $from_time1 = $payroll_module_parameters['default_from_time1'];
    $to_time1 = $payroll_module_parameters['default_to_time1'];

    $from_time2 = "00:00";
    $to_time2 = "00:00";
    
    $_POST["editing_employee_code"] = "";
    
    $_POST["editing_shift_id"] = "";
    
    $_POST["editing_from_date1"] = "";
    $_POST["editing_from_time1"] = $from_time1;    
    $_POST["editing_to_date1"] = "";
    $_POST["editing_to_time1"] = $to_time1;
    
    $_POST["editing_split_shift"] = 0;

    $_POST["editing_from_date2"] = "";
    $_POST["editing_from_time2"] = $from_time2;    
    $_POST["editing_to_date2"] = "";
    $_POST["editing_to_time2"] = $to_time2;
    
    $_POST["editing_out_of_office_time1"] = "00:00";
    
    $_POST["editing_deducted_deficit_time"] = "00:00";
    
    $_POST["editing_granted_ot_time"] = "00:00";
    
    $_POST["editing_rate_table_id"] = 0;
    
    $_POST["editing_comment"] = "";
    
    $_POST["editing_shift_id"] = 0;
    
    $_POST["editing_task_id"] = 0;
    
    $_POST['editing_leave_time_code'] = "";

    $_POST["editing_leave_comment"] = "";
}



br(3);


$Ajax->activate('controls');
div_start('controls');

if($selected_id == -1){
    start_table(TABLESTYLE2, "width=29%");

    emoplyees_list_row(_("Select an Employee:"), 'editing_employee_code', null,
      false, false, false);
    end_table(1);
}
else{
    hidden('editing_employee_code');
    hidden('editing_shift_id');
}

start_table(TABLESTYLE2);

if($selected_id > 0 ){
    label_row('Employee :', $_POST["editing_employee_code"].", ".$_POST["editing_employee_name"]);
}

table_section_title("Project Time");

tasks_all_list_row('Project Task', 'editing_task_id', null, 'Select a Task');

table_section_title("Work Time");

shift_list_row("Shift:", 'editing_shift_id', $_POST['editing_shift_id'], 'Use Shift Schedule Shift');

date_row("In Date:", 'editing_from_date1',$_POST["editing_from_date1"]);
time_row("In Time (24 hr): ", "editing_from_time1", $_POST["editing_from_time1"], 5);

date_row("Out Date:", 'editing_to_date1',$_POST["editing_to_date1"]);
time_row("Out Time (24 hr): ", "editing_to_time1", $_POST["editing_to_time1"], 5);

time_row("Out Of Office Time (hh:mm): ", "editing_out_of_office_time1", $_POST["editing_out_of_office_time1"], 5);

//table_section_title("Split Shift");
//
////yesno_list_row("Split Shift", "split_shift", $_POST['editing_split_shift'], "Yes", "No");
//
//date_row("In2 Date:", 'editing_from_date2',$_POST["editing_from_date2"]);
//time_row("In2 Time (24 hr): ", "editing_from_time2", $_POST["editing_from_time2"], 5);
//
//date_row("Out2 Date:", 'editing_to_date2',$_POST["editing_to_date2"]);
//time_row("Out2 Time (24 hr): ", "editing_to_time2", $_POST["editing_to_time2"], 5);

table_section_title("Deficit and Over Time");

time_row("Granted OT Time (hh:mm): ", "editing_granted_ot_time", $_POST["editing_granted_ot_time"], 5);

//amount_row("Rate Table:", "rate_table_id", null, null, null, 2);
value_list_row('Rate Table:', 'editing_rate_table_id', 'VALUE_TABLES', $_POST['editing_rate_table_id'],"Use Shift Rate Table",false);

time_row("Deducted Deficit (hh:mm): ", "editing_deducted_deficit_time", $_POST["editing_deducted_deficit_time"], 5);

textarea_row(_("Comment:"), 'editing_comment', $_POST["editing_comment"], 20, 3);

table_section_title("Apply Leave");

value_list_code_row("Leave Time", 'editing_leave_time_code', 'HOLIDAY_TIME', null ,"",false);

textarea_row(_("Comment:"), 'editing_leave_comment', $_POST["editing_leave_comment"], 20, 3);


end_table(1);
div_end();

submit_add_or_update_center($selected_id == -1, '', 'both');



if (!@$_GET['popup'])
{
	end_form();
	end_page(@$_GET['popup'], false, false);
}
?>
