function Templates_List()
{
    global $mysqli;
    $session =& $_SESSION['Templates'];
    $smarty = smarty_init(dirname(__FILE__) . '/templates');
    // Init message (Message)
    $Message = isset($_REQUEST['msg']) ? $_REQUEST['msg'] : "";
    // If a create template was requested
    if ($_REQUEST['submit'] == 'create') {
        $PK_Template = create_new_template($_REQUEST['Name']);
        if ($PK_Template) {
            header("Location: Templates_Modify.php?PK_Template={$PK_Template}&msg=CREATE_TEMPLATE");
            die;
        } else {
            $Message = "ERR_TEMPLATE_NAME";
        }
    }
    // Init sort order (Order)
    if ($session['Sort'] == $_REQUEST['Sort']) {
        $Order = $session['Order'] == "asc" ? "desc" : "asc";
    } elseif ($session['Sort'] != $_REQUEST['Sort']) {
        $Order = 'asc';
    }
    $session['Order'] = $Order;
    // Init sort field (Sort)
    if (isset($_REQUEST['Sort'])) {
        $Sort = $_REQUEST['Sort'];
    } else {
        $Sort = 'Name';
    }
    $session['Sort'] = $Sort;
    // Init table fields (Templates)
    $Templates = array();
    $query = "\n\t\tSELECT\n\t\t\tPK_Template AS _PK_,\n\t\t\tName                 AS Name,\n\t\t\tDATE_FORMAT(DateCreated,'%m/%d/%y, %h:%i %p') AS DateCreated,\n\t\t\tProtected\n\t\tFROM\n\t\t\tTemplates\n\t\tORDER BY \n\t\t\t{$Sort} {$Order}\n\t";
    $result = $mysqli->query($query) or die($mysqli->error);
    while ($row = $result->fetch_assoc()) {
        $Templates[] = $row;
    }
    $smarty->assign('Templates', $Templates);
    $smarty->assign('Sort', $Sort);
    $smarty->assign('Order', $Order);
    $smarty->assign('Message', $Message);
    $smarty->assign('Hilight', isset($_REQUEST['hilight']) ? $_REQUEST['hilight'] : "");
    return $smarty->fetch('Templates_List.tpl');
}
$extraflags = "";
if ($row_template_type['template_framework'] == 'xerte') {
    if ($row_template_type['template_name'] == 'Nottingham') {
        $extraflags = "engine=javascript";
    } else {
        $extraflags = "engine=flash";
    }
}
$query_for_new_template = "INSERT INTO {$xerte_toolkits_site->database_table_prefix}templatedetails (template_id, creator_id, template_type_id, date_created, date_modified, number_of_uses, access_to_whom, template_name, extra_flags)\n    VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
$lastid = db_query($query_for_new_template, array($new_template_id, $_SESSION['toolkits_logon_id'], $row_template_type['template_type_id'], date('Y-m-d'), date('Y-m-d'), 0, "Private", str_replace(" ", "_", $_POST['tutorialname']), $extraflags));
// templatedetails doesn't have a AI field, so $lastid returns always 0, check explicitly for false
if ($lastid !== false) {
    _debug("Created new template entry in db");
    $query_for_template_rights = "INSERT INTO {$xerte_toolkits_site->database_table_prefix}templaterights (template_id, user_id, role, folder) VALUES (?,?,?,?)";
    $lastid = db_query($query_for_template_rights, array($new_template_id, $_SESSION['toolkits_logon_id'], "creator", "" . $folder_id));
    // templaterights doesn't have a AI field, so $lastid returns always 0, check explicitly for false
    if ($lastid !== false) {
        _debug("Setup template rights ok");
        receive_message($_SESSION['toolkits_logon_username'], "ADMIN", "SUCCESS", "Created new template record for the database", $query_for_new_template . " " . $query_for_template_rights);
        include $xerte_toolkits_site->root_file_path . $xerte_toolkits_site->module_path . $row_template_type['template_framework'] . "/new_template.php";
        create_new_template($new_template_id, $_POST['tutorialid']);
        echo trim($new_template_id);
        echo "," . $xerte_toolkits_site->learning_objects->{$row_template_type['template_framework'] . "_" . $row_template_type['template_name']}->editor_size;
    } else {
        receive_message($_SESSION['toolkits_logon_username'], "ADMIN", "CRITICAL", "Failed to create new template record for the database", $query_for_template_rights);
        echo "FAILED-" . $_SESSION['toolkits_most_recent_error'];
    }
} else {
    receive_message($_SESSION['toolkits_logon_username'], "ADMIN", "CRITICAL", "Failed to create new template record for the database", $query_for_new_template);
    echo "FAILED-" . $_SESSION['toolkits_most_recent_error'];
}