var cr = '\r\n', N = 0, vze = 0;

function kreslitab()
{
    var pis = ('<table id=triditelna border=1 width=\"100%\" bgColor=#d3d3d3  style=\"position:relative;left:0;top:-26;\"><thead>' + cr);
    pis += ('<tr><th class=but onclick=\"tridit(0);\" onmouseover=\"tit(0);\" onmouseout=\"hlp(\'&nbsp;\');\"><img id=im0 src=\"dn.gif\" border=0 alt=\"\" width=9 height=12 \/>&nbsp;chovatelská&nbsp;stanice<\/th><th class=but id=th1 onclick=\"tridit(1);\" onmouseover=\"tit(1);\" onmouseout=\"hlp(\'&nbsp;\');\"><img id=im1 src=\"nic.gif\" border=0 alt=\"\" width=9 height=12 \/>&nbsp;majitel<\/th><th class=but id=th2 onclick=\"tridit(2);\" onmouseover=\"tit(2);\" onmouseout=\"hlp(\'&nbsp;\');\"><img id=im2 src=\"nic.gif\" border=0 alt=\"\" width=9 height=12 \/>&nbsp;lokalita<\/th><th class=butc style=\"width:40;\" onmouseover=\"hlp(\'klik na \@ odešle e-mail\');\" onmouseout=\"hlp(\'&nbsp;\');\">e-mail<\/th><th class=butc onmouseover=\"hlp(\'klik na telefonní èíslo volá vèetnì pøedvolby\');\" onmouseout=\"hlp(\'&nbsp;\');\">telefon&nbsp;+420<\/th><th class=butc onmouseover=\"hlp(\'poznámka\');\" onmouseout=\"hlp(\'&nbsp;\');\">info<\/th><\/tr><\/thead><tbody>');
    for (i = 0 ; i < seznam.length ; i ++)
    {
        pis += ('<tr onmouseover=\"this.bgColor=\'#f0f0f0\'\" onmouseout=\"this.bgColor=\'#d3d3d3\'\">');
        pis += ('<td nowrap onclick=\"chslink(' + i + ');\"><\/td>' + cr); //CHS
        pis += ('<td nowrap><\/td>' + cr); //majitel
        pis += ('<td nowrap><\/td>' + cr); //lokalita
        pis += ('<td nowrap onclick=\"chsmail(' + i + ')\" style=\"text-align:center;font-family:Comic Sans MS;color:#0000ff;\"><\/td>' + cr); //email
        pis += ('<td nowrap onclick=\"chstelef(' + i + ');\" style=\"text-align:center;\"><\/td>' + cr); //telefon
        pis += ('<td nowrap><\/td>' + cr); //info
        pis += cr;
    }
    pis += '<\/tbody><\/table>';
    document.getElementById('gut').innerHTML = pis;
}

kreslitab();

var trs = document.getElementById('triditelna').getElementsByTagName('tr');

function tridit(n)
{
    for (i = 0 ; i < 3; i ++)
    {
        tb_ = document.getElementById('im' + i);
        cha = tb_.src;
        cha = cha.charAt(cha.length - 5);
        tb_.src = (i != n) ? 'nic.gif' : ((cha == 'n') ? 'up.gif' : 'dn.gif');
    }
    cha = document.getElementById('im' + n).src;
    cha = cha.charAt(cha.length - 5);
    vze = (cha == 'p') ? 1 : 0;
    document.getElementById('skof').src = '3dim.gif';
    N = n + ((n) ? 1 : 0);
    tit(n);
    setTimeout('trid()', 100);
}

function trid()
{
    seznam.sort(abcesky);
    kresli();
    document.getElementById('skof').src = 'nic.gif';
}

