function sv_link()
{
    return "<a href=\"http://" + this.ref + "\">" + this.name + "</a>";
}

function SV( name, ref )
{
    this.name  = name;
    this.ref   = ref;

    this.link  = sv_link;
}

function CC( cc, name )
{
    this.cc   = cc;
    this.name = name;
    this.content = "<img src=\"http://ed2k.2x4u.de/cc/" + cc + ".gif\" alt=\"" + name + "\" width=\"34\" height=\"18\">";
}

function si_name()
{
    return this.name;
}

function ipstr( addr )
{
    return ( addr & 0xff ) + "." + ( ( addr >> 8 ) & 0xff ) + "." + ( ( addr >> 16 ) & 0xff ) + "." + ( ( addr >> 24 ) & 0xff );
}
    
function link( addr, port, title, xref )
{
    xip = ipstr( addr );
    if( !xref )
        xref = "ed2k://|server|" + xip + "|" + port + "|";
    if( title )
        return "<a title=\"" + title + "\" href=\"" + xref + "\">" + xip + ":" + port + "</a>";
    return "<a href=\"" + xref + "\">" + xip + ":" + port + "</a>";
}

function si_version()
{
    tmp1 = vers[ this.info1 & 0x1f ];
    if( tmp1.name == "" )
        return "&nbsp;";
    return "<a href=\"" + tmp1.ref + "\">" + tmp1.name + "</a>";
}

function si_min()
{
    if( ( this.info1 & 0x3800 ) == 0 )
        return "&nbsp;";
    tmp2 = ( this.info1 >> 5 ) & 0x7;
    return ( tmp2 > 0 ) ? tmp2 + 55 : 0;
}

function si_max()
{
    if( ( this.info1 & 0x3800 ) == 0 )
        return "&nbsp;";
    tmp2 = ( this.info1 >> 8 ) & 0x7;
    return ( tmp2 > 0 ) ? tmp2 + 55 : 0;
}

function si_high()
{
    if( ( this.info1 & 0x3800 ) == 0 )
        return "&nbsp;";
    return ( ( this.info1 & 0x800  ) == 0x800 ) ? "Yes" : "No";
}

function si_low()
{
    if( this.info1 & 0x80000000 )
        return "&nbsp;";
    if( ( this.info1 & 0x3800 ) == 0 )
        return "&nbsp;";
    return ( ( this.info1 & 0x1000  ) == 0x1000 ) ? "Yes" : "No";
}

function si_bot()
{
    if( ( this.info1 & 0x2000 ) == 0 )
        return "&nbsp;";
    return ( ( this.info1 & 0x20000  ) == 0x20000 ) ? "Yes" : "No";
}

function si_mld()
{
    if( ( this.info1 & 0x2000 ) == 0 )
        return "&nbsp;";
    return ( ( this.info1 & 0x40000  ) == 0x40000 ) ? "Yes" : "No";
}

function si_refs()
{
    return ( this.info1 >> 21 ) & 0x1ff;
}

function si_known()
{
    return this.info2 & 0x3ff;
}

function si_uptime()
{
    tmp2 = ( ( this.info2 >> 18 ) & 0x3fff );
    if( tmp2 < 60 )
        return tmp2 + " min";
    mins = tmp2 & 63;
    hours = ( tmp2 >> 6 ) & 31;
    if( tmp2 < 0x7ff )
        return hours + ( mins < 10 ? ":0" : ":" ) + mins + " h";
    if( tmp2 < 0xfff )
       return "1 day, " + hours + ( mins < 10 ? ":0" : ":" ) + mins + " h";
    if( tmp2 == 0x3fff )
       return "8 days ++"
    return ( ( tmp2 >> 11 ) & 7 ) + "days, " + hours + ( mins < 10 ? ":0" : ":" ) + mins + " h";
}

function addServer( name, pos )
{
    si = ldata[ pos ];
    location.href = "ed2k://|server|" + ipstr( si.addr ) + "|" + si.port + "|/";
}

scount = 0;
function ilink( si )
{
    return "<a href=\"javascript:addServer('" + si.name + "'," + scount + ")\">---.---.---.---:" + si.port + "</a>";
}

function SI( addr, port, name, descr, users, files, info1, info2, cc, fakeIp1, fakeIp2 )
{
    this.addr  = addr;
    this.port  = port;
    this.users = users;
    this.files = files;
    this.name  = name;
    this.descr = descr;
    this.info1 = info1;
    this.info2 = info2;
    this.cc    = ccs[ cc ];
    this.fakeIp1 = fakeIp1;
    this.fakeIp2 = fakeIp2;

    this.version = si_version;
    this.min     = si_min;
    this.max     = si_max;
    this.high    = si_high;
    this.low     = si_low;
    this.bot     = si_bot;
    this.mld     = si_mld;
    this.refs    = si_refs;
    this.known   = si_known;
    this.uptime  = si_uptime;

    if( info1 & 0x40000000 )
        this.daddr = ilink( this );
    else
    {
        if( fakeIp1 || fakeIp2 )
        {
           this.daddr = link( this.addr, this.port, "The true Address (Response Sender)" );
           if( fakeIp2 )
               this.daddr += "<font size=\"-1\"><br>" + link( fakeIp2, this.port, "Faked Address (wrong thisIP Setting)", "javascript:void 0" ) + "</font>";
           else
               this.daddr += "<font size=\"-1\"><br>" + link( fakeIp1, this.port, "Faked Address (wrong thisIP Setting)" ) + "</font>";
        }
        else
            this.daddr = link( this.addr, this.port );
    }
    this.dversion = this.version();
    scount++;
}

