var editorTables = [];
var editorTablesOrig = [];
var editorSafeguard = [];
var editorColumnNames = [];
var editorColumnNamesOrig = [];
var editorColumnDescriptions = [];
var editorColumnInfos = [];
var editorColumnUnits = [];
var editorColumnUCDs = [];

var editorTableCount;
var editorColumnCount = [];

var editorCurrentTable = 0;
var editorCurrentColumn = 0;

function editorSetTable(table_id)
{
    editorCurrentTable = table_id;
    editorCurrentColumn = 0;
    editorShowTables();
}

function editorSetColumn(column_id)
{
    editorCurrentColumn = column_id;
    editorShowTables();
}

function editorShowTables()
{
    text = "<br><span class='tables'><b>Tables:</b> ";
    
    if(editorSafeguard[editorCurrentTable] != "safe")
        editorCurrentTable = -1;

    // Tables
    for(table_id in editorTables)
        if(editorSafeguard[table_id] == "safe"){
            if(editorCurrentTable < 0)
                editorCurrentTable = table_id;
            
            if(table_id == editorCurrentTable)
                text += "<span class='tables_selected'>";
            else
                text += "<span class='tables_unselected'><a href='javascript:void()' onclick='editorSetTable(" + table_id + "); return false;'>"
            
            text += editorTables[table_id];

            if(table_id == editorCurrentTable)
                text += "</span>";
            else
                text += "</a></span>";
        }
    
    // New table button
    // text += " <button onclick=\"editorAddTable('New Table'); editorShowTables();\">New table</button>";
    text += "</span><br>";

    // Current table
    if(editorCurrentTable >= 0)
        text += editorShowTable(editorCurrentTable);
    
    document.getElementById('catalog_editor_tables').innerHTML = text;
    
    editorFillForms();
}

function editorShowTable(table_id)
{
    text = "<br><b>Table name:</b> ";
    text += "<input type='text' name='table_name' value='' title='System identifier of a table' onblur='editorUpdateTable(" + table_id + ");'> ";
    // text += "<button onclick=\"editorDeleteTable(" + table_id + "); editorShowTables();\">Delete table</button>";
    text += "<br>";
    text += "<br>";
    text += "<span class='column'><b>Columns:</b></span>";

    if(editorSafeguard[table_id + "_" + editorCurrentColumn] != "safe")
        editorCurrentColumn = -1;

    // Columns
    for(column_id in editorColumnNames[table_id])
        if(editorSafeguard[table_id + "_" + column_id] == "safe"){
            if(editorCurrentColumn < 0)
                editorCurrentColumn = column_id;
            
            if(column_id == editorCurrentColumn)
                text += "<span class='column_selected'>";
            else
                text += "<span class='column_unselected'><a href='javascript:void()' onclick='editorSetColumn(" + column_id + "); return false;'>"
            
            text += editorColumnNames[table_id][column_id];

            if(column_id == editorCurrentColumn)
                text += "</span> ";
            else
                text += "</a></span> ";
        }
    // New column
    // text += "<span class='column'><button onclick=\"editorAddColumn(" + table_id + ", 'New Column', '', '', '', ''); editorShowTables();\">New column</button></span><br>";
    
    // Current column
    if(editorCurrentColumn >= 0)
        text += editorShowColumn(table_id, editorCurrentColumn);

    return text;
}

function editorShowColumn(table_id, column_id)
{
    text = "<br><b>Column name:</b> <input type='text' name='column_name' value='' title='System identifier of a column' onblur='editorUpdateColumn(" + table_id + ", " + column_id + ");'> ";
    text += "<b>Description:</b> <input type='text' size='30' name='column_description' value='' title='Brief description of a column' onblur='editorUpdateColumn(" + table_id + ", " + column_id + ");'> ";
    // text += "<button onclick=\"editorDeleteColumn(" + table_id + ", " + column_id + "); editorShowTables();\">Delete column</button><br>";
    text += "<br>";
    text += "<b>Units:</b> <input type='text' name='column_units' value='' title='Units of a column' onblur='editorUpdateColumn(" + table_id + ", " + column_id + ");'><br>";
    text += "<b>UCD:</b> <input type='text' name='column_ucd' value='' title='UCD of a column' onblur='editorUpdateColumn(" + table_id + ", " + column_id + ");'><br>";
    text += "<b>Info:</b><br><textarea name='column_info' rows='5' cols='80'  onblur='editorUpdateColumn(" + table_id + ", " + column_id + ");' title='Detailed information on a column'></textarea><br>";
    
    return text;
}

