function bill_select_pos()
{
    /*
    name:
    bill_select_pos()
    returns:
    0 - no error
    1 - generic dish with no price found
    2 - internal error clearing prices
    3 - 
    other - mysql error number
    */
    global $tpl;
    if (!bill_orders_to_print($_SESSION['sourceid'])) {
        printing_choose_pos(true);
        return 0;
    }
    $tpl->set_waiter_template_file('bill_select_pos');
    $script = "<script language=\"JavaScript\" type=\"text/javascript\">\r\n\t\t\$(document).ready(function(){\r\n\t\t\t\$('#tabContent>li:gt(0)').hide();\r\n\t\t\t\$('#tabsNav li:first').addClass('active');\r\n\t\t\t\$('#tabsAndContent #tabsNav li').bind('click', function() {\r\n\t\t\t\t\$('li.active').removeClass('active');\r\n\t\t\t\t\$(this).addClass('active');\r\n\t\t\t\tvar target = \$('a', this).attr('href');\r\n\t\t\t\t\$(target).slideDown(\"fast\").siblings().slideUp(\"fast\");\r\n\t\t\t\treturn false;\r\n\t\t\t});\r\n\t\t});\t\r\n\t</script>";
    $tpl->assign('script', $script);
    $_SESSION['bill_printed'] = 0;
    if (order_found_generic_not_priced($_SESSION['sourceid'])) {
        return ERR_GENERIC_ORDER_NOT_PRICED_FOUND;
    }
    $keep_separated = bill_check_keep_separated();
    if ($keep_separated == 1) {
        $tmp = navbar_separatebills_pos('form_type', 'orders.php?command=printing_choose');
        $tpl->assign('navbar', $tmp);
    } else {
        $tmp = navbar_form_pos('form_type', 'orders.php?command=printing_choose');
        $tpl->assign('navbar', $tmp);
    }
    if ($err = bill_clear_prices($_SESSION['sourceid'])) {
        return $err;
    }
    if ($err = bill_save_session($_SESSION['sourceid'])) {
        return $err;
    }
    $tmp = bill_method_selector();
    $tpl->assign('method', $tmp);
    $tmp = bill_type_selection_pos($_SESSION['sourceid']);
    $tpl->assign('type', $tmp);
    $tmp = discount_form_javascript($_SESSION['sourceid']);
    $tpl->assign('discount', $tmp);
    $tmp = bill_show_list();
    $tpl->assign('orders', $tmp);
    return 0;
}
function tables_list_cell_pos($row)
{
    $output = '';
    $sourceid = $row['id'];
    $tablenum = $row['name'];
    $owneruserid = $row['userid'];
    $tableclosed = $row['toclose'];
    $paid = $row['paid'];
    //RTG: in the master query we get the num of the order associated to this table,
    //if any, so we don't need create a table object just to see if is empty.
    $empty = $row['order'] == null || $row['order'] == 0;
    $user = new user($_SESSION['userid']);
    if ($user->level[USER_BIT_CASHIER] && order_found_generic_not_priced($sourceid)) {
        $msg = ucfirst(phr('GENERIC_NOT_PRICED'));
        $class = COLOR_TABLE_GENERIC_NOT_PRICED;
    } elseif ($tableclosed && $user->level[USER_BIT_CASHIER] && !$paid) {
        $msg = ucfirst(phr('CLOSED'));
        $class = COLOR_TABLE_CLOSED_OPENABLE;
    } elseif ($tableclosed && $user->level[USER_BIT_CASHIER] && $paid) {
        $msg = ucfirst(phr('PAID'));
        $class = COLOR_TABLE_CLOSED_OPENABLE;
    } elseif ($tableclosed && !$user->level[USER_BIT_CASHIER] && !$paid) {
        $msg = ucfirst(phr('CLOSED'));
        $class = COLOR_TABLE_NOT_OPENABLE;
    } elseif ($tableclosed && !$user->level[USER_BIT_CASHIER] && $paid) {
        $msg = ucfirst(phr('PAID'));
        $class = COLOR_TABLE_NOT_OPENABLE;
    } elseif ($_SESSION['userid'] == $owneruserid) {
        $msg = ucfirst(phr('OPEN'));
        $class = COLOR_TABLE_MINE;
        if (categories_orders_present($sourceid, 2) && !categories_printed($sourceid, 2)) {
            $class = COLOR_ORDER_PRIORITY_2;
        }
        if (categories_orders_present($sourceid, 3) && !categories_printed($sourceid, 3)) {
            $class = COLOR_ORDER_PRIORITY_2;
        }
    } elseif (!$empty && $owneruserid == 0) {
        $msg = ucfirst(phr('TO_BE_ASSOCIATED'));
        $class = COLOR_TABLE_CLOSED_OPENABLE;
    } elseif ($owneruserid == 0 && !$tableclosed && $empty) {
        $msg = ucfirst(phr('FREE'));
        $class = COLOR_TABLE_FREE;
    } else {
        $user = new user($owneruserid);
        $ownerusername = $user->data['name'];
        unset($user);
        $msg = $ownerusername;
        $class = COLOR_TABLE_OTHER;
    }
    if ($sourceid) {
        $link = 'orders.php?data[sourceid]=' . $sourceid;
        if (isset($command) && !empty($command)) {
            $link .= '&amp;command=' . $command;
        }
        $msg = strtoupper($msg);
        $output .= '
		<td bgcolor="' . $class . '" onclick="redir(\'' . $link . '\');return(false);">
			<a href="' . $link . '"><img src="' . IMAGE_TABLE . '"><br>
			<strong>' . $tablenum . '
			' . $msg . '</strong></a>
		</td>' . "\n";
    } else {
        $output .= '
		<td bgcolor="' . COLOR_TABLE_FREE . '">
		&nbsp;
		</td>' . "\n";
    }
    return $output;
}