function showlist( sil )
{
    document.writeln( "<font size=\"2\" face=\"Microsoft Sans Serif, Arial, Helvetica, sans-serif\">" );
    document.writeln( "<table width=\"100%\" border=\"0\" cellspacing=\"1\" cellpadding=\"2\">" );
    document.write( "<tr bgcolor=\"#cfcfcf\">" );
    document.write( "<th nowrap align=\"center\"><a href=\"http://ed2k.2x4u.de/info.html\">Address:Port</a></th>" );
    document.write( "<th nowrap align=\"center\"><a href=\"http://ed2k.2x4u.de/info.html\">Name</a></th>" );
    document.write( "<th nowrap align=\"center\"><a href=\"http://ed2k.2x4u.de/info.html\">CC</a></th>" );
    document.write( "<th nowrap align=\"right\"><a href=\"http://ed2k.2x4u.de/info.html\">Users</a></th>" );
    document.write( "<th nowrap align=\"right\"><a href=\"http://ed2k.2x4u.de/info.html\">Files</a></th>" );
    document.write( "<th nowrap align=\"right\"><a href=\"http://ed2k.2x4u.de/info.html\">Uptime</a></th>" );
    document.write( "<th nowrap align=\"right\"><a href=\"http://ed2k.2x4u.de/info.html\">Min</a></th>" );
    document.write( "<th nowrap align=\"right\"><a href=\"http://ed2k.2x4u.de/info.html\">Max</a></th>" );
    document.write( "<th nowrap align=\"center\"><a href=\"http://ed2k.2x4u.de/info.html\">High</a></th>" );
    document.write( "<th nowrap align=\"center\"><a href=\"http://ed2k.2x4u.de/info.html\">Low</a></th>" );
    document.write( "<th nowrap align=\"left\"><a href=\"http://ed2k.2x4u.de/info.html\">Version</a></th>" );
    document.write( "<th nowrap align=\"right\"><a href=\"http://ed2k.2x4u.de/info.html\">Refs</a></th>" );
    document.write( "<th nowrap align=\"right\"><a href=\"http://ed2k.2x4u.de/info.html\">Known</a></th>" );
    document.write( "<th nowrap align=\"center\"><a href=\"http://ed2k.2x4u.de/info.html\">Bot</a></th>" );
    document.write( "<th nowrap align=\"center\"><a href=\"http://ed2k.2x4u.de/info.html\">MLD</a></th>" );
    document.write( "<th nowrap align=\"center\"><a href=\"http://ed2k.2x4u.de/info.html\">Description</a></th>" );
    document.writeln( "</tr>" );
    for( idx = 0; idx < sil.length; idx++ )
    {
        var si = sil[ idx ];
        document.write( "<tr bgcolor=\"#fdf5cd\"><td nowrap align=\"center\">" + 
        si.daddr + "</td><td align=\"center\">" + 
        ( si.name == "" ? "&nbsp;" : si.name ) + "</td><td nowrap align=\"center\">" + 
        si.cc.content + "</td><td nowrap align=\"right\">" +
        // si.cc + "</td><td nowrap align=\"right\">" +
        ( ( si.info1 & 0x7800 ) == 0 ? "&nbsp;" : si.users ) + "</td><td nowrap align=\"right\">" + 
        ( ( si.info1 & 0x7800 ) == 0 ? "&nbsp;" : si.files ) + "</td><td nowrap align=\"right\">" + 
        si.uptime() + "</td><td nowrap align=\"right\">" +
        si.min() + "</td><td nowrap align=\"right\">" + 
        si.max() + "</td><td nowrap align=\"center\">" + 
        si.high() + "</td><td nowrap align=\"center\">" + 
        si.low() + "</td><td nowrap align=\"left\">" + 
        si.dversion + "</td><td nowrap align=\"right\">" + 
        si.refs() + "</td><td nowrap align=\"right\">" + 
        si.known() + "</td><td nowrap align=\"center\">" + 
        si.bot() + "</td><td nowrap align=\"center\">" + 
        si.mld() + "</td><td align=\"center\" width=\"*\">" + 
        ( si.descr == "" ? "&nbsp;" : si.descr ) + "</td></tr>" );
        status = "read server " + idx;
    }
    document.writeln( "</table>" );
    document.writeln( "</font>" );
    document.close();
    status = "document ready (" + sil.length + " servers)";
}