function abcesky(A, B)
{
    var dif = 0;
    cz = " aábcèdïeéìfghðiíjklmnòoópqrøsštuúùvwxyýzž0123456789" ;
    a = A[N].toLowerCase() + ' ';
    a = a.replace(/ch/g, "ð") ;
    b = B[N].toLowerCase() + ' ';
    b = b.replace(/ch/g, "ð") ;
    for (z = 0 ; z < Math.min(a.length, b.length) ; z++)
    {
        dif = cz.indexOf(a.charAt(z)) - cz.indexOf(b.charAt(z)) ;
        if (dif != 0) { break ; }
    }
    return dif * Math.pow(-1, vze);
}

function kresli()
{
    var tds = document.getElementById('triditelna').getElementsByTagName('td');

    for (i = 0 ; i < seznam.length ; i ++)
    {
        delka = seznam[i].length - 1;
        tds[i * delka].innerHTML = seznam[i][0];
        if (seznam[i][1])
        {
            tds[i * delka].style.cursor = 'pointer';
            tds[i * delka].style.cursor = 'hand';
        }
        else
        {
            tds[i * delka].style.cursor = 'default';
        }
        tds[i * delka + 1].innerHTML = (((seznam[i][2]) ? seznam[i][2] : '&nbsp;')); //majitel
        tds[i * delka + 2].innerHTML = (((seznam[i][3]) ? seznam[i][3] : '&nbsp;')); //lokalita
        tds[i * delka + 3].innerHTML = ((seznam[i][4]) ? '@' : '&nbsp;'); //email
        if (seznam[i][4])
        {
            tds[i * delka + 3].style.cursor = 'pointer';
            tds[i * delka + 3].style.cursor = 'hand';
        }
        else
        {
            tds[i * delka + 3].style.cursor = 'default';
        }
        
        tds[i * delka + 4].innerHTML = (((seznam[i][5]) ? (seznam[i][5].substr(0, 3) + ' ' + seznam[i][5].substr(3, 3) + ' ' + seznam[i][5].substring(6)) : '&nbsp;')); //telefon
        if (seznam[i][5])
        {
            tds[i * delka + 4].style.cursor = 'pointer';
            tds[i * delka + 4].style.cursor = 'hand';
        }
        else
        {
            tds[i * delka + 4].style.cursor = 'default';
        }
        tds[i * delka + 5].innerHTML = (((seznam[i][6]) ? seznam[i][6] : '&nbsp;')); //info
        trs[i + 1].bgColor = '#d3d3d3';
    }
}

function ctr()
{
    if (event.keyCode == 17 || event.keyCode == 65 || event.keyCode == 97) return event.keyCode = 0
}

function najdi()
{
    fin = document.getElementById('hledat').value.toLowerCase();
    ozn = -1;
    for (i = 0 ; i < seznam.length ; i ++)
    {
        trs[i + 1].bgColor = '#d3d3d3';
        sea = seznam[i][0].toLowerCase();
        if (sea.indexOf(fin) > -1)
        {
            ozn = (ozn < 0) ? i : ozn;
            trs[i + 1].bgColor = '#ffffc0';
        }
    }
    document.getElementById('gut').scrollTop = 20 * ozn;
}

function tit(t)
{
    obr = document.getElementById('im' + t).src;
    cha = obr.charAt(obr.length - 5);
    joo = (cha == 'n');
    hlp('setøídit ' + ((joo) ? 's' : 'vz') + 'estupnì (' + ((joo) ? 'z ÷ a' : 'a ÷ z') + ')');
}

function hlp(tx)
{
    document.getElementById('na').innerHTML = tx;
}

function chslink(kam)
{
    if (seznam[kam][1])
    {
        open ('http:\/\/' + seznam[kam][1], '', '');
    }
}

function chsmail(m)
{
    if (seznam[m][4])
    {
        document.getElementById('odeslatprosim').setAttribute('href', 'mailto:' + seznam[m][4].replace('£', '@'));
        document.getElementById('odeslatprosim').click();
    }
}

function chstelef(te)
{
    if (seznam[te][5])
    {
        document.getElementById('odeslatprosim').setAttribute('href', 'callto:+420' + seznam[te][5]);
        document.getElementById('odeslatprosim').click();
    }
}

kresli();

document.onkeydown = ctr;
document.onkeyup = ctr;
