function analyse_examination_request($post)
{
    $requested_examination = array();
    foreach ($post as $key => $value) {
        $exploded_result = explode("|", $key);
        //print_r($exploded_result);
        if ($exploded_result[0] == 'examination' && $value == 'yes') {
            if (in_array($exploded_result[1], $requested_examination) === FALSE) {
                $requested_examination[] = $exploded_result[1];
            }
        }
        if ($exploded_result[0] == 'profile' && $value == 'yes') {
            $link = start_nchsls();
            $sql = 'select * from profile where profile=\'' . $exploded_result[1] . '\'';
            $result = mysql_query($sql, $link);
            while ($profile_row = mysql_fetch_assoc($result)) {
                foreach ($profile_row as $key => $value) {
                    if ($key != 'profile' && $key != 'sample_type' && $key != 'preservative') {
                        if ($value > 0) {
                            $temp_code = get_code_from_id($value);
                            if (in_array($temp_code, $requested_examination) === FALSE) {
                                $requested_examination[] = $temp_code;
                            }
                        }
                    }
                }
            }
        }
    }
    return $requested_examination;
}
function insert_required_samples($post)
{
    echo '<button type=button  style="background:lightpink;" id=showhide onclick="showhide()">hide</button>';
    $req = analyse_examination_request($post);
    if (count($req) == 0) {
        return FALSE;
    }
    //echo '<pre>';
    //echo 'Reuqested Examinations';
    //print_r($req);
    //echo '</pre>';
    $sample_id = confirm_next_sample_id($post['selected_location']);
    echo '<div id=display>';
    $sample_array = array();
    if (isset($sample_id)) {
        $sample_array['sample_id'] = $sample_id;
        $sample_array['patient_id'] = 'SUR/' . date("y") . '/' . str_pad($post['mrd'], 8, '0', STR_PAD_LEFT);
        $sample_array['patient_name'] = $post['name'];
        $sample_array['clinician'] = $post['selected_department'];
        $sample_array['unit'] = $post['selected_unit'];
        $sample_array['location'] = $post['selected_location'];
        $sample_array['sample_type'] = $post['selected_sample_type'];
        $sample_array['preservative'] = 'None';
        $sample_array['sample_details'] = $post['selected_sample_details'];
        $sample_array['urgent'] = 'N';
        $sample_array['status'] = 'entered';
        $sample_array['sample_receipt_time'] = strftime("%Y-%m-%d %H:%M:%S");
        $sample_array['sample_collection_time'] = strftime("%Y-%m-%d %H:%M:%S");
        if (save_sample($sample_array) === FALSE) {
            echo '<h4>Can not insert the last ID found. Retry</h4>';
            return FALSE;
        }
        $ex_list = '';
        foreach ($req as $key => $value) {
            insert_single_examination($sample_array['sample_id'], $value);
            $ex_list = $ex_list . ',' . get_code_from_id($value);
        }
        echo '<table style="background:lightblue;"><tr><td>';
        echo '<form method=post target=_blank action=print_sample_barcode.php>';
        echo '<input type=hidden name=patient_name value=\'' . $sample_array['patient_name'] . '\'>';
        echo '<input type=hidden name=patient_id value=\'' . $sample_array['patient_id'] . '\'>';
        echo '<input type=hidden name=ex_list value=\'' . $ex_list . '\'>';
        echo '<input type=hidden name=tube value=\'S\'>';
        echo '<button  style="background:lightpink;" type=submit name=sample_id value=\'' . $sample_array['sample_id'] . '\'>Barcode-' . $sample_array['sample_id'] . '</button>';
        echo '</form>';
        echo '<form method=post target=_blank action=edit_request.php>';
        echo '<input type=hidden name=sample_id value=\'' . $sample_array['sample_id'] . '\'>';
        echo '<button style="background:lightgreen;" type=submit name=action value=edit_sample>Edit-' . $sample_array['sample_id'] . '</button>';
        echo '</form></td><td>';
        edit_sample($sample_array['sample_id'], '', 'disabled', 'no');
        edit_examination($sample_array['sample_id'], '', 'disabled');
        echo '</td></tr></table>';
    }
    echo '</div>';
}