var catalogs = [];
var tables = [];
var descriptions = [];
var safeguard = [];
var selectSingle = false;

function viewTables()
{
    count = 0;
    text = "";
    
    for(i in catalogs)
        if(safeguard[i] == 'safe'){
            count ++;
            if(!selectSingle)
                text += "<button onclick='return deleteTable(\"" + catalogs[i] + "\", \"" + tables[i] + "\");' type='button'>Remove</button>";
            text += " <b>" + tables[i] + "</b> of <b>" + unescape(descriptions[i]) + "</b><br>";
        }

    if(count > 0){
        if(selectSingle)
            text = "Selected table:<br>" + text;
        else
            text = "Selected tables:<br>" + text;
    } else
        text = "No tables selected. Click on the table name to select/unselect it.";
    
    document.getElementById('multicat').innerHTML = text;
}

function countTables()
{
    count = 0;
    
    for(i in catalogs)
        if(safeguard[i] == 'safe')
            count ++;
    
    return count;
}

function addTable(_catalog, _table, _description)
{
    if(selectSingle){
        for(i in catalogs)
            if(safeguard[i] == 'safe')
                deleteTable(catalogs[i], tables[i]);
    }

    catalogs[_catalog + "_" + _table] = _catalog;
    tables[_catalog + "_" + _table] = _table;
    descriptions[_catalog + "_" + _table] = _description;
    
    safeguard[_catalog + "_" + _table] = 'safe';
    
    viewTables();

    return false;
}

function deleteTable(_catalog, _table)
{
    delete catalogs[_catalog + "_" + _table];
    delete tables[_catalog + "_" + _table];
    delete descriptions[_catalog + "_" + _table];
    delete safeguard[_catalog + "_" + _table];

    viewTables();

    return false;
}

function toggleTable(_catalog, _table, _description)
{
    if(safeguard[_catalog + "_" + _table] == 'safe')
        deleteTable(_catalog, _table);
    else
        addTable(_catalog, _table, _description);

    viewTables();

    return false;
}

function getRadioValue(radio)
{
    var result = "";
    
    for(d = 0; d < radio.length; d++)
        if(radio[d].checked)
            result = radio[d].value;

    return result;
}

function getFirstCatalog()
{
    value = "";

    for(i in catalogs)
        if(safeguard[i] == 'safe'){
            value = catalogs[i];
            break;
        }
    
    return value;
}

function getFirstTable()
{
    catalog = getFirstCatalog();
    table = "";

    if(catalog != ""){
        for(i in tables)
            if(safeguard[catalog + "_" + tables[i]] == 'safe'){
                table = tables[i];
                break;
            }
    }

    return table;
}

function getRadius(value_in, format)
{
    var value = value_in;

    if(format == 'arcmin')
        value *= 1./60;
    else if(format == 'arcsec')
        value *= 1./3600;
    
    return value;
}

function setCookie(name,value)
{
    var expiry = new Date();
    
    expiry.setTime(expiry.getTime() + 24*60*60*1000);
    
    document.cookie=name + '=' + value + '; path=/; expires=' + expiry.toGMTString();
}

function toggleCatalogTables(id_toggle, id_in)
{
    internal = document.getElementById(id_in);
    toggle = document.getElementById(id_toggle);
    
    if(internal.style.display == 'block') {
        internal.style.display = 'none';
        toggle.src = 'icons/closed.gif';
        toggle.title = 'Show tables';
        setCookie(id_toggle, 'hidden');
    } else {
        internal.style.display = 'block';
        toggle.src = 'icons/open.gif';
        toggle.title = 'Hide tables';
        setCookie(id_toggle, 'shown');
    }

    return false;
}