function editorFillForms()
{
    if(editorCurrentTable >= 0){
        document.catalog_editor_form.table_name.value = editorTables[editorCurrentTable];
        
        if(editorCurrentColumn >= 0){
            document.catalog_editor_form.column_name.value = unescape(editorColumnNames[editorCurrentTable][editorCurrentColumn]);
            document.catalog_editor_form.column_description.value = unescape(editorColumnDescriptions[editorCurrentTable][editorCurrentColumn]);
            document.catalog_editor_form.column_units.value = unescape(editorColumnUnits[editorCurrentTable][editorCurrentColumn]);
            document.catalog_editor_form.column_ucd.value = unescape(editorColumnUCDs[editorCurrentTable][editorCurrentColumn]);
            document.catalog_editor_form.column_info.value = unescape(editorColumnInfos[editorCurrentTable][editorCurrentColumn]);
        }
    }
}

function editorUpdateTable(table_id)
{
    name = document.catalog_editor_form.table_name.value;
    
    editorTables[table_id] = name;
}

function editorUpdateColumn(table_id, column_id)
{
    name = document.catalog_editor_form.column_name.value;
    description = document.catalog_editor_form.column_description.value;
    units = document.catalog_editor_form.column_units.value;
    ucd = document.catalog_editor_form.column_ucd.value;
    info = document.catalog_editor_form.column_info.value;

    editorColumnNames[table_id][column_id] = escape(name);
    editorColumnDescriptions[table_id][column_id] = escape(description);
    editorColumnUnits[table_id][column_id] = escape(units);
    editorColumnUCDs[table_id][column_id] = escape(ucd);
    editorColumnInfos[table_id][column_id] = escape(info);
}

function editorAddTable(table)
{
    id = editorTableCount;

    editorTables[id] = table;
    editorTablesOrig[id] = table;
    editorSafeguard[id] = "safe";

    editorColumnNames[id] = [];
    editorColumnNamesOrig[id] = [];
    editorColumnDescriptions[id] = [];
    editorColumnInfos[id] = [];
    editorColumnUnits[id] = [];
    editorColumnUCDs[id] = [];

    editorColumnCount[id] = 0;

    editorTableCount ++;
}

function editorDeleteTable(table_id)
{
    delete editorTables[table_id];
    delete editorSafeguard[table_id];
}

function editorFindTableByName(table)
{
    table_id = -1;

    for(id in editorTables)
        if(editorSafeguard[id] && editorTables[id] == table)
            table_id = id;

    return table_id;
}

function editorAddColumn(table_id, name, description, info, units, ucd)
{
    /* May operate on the table name, not table id */
    if(editorSafeguard[table_id] != "safe")
        table_id = editorFindTableByName(table_id);
    
    if(editorSafeguard[table_id] != "safe"){
        return;
    }

    id = editorColumnCount[table_id];
    
    editorColumnNames[table_id][id] = name;
    editorColumnNamesOrig[table_id][id] = name;
    editorColumnDescriptions[table_id][id] = description;
    editorColumnInfos[table_id][id] = info;
    editorColumnUnits[table_id][id] = units;
    editorColumnUCDs[table_id][id] = ucd;
    editorSafeguard[table_id + "_" + id] = "safe";

    editorColumnCount[table_id] ++;
}

function editorDeleteColumn(table_id, column_id)
{
    delete editorColumnNames[table_id][column_id];
    delete editorSafeguard[table_id + "_" + column_id];
}

function editorSetInfo(description, info)
{
    document.catalog_editor_form.info.value = unescape(info);
    document.catalog_editor_form.description.value = unescape(description);
}

function editorReset()
{
    editorTables = [];
    editorSafeguard = [];
    editorColumnNames = [];
    editorColumnDescriptions = [];
    editorColumnInfos = [];
    editorColumnUnits = [];
    editorColumnUCDs = [];

    editorTableCount = 0;
}